Coverage for tests / test_geo_spec_formatter.py: 98%
58 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-11 16:02 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-11 16:02 +0000
1"""
2Tests for the geographic specifier formatter interface.
3"""
5import unittest
7from acspsuedo.source.geog import GeoSpecFmtter, GeoScopeException
8from acspsuedo.source.low.exceptions import APIException
9from acspsuedo.datasets import ACS1, ACS5
10from acspsuedo.fips.states import MONTANA
11from acspsuedo.fips.counties.montana import Lewis_And_Clark_County
15class TestGeoSpecFmtter(unittest.TestCase):
17 def setUp(self) -> None:
18 self.GSF_INTERFACE = GeoSpecFmtter
20 self.DATASET = ACS1
21 self.YEAR = 2014
23 self.PANDEMIC_YEAR = 2020
25 self.GEO_SPECS_MONTANA_COUNTIES = {'state': MONTANA, 'county': '*'}
26 self.GEO_SPECS_MONTANA_COUNTIES_FMTTER = '&for=county:*&in=state:30'
28 self.GEO_SPECS_LEWIS_AND_CLARK_COUNTY = {'state': MONTANA, 'county': Lewis_And_Clark_County}
29 self.GEO_SPECS_LEWIS_AND_CLARK_COUNTY_FMTTER = '&for=county:049&in=state:30'
31 self.GEO_SPECS_NOT_SUPPORTED = ['state', 'block_group']
33 self.GEO_SPECS_UNSUPPORTED_WC = {'state': '*', 'school_district_unified': '*'}
35 def test_interface_instance(self):
36 """
37 Check the dunder methods for instances of the interface.
38 """
39 gsf_obj = self.GSF_INTERFACE(state = '53', place = '63000')
40 self.assertTrue(len(gsf_obj) == 2)
42 self.assertEqual(str(gsf_obj), "GeoSpecFmtter(state = '53', place = '63000')")
44 self.assertIsInstance(gsf_obj(self.DATASET, self.YEAR), str)
46 self.assertEqual(gsf_obj.geog_specifiers, {'place': '63000', 'state': '53'})
48 gsf_obj.geog_specifiers = {'state': '53'}
50 def test_check_path_existence(self):
51 """
52 Check if our path existence locator works.
53 """
54 self.GSF_INTERFACE.check_path_existence(
55 self.DATASET,
56 self.YEAR,
57 'state'
58 )
60 def test_geo_spec_formatter_montana_counties_acs1_2014(self):
61 """
62 Formatter for Montana counties, 2014.
63 """
64 fmtter, _ = self.GSF_INTERFACE.get_fmt_path(
65 self.DATASET,
66 self.YEAR,
67 **self.GEO_SPECS_MONTANA_COUNTIES
68 )
70 self.assertEqual(fmtter, self.GEO_SPECS_MONTANA_COUNTIES_FMTTER)
72 def test_geo_spec_formatter_lewis_and_clark_county_acs1_2014(self):
73 """
74 Formatter for Lewis and Clark county, Montana, 2014.
75 """
76 fmtter, _ = self.GSF_INTERFACE.get_fmt_path(
77 self.DATASET,
78 self.YEAR,
79 **self.GEO_SPECS_LEWIS_AND_CLARK_COUNTY
80 )
82 self.assertEqual(fmtter, self.GEO_SPECS_LEWIS_AND_CLARK_COUNTY_FMTTER)
84 def test_partial_path_inference_acs5_2009(self):
85 """
86 Check if the default to partial inference works.
87 """
88 with self.assertRaises(GeoScopeException):
89 self.GSF_INTERFACE.get_fmt_path(
90 ACS5, 2009, block_group = '*', foo = 'bar'
91 )
93 with self.assertRaises(GeoScopeException):
94 self.GSF_INTERFACE._infer_path(
95 ACS5, 2009, school_district_elementary = '*'
96 )
98 def test_unsupported_year_acs1_2020_2004(self):
99 """
100 Check if an error is raised for unsupported years.
101 """
102 with self.assertRaises(APIException):
103 self.GSF_INTERFACE.get_fmt_path(
104 self.DATASET,
105 self.PANDEMIC_YEAR,
106 **self.GEO_SPECS_MONTANA_COUNTIES
107 )
109 with self.assertRaises(APIException):
110 self.GSF_INTERFACE.get_fmt_path(
111 self.DATASET,
112 2004,
113 **self.GEO_SPECS_MONTANA_COUNTIES
114 )
116 def test_unsupported_dataset(self):
117 """
118 Check if an error is raised for an unsupported dataset.
119 """
120 with self.assertRaises(KeyError):
121 self.GSF_INTERFACE.get_fmt_path(
122 'foo/bar',
123 self.YEAR,
124 **self.GEO_SPECS_MONTANA_COUNTIES
125 )
127 def test_cache_acs1_2014(self):
128 """
129 Check if our paths are represented as a list of lists.
130 """
131 paths = self.GSF_INTERFACE.view_geographic_paths(self.DATASET, self.YEAR)
133 self.assertTrue(paths, list)
134 self.assertTrue(all(isinstance(path, list) for path in paths))
136 def test_unsupported_specifiers_acs1_2014(self):
137 """
138 Check if an unsupported set of geographic specifiers raises a warning.
139 """
140 with self.assertWarns(UserWarning):
141 self.GSF_INTERFACE.check_path_existence(
142 self.DATASET,
143 self.YEAR,
144 self.GEO_SPECS_NOT_SUPPORTED
145 )
147 def test_unsupported_wildcard_specifier_acs1_2014(self):
148 """
149 Check if the interface raises an error for specifiers that are
150 accidentally supplied with a wildcard operator.
151 """
152 with self.assertRaises(GeoScopeException):
153 self.GSF_INTERFACE._infer_path(
154 self.DATASET,
155 self.YEAR,
156 **self.GEO_SPECS_UNSUPPORTED_WC
157 )
164if __name__ == '__main__':
165 unittest.main()