]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_postcodes.py
Merge pull request #2310 from RhinoDevel/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 import dummy_tokenizer
9
10 @pytest.fixture
11 def tokenizer():
12     return dummy_tokenizer.DummyTokenizer(None, None)
13
14 @pytest.fixture
15 def postcode_table(temp_db_with_extensions, temp_db_cursor, table_factory,
16                    placex_table, word_table):
17     table_factory('location_postcode',
18                   """ place_id BIGINT,
19                       parent_place_id BIGINT,
20                       rank_search SMALLINT,
21                       rank_address SMALLINT,
22                       indexed_status SMALLINT,
23                       indexed_date TIMESTAMP,
24                       country_code varchar(2),
25                       postcode TEXT,
26                       geometry GEOMETRY(Geometry, 4326)""")
27     temp_db_cursor.execute('CREATE SEQUENCE seq_place')
28     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION token_normalized_postcode(postcode TEXT)
29                               RETURNS TEXT AS $$ BEGIN RETURN postcode; END; $$ LANGUAGE plpgsql;
30                            """)
31
32
33 def test_import_postcodes_empty(dsn, temp_db_cursor, postcode_table, tmp_path, tokenizer):
34     postcodes.import_postcodes(dsn, tmp_path, tokenizer)
35
36     assert temp_db_cursor.table_exists('gb_postcode')
37     assert temp_db_cursor.table_exists('us_postcode')
38     assert temp_db_cursor.table_rows('location_postcode') == 0
39
40
41 def test_import_postcodes_from_placex(dsn, temp_db_cursor, postcode_table, tmp_path, tokenizer):
42     temp_db_cursor.execute("""
43         INSERT INTO placex (place_id, country_code, address, geometry)
44           VALUES (1, 'xx', '"postcode"=>"9486"', 'SRID=4326;POINT(10 12)')
45     """)
46
47     postcodes.import_postcodes(dsn, tmp_path, tokenizer)
48
49     rows = temp_db_cursor.row_set(""" SELECT postcode, country_code,
50                                       ST_X(geometry), ST_Y(geometry)
51                                       FROM location_postcode""")
52     print(rows)
53     assert len(rows) == 1
54     assert rows == set((('9486', 'xx', 10, 12), ))
55