Coverage for tests / test_shpfile_handler.py: 99%

72 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-11 16:02 +0000

1""" 

2Tests for the shapefile handler interface. 

3""" 

4 

5import unittest 

6import shutil 

7from pathlib import Path 

8 

9from acspsuedo.query import shapefile_handler 

10from acspsuedo.source.shpfile import ShpFileHandler, ShpfileWarning, ShpfileFormatterException 

11 

12from acspsuedo.fips.states import PUERTO_RICO 

13 

14 

15 

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

17 

18 

19 

20class TestShpFileHandler(unittest.TestCase): 

21 

22 def setUp(self) -> None: 

23 

24 self.GEOG_SPECS_ZCTA = {'zip_code_tabulation_area': '*'} 

25 self.ZCTA_2000_URL = 'https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/2000/tl_2010_us_zcta500.zip' 

26 

27 self.GEOG_SPECS_UA = {'urban_area': '*'} 

28 self.UA_2008_URL = 'https://www2.census.gov/geo/tiger/TIGER2008/tl_2008_us_uac00.zip' 

29 

30 self.GEOG_SPECS_GUAM_BG = {'state': '66', 'block_group': '*'} 

31 self.GUAM_BG_2008_URL = 'https://www2.census.gov/geo/tiger/TIGER2008/66_GUAM/tl_2008_66_bg00.zip' 

32 

33 self.GEOG_SPECS_IDAHO_TRACT = {'state': '16', 'tract': '*'} 

34 self.IDAHO_TRACT_2010_URL = 'https://www2.census.gov/geo/tiger/TIGER2010/TRACT/2010/tl_2010_16_tract10.zip' 

35 

36 self.MISSING_GEOG_SPECS_NY_PUMA = {'public_use_microdata_area': '*'} 

37 

38 def test_shapefile_handler_instance(self): 

39 """ 

40 Simulate a scenario where a user introspects the shapefile handler interface. 

41 """ 

42 sfh_interface = ShpFileHandler(cache_path = Path.cwd() / 'tests' / 'instance_cache') 

43 

44 # Auto caching preferences 

45 self.assertTrue(sfh_interface.auto_cache) 

46 sfh_interface.auto_cache = False 

47 self.assertFalse(sfh_interface.auto_cache) 

48 

49 # Tracking new caches 

50 self.assertTrue(sfh_interface.track_updated_cache) 

51 sfh_interface.track_updated_cache = False 

52 self.assertFalse(sfh_interface.track_updated_cache) 

53 

54 # Cache path preferences 

55 self.assertEqual(sfh_interface.cache_path, Path.cwd() / 'tests' / 'instance_cache') 

56 with self.assertRaises(AttributeError): 

57 del sfh_interface.cache_path 

58 

59 def test_shapefile_handler_tracking(self): 

60 """ 

61 Simulate a scenario where a user updates the cache location. 

62 """ 

63 old_cache = Path.cwd() / 'tests' / 'instance_cache' 

64 sfh_interface = ShpFileHandler(cache_path = old_cache) 

65 

66 sfh_interface._cache_fetch_tiger_shpfile(2013, state = PUERTO_RICO, subminor_civil_division = '*') 

67 sfh_interface._cache_fetch_tiger_shpfile(2020, state = PUERTO_RICO, subminor_civil_division = '*') 

68 

69 new_cache = Path.cwd() / 'tests' / 'new_instance_cache' 

70 new_cache.mkdir(parents = True, exist_ok = True) 

71 sfh_interface.cache_path = new_cache 

72 

73 sfh_interface._cache_fetch_tiger_shpfile(2013, state = PUERTO_RICO, subminor_civil_division = '*') 

74 sfh_interface._cache_fetch_tiger_shpfile(2020, state = PUERTO_RICO, subminor_civil_division = '*') 

75 sfh_interface.cache_path = old_cache 

76 

77 new_cache.rmdir() 

78 

79 self.assertTrue(any(old_cache.iterdir())) 

80 

81 shutil.rmtree(old_cache) 

82 

83 self.assertFalse(old_cache.exists()) 

84 

85 

86 def test_shpfile_url_constructor_2000_zcta(self) -> None: 

87 """ 

88 URL constructor for zip-code tabulation areas, 2000. 

89 """ 

90 url_comps = shapefile_handler._tiger_url_fmtter(year = 2000, **self.GEOG_SPECS_ZCTA) 

91 url = ''.join(url_comps) 

92 

93 self.assertEqual(url, self.ZCTA_2000_URL) 

94 

95 def test_shpfile_url_constructor_2008_guam_block_group(self) -> None: 

96 """ 

97 URL constructor for block groups in Guam, 2008. 

98 """ 

99 url_comps = shapefile_handler._tiger_url_fmtter(year = 2008, **self.GEOG_SPECS_GUAM_BG) 

100 url = ''.join(url_comps) 

101 

102 self.assertEqual(url, self.GUAM_BG_2008_URL) 

103 

104 def test_shpfile_url_constructor_2008_ua(self) -> None: 

105 """ 

106 URL constructor for urban areas, 2008. 

107 """ 

108 url_comps = shapefile_handler._tiger_url_fmtter(year = 2008, **self.GEOG_SPECS_UA) 

109 url = ''.join(url_comps) 

110 

111 self.assertEqual(url, self.UA_2008_URL) 

112 

113 def test_shpfile_url_constructor_2010_idaho_tracts(self) -> None: 

114 """ 

115 URL constructor for census tracts in Idaho, 2010. 

116 """ 

117 url_comps = shapefile_handler._tiger_url_fmtter(year = 2010, **self.GEOG_SPECS_IDAHO_TRACT) 

118 url = ''.join(url_comps) 

119 

120 self.assertEqual(url, self.IDAHO_TRACT_2010_URL) 

121 

122 

123 

124 def test_shpfile_existence_2008_ua(self) -> None: 

125 """ 

126 A successful TIGER shapefile query for urban areas, 2008. 

127 """ 

128 shapefile_handler.fetch_tiger_shpfile(year = 2008, **self.GEOG_SPECS_UA) 

129 

130 def test_shpfile_existence_2010_idaho_tracts(self) -> None: 

131 """ 

132 A successful TIGER shapefile query for census tracts in Idaho, 2010. 

133 """ 

134 shapefile_handler.fetch_tiger_shpfile(year = 2010, **self.GEOG_SPECS_IDAHO_TRACT) 

135 

136 def test_failed_shpfile_query_nonexistence_2011_zcta(self) -> None: 

137 """ 

138 A failed TIGER shapefile query due to non-existence. 

139 """ 

140 with self.assertWarns(ShpfileWarning): 

141 shapefile_handler.fetch_tiger_shpfile(year = 2011, **self.GEOG_SPECS_ZCTA) 

142 

143 def test_failed_shpfile_query_missing_formatters_2024_new_york_puma(self) -> None: 

144 """ 

145 A failed TIGER shapefile query due to missing formatters. 

146 """ 

147 with self.assertRaises(ShpfileFormatterException): 

148 shapefile_handler.fetch_tiger_shpfile(year = 2024, **self.MISSING_GEOG_SPECS_NY_PUMA) 

149 

150 

151if __name__ == '__main__': 

152 unittest.main()