11"""Test async geocoder initialization and data file loading."""
22
33import os
4+
45import psycopg
56import pytest
67import pytest_asyncio
78
8- from dataclasses import dataclass
99from pg_nearest_city .async_nearest_city import AsyncNearestCity
10- from pg_nearest_city .base_nearest_city import Location , InitializationStatus , DbConfig
10+ from pg_nearest_city .base_nearest_city import DbConfig , Location
1111
1212
1313def get_test_config ():
@@ -20,24 +20,25 @@ def get_test_config():
2020 port = int (os .getenv ("PGNEAREST_TEST_PORT" , "5432" )),
2121 )
2222
23- @pytest_asyncio .fixture (loop_scope = "function" )
23+
24+ @pytest_asyncio .fixture ()
2425async def test_db ():
2526 """Provide a clean database connection for each test."""
2627 config = get_test_config ()
27-
28+
2829 # Create a single connection for the test
2930 conn = await psycopg .AsyncConnection .connect (config .get_connection_string ())
30-
31+
3132 # Clean up any existing state
3233 async with conn .cursor () as cur :
3334 await cur .execute ("DROP TABLE IF EXISTS pg_nearest_city_geocoding;" )
3435 await conn .commit ()
35-
36+
3637 yield conn
37-
38+
3839 await conn .close ()
3940
40- @ pytest . mark . asyncio ( loop_scope = "function" )
41+
4142async def test_full_initialization_connect ():
4243 """Test completet database initialization and basic query through connect method."""
4344 async with AsyncNearestCity .connect (get_test_config ()) as geocoder :
@@ -47,7 +48,7 @@ async def test_full_initialization_connect():
4748 assert location .city == "New York City"
4849 assert isinstance (location , Location )
4950
50- @ pytest . mark . asyncio
51+
5152async def test_full_initialization (test_db ):
5253 """Test complete database initialization and basic query."""
5354 geocoder = AsyncNearestCity (test_db )
@@ -59,17 +60,17 @@ async def test_full_initialization(test_db):
5960 assert location .city == "New York City"
6061 assert isinstance (location , Location )
6162
62- @ pytest . mark . asyncio
63+
6364async def test_check_initialization_fresh_database (test_db ):
6465 """Test initialization check on a fresh database with no tables."""
6566 geocoder = AsyncNearestCity (test_db )
6667 async with test_db .cursor () as cur :
6768 status = await geocoder ._check_initialization_status (cur )
68-
69+
6970 assert not status .is_fully_initialized
7071 assert not status .has_table
7172
72- @ pytest . mark . asyncio
73+
7374async def test_check_initialization_incomplete_table (test_db ):
7475 """Test initialization check with a table that's missing columns."""
7576 geocoder = AsyncNearestCity (test_db )
@@ -82,30 +83,30 @@ async def test_check_initialization_incomplete_table(test_db):
8283 );
8384 """ )
8485 await test_db .commit ()
85-
86+
8687 status = await geocoder ._check_initialization_status (cur )
87-
88+
8889 assert not status .is_fully_initialized
8990 assert status .has_table
9091 assert not status .has_valid_structure
9192
92- @ pytest . mark . asyncio
93+
9394async def test_check_initialization_empty_table (test_db ):
9495 """Test initialization check with properly structured but empty table."""
9596 geocoder = AsyncNearestCity (test_db )
9697
9798 async with test_db .cursor () as cur :
9899 await geocoder ._create_geocoding_table (cur )
99100 await test_db .commit ()
100-
101+
101102 status = await geocoder ._check_initialization_status (cur )
102-
103+
103104 assert not status .is_fully_initialized
104105 assert status .has_table
105106 assert status .has_valid_structure
106107 assert not status .has_data
107108
108- @ pytest . mark . asyncio
109+
109110async def test_check_initialization_missing_voronoi (test_db ):
110111 """Test initialization check when Voronoi polygons are missing."""
111112 geocoder = AsyncNearestCity (test_db )
@@ -114,14 +115,14 @@ async def test_check_initialization_missing_voronoi(test_db):
114115 await geocoder ._create_geocoding_table (cur )
115116 await geocoder ._import_cities (cur )
116117 await test_db .commit ()
117-
118+
118119 status = await geocoder ._check_initialization_status (cur )
119-
120+
120121 assert not status .is_fully_initialized
121122 assert status .has_data
122123 assert not status .has_complete_voronoi
123124
124- @ pytest . mark . asyncio
125+
125126async def test_check_initialization_missing_index (test_db ):
126127 """Test initialization check when spatial index is missing."""
127128 geocoder = AsyncNearestCity (test_db )
@@ -131,36 +132,36 @@ async def test_check_initialization_missing_index(test_db):
131132 await geocoder ._import_cities (cur )
132133 await geocoder ._import_voronoi_polygons (cur )
133134 await test_db .commit ()
134-
135+
135136 status = await geocoder ._check_initialization_status (cur )
136-
137+
137138 assert not status .is_fully_initialized
138139 assert status .has_data
139140 assert status .has_complete_voronoi
140141 assert not status .has_spatial_index
141142
142- @ pytest . mark . asyncio
143+
143144async def test_check_initialization_complete (test_db ):
144145 """Test initialization check with a properly initialized database."""
145146 geocoder = AsyncNearestCity (test_db )
146147 await geocoder .initialize ()
147148
148149 async with test_db .cursor () as cur :
149150 status = await geocoder ._check_initialization_status (cur )
150-
151+
151152 assert status .is_fully_initialized
152153 assert status .has_spatial_index
153154 assert status .has_complete_voronoi
154155 assert status .has_data
155156
156- @ pytest . mark . asyncio
157+
157158async def test_invalid_coordinates (test_db ):
158159 """Test that invalid coordinates are properly handled."""
159160 geocoder = AsyncNearestCity (test_db )
160161 await geocoder .initialize ()
161-
162+
162163 with pytest .raises (ValueError ):
163164 await geocoder .query (91 , 0 ) # Invalid latitude
164-
165+
165166 with pytest .raises (ValueError ):
166167 await geocoder .query (0 , 181 ) # Invalid longitude
0 commit comments