2 Tests for functions to maintain the artificial postcode table.
 
   8 from nominatim.tools import postcodes
 
  11 class MockPostcodeTable:
 
  12     """ A location_postcode table for testing.
 
  14     def __init__(self, conn):
 
  16         with conn.cursor() as cur:
 
  17             cur.execute("""CREATE TABLE location_postcode (
 
  19                                parent_place_id BIGINT,
 
  21                                rank_address SMALLINT,
 
  22                                indexed_status SMALLINT,
 
  23                                indexed_date TIMESTAMP,
 
  24                                country_code varchar(2),
 
  26                                geometry GEOMETRY(Geometry, 4326))""")
 
  27             cur.execute("""CREATE OR REPLACE FUNCTION token_normalized_postcode(postcode TEXT)
 
  28                            RETURNS TEXT AS $$ BEGIN RETURN postcode; END; $$ LANGUAGE plpgsql;
 
  30                            CREATE OR REPLACE FUNCTION get_country_code(place geometry)
 
  31                            RETURNS TEXT AS $$ BEGIN 
 
  33                            END; $$ LANGUAGE plpgsql;
 
  37     def add(self, country, postcode, x, y):
 
  38         with self.conn.cursor() as cur:
 
  39             cur.execute("""INSERT INTO location_postcode (place_id, indexed_status,
 
  40                                                           country_code, postcode,
 
  42                            VALUES (nextval('seq_place'), 1, %s, %s,
 
  43                                    'SRID=4326;POINT(%s %s)')""",
 
  44                         (country, postcode, x, y))
 
  50         with self.conn.cursor() as cur:
 
  51             cur.execute("""SELECT country_code, postcode,
 
  52                                   ST_X(geometry), ST_Y(geometry)
 
  53                            FROM location_postcode""")
 
  54             return set((tuple(row) for row in cur))
 
  59     return dummy_tokenizer.DummyTokenizer(None, None)
 
  62 def postcode_table(temp_db_conn, placex_table, word_table):
 
  63     return MockPostcodeTable(temp_db_conn)
 
  66 def test_postcodes_empty(dsn, postcode_table, place_table,
 
  68     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
  70     assert not postcode_table.row_set
 
  73 def test_postcodes_add_new(dsn, postcode_table, tmp_path,
 
  74                            insert_implicit_postcode, tokenizer):
 
  75     insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='9486'))
 
  76     postcode_table.add('yy', '9486', 99, 34)
 
  78     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
  80     assert postcode_table.row_set == {('xx', '9486', 10, 12), }
 
  83 def test_postcodes_replace_coordinates(dsn, postcode_table, tmp_path,
 
  84                                        insert_implicit_postcode, tokenizer):
 
  85     insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
 
  86     postcode_table.add('xx', 'AB 4511', 99, 34)
 
  88     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
  90     assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12)}
 
  93 def test_postcodes_replace_coordinates_close(dsn, postcode_table, tmp_path,
 
  94                                              insert_implicit_postcode, tokenizer):
 
  95     insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
 
  96     postcode_table.add('xx', 'AB 4511', 10, 11.99999)
 
  98     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 100     assert postcode_table.row_set == {('xx', 'AB 4511', 10, 11.99999)}
 
 103 def test_postcodes_remove(dsn, postcode_table, tmp_path,
 
 104                           insert_implicit_postcode, tokenizer):
 
 105     insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
 
 106     postcode_table.add('xx', 'badname', 10, 12)
 
 108     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 110     assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12)}
 
 113 def test_postcodes_ignore_empty_country(dsn, postcode_table, tmp_path,
 
 114                                         insert_implicit_postcode, tokenizer):
 
 115     insert_implicit_postcode(1, None, 'POINT(10 12)', dict(postcode='AB 4511'))
 
 116     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 117     assert not postcode_table.row_set
 
 120 def test_postcodes_remove_all(dsn, postcode_table, place_table,
 
 121                               tmp_path, tokenizer):
 
 122     postcode_table.add('ch', '5613', 10, 12)
 
 123     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 125     assert not postcode_table.row_set
 
 128 def test_postcodes_multi_country(dsn, postcode_table, tmp_path,
 
 129                                  insert_implicit_postcode, tokenizer):
 
 130     insert_implicit_postcode(1, 'de', 'POINT(10 12)', dict(postcode='54451'))
 
 131     insert_implicit_postcode(2, 'cc', 'POINT(100 56)', dict(postcode='DD23 T'))
 
 132     insert_implicit_postcode(3, 'de', 'POINT(10.3 11.0)', dict(postcode='54452'))
 
 133     insert_implicit_postcode(4, 'cc', 'POINT(10.3 11.0)', dict(postcode='54452'))
 
 135     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 137     assert postcode_table.row_set == {('de', '54451', 10, 12),
 
 138                                       ('de', '54452', 10.3, 11.0),
 
 139                                       ('cc', '54452', 10.3, 11.0),
 
 140                                       ('cc', 'DD23 T', 100, 56)}
 
 143 @pytest.mark.parametrize("gzipped", [True, False])
 
 144 def test_postcodes_extern(dsn, postcode_table, tmp_path,
 
 145                           insert_implicit_postcode, tokenizer, gzipped):
 
 146     insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
 
 148     extfile = tmp_path / 'xx_postcodes.csv'
 
 149     extfile.write_text("postcode,lat,lon\nAB 4511,-4,-1\nCD 4511,-5, -10")
 
 152         subprocess.run(['gzip', str(extfile)])
 
 153         assert not extfile.is_file()
 
 155     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 157     assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12),
 
 158                                       ('xx', 'CD 4511', -10, -5)}
 
 161 def test_postcodes_extern_bad_column(dsn, postcode_table, tmp_path, 
 
 162                                      insert_implicit_postcode, tokenizer):
 
 163     insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
 
 165     extfile = tmp_path / 'xx_postcodes.csv'
 
 166     extfile.write_text("postode,lat,lon\nAB 4511,-4,-1\nCD 4511,-5, -10")
 
 168     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 170     assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12)}
 
 173 def test_postcodes_extern_bad_number(dsn, insert_implicit_postcode,
 
 174                                      postcode_table, tmp_path, tokenizer):
 
 175     insert_implicit_postcode(1, 'xx', 'POINT(10 12)', dict(postcode='AB 4511'))
 
 177     extfile = tmp_path / 'xx_postcodes.csv'
 
 178     extfile.write_text("postcode,lat,lon\nXX 4511,-4,NaN\nCD 4511,-5, -10\n34,200,0")
 
 180     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 182     assert postcode_table.row_set == {('xx', 'AB 4511', 10, 12),
 
 183                                       ('xx', 'CD 4511', -10, -5)}
 
 185 def test_can_compute(dsn, table_factory):
 
 186     assert not postcodes.can_compute(dsn)
 
 187     table_factory('place')
 
 188     assert postcodes.can_compute(dsn)
 
 190 def test_no_placex_entry(dsn, tmp_path, temp_db_cursor, place_row, postcode_table, tokenizer):
 
 191     #Rewrite the get_country_code function to verify its execution.
 
 192     temp_db_cursor.execute("""
 
 193         CREATE OR REPLACE FUNCTION get_country_code(place geometry)
 
 194         RETURNS TEXT AS $$ BEGIN 
 
 196         END; $$ LANGUAGE plpgsql;
 
 198     place_row(geom='SRID=4326;POINT(10 12)', address=dict(postcode='AB 4511'))
 
 199     postcodes.update_postcodes(dsn, tmp_path, tokenizer)
 
 201     assert postcode_table.row_set == {('fr', 'AB 4511', 10, 12)}
 
 204 def insert_implicit_postcode(placex_table, place_row):
 
 206         Inserts data into the placex and place table
 
 207         which can then be used to compute one postcode.
 
 209     def _insert_implicit_postcode(osm_id, country, geometry, address):
 
 210         placex_table.add(osm_id=osm_id, country=country, geom=geometry)
 
 211         place_row(osm_id=osm_id, geom='SRID=4326;'+geometry, address=address)
 
 213     return _insert_implicit_postcode