1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Test for tiger data function
11 from textwrap import dedent
14 import pytest_asyncio # noqa: F401
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
23 def __init__(self, conn):
25 with conn.cursor() as cur:
26 cur.execute("""CREATE TABLE tiger (linegeo GEOMETRY,
33 # We need this table to determine if the database is frozen or not
34 cur.execute("CREATE TABLE place (number INTEGER)")
35 # We need this table to determine if the database is in reverse-only mode
36 cur.execute("CREATE TABLE search_name (place_id BIGINT)")
39 return execute_scalar(self.conn, "SELECT count(*) FROM tiger")
42 with self.conn.cursor() as cur:
43 cur.execute("SELECT * FROM tiger LIMIT 1")
48 def tiger_table(def_config, temp_db_conn, sql_preprocessor,
49 temp_db_with_extensions, tmp_path):
50 def_config.lib_dir.sql = tmp_path / 'sql'
51 def_config.lib_dir.sql.mkdir()
53 (def_config.lib_dir.sql / 'tiger_import_start.sql').write_text(
54 """CREATE OR REPLACE FUNCTION tiger_line_import(linegeo GEOMETRY, start INTEGER,
55 stop INTEGER, interpol TEXT,
56 token_info JSONB, postcode TEXT)
58 INSERT INTO tiger VALUES(linegeo, start, stop, interpol, token_info, postcode)
61 (def_config.lib_dir.sql / 'tiger_import_finish.sql').write_text(
62 """DROP FUNCTION tiger_line_import (linegeo GEOMETRY, in_startnumber INTEGER,
63 in_endnumber INTEGER, interpolationtype TEXT,
64 token_info JSONB, in_postcode TEXT);""")
66 return MockTigerTable(temp_db_conn)
70 def csv_factory(tmp_path):
71 def _mk_file(fname, hnr_from=1, hnr_to=9, interpol='odd', street='Main St',
72 city='Newtown', state='AL', postcode='12345',
73 geometry='LINESTRING(-86.466995 32.428956,-86.466923 32.428933)'):
74 (tmp_path / (fname + '.csv')).write_text(dedent("""\
75 from;to;interpolation;street;city;state;postcode;geometry
76 {};{};{};{};{};{};{};{}
77 """.format(hnr_from, hnr_to, interpol, street, city, state,
83 @pytest.mark.parametrize("threads", (1, 5))
85 async def test_add_tiger_data(def_config, src_dir, tiger_table, tokenizer_mock, threads):
86 await tiger_data.add_tiger_data(str(src_dir / 'test' / 'testdb' / 'tiger'),
87 def_config, threads, tokenizer_mock())
89 assert tiger_table.count() == 6213
92 @pytest.mark.parametrize("threads", (1, 5))
94 async def test_add_tiger_data_database_frozen(def_config, src_dir, temp_db_conn, tiger_table,
95 tokenizer_mock, threads):
96 freeze.drop_update_tables(temp_db_conn)
98 await tiger_data.add_tiger_data(str(src_dir / 'test' / 'testdb' / 'tiger'),
99 def_config, threads, tokenizer_mock())
101 assert tiger_table.count() == 6213
105 async def test_add_tiger_data_reverse_only(def_config, src_dir, temp_db_conn, tiger_table,
107 with temp_db_conn.cursor() as cur:
108 cur.execute("DROP TABLE search_name")
109 temp_db_conn.commit()
111 with pytest.raises(UsageError,
112 match="Cannot perform tiger import: required tables are missing. "
113 "See https://github.com/osm-search/Nominatim/issues/2463 for details."):
114 await tiger_data.add_tiger_data(str(src_dir / 'test' / 'testdb' / 'tiger'),
115 def_config, 1, tokenizer_mock())
117 assert tiger_table.count() == 0
121 async def test_add_tiger_data_no_files(def_config, tiger_table, tokenizer_mock,
123 await tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
125 assert tiger_table.count() == 0
129 async def test_add_tiger_data_bad_file(def_config, tiger_table, tokenizer_mock,
131 sqlfile = tmp_path / '1010.csv'
132 sqlfile.write_text("""Random text""")
134 await tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
136 assert tiger_table.count() == 0
140 async def test_add_tiger_data_hnr_nan(def_config, tiger_table, tokenizer_mock,
141 csv_factory, tmp_path):
142 csv_factory('file1', hnr_from=99)
143 csv_factory('file2', hnr_from='L12')
144 csv_factory('file3', hnr_to='12.4')
146 await tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
148 assert tiger_table.count() == 1
149 assert tiger_table.row().start == 99
152 @pytest.mark.parametrize("threads", (1, 5))
154 async def test_add_tiger_data_tarfile(def_config, tiger_table, tokenizer_mock,
155 tmp_path, src_dir, threads):
156 tar = tarfile.open(str(tmp_path / 'sample.tar.gz'), "w:gz")
157 tar.add(str(src_dir / 'test' / 'testdb' / 'tiger' / '01001.csv'))
160 await tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, threads,
163 assert tiger_table.count() == 6213
167 async def test_add_tiger_data_bad_tarfile(def_config, tiger_table, tokenizer_mock,
169 tarfile = tmp_path / 'sample.tar.gz'
170 tarfile.write_text("""Random text""")
172 with pytest.raises(UsageError):
173 await tiger_data.add_tiger_data(str(tarfile), def_config, 1, tokenizer_mock())
177 async def test_add_tiger_data_empty_tarfile(def_config, tiger_table, tokenizer_mock,
179 tar = tarfile.open(str(tmp_path / 'sample.tar.gz'), "w:gz")
183 await tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, 1,
186 assert tiger_table.count() == 0