Skip to content

Commit 3913c6d

Browse files
committed
bdd: use a SQL statement template for index checks
Also introduces substring matching.
1 parent 775beff commit 3913c6d

File tree

3 files changed

+92
-65
lines changed

3 files changed

+92
-65
lines changed

tests/bdd/environment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def before_scenario(context, scenario):
122122
context.geometry_factory = GeometryFactory()
123123
context.osm2pgsql_replication.ReplicationServer = ReplicationServerMock()
124124
context.urlrequest_responses = {}
125+
context.sql_statements = {}
125126

126127
def _mock_urlopen(request):
127128
if not request.full_url in context.urlrequest_responses:

tests/bdd/flex/lua-index-definitions.feature

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
Feature: Index definitions in Lua file
22

3+
Background:
4+
Given the SQL statement mytable_indexes
5+
"""
6+
SELECT indexdef, indisprimary as is_primary
7+
FROM pg_catalog.pg_index, pg_catalog.pg_indexes
8+
WHERE schemaname = 'public'
9+
AND tablename = 'mytable'
10+
AND indrelid = tablename::regclass
11+
AND indexrelid = indexname::regclass
12+
"""
13+
314
Scenario: Indexes field in table definition must be an array
415
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
516
And the lua style
@@ -36,9 +47,9 @@ Feature: Index definitions in Lua file
3647
})
3748
"""
3849
When running osm2pgsql flex
39-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING gist (geom)%'
40-
| schemaname | tablename |
41-
| public | mytable |
50+
Then statement mytable_indexes returns
51+
| indexdef@substr |
52+
| USING gist (geom) |
4253

4354
Scenario: Empty indexes field in table definition gets you no index
4455
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -56,8 +67,8 @@ Feature: Index definitions in Lua file
5667
})
5768
"""
5869
When running osm2pgsql flex
59-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable'
60-
| schemaname | tablename |
70+
Then statement mytable_indexes returns exactly
71+
| indexdef |
6172

6273
Scenario: Explicitly setting an index column works
6374
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -77,9 +88,9 @@ Feature: Index definitions in Lua file
7788
})
7889
"""
7990
When running osm2pgsql flex
80-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%'
81-
| schemaname | tablename |
82-
| public | mytable |
91+
Then statement mytable_indexes returns
92+
| indexdef@substr |
93+
| USING btree (name) |
8394

8495
Scenario: Explicitly setting multiple indexes
8596
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -101,15 +112,11 @@ Feature: Index definitions in Lua file
101112
})
102113
"""
103114
When running osm2pgsql flex
104-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%'
105-
| schemaname | tablename |
106-
| public | mytable |
107-
And SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING gist (geom)%'
108-
| schemaname | tablename |
109-
| public | mytable |
110-
And SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name, tags)%'
111-
| schemaname | tablename |
112-
| public | mytable |
115+
Then statement mytable_indexes returns exactly
116+
| indexdef@substr |
117+
| USING btree (name) |
118+
| USING gist (geom) |
119+
| USING btree (name, tags) |
113120

114121
Scenario: Method can not be missing
115122
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -244,9 +251,9 @@ Feature: Index definitions in Lua file
244251
})
245252
"""
246253
When running osm2pgsql flex
247-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (lower(name))%'
248-
| schemaname | tablename |
249-
| public | mytable |
254+
Then statement mytable_indexes returns
255+
| indexdef@substr |
256+
| USING btree (lower(name)) |
250257

251258
@needs-pg-index-includes
252259
Scenario: Include field must be a string or array
@@ -315,9 +322,9 @@ Feature: Index definitions in Lua file
315322
})
316323
"""
317324
When running osm2pgsql flex
318-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%' AND indexdef LIKE '%INCLUDE (tags)%'
319-
| schemaname | tablename |
320-
| public | mytable |
325+
Then statement mytable_indexes returns
326+
| indexdef@substr |
327+
| USING btree (name) INCLUDE (tags) |
321328

