]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_postcodes.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / test_tools_postcodes.py
1 """
2 Tests for functions to maintain the artificial postcode table.
3 """
4
5 import pytest
6
7 from nominatim.tools import postcodes
8
9 @pytest.fixture
10 def postcode_table(temp_db_with_extensions, temp_db_cursor, table_factory,
11                    placex_table, word_table):
12     table_factory('location_postcode',
13                   """ place_id BIGINT,
14                       parent_place_id BIGINT,
15                       rank_search SMALLINT,
16                       rank_address SMALLINT,
17                       indexed_status SMALLINT,
18                       indexed_date TIMESTAMP,
19                       country_code varchar(2),
20                       postcode TEXT,
21                       geometry GEOMETRY(Geometry, 4326)""")
22     temp_db_cursor.execute('CREATE SEQUENCE seq_place')
23     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION getorcreate_postcode_id(postcode TEXT)
24                               RETURNS INTEGER AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;
25                            """)
26
27
28 def test_import_postcodes_empty(dsn, temp_db_cursor, postcode_table, tmp_path):
29     postcodes.import_postcodes(dsn, tmp_path)
30
31     assert temp_db_cursor.table_exists('gb_postcode')
32     assert temp_db_cursor.table_exists('us_postcode')
33     assert temp_db_cursor.table_rows('location_postcode') == 0
34
35
36 def test_import_postcodes_from_placex(dsn, temp_db_cursor, postcode_table, tmp_path):
37     temp_db_cursor.execute("""
38         INSERT INTO placex (place_id, country_code, address, geometry)
39           VALUES (1, 'xx', '"postcode"=>"9486"', 'SRID=4326;POINT(10 12)')
40     """)
41
42     postcodes.import_postcodes(dsn, tmp_path)
43
44     rows = temp_db_cursor.row_set(""" SELECT postcode, country_code,
45                                       ST_X(geometry), ST_Y(geometry)
46                                       FROM location_postcode""")
47     print(rows)
48     assert len(rows) == 1
49     assert rows == set((('9486', 'xx', 10, 12), ))
50