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

1""" 

2Tests for the download functions. 

3""" 

4 

5import unittest 

6from pathlib import Path 

7 

8import pandas as pd 

9import geopandas as gpd 

10 

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 

15 

16 

17 

18apq.shapefile_handler.cache_path = Path.cwd() / 'tests' / 'cache' 

19 

20 

21 

22class TestDownload(unittest.TestCase): 

23 

24 def setUp(self) -> None: 

25 self.DATASET = ACS5_PROFILE 

26 self.YEAR = 2009 

27 self.VARS = ['DP04_0016E'] 

28 

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) 

37 

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) 

44 

45 self.assertEqual(query1, query2) 

46 

47 query3 = apq.confined_download(0.1, state = NY) 

48 

49 self.assertNotEqual(query3, query1) 

50 

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) 

56 

57 self.assertEqual(query1.area_threshold, 0.8) 

58 

59 with self.assertRaises(ValueError): 

60 query1.area_threshold = -0.1 

61 

62 query1.area_threshold = 0.5 

63 

64 self.assertTrue(query1.geographic_specifiers == {'place': Albany, 'state': NY}) 

65 

66 self.assertFalse(query1 == dict(state = NY, place = Albany)) 

67 

68 query1.geographic_specifiers = {'state': NY} 

69 

70 self.assertTrue(str(query1) == '_ConfinedDownload(area_threshold = 0.5, geographic_specifiers = {state = 36})') 

71 

72 def test_confined_query_download(self): 

73 cfi = apq.confined_download(0.8, state = NY, place = Albany) 

74 

75 gdf = cfi.download( 

76 self.DATASET, 

77 self.YEAR, 

78 variables = self.VARS, 

79 include_geometries = True, 

80 state = NY, 

81 tract = '*' 

82 ) 

83 

84 self.assertIsInstance(gdf, gpd.GeoDataFrame) 

85 

86 df = cfi.download( 

87 self.DATASET, 

88 self.YEAR, 

89 variables = self.VARS, 

90 state = NY, 

91 tract = '*' 

92 ) 

93 

94 self.assertIsInstance(df, pd.DataFrame) 

95 

96 # NOTE: 

97 # Due to our deterministic rules, we have provided 

98 

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. 

103 

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') 

108 

109 with self.assertWarns(UserWarning): 

110 cfq.download( 

111 self.DATASET, 

112 2011, 

113 variables = self.VARS, 

114 zip_code_tabulation_area = '*', 

115 ) 

116 

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. 

121 

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') 

126 

127 with self.assertWarns(UserWarning): 

128 cfq.download( 

129 self.DATASET, 

130 2016, 

131 variables = self.VARS, 

132 state = '*', 

133 ) 

134 

135 

136 

137if __name__ == '__main__': 

138 unittest.main()