322329
@needs-pg-index-includes
323330
Scenario: Include field works with array
@@ -338,9 +345,9 @@ Feature: Index definitions in Lua file
338345
})
339346
"""
340347
When running osm2pgsql flex
341-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%' AND indexdef LIKE '%INCLUDE (tags)%'
342-
| schemaname | tablename |
343-
| public | mytable |
348+
Then statement mytable_indexes returns
349+
| indexdef@substr |
350+
| USING btree (name) INCLUDE (tags) |
344351

345352
Scenario: Tablespace needs a string
346353
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -383,9 +390,9 @@ Feature: Index definitions in Lua file
383390
})
384391
"""
385392
When running osm2pgsql flex
386-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%'
387-
| schemaname | tablename |
388-
| public | mytable |
393+
Then statement mytable_indexes returns
394+
| indexdef@substr |
395+
| USING btree (name) |
389396

390397
Scenario: Unique needs a boolean
391398
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -428,9 +435,9 @@ Feature: Index definitions in Lua file
428435
})
429436
"""
430437
When running osm2pgsql flex
431-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%' AND indexdef LIKE '%UNIQUE%'
432-
| schemaname | tablename |
433-
| public | mytable |
438+
Then statement mytable_indexes returns
439+
| indexdef@fullmatch |
440+
| .*UNIQUE.*USING btree \(name\).* |
434441

435442
Scenario: Where condition needs a string
436443
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -473,9 +480,9 @@ Feature: Index definitions in Lua file
473480
})
474481
"""
475482
When running osm2pgsql flex
476-
Then table pg_catalog.pg_indexes contains
477-
| schemaname | tablename | indexdef@fullmatch |
478-
| public | mytable | .*USING btree \(name\).*WHERE \(name = lower\(name\)\).* |
483+
Then statement mytable_indexes returns
484+
| indexdef@substr |
485+
| USING btree (name) WHERE (name = lower(name)) |
479486

480487

481488
Scenario: Don't create id index if the configuration doesn't mention it
@@ -493,9 +500,9 @@ Feature: Index definitions in Lua file
493500
})
494501
"""
495502
When running osm2pgsql flex
496-
Then table pg_catalog.pg_indexes doesn't contain
497-
| schemaname | tablename | indexname@fullmatch |
498-
| public | mytable | .*node_id.* |
503+
Then statement mytable_indexes returns exactly
504+
| indexdef@substr |
505+
| USING gist (geom) |
499506

500507
Scenario: Don't create id index if the configuration doesn't says so
501508
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -512,9 +519,9 @@ Feature: Index definitions in Lua file
512519
})
513520
"""
514521
When running osm2pgsql flex
515-
Then table pg_catalog.pg_indexes doesn't contain
516-
| schemaname | tablename | indexname@fullmatch |
517-
| public | mytable | .*node_id.* |
522+
Then statement mytable_indexes returns exactly
523+
| indexdef@substr |
524+
| USING gist (geom) |
518525

