- 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()
-
- while True:
- context.nominatim.run_update_script('index')
-
- cur = context.db.cursor()
- cur.execute("SELECT 'a' FROM placex WHERE indexed_status != 0 LIMIT 1")
- if cur.rowcount == 0:
- break
-
-@then("placex contains(?P<exact> exactly)?")
-def check_placex_contents(context, exact):
- cur = context.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
-
- expected_content = set()
- for row in context.table:
- nid = NominatimID(row['object'])
- where, params = nid.table_select()
- cur.execute("""SELECT *, ST_AsText(geometry) as geomtxt,
- ST_X(centroid) as cx, ST_Y(centroid) as cy
- FROM placex where %s""" % where,
- params)
- assert cur.rowcount > 0, "No rows found for " + row['object']
-
- for res in cur:
- if exact:
- expected_content.add((res['osm_type'], res['osm_id'], res['class']))
- for h in row.headings:
- if h in ('extratags', 'address'):
- if row[h] == '-':
- assert res[h] is None
- else:
- vdict = eval('{' + row[h] + '}')
- assert vdict == res[h]
- elif h.startswith('name'):
- name = h[5:] if h.startswith('name+') else 'name'
- assert name in res['name']
- assert res['name'][name] == row[h]
- elif h.startswith('extratags+'):
- assert res['extratags'][h[10:]] == row[h]
- elif h.startswith('addr+'):
- if row[h] == '-':
- if res['address'] is not None:
- assert h[5:] not in res['address']
- else:
- assert h[5:] in res['address'], "column " + h
- assert res['address'][h[5:]] == row[h], "column %s" % h
- elif h in ('linked_place_id', 'parent_place_id'):
- compare_place_id(row[h], res[h], h, context)
- else:
- assert_db_column(res, h, row[h], context)
-
- if exact:
- cur.execute('SELECT osm_type, osm_id, class from placex')
- assert expected_content == set([(r[0], r[1], r[2]) for r in cur])
-
- context.db.commit()
-
-@then("place contains(?P<exact> exactly)?")
-def check_placex_contents(context, exact):
- cur = context.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
-
- expected_content = set()
- for row in context.table:
- nid = NominatimID(row['object'])
- where, params = nid.table_select()
- cur.execute("""SELECT *, ST_AsText(geometry) as geomtxt,
- ST_GeometryType(geometry) as geometrytype
- FROM place where %s""" % where,
- params)
- assert cur.rowcount > 0, "No rows found for " + row['object']
+ """ Remove entries from the place table. Multiple ids may be given
+ separated by commas. Also runs all triggers
+ related to updates and reindexes the new data.
+ """
+ context.nominatim.run_nominatim('refresh', '--functions')
+ with context.db.cursor() as cur:
+ cur.execute('TRUNCATE place_to_be_deleted')
+ for oid in oids.split(','):
+ NominatimID(oid).query_osm_id(cur, 'DELETE FROM place WHERE {}')
+ cur.execute('SELECT flush_deleted_places()')
+
+ context.nominatim.reindex_placex(context.db)
+
+ # Remove the output of the input, when all was right. Otherwise it will be
+ # output when there are errors that had nothing to do with the import
+ # itself.
+ context.log_capture.buffer.clear()
+
+################################ THEN ##################################
+
+@then("(?P<table>placex|place) contains(?P<exact> exactly)?")
+def check_place_contents(context, table, exact):
+ """ Check contents of place/placex tables. Each row represents a table row
+ and all data must match. Data not present in the expected table, may
+ be arbitry. The rows are identified via the 'object' column which must
+ have an identifier of the form '<NRW><osm id>[:<class>]'. When multiple
+ rows match (for example because 'class' was left out and there are
+ multiple entries for the given OSM object) then all must match. All
+ expected rows are expected to be present with at least one database row.
+ When 'exactly' is given, there must not be additional rows in the database.
+ """
+ with context.db.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
+ expected_content = set()
+ for row in context.table:
+ nid = NominatimID(row['object'])
+ query = 'SELECT *, ST_AsText(geometry) as geomtxt, ST_GeometryType(geometry) as geometrytype'
+ if table == 'placex':
+ query += ' ,ST_X(centroid) as cx, ST_Y(centroid) as cy'
+ query += " FROM %s WHERE {}" % (table, )
+ nid.query_osm_id(cur, query)
+ assert cur.rowcount > 0, "No rows found for " + row['object']
+
+ for res in cur:
+ if exact:
+ expected_content.add((res['osm_type'], res['osm_id'], res['class']))
+
+ DBRow(nid, res, context).assert_row(row, ['object'])
+
+ if exact:
+ cur.execute('SELECT osm_type, osm_id, class from {}'.format(table))
+ actual = set([(r[0], r[1], r[2]) for r in cur])
+ assert expected_content == actual, \
+ f"Missing entries: {expected_content - actual}\n" \
+ f"Not expected in table: {actual - expected_content}"