]> git.openstreetmap.org Git - nominatim.git/commitdiff
correctly handle removing all postcodes for country
authorSarah Hoffmann <lonvia@denofr.de>
Thu, 13 May 2021 07:59:34 +0000 (09:59 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Thu, 13 May 2021 12:15:42 +0000 (14:15 +0200)
nominatim/tools/postcodes.py
test/bdd/steps/steps_db_ops.py
test/python/dummy_tokenizer.py
test/python/test_tools_postcodes.py

index e172a77ce790020cd4d94ebff0e1d617cc90779f..418b814faafc22c98d7428d2fad0e1a1c8479564 100644 (file)
@@ -142,6 +142,13 @@ def update_postcodes(dsn, project_dir, tokenizer):
     """
     with tokenizer.name_analyzer() as analyzer:
         with connect(dsn) as conn:
+            # First get the list of countries that currently have postcodes.
+            # (Doing this before starting to insert, so it is fast on import.)
+            with conn.cursor() as cur:
+                cur.execute("SELECT DISTINCT country_code FROM location_postcode")
+                todo_countries = set((row[0] for row in cur))
+
+            # Recompute the list of valid postcodes from placex.
             with conn.cursor(name="placex_postcodes") as cur:
                 cur.execute("""SELECT country_code, pc, ST_X(centroid), ST_Y(centroid)
                                FROM (
@@ -161,11 +168,16 @@ def update_postcodes(dsn, project_dir, tokenizer):
                         if collector is not None:
                             collector.commit(conn, analyzer, project_dir)
                         collector = _CountryPostcodesCollector(country)
+                        todo_countries.discard(country)
                     collector.add(postcode, x, y)
 
                 if collector is not None:
                     collector.commit(conn, analyzer, project_dir)
 
+            # Now handle any countries that are only in the postcode table.
+            for country in todo_countries:
+                _CountryPostcodesCollector(country).commit(conn, analyzer, project_dir)
+
             conn.commit()
 
         analyzer.update_postcodes_from_db()
index 6d7bc188905c597211110c13b92952e336bab088..8285f9389dfc8ee8f027950e76ce058ebf19fc14 100644 (file)
@@ -251,7 +251,7 @@ def check_location_postcode(context):
     with context.db.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
         cur.execute("SELECT *, ST_AsText(geometry) as geomtxt FROM location_postcode")
         assert cur.rowcount == len(list(context.table)), \
-            "Postcode table has {} rows, expected {}.".foramt(cur.rowcount, len(list(context.table)))
+            "Postcode table has {} rows, expected {}.".format(cur.rowcount, len(list(context.table)))
 
         results = {}
         for row in cur:
index 8402e160bd76829dc21538ed4581b66af0ba866e..0a86ba8d1598752a00af622e3466f4deede31c0e 100644 (file)
@@ -54,7 +54,7 @@ class DummyNameAnalyzer:
     def normalize_postcode(self, postcode):
         return postcode
 
-    def add_postcodes_from_db(self):
+    def update_postcodes_from_db(self):
         pass
 
     def update_special_phrases(self, phrases):
index f0838b9e80b7a5c3337ea847bea0c29563399eee..4348b0197abff791117f9082518653ef29385b03 100644 (file)
@@ -71,8 +71,7 @@ def test_import_postcodes_add_new(dsn, placex_table, postcode_table, tmp_path, t
 
     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
-    assert postcode_table.row_set == {('xx', '9486', 10, 12),
-                                      ('yy', '9486', 99, 34)}
+    assert postcode_table.row_set == {('xx', '9486', 10, 12), }
 
 
 def test_import_postcodes_replace_coordinates(dsn, placex_table, postcode_table, tmp_path, tokenizer):
@@ -105,6 +104,14 @@ def test_import_postcodes_remove(dsn, placex_table, postcode_table, tmp_path, to
     assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12)}
 
 
+def test_import_postcodes_remove_all(dsn, placex_table, postcode_table, tmp_path, tokenizer):
+    postcode_table.add('ch', '5613', 10, 12)
+
+    postcodes.update_postcodes(dsn, tmp_path, tokenizer)
+
+    assert not postcode_table.row_set
+
+
 def test_import_postcodes_multi_country(dsn, placex_table, postcode_table, tmp_path, tokenizer):
     placex_table.add(country='de', geom='POINT(10 12)',
                      address=dict(postcode='54451'))