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