519526
Scenario: Always create id index if the configuration says so
520527
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -531,16 +538,16 @@ Feature: Index definitions in Lua file
531538
})
532539
"""
533540
When running osm2pgsql flex
534-
Then table pg_catalog.pg_indexes contains
535-
| schemaname | tablename | indexname@fullmatch |
536-
| public | mytable | .*node_id.* |
541+
Then statement mytable_indexes returns
542+
| indexdef@substr |
543+
| USING btree (node_id) |
537544

538545
Scenario: Create a unique id index when requested
539546
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
540547
And the lua style
541548
"""
542549
local t = osm2pgsql.define_table({
543-
name = 'foo',
550+
name = 'mytable',
544551
ids = { type = 'node', id_column = 'node_id', create_index = 'unique' },
545552
columns = {}
546553
})
@@ -550,20 +557,17 @@ Feature: Index definitions in Lua file
550557
end
551558
"""
552559
When running osm2pgsql flex
553-
Then table foo has 1562 rows
554-
Then table pg_catalog.pg_indexes contains
555-
| tablename | indexdef@fullmatch |
556-
| foo | CREATE UNIQUE INDEX .* USING .*\(node_id\) |
557-
And SELECT count(*) FROM pg_catalog.pg_index WHERE indrelid = 'foo'::regclass and indisprimary
558-
| count |
559-
| 0 |
560+
Then table mytable has 1562 rows
561+
Then statement mytable_indexes returns
562+
| indexdef@fullmatch | is_primary |
563+
| CREATE UNIQUE INDEX .* USING .*\(node_id\).* | False |
560564

561565
Scenario: Create a primary key id index when requested
562566
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
563567
And the lua style
564568
"""
565569
local t = osm2pgsql.define_table({
566-
name = 'foo',
570+
name = 'mytable',
567571
ids = { type = 'node', id_column = 'node_id', create_index = 'primary_key' },
568572
columns = {}
569573
})
@@ -573,10 +577,7 @@ Feature: Index definitions in Lua file
573577
end
574578
"""
575579
When running osm2pgsql flex
576-
Then table foo has 1562 rows
577-
Then table pg_catalog.pg_indexes contains
578-
| tablename | indexdef@fullmatch |
579-
| foo | CREATE UNIQUE INDEX .* USING .*\(node_id\) |
580-
And SELECT count(*) FROM pg_catalog.pg_index WHERE indrelid = 'foo'::regclass and indisprimary
581-
| count |
582-
| 1 |
580+
Then table mytable has 1562 rows
581+
Then statement mytable_indexes returns
582+
| indexdef@fullmatch | is_primary |
583+
| CREATE UNIQUE INDEX .* USING .*\(node_id\) | True |

tests/bdd/steps/steps_db.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,30 @@ def db_check_table_absence(context, table):
9292
assert not row in actuals, f"Row unexpectedly found: {row}. Full content:\n{actuals}"
9393

9494

95-
@then("(?P<query>SELECT .*)")
96-
def db_check_sql_statement(context, query):
95+
@given("the SQL statement (?P<sql>.+)")
96+
def db_define_sql_statement(context, sql):
97+
context.sql_statements[sql] = context.text
98+
99+
@then("statement (?P<stmt>.+) returns(?P<exact> exactly)?")
100+
def db_check_sql_statement(context, stmt, exact):
97101
with context.db.cursor() as cur:
98-
cur.execute(query)
102+
assert stmt in context.sql_statements
103+
cur.execute(context.sql_statements[stmt])
99104

100105
actuals = list(DBRow(r, context.table.headings, context.geometry_factory) for r in cur)
101106

102107
linenr = 1
103108
for row in context.table.rows:
104-
assert any(r == row for r in actuals),\
105-
f"{linenr}. entry not found in table. Full content:\n{actuals}"
109+
try:
110+
actuals.remove(row)
111+
except ValueError:
112+
assert False,\
113+
f"{linenr}. entry not found in result. Full response:\n{actuals}"
106114
linenr += 1
107115

116+
assert not exact or not actuals,\
117+
f"Unexpected lines in result:\n{actuals}"
118+
108119

109120
### Helper functions and classes
110121

@@ -152,6 +163,8 @@ def __init__(self, row, headings, factory):
152163
self.data.append(DBValueGeometry(value, props, factory))
153164
elif props == 'fullmatch':
154165
self.data.append(DBValueRegex(value))
166+
elif props == 'substr':
167+
self.data.append(DBValueSubString(value))
155168
else:
156169
self.data.append(str(value))
157170

@@ -310,3 +323,15 @@ def __eq__(self, other):
310323

311324
def __repr__(self):
312325
return repr(self.value)
326+
327+
328+
class DBValueSubString:
329+
330+
def __init__(self, value):
331+
self.value = str(value)
332+
333+
def __eq__(self, other):
334+
return str(other) in self.value
335+
336+
def __repr__(self):
337+
return repr(self.value)

0 commit comments

Comments
 (0)