Skip to content

Commit bba7fdf

Browse files
committed
feat: Add support for additional data fields and improve test coverage
- Remove unused format parameter from geocoding client - Add comprehensive test coverage for new data models (ZIP+4, Canadian riding, Statistics Canada, FFIEC) - Reorganize and clean up existing test cases
1 parent 9378982 commit bba7fdf

File tree

2 files changed

+101
-31
lines changed

2 files changed

+101
-31
lines changed

src/geocodio/client.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def geocode(
4242
fields: Optional[List[str]] = None,
4343
limit: Optional[int] = None,
4444
country: Optional[str] = None,
45-
format: Optional[str] = None,
4645
) -> GeocodingResponse:
4746
params: Dict[str, Union[str, int]] = {"api_key": self.api_key}
4847
if fields:
@@ -51,8 +50,6 @@ def geocode(
5150
params["limit"] = int(limit)
5251
if country:
5352
params["country"] = country
54-
if format:
55-
params["format"] = format
5653

5754
endpoint: str
5855
data: Union[List, Dict] | None

tests/test_models.py

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
import pytest
22
from geocodio.models import (
33
AddressComponents, Timezone, CongressionalDistrict,
4-
GeocodioFields, GeocodingResult, GeocodingResponse, Location, StateLegislativeDistrict, SchoolDistrict, CensusData, Demographics, Economics, Families, Housing
4+
GeocodioFields, GeocodingResult, GeocodingResponse, Location, StateLegislativeDistrict, SchoolDistrict, CensusData, Demographics, Economics, Families, Housing, Social, ZIP4Data, CanadianRiding, StatisticsCanadaData, FFIECData
55
)
66

77

8+
def test_has_extras_mixin():
9+
"""Test the _HasExtras mixin functionality."""
10+
# Test with AddressComponents as an example
11+
data = {
12+
"number": "1109",
13+
"street": "Highland",
14+
"suffix": "St",
15+
"city": "Arlington",
16+
"state": "VA",
17+
"zip": "22201",
18+
"extra_field": "extra value",
19+
"another_extra": 123
20+
}
21+
22+
ac = AddressComponents.from_api(data)
23+
24+
# Test get_extra with default value
25+
assert ac.get_extra("nonexistent", "default") == "default"
26+
27+
# Test get_extra with existing value
28+
assert ac.get_extra("extra_field") == "extra value"
29+
assert ac.get_extra("another_extra") == 123
30+
31+
# Test attribute access for extras
32+
assert ac.extra_field == "extra value"
33+
assert ac.another_extra == 123
34+
35+
# Test attribute error for non-existent fields
36+
with pytest.raises(AttributeError):
37+
_ = ac.nonexistent_field
38+
39+
840
def test_address_components_extras():
941
# Test that extra fields are stored in extras
1042
data = {
@@ -26,9 +58,6 @@ def test_address_components_extras():
2658
assert ac.city == "Arlington"
2759
assert ac.state == "VA"
2860
assert ac.zip == "22201"
29-
assert ac.get_extra("extra_field") == "extra value"
30-
assert ac.get_extra("another_extra") == 123
31-
assert ac.get_extra("nonexistent", "default") == "default"
3261

3362

3463
def test_timezone_extras():
@@ -45,16 +74,6 @@ def test_timezone_extras():
4574
assert tz.name == "America/New_York"
4675
assert tz.utc_offset == -5
4776
assert tz.observes_dst is True
48-
assert tz.get_extra("extra_field") == "extra value"
49-
assert tz.get_extra("nonexistent", "default") == "default"
50-
51-
52-
def test_has_extras_attribute_error():
53-
# Test that accessing non-existent attributes raises AttributeError
54-
ac = AddressComponents.from_api({"number": "1109"})
55-
56-
with pytest.raises(AttributeError):
57-
_ = ac.nonexistent_field
5877

5978

6079
def test_geocoding_response_empty_results():
@@ -109,8 +128,6 @@ def test_state_legislative_district_extras():
109128
assert district.chamber == "house"
110129
assert district.ocd_id == "ocd-division/country:us/state:va/sldl:8"
111130
assert district.proportion == 1.0
112-
assert district.get_extra("extra_field") == "extra value"
113-
assert district.get_extra("nonexistent", "default") == "default"
114131

115132

116133
def test_school_district_extras():
@@ -129,8 +146,6 @@ def test_school_district_extras():
129146
assert district.district_number == "001"
130147
assert district.lea_id == "5100000"
131148
assert district.nces_id == "5100000"
132-
assert district.get_extra("extra_field") == "extra value"
133-
assert district.get_extra("nonexistent", "default") == "default"
134149

135150

136151
def test_census_data_extras():
@@ -155,8 +170,6 @@ def test_census_data_extras():
155170
assert census.state_fips == "51"
156171
assert census.msa_code == "47900"
157172
assert census.csa_code == "548"
158-
assert census.get_extra("extra_field") == "extra value"
159-
assert census.get_extra("nonexistent", "default") == "default"
160173

161174

162175
def test_demographics_extras():
@@ -183,8 +196,6 @@ def test_demographics_extras():
183196
assert demographics.black_population == 200
184197
assert demographics.asian_population == 100
185198
assert demographics.hispanic_population == 100
186-
assert demographics.get_extra("extra_field") == "extra value"
187-
assert demographics.get_extra("nonexistent", "default") == "default"
188199

189200

190201
def test_economics_extras():
@@ -205,8 +216,6 @@ def test_economics_extras():
205216
assert economics.per_capita_income == 35000
206217
assert economics.poverty_rate == 10.5
207218
assert economics.unemployment_rate == 5.2
208-
assert economics.get_extra("extra_field") == "extra value"
209-
assert economics.get_extra("nonexistent", "default") == "default"
210219

211220

212221
def test_families_extras():
@@ -231,8 +240,6 @@ def test_families_extras():
231240
assert families.single_male_households == 100
232241
assert families.single_female_households == 100
233242
assert families.average_household_size == 2.5
234-
assert families.get_extra("extra_field") == "extra value"
235-
assert families.get_extra("nonexistent", "default") == "default"
236243

237244

238245
def test_housing_extras():
@@ -257,5 +264,71 @@ def test_housing_extras():
257264
assert housing.renter_occupied_units == 300
258265
assert housing.median_home_value == 350000
259266
assert housing.median_rent == 1500
260-
assert housing.get_extra("extra_field") == "extra value"
261-
assert housing.get_extra("nonexistent", "default") == "default"
267+
268+
269+
def test_social_extras():
270+
# Test that extra fields are stored in extras
271+
data = {
272+
"high_school_graduate_or_higher": 800,
273+
"bachelors_degree_or_higher": 400,
274+
"graduate_degree_or_higher": 200,
275+
"veterans": 100,
276+
"veterans_percentage": 10.5,
277+
"extra_field": "extra value"
278+
}
279+
280+
social = Social.from_api(data)
281+
282+
assert social.high_school_graduate_or_higher == 800
283+
assert social.bachelors_degree_or_higher == 400
284+
assert social.graduate_degree_or_higher == 200
285+
assert social.veterans == 100
286+
assert social.veterans_percentage == 10.5
287+
288+
289+
def test_zip4_data():
290+
"""Test ZIP+4 data model."""
291+
data = {
292+
"zip4": "1234",
293+
"delivery_point": "01",
294+
"carrier_route": "C001",
295+
"extra_field": "extra value"
296+
}
297+
zip4 = ZIP4Data.from_api(data)
298+
assert zip4.zip4 == "1234"
299+
assert zip4.delivery_point == "01"
300+
assert zip4.carrier_route == "C001"
301+
assert zip4.get_extra("extra_field") == "extra value"
302+
303+
304+
def test_canadian_riding():
305+
"""Test Canadian riding data model."""
306+
data = {
307+
"name": "Toronto Centre",
308+
"number": "35052",
309+
"ocd_id": "ocd-division/country:ca/ed:35052",
310+
"extra_field": "extra value"
311+
}
312+
riding = CanadianRiding.from_api(data)
313+
assert riding.name == "Toronto Centre"
314+
assert riding.number == "35052"
315+
assert riding.ocd_id == "ocd-division/country:ca/ed:35052"
316+
assert riding.get_extra("extra_field") == "extra value"
317+
318+
319+
def test_statistics_canada_data():
320+
"""Test Statistics Canada data model."""
321+
data = {
322+
"extra_field": "extra value"
323+
}
324+
statcan = StatisticsCanadaData.from_api(data)
325+
assert statcan.get_extra("extra_field") == "extra value"
326+
327+
328+
def test_ffiec_data():
329+
"""Test FFIEC data model."""
330+
data = {
331+
"extra_field": "extra value"
332+
}
333+
ffiec = FFIECData.from_api(data)
334+
assert ffiec.get_extra("extra_field") == "extra value"

0 commit comments

Comments
 (0)