Coverage for tests / test_confined_download.py: 98%
51 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 download functions.
3"""
5import unittest
6from pathlib import Path
8import pandas as pd
9import geopandas as gpd
11import acspsuedo.query as apq
12from acspsuedo.datasets import ACS5_PROFILE
13from acspsuedo.fips.states import NY
14from acspsuedo.fips.places.new_york import Albany
18apq.shapefile_handler.cache_path = Path.cwd() / 'tests' / 'cache'
22class TestDownload(unittest.TestCase):
24 def setUp(self) -> None:
25 self.DATASET = ACS5_PROFILE
26 self.YEAR = 2009
27 self.VARS = ['DP04_0016E']
29 def test_confined_query_arg_space(self):
30 """
31 Test the argument space for the confined query interface.
32 """
33 with self.assertRaises(ValueError):
34 apq.confined_download(-0.1)
35 with self.assertRaises(ValueError):
36 apq.confined_download(1.1)
38 def test_confined_query_instance_equality(self):
39 """
40 Simulate a scenario where confined query interfaces are equal/unequal.
41 """
42 query1 = apq.confined_download(0.8, state = NY, place = Albany)
43 query2 = apq.confined_download(0.80, place = Albany, state = NY)
45 self.assertEqual(query1, query2)
47 query3 = apq.confined_download(0.1, state = NY)
49 self.assertNotEqual(query3, query1)
51 def test_confined_query_instance(self):
52 """
53 Simulate a scenario where a user introspects the confined query interface.
54 """
55 query1 = apq.confined_download(0.8, state = NY, place = Albany)
57 self.assertEqual(query1.area_threshold, 0.8)
59 with self.assertRaises(ValueError):
60 query1.area_threshold = -0.1
62 query1.area_threshold = 0.5
64 self.assertTrue(query1.geographic_specifiers == {'place': Albany, 'state': NY})
66 self.assertFalse(query1 == dict(state = NY, place = Albany))
68 query1.geographic_specifiers = {'state': NY}
70 self.assertTrue(str(query1) == '_ConfinedDownload(area_threshold = 0.5, geographic_specifiers = {state = 36})')
72 def test_confined_query_download(self):
73 cfi = apq.confined_download(0.8, state = NY, place = Albany)
75 gdf = cfi.download(
76 self.DATASET,
77 self.YEAR,
78 variables = self.VARS,
79 include_geometries = True,
80 state = NY,
81 tract = '*'
82 )
84 self.assertIsInstance(gdf, gpd.GeoDataFrame)
86 df = cfi.download(
87 self.DATASET,
88 self.YEAR,
89 variables = self.VARS,
90 state = NY,
91 tract = '*'
92 )
94 self.assertIsInstance(df, pd.DataFrame)
96 # NOTE:
97 # Due to our deterministic rules, we have provided
99 def test_confined_query_download_without_inner_geometries(self):
100 """
101 Simulate a known scenario whereby the geographic information for the
102 inner-layer set of geographies cannot be found or is non-existent.
104 The example queries data for ZCTAs within New York's 11th congressional
105 district for the congressional session corresponding to 2011.
106 """
107 cfq = apq.confined_download(0.8, state = NY, congressional_district = '11')
109 with self.assertWarns(UserWarning):
110 cfq.download(
111 self.DATASET,
112 2011,
113 variables = self.VARS,
114 zip_code_tabulation_area = '*',
115 )
117 def test_confined_query_download_without_outer_geometries(self):
118 """
119 Simulate a known scenario whereby the geographic information for the
120 outer-layer set of geographies cannot be found or is non-existent.
122 The example queries data for all states in the New England division.
123 Cf. https://www2.census.gov/geo/pdfs/maps-data/maps/reference/us_regdiv.pdf
124 """
125 cfq = apq.confined_download(0.8, division = '2')
127 with self.assertWarns(UserWarning):
128 cfq.download(
129 self.DATASET,
130 2016,
131 variables = self.VARS,
132 state = '*',
133 )
137if __name__ == '__main__':
138 unittest.main()