From 7faa7d632fbd3d1bb4b15990099416874e84ba43 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Mon, 18 Aug 2025 23:07:02 +0200 Subject: [PATCH] move and fix tests for unique and primary key id indexes Puts the index tests to where all the other tests reside and adds actual tests for the presence of the indexes. Needs some regular expression matching of the values. --- tests/bdd/flex/lua-index-definitions.feature | 48 ++++++++++++++++++++ tests/bdd/flex/lua-table-definitions.feature | 34 -------------- tests/bdd/steps/steps_db.py | 14 ++++++ 3 files changed, 62 insertions(+), 34 deletions(-) diff --git a/tests/bdd/flex/lua-index-definitions.feature b/tests/bdd/flex/lua-index-definitions.feature index 04b642f61..da77db887 100644 --- a/tests/bdd/flex/lua-index-definitions.feature +++ b/tests/bdd/flex/lua-index-definitions.feature @@ -537,3 +537,51 @@ Feature: Index definitions in Lua file schemaname = 'public' AND tablename = 'mytable' AND indexname LIKE '%node_id%' """ + Scenario: Create a unique id index when requested + Given the input file 'liechtenstein-2013-08-03.osm.pbf' + And the lua style + """ + local t = osm2pgsql.define_table({ + name = 'foo', + ids = { type = 'node', id_column = 'node_id', create_index = 'unique' }, + columns = {} + }) + + function osm2pgsql.process_node(object) + t:insert({}) + end + """ + When running osm2pgsql flex + Then table foo has 1562 rows + And SELECT indexdef FROM pg_indexes WHERE tablename = 'foo' + | indexdef@fullmatch | + | CREATE UNIQUE INDEX .* USING .*\(node_id\) | + And table pg_catalog.pg_index has 0 rows with condition + """ + indrelid = 'foo'::regclass and indisprimary + """ + + Scenario: Create a primary key id index when requested + Given the input file 'liechtenstein-2013-08-03.osm.pbf' + And the lua style + """ + local t = osm2pgsql.define_table({ + name = 'foo', + ids = { type = 'node', id_column = 'node_id', create_index = 'primary_key' }, + columns = {} + }) + + function osm2pgsql.process_node(object) + t:insert({}) + end + """ + When running osm2pgsql flex + Then table foo has 1562 rows + And SELECT indexdef FROM pg_indexes WHERE tablename = 'foo' + | indexdef@fullmatch | + | CREATE UNIQUE INDEX .* USING .*\(node_id\) | + And table pg_catalog.pg_index has 1 row with condition + """ + indrelid = 'foo'::regclass and indisprimary + """ + diff --git a/tests/bdd/flex/lua-table-definitions.feature b/tests/bdd/flex/lua-table-definitions.feature index 24364825d..a061b1b28 100644 --- a/tests/bdd/flex/lua-table-definitions.feature +++ b/tests/bdd/flex/lua-table-definitions.feature @@ -99,40 +99,6 @@ Feature: Table definitions in Lua file When running osm2pgsql flex Then table foo has 1562 rows - Scenario: Unique index is okay - Given the input file 'liechtenstein-2013-08-03.osm.pbf' - And the lua style - """ - local t = osm2pgsql.define_table({ - name = 'foo', - ids = { type = 'node', id_column = 'node_id', index = 'unique' }, - columns = {} - }) - - function osm2pgsql.process_node(object) - t:insert({}) - end - """ - When running osm2pgsql flex - Then table foo has 1562 rows - - Scenario: Primary key is okay - Given the input file 'liechtenstein-2013-08-03.osm.pbf' - And the lua style - """ - local t = osm2pgsql.define_table({ - name = 'foo', - ids = { type = 'node', id_column = 'node_id', index = 'primary_key' }, - columns = {} - }) - - function osm2pgsql.process_node(object) - t:insert({}) - end - """ - When running osm2pgsql flex - Then table foo has 1562 rows - Scenario: Can not create two tables with the same name Given the input file 'liechtenstein-2013-08-03.osm.pbf' And the lua style diff --git a/tests/bdd/steps/steps_db.py b/tests/bdd/steps/steps_db.py index 027d1af92..5d7a99523 100644 --- a/tests/bdd/steps/steps_db.py +++ b/tests/bdd/steps/steps_db.py @@ -153,6 +153,8 @@ def __init__(self, row, headings, factory): self.data.append(None) elif head.lower().startswith('st_astext('): self.data.append(DBValueGeometry(value, props, factory)) + elif props == 'fullmatch': + self.data.append(DBValueRegex(value)) else: self.data.append(str(value)) @@ -234,3 +236,15 @@ def __eq__(self, other): def __repr__(self): return repr(self.value) + + +class DBValueRegex: + + def __init__(self, value): + self.value = str(value) + + def __eq__(self, other): + return re.fullmatch(str(other), self.value) is not None + + def __repr__(self): + return repr(self.value)