Skip to content

Commit 9000fce

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

File tree

3 files changed

+91
-65
lines changed

3 files changed

+91
-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: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
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' and tablename = 'mytable'
9+
and indrelid = tablename::regclass
10+
and indexrelid = indexname::regclass
11+
"""
12+
313
Scenario: Indexes field in table definition must be an array
414
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
515
And the lua style
@@ -36,9 +46,9 @@ Feature: Index definitions in Lua file
3646
})
3747
"""
3848
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 |
49+
Then statement mytable_indexes returns
50+
| indexdef@substr |
51+
| USING gist (geom) |
4252

4353
Scenario: Empty indexes field in table definition gets you no index
4454
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -56,8 +66,8 @@ Feature: Index definitions in Lua file
5666
})
5767
"""
5868
When running osm2pgsql flex
59-
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable'
60-
| schemaname | tablename |
69+
Then statement mytable_indexes returns exactly
70+
| indexdef |
6171

6272
Scenario: Explicitly setting an index column works
6373
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -77,9 +87,9 @@ Feature: Index definitions in Lua file
7787
})
7888
"""
7989
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 |
90+
Then statement mytable_indexes returns
91+
| indexdef@substr |
92+
| USING btree (name) |
8393

8494
Scenario: Explicitly setting multiple indexes
8595
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -101,15 +111,11 @@ Feature: Index definitions in Lua file
101111
})
102112
"""
103113
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 |
114+
Then statement mytable_indexes returns exactly
115+
| indexdef@substr |
116+
| USING btree (name) |
117+
| USING gist (geom) |
118+
| USING btree (name, tags) |
113119

114120
Scenario: Method can not be missing
115121
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -244,9 +250,9 @@ Feature: Index definitions in Lua file
244250
})
245251
"""
246252
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 |
253+
Then statement mytable_indexes returns
254+
| indexdef@substr |
255+
| USING btree (lower(name)) |
250256

251257
@needs-pg-index-includes
252258
Scenario: Include field must be a string or array
@@ -315,9 +321,9 @@ Feature: Index definitions in Lua file
315321
})
316322
"""
317323
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 |
324+
Then statement mytable_indexes returns
325+
| indexdef@substr |
326+
| USING btree (name) INCLUDE (tags) |
321327

322328
@needs-pg-index-includes
323329
Scenario: Include field works with array
@@ -338,9 +344,9 @@ Feature: Index definitions in Lua file
338344
})
339345
"""
340346
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 |
347+
Then statement mytable_indexes returns
348+
| indexdef@substr |
349+
| USING btree (name) INCLUDE (tags) |
344350

345351
Scenario: Tablespace needs a string
346352
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -383,9 +389,9 @@ Feature: Index definitions in Lua file
383389
})
384390
"""
385391
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 |
392+
Then statement mytable_indexes returns
393+
| indexdef@substr |
394+
| USING btree (name) |
389395

390396
Scenario: Unique needs a boolean
391397
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -428,9 +434,9 @@ Feature: Index definitions in Lua file
428434
})
429435
"""
430436
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 |
437+
Then statement mytable_indexes returns
438+
| indexdef@fullmatch |
439+
| .*UNIQUE.*USING btree \(name\).* |
434440

435441
Scenario: Where condition needs a string
436442
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
@@ -473,9 +479,9 @@ Feature: Index definitions in Lua file
473479
})
474480
"""
475481
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\)\).* |
482+
Then statement mytable_indexes returns
483+
| indexdef@substr |
484+
| USING btree (name) WHERE (name = lower(name)) |
479485

480486

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

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

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

538544
Scenario: Create a unique id index when requested
539545
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
540546
And the lua style
541547
"""
542548
local t = osm2pgsql.define_table({
543-
name = 'foo',
549+
name = 'mytable',
544550
ids = { type = 'node', id_column = 'node_id', create_index = 'unique' },
545551
columns = {}
546552
})
@@ -550,20 +556,17 @@ Feature: Index definitions in Lua file
550556
end
551557
"""
552558
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 |
559+
Then table mytable has 1562 rows
560+
Then statement mytable_indexes returns
561+
| indexdef@fullmatch | is_primary |
562+
| CREATE UNIQUE INDEX .* USING .*\(node_id\).* | False |
560563

561564
Scenario: Create a primary key id index when requested
562565
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
563566
And the lua style
564567
"""
565568
local t = osm2pgsql.define_table({
566-
name = 'foo',
569+
name = 'mytable',
567570
ids = { type = 'node', id_column = 'node_id', create_index = 'primary_key' },
568571
columns = {}
569572
})
@@ -573,10 +576,7 @@ Feature: Index definitions in Lua file
573576
end
574577
"""
575578
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 |
579+
Then table mytable has 1562 rows
580+
Then statement mytable_indexes returns
581+
| indexdef@fullmatch | is_primary |
582+
| 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)