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