]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_tiger_data.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / tools / test_tiger_data.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Test for tiger data function
9 """
10 import tarfile
11 from textwrap import dedent
12
13 import pytest
14
15 from nominatim.tools import tiger_data, freeze
16 from nominatim.errors import UsageError
17
18 class MockTigerTable:
19
20     def __init__(self, conn):
21         self.conn = conn
22         with conn.cursor() as cur:
23             cur.execute("""CREATE TABLE tiger (linegeo GEOMETRY,
24                                                start INTEGER,
25                                                stop INTEGER,
26                                                interpol TEXT,
27                                                token_info JSONB,
28                                                postcode TEXT)""")
29
30             # We need this table to determine if the database is frozen or not
31             cur.execute("CREATE TABLE place (number INTEGER)")
32
33     def count(self):
34         with self.conn.cursor() as cur:
35             return cur.scalar("SELECT count(*) FROM tiger")
36
37     def row(self):
38         with self.conn.cursor() as cur:
39             cur.execute("SELECT * FROM tiger LIMIT 1")
40             return cur.fetchone()
41
42 @pytest.fixture
43 def tiger_table(def_config, temp_db_conn, sql_preprocessor,
44                 temp_db_with_extensions, tmp_path):
45     def_config.lib_dir.sql = tmp_path / 'sql'
46     def_config.lib_dir.sql.mkdir()
47
48     (def_config.lib_dir.sql / 'tiger_import_start.sql').write_text(
49         """CREATE OR REPLACE FUNCTION tiger_line_import(linegeo GEOMETRY, start INTEGER,
50                                                         stop INTEGER, interpol TEXT,
51                                                         token_info JSONB, postcode TEXT)
52            RETURNS INTEGER AS $$
53             INSERT INTO tiger VALUES(linegeo, start, stop, interpol, token_info, postcode)
54             RETURNING 1
55            $$ LANGUAGE SQL;""")
56     (def_config.lib_dir.sql / 'tiger_import_finish.sql').write_text(
57         """DROP FUNCTION tiger_line_import (linegeo GEOMETRY, in_startnumber INTEGER,
58                                  in_endnumber INTEGER, interpolationtype TEXT,
59                                  token_info JSONB, in_postcode TEXT);""")
60
61     return MockTigerTable(temp_db_conn)
62
63
64 @pytest.fixture
65 def csv_factory(tmp_path):
66     def _mk_file(fname, hnr_from=1, hnr_to=9, interpol='odd', street='Main St',
67                  city='Newtown', state='AL', postcode='12345',
68                  geometry='LINESTRING(-86.466995 32.428956,-86.466923 32.428933)'):
69         (tmp_path / (fname + '.csv')).write_text(dedent("""\
70         from;to;interpolation;street;city;state;postcode;geometry
71         {};{};{};{};{};{};{};{}
72         """.format(hnr_from, hnr_to, interpol, street, city, state,
73                    postcode, geometry)))
74
75     return _mk_file
76
77
78 @pytest.mark.parametrize("threads", (1, 5))
79 def test_add_tiger_data(def_config, src_dir, tiger_table, tokenizer_mock, threads):
80     tiger_data.add_tiger_data(str(src_dir / 'test' / 'testdb' / 'tiger'),
81                               def_config, threads, tokenizer_mock())
82
83     assert tiger_table.count() == 6213
84
85
86 def test_add_tiger_data_database_frozen(def_config, temp_db_conn, tiger_table, tokenizer_mock,
87                                  tmp_path):
88     freeze.drop_update_tables(temp_db_conn)
89
90     with pytest.raises(UsageError) as excinfo:
91         tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
92
93         assert "database frozen" in str(excinfo.value)
94
95     assert tiger_table.count() == 0
96
97 def test_add_tiger_data_no_files(def_config, tiger_table, tokenizer_mock,
98                                  tmp_path):
99     tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
100
101     assert tiger_table.count() == 0
102
103
104 def test_add_tiger_data_bad_file(def_config, tiger_table, tokenizer_mock,
105                                  tmp_path):
106     sqlfile = tmp_path / '1010.csv'
107     sqlfile.write_text("""Random text""")
108
109     tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
110
111     assert tiger_table.count() == 0
112
113
114 def test_add_tiger_data_hnr_nan(def_config, tiger_table, tokenizer_mock,
115                                 csv_factory, tmp_path):
116     csv_factory('file1', hnr_from=99)
117     csv_factory('file2', hnr_from='L12')
118     csv_factory('file3', hnr_to='12.4')
119
120     tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
121
122     assert tiger_table.count() == 1
123     assert tiger_table.row()['start'] == 99
124
125
126 @pytest.mark.parametrize("threads", (1, 5))
127 def test_add_tiger_data_tarfile(def_config, tiger_table, tokenizer_mock,
128                                 tmp_path, src_dir, threads):
129     tar = tarfile.open(str(tmp_path / 'sample.tar.gz'), "w:gz")
130     tar.add(str(src_dir / 'test' / 'testdb' / 'tiger' / '01001.csv'))
131     tar.close()
132
133     tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, threads,
134                               tokenizer_mock())
135
136     assert tiger_table.count() == 6213
137
138
139 def test_add_tiger_data_bad_tarfile(def_config, tiger_table, tokenizer_mock,
140                                     tmp_path):
141     tarfile = tmp_path / 'sample.tar.gz'
142     tarfile.write_text("""Random text""")
143
144     with pytest.raises(UsageError):
145         tiger_data.add_tiger_data(str(tarfile), def_config, 1, tokenizer_mock())
146
147
148 def test_add_tiger_data_empty_tarfile(def_config, tiger_table, tokenizer_mock,
149                                       tmp_path):
150     tar = tarfile.open(str(tmp_path / 'sample.tar.gz'), "w:gz")
151     tar.add(__file__)
152     tar.close()
153
154     tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, 1,
155                               tokenizer_mock())
156
157     assert tiger_table.count() == 0