From c20f8b13a57f25ce9dff25cf17666f5ddb790812 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sun, 27 Nov 2016 10:12:07 +0100 Subject: [PATCH] add simple db update tests --- test/bdd/db/update/simple.feature | 71 +++++++++++++++++++++++++++++++ test/bdd/environment.py | 3 ++ test/bdd/steps/db_ops.py | 37 +++++++++++++--- 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 test/bdd/db/update/simple.feature diff --git a/test/bdd/db/update/simple.feature b/test/bdd/db/update/simple.feature new file mode 100644 index 00000000..0833c90c --- /dev/null +++ b/test/bdd/db/update/simple.feature @@ -0,0 +1,71 @@ +@DB +Feature: Update of simple objects + Testing simple updating functionality + + Scenario: Do delete small boundary features + Given the places + | osm | class | type | admin | geometry | + | R1 | boundary | administrative | 3 | poly-area:1.0 | + When importing + Then placex contains + | object | rank_search | + | R1 | 6 | + When marking for delete R1 + Then placex has no entry for R1 + + Scenario: Do not delete large boundary features + Given the places + | osm | class | type | admin | geometry | + | R1 | boundary | administrative | 3 | poly-area:5.0 | + When importing + Then placex contains + | object | rank_search | + | R1 | 6 | + When marking for delete R1 + Then placex contains + | object | rank_search | + | R1 | 6 | + + Scenario: Do delete large features of low rank + Given the named places + | osm | class | type | geometry | + | W1 | place | house | poly-area:5.0 | + | R1 | boundary | national_park | poly-area:5.0 | + When importing + Then placex contains + | object | rank_address | + | R1 | 0 | + | W1 | 30 | + When marking for delete R1,W1 + Then placex has no entry for W1 + Then placex has no entry for R1 + + Scenario: type mutation + Given the places + | osm | class | type | geometry | + | N3 | shop | toys | 1 -1 | + When importing + Then placex contains + | object | class | type | centroid | + | N3 | shop | toys | 1 -1 | + When updating places + | osm | class | type | geometry | + | N3 | shop | grocery | 1 -1 | + Then placex contains + | object | class | type | centroid | + | N3 | shop | grocery | 1 -1 | + + Scenario: remove postcode place when house number is added + Given the places + | osm | class | type | postcode | geometry | + | N3 | place | postcode | 12345 | 1 -1 | + When importing + Then placex contains + | object | class | type | + | N3 | place | postcode | + When updating places + | osm | class | type | postcode | housenr | geometry | + | N3 | place | house | 12345 | 13 | 1 -1 | + Then placex contains + | object | class | type | + | N3 | place | house | diff --git a/test/bdd/environment.py b/test/bdd/environment.py index a954b253..cf844f1e 100644 --- a/test/bdd/environment.py +++ b/test/bdd/environment.py @@ -125,6 +125,9 @@ class NominatimEnvironment(object): def run_setup_script(self, *args): self.run_nominatim_script('setup', *args) + def run_update_script(self, *args): + self.run_nominatim_script('update', *args) + def run_nominatim_script(self, script, *args): cmd = [os.path.join(self.build_dir, 'utils', '%s.php' % script)] cmd.extend(['--%s' % x for x in args]) diff --git a/test/bdd/steps/db_ops.py b/test/bdd/steps/db_ops.py index da65984d..82209462 100644 --- a/test/bdd/steps/db_ops.py +++ b/test/bdd/steps/db_ops.py @@ -98,7 +98,7 @@ class NominatimID: params = [self.typ, self. oid] if self.cls is not None: - where += ' class = %s' + where += ' and class = %s' params.append(self.cls) return where, params @@ -116,16 +116,16 @@ def assert_db_column(row, column, value, context): return if column.startswith('centroid'): - fac = float(column[9:]) if h.startswith('centroid*') else 1.0 + fac = float(column[9:]) if column.startswith('centroid*') else 1.0 x, y = value.split(' ') assert_almost_equal(float(x) * fac, row['cx']) assert_almost_equal(float(y) * fac, row['cy']) elif column == 'geometry': geom = context.osm.parse_geometry(value, context.scene) cur = context.db.cursor() - cur.execute("SELECT ST_MaxDistance(%s, ST_SetSRID(%%s::geometry, 4326))" % geom, + cur.execute("SELECT ST_Equals(%s, ST_SetSRID(%%s::geometry, 4326))" % geom, (row['geomtxt'],)) - assert_less(cur.fetchone()[0], 0.005) + eq_(cur.fetchone()[0], True) else: eq_(value, str(row[column]), "Row '%s': expected: %s, got: %s" @@ -219,7 +219,32 @@ def import_and_index_data_from_place_table(context): context.db.commit() context.nominatim.run_setup_script('index', 'index-noanalyse') +@when("updating places") +def update_place_table(context): + context.nominatim.run_setup_script( + 'create-functions', 'create-partition-functions', 'enable-diff-updates') + cur = context.db.cursor() + for r in context.table: + col = PlaceColumn(context, False) + + for h in r.headings: + col.add(h, r[h]) + col.db_insert(cur) + + context.db.commit() + context.nominatim.run_update_script('index') + +@when("marking for delete (?P.*)") +def delete_places(context, oids): + context.nominatim.run_setup_script( + 'create-functions', 'create-partition-functions', 'enable-diff-updates') + cur = context.db.cursor() + for oid in oids.split(','): + where, params = NominatimID(oid).table_select() + cur.execute("DELETE FROM place WHERE " + where, params) + context.db.commit() + context.nominatim.run_update_script('index') @then("placex contains(?P exactly)?") def check_placex_contents(context, exact): @@ -300,7 +325,7 @@ def check_location_property_osmline(context, oid): FROM location_property_osmline WHERE osm_id = %s""", (nid.oid, )) - todo = list(range(len([context.table]))) + todo = list(range(len(list(context.table)))) for res in cur: for i in todo: row = context.table[i] @@ -309,7 +334,7 @@ def check_location_property_osmline(context, oid): todo.remove(i) break else: - assert(False, "Unexpected row %s" % (str(res))) + assert False, "Unexpected row %s" % (str(res)) for h in row.headings: if h in ('start', 'end'): -- 2.39.5