Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 543 lines (421 sloc) 23.307 kb
42b0198 Added stub unit tests.
cgroskopf authored
1 #!/usr/bin/env python
2
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
3 from django.utils import unittest
42b0198 Added stub unit tests.
cgroskopf authored
4
5 import config
70eb7a3 @JoeGermuska push mongo collections back into dataprocessing/utils
JoeGermuska authored
6 import utils
42b0198 Added stub unit tests.
cgroskopf authored
7
8 class TestSimpleGeographies(unittest.TestCase):
9 def setUp(self):
7bc7b0d @JoeGermuska fix tests -- just broke the mongo connection management
JoeGermuska authored
10 self.geographies = utils.get_geography_collection()
42b0198 Added stub unit tests.
cgroskopf authored
11
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
12 def _test_totalpop(self, obj, known_2000, known_2010):
13 """
14 Shortcut to test "total population" field from the P1 (race)
15 table since this table exists for both 2000 and 2010.
16 """
feee23c Added tract split test from Paul.
cgroskopf authored
17 known_delta = known_2010 - known_2000
18 known_pct = float(known_delta) / float(known_2000)
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
19
875c153 First sf test passes.
cgroskopf authored
20 self.assertEqual(float(obj['data']['2000']["P1"]['P001001']), known_2000)
21 self.assertEqual(float(obj['data']['2010']["P1"]['P001001']), known_2010)
22 self.assertEqual(float(obj['data']['delta']["P1"]['P001001']), known_delta)
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
23 self.assertAlmostEqual(
875c153 First sf test passes.
cgroskopf authored
24 float(obj['data']['pct_change']["P1"]['P001001']),
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
25 known_pct
26 )
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
27
34a1bcd Added more tests.
cgroskopf authored
28 def test_only_complete_geographies(self):
29 geos = self.geographies.find({ 'metadata.GEOCOMP': { '$ne': '00' }})
30 self.assertEqual(geos.count(), 0)
31
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
32 def test_state_count(self):
1a7cb8d More tests pass.
cgroskopf authored
33 states = self.geographies.find({ 'sumlev': '040' })
34
35 self.assertEqual(states.count(), 1)
42b0198 Added stub unit tests.
cgroskopf authored
36
37 def test_state(self):
feee23c Added tract split test from Paul.
cgroskopf authored
38 """
875c153 First sf test passes.
cgroskopf authored
39 Data import test against known values that Hawaii should have.
feee23c Added tract split test from Paul.
cgroskopf authored
40 """
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
41 states = self.geographies.find({ 'geoid': config.SUMLEV_STATE })
42b0198 Added stub unit tests.
cgroskopf authored
42
43 self.assertEqual(states.count(), 1)
44
45 state = states[0]
46
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
47 self.assertEqual(state['sumlev'], config.SUMLEV_STATE)
875c153 First sf test passes.
cgroskopf authored
48 self.assertEqual(state['metadata']['NAME'], 'Hawaii')
49 self.assertEqual(state['metadata']['STATE'], '15')
42b0198 Added stub unit tests.
cgroskopf authored
50
875c153 First sf test passes.
cgroskopf authored
51 pop_2000 = 1211537
52 pop_2010 = 1360301
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
53 self._test_totalpop(state, pop_2000, pop_2010)
9789495 Added notes to test stubs.
cgroskopf authored
54
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
55 def test_county_count(self):
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
56 counties = self.geographies.find({ 'sumlev': config.SUMLEV_COUNTY })
1a7cb8d More tests pass.
cgroskopf authored
57
58 self.assertEqual(counties.count(), 5)
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
59
42b0198 Added stub unit tests.
cgroskopf authored
60 def test_county(self):
feee23c Added tract split test from Paul.
cgroskopf authored
61 """
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
62 Data import test against known values that Maui County, HI should have.
feee23c Added tract split test from Paul.
cgroskopf authored
63 """
3847d62 Simple tests pass.
cgroskopf authored
64 counties = self.geographies.find({ 'geoid': '15009' })
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
65
66 self.assertEqual(counties.count(), 1)
67
68 county = counties[0]
69
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
70 self.assertEqual(county['sumlev'], config.SUMLEV_COUNTY)
3847d62 Simple tests pass.
cgroskopf authored
71 self.assertEqual(county['metadata']['NAME'], 'Maui County')
72 self.assertEqual(county['metadata']['STATE'], '15')
73 self.assertEqual(county['metadata']['COUNTY'], '009')
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
74
3847d62 Simple tests pass.
cgroskopf authored
75 pop_2000 = 128094
76 pop_2010 = 154834
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
77 self._test_totalpop(county, pop_2000, pop_2010)
78
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
79 def test_county_subdivision_count(self):
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
80 county_subdivisions = self.geographies.find({ 'sumlev': config.SUMLEV_COUNTY_SUBDIVISION })
1a7cb8d More tests pass.
cgroskopf authored
81
82 self.assertEqual(county_subdivisions.count(), 44)
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
83
84 def test_county_subdivision(self):
7a81f53 Added more tests.
cgroskopf authored
85 """
86 Data import test against known values that Hilo CCD County Subdivision, HI should have.
87 """
88 counties = self.geographies.find({ 'geoid': '1500190630' })
89
90 self.assertEqual(counties.count(), 1)
91
92 county = counties[0]
93
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
94 self.assertEqual(county['sumlev'], config.SUMLEV_COUNTY_SUBDIVISION)
7a81f53 Added more tests.
cgroskopf authored
95 self.assertEqual(county['metadata']['NAME'], 'Hilo CCD')
96 self.assertEqual(county['metadata']['STATE'], '15')
97 self.assertEqual(county['metadata']['COUNTY'], '001')
98
99 pop_2000 = 42425
100 pop_2010 = 45714
101 self._test_totalpop(county, pop_2000, pop_2010)
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
102
103 def test_place_count(self):
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
104 places = self.geographies.find({ 'sumlev': config.SUMLEV_PLACE })
1a7cb8d More tests pass.
cgroskopf authored
105
106 self.assertEqual(places.count(), 151)
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
107
42b0198 Added stub unit tests.
cgroskopf authored
108 def test_place(self):
feee23c Added tract split test from Paul.
cgroskopf authored
109 """
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
110 Data import test against known values that Pearl City CDP, HI should have.
feee23c Added tract split test from Paul.
cgroskopf authored
111 """
3847d62 Simple tests pass.
cgroskopf authored
112 places = self.geographies.find({ 'geoid': '1562600' })
42b0198 Added stub unit tests.
cgroskopf authored
113
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
114 self.assertEqual(places.count(), 1)
115
116 place = places[0]
117
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
118 self.assertEqual(place['sumlev'], config.SUMLEV_PLACE)
3847d62 Simple tests pass.
cgroskopf authored
119 self.assertEqual(place['metadata']['NAME'], 'Pearl City CDP')
120 self.assertEqual(place['metadata']['STATE'], '15')
121 self.assertEqual(place['metadata']['PLACE'], '62600')
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
122
3847d62 Simple tests pass.
cgroskopf authored
123 pop_2000 = 30976
124 pop_2010 = 47698
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
125 self._test_totalpop(place, pop_2000, pop_2010)
126
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
127 def test_tract_count(self):
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
128 tracts = self.geographies.find({ 'sumlev': config.SUMLEV_TRACT })
1a7cb8d More tests pass.
cgroskopf authored
129
130 self.assertEqual(tracts.count(), 351)
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
131
f40b83e @mtigas stub for tract crosswalk unittest
mtigas authored
132 def test_simple_tract(self):
feee23c Added tract split test from Paul.
cgroskopf authored
133 """
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
134 Data import test against known values that Tract 405, HI should have.
feee23c Added tract split test from Paul.
cgroskopf authored
135 """
3847d62 Simple tests pass.
cgroskopf authored
136 tracts = self.geographies.find({ 'geoid': '15007040500' })
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
137
138 self.assertEqual(tracts.count(), 1)
139
140 tract = tracts[0]
141
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
142 self.assertEqual(tract['sumlev'], config.SUMLEV_TRACT)
3847d62 Simple tests pass.
cgroskopf authored
143 self.assertEqual(tract['metadata']['NAME'], 'Census Tract 405')
144 self.assertEqual(tract['metadata']['STATE'], '15')
145 self.assertEqual(tract['metadata']['COUNTY'], '007')
42b0198 Added stub unit tests.
cgroskopf authored
146
3847d62 Simple tests pass.
cgroskopf authored
147 pop_2000 = 5162
148 pop_2010 = 5943
df9dec0 @mtigas partially flesh out unit tests for dataprocessing/importer bits
mtigas authored
149 self._test_totalpop(tract, pop_2000, pop_2010)
42b0198 Added stub unit tests.
cgroskopf authored
150
793a9ea @onyxfish Added simple tests for blocks.
onyxfish authored
151 def test_block_count(self):
152 if config.SUMLEV_BLOCK not in config.SUMLEVS:
153 pass
154
155 blocks = self.geographies.find({ 'sumlev': config.SUMLEV_BLOCK })
156
157 self.assertEqual(blocks.count(), 25016)
158
159 def test_simple_block(self):
160 """
161 Data import test against known values for Block 3029 in Tract 210.05, HI.
162
163 Note: The test block had the same geography but a different name in 2000.
164 It was geoid 150010210011277 in that census.
165 """
166 if config.SUMLEV_BLOCK not in config.SUMLEVS:
167 pass
168
169 blocks = self.geographies.find({ 'geoid': '150010210053029' })
170
171 self.assertEqual(blocks.count(), 1)
172
173 block = blocks[0]
174
175 self.assertEqual(block['sumlev'], config.SUMLEV_BLOCK)
176 self.assertEqual(block['metadata']['NAME'], 'Block 3029')
177 self.assertEqual(block['metadata']['STATE'], '15')
178 self.assertEqual(block['metadata']['COUNTY'], '001')
179 self.assertEqual(block['metadata']['TRACT'], '021005')
180
181 pop_2000 = 33
182 pop_2010 = 93
183 self._test_totalpop(block, pop_2000, pop_2010)
184
f40b83e @mtigas stub for tract crosswalk unittest
mtigas authored
185 class TestTracts(unittest.TestCase):
186 def setUp(self):
7bc7b0d @JoeGermuska fix tests -- just broke the mongo connection management
JoeGermuska authored
187 self.geographies = utils.get_geography_collection()
f40b83e @mtigas stub for tract crosswalk unittest
mtigas authored
188
feee23c Added tract split test from Paul.
cgroskopf authored
189 def test_tract_split(self):
190 """
a28cd07 Added merged tract test from Paul.
cgroskopf authored
191 Verify that a split tract is crosswalked correctly.
feee23c Added tract split test from Paul.
cgroskopf authored
192 """
193 # Check that split tract does not exist in 2010
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
194 split_tract = self.geographies.find({ 'geoid': '15003003500' })
feee23c Added tract split test from Paul.
cgroskopf authored
195 self.assertEqual(split_tract.count(), 0)
196
197 # Validate first new tract from the split tract
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
198 # Tract 35.01
199 tract1 = self.geographies.find({ 'geoid': '15003003501' })
feee23c Added tract split test from Paul.
cgroskopf authored
200 self.assertEqual(tract1.count(), 1)
201 tract1 = tract1[0]
202
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
203 split_tract_pop_2000 = 5834
bf33f3b Tests pass.
cgroskopf authored
204 tract1_pop_pct = 0.3706
feee23c Added tract split test from Paul.
cgroskopf authored
205 tract1_pop_2000 = int(tract1_pop_pct * split_tract_pop_2000)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
206 tract1_pop_2010 = 2282
591b833 Added delta + pct_change tests for split tracts.
cgroskopf authored
207 tract1_pop_delta = tract1_pop_2010 - tract1_pop_2000
208 tract1_pop_pct_change = float(tract1_pop_delta) / tract1_pop_2000
feee23c Added tract split test from Paul.
cgroskopf authored
209
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
210 self.assertAlmostEqual(tract1['xwalk']['15003003500']['POPPCT00'], tract1_pop_pct, places=4)
211 self.assertAlmostEqual(tract1['data']['2000']['P1']['P001001'], tract1_pop_2000)
bf33f3b Tests pass.
cgroskopf authored
212 self.assertAlmostEqual(float(tract1['data']['2010']['P1']['P001001']), tract1_pop_2010)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
213 self.assertAlmostEqual(float(tract1['data']['delta']['P1']['P001001']), tract1_pop_delta)
214 self.assertAlmostEqual(float(tract1['data']['pct_change']['P1']['P001001']), tract1_pop_pct_change)
a243565 Added housing percentages to xwalk attr of geographies.
cgroskopf authored
215
feee23c Added tract split test from Paul.
cgroskopf authored
216 # Validate second new part from the split tract
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
217 # Tract 35.02
218 tract2 = self.geographies.find({ 'geoid': '15003003502' })
feee23c Added tract split test from Paul.
cgroskopf authored
219 self.assertEqual(tract2.count(), 1)
220 tract2 = tract2[0]
221
bf33f3b Tests pass.
cgroskopf authored
222 tract2_pop_pct = 0.6294
feee23c Added tract split test from Paul.
cgroskopf authored
223 tract2_pop_2000 = int(tract2_pop_pct * split_tract_pop_2000)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
224 tract2_pop_2010 = 3876
591b833 Added delta + pct_change tests for split tracts.
cgroskopf authored
225 tract2_pop_delta = tract2_pop_2010 - tract2_pop_2000
226 tract2_pop_pct_change = float(tract2_pop_delta) / tract2_pop_2000
feee23c Added tract split test from Paul.
cgroskopf authored
227
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
228 self.assertAlmostEqual(tract2['xwalk']['15003003500']['POPPCT00'], tract2_pop_pct, places=4)
229 self.assertAlmostEqual(tract2['data']['2000']['P1']['P001001'], tract2_pop_2000)
bf33f3b Tests pass.
cgroskopf authored
230 self.assertAlmostEqual(float(tract2['data']['2010']['P1']['P001001']), tract2_pop_2010)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
231 self.assertAlmostEqual(float(tract2['data']['delta']['P1']['P001001']), tract2_pop_delta)
232 self.assertAlmostEqual(float(tract2['data']['pct_change']['P1']['P001001']), tract2_pop_pct_change)
feee23c Added tract split test from Paul.
cgroskopf authored
233
234 # Verify that no other tracts got crosswalk allocations from the split tract
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
235 allocated = self.geographies.find({ 'xwalk.15003003500': { '$exists': True } })
feee23c Added tract split test from Paul.
cgroskopf authored
236 self.assertEqual(allocated.count(), 2)
f40b83e @mtigas stub for tract crosswalk unittest
mtigas authored
237
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
238 def test_tract_split_housing(self):
239 """
240 Verify that a split tract is crosswalked correctly.
241 """
242 # Validate first new tract from the split tract
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
243 # Tract 35.01
244 tract1 = self.geographies.find({ 'geoid': '15003003501' })
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
245 self.assertEqual(tract1.count(), 1)
246 tract1 = tract1[0]
247
bf33f3b Tests pass.
cgroskopf authored
248 split_tract_house_2000 = 3370
249 tract1_house_pct = 0.383
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
250 tract1_house_2000 = int(tract1_house_pct * split_tract_house_2000)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
251 tract1_house_2010 = 1353
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
252 tract1_house_delta = tract1_house_2010 - tract1_house_2000
253 tract1_house_pct_change = float(tract1_house_delta) / tract1_house_2000
254
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
255 self.assertAlmostEqual(tract1['xwalk']['15003003500']['HUPCT00'], tract1_house_pct, places=4)
256 self.assertAlmostEqual(tract1['data']['2000']['H1']['H001001'], tract1_house_2000)
bf33f3b Tests pass.
cgroskopf authored
257 self.assertAlmostEqual(float(tract1['data']['2010']['H1']['H001001']), tract1_house_2010)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
258 self.assertAlmostEqual(float(tract1['data']['delta']['H1']['H001001']), tract1_house_delta)
259 self.assertAlmostEqual(float(tract1['data']['pct_change']['H1']['H001001']), tract1_house_pct_change)
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
260
261 # Validate second new part from the split tract
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
262 # Tract 35.02
263 tract2 = self.geographies.find({ 'geoid': '15003003502' })
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
264 self.assertEqual(tract2.count(), 1)
265 tract2 = tract2[0]
266
bf33f3b Tests pass.
cgroskopf authored
267 tract2_house_pct = 0.617
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
268 tract2_house_2000 = int(tract2_house_pct * split_tract_house_2000)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
269 tract2_house_2010 = 2180
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
270 tract2_house_delta = tract2_house_2010 - tract2_house_2000
271 tract2_house_pct_change = float(tract2_house_delta) / tract2_house_2000
272
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
273 self.assertAlmostEqual(tract2['xwalk']['15003003500']['HUPCT00'], tract2_house_pct, places=4)
274 self.assertAlmostEqual(tract2['data']['2000']['H1']['H001001'], tract2_house_2000)
bf33f3b Tests pass.
cgroskopf authored
275 self.assertAlmostEqual(float(tract2['data']['2010']['H1']['H001001']), tract2_house_2010)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
276 self.assertAlmostEqual(float(tract2['data']['delta']['H1']['H001001']), tract2_house_delta)
277 self.assertAlmostEqual(float(tract2['data']['pct_change']['H1']['H001001']), tract2_house_pct_change)
f102fce Updated crosswalk to use housing percentages for appropriate tables.
cgroskopf authored
278
a28cd07 Added merged tract test from Paul.
cgroskopf authored
279 def test_tract_merged(self):
280 """
281 Verify that a merged tract is crosswalked correctly.
282
283 TODO - test housing
284 """
285 # Verify that the first dissolved tract no longer exists
3f800a5 Merged tract test.
cgroskopf authored
286 tract1 = self.geographies.find({ 'geoid': '15003008607' })
a28cd07 Added merged tract test from Paul.
cgroskopf authored
287 self.assertEqual(tract1.count(), 0)
288
3f800a5 Merged tract test.
cgroskopf authored
289 tract2 = self.geographies.find({ 'geoid': '15003008608' })
a28cd07 Added merged tract test from Paul.
cgroskopf authored
290 self.assertEqual(tract2.count(), 0)
291
3f800a5 Merged tract test.
cgroskopf authored
292 tract3 = self.geographies.find({ 'geoid': '15003008500' })
293 self.assertEqual(tract3.count(), 0)
294
a28cd07 Added merged tract test from Paul.
cgroskopf authored
295 # Compute crosswalked values
3f800a5 Merged tract test.
cgroskopf authored
296 tract1_pop_2000 = 1544
297 tract2_pop_2000 = 0
298 tract3_pop_2000 = 1311
299 merged_pop_2000 = tract1_pop_2000 # only this tract contributed population
300 merged_pop_2010 = 5493
5454075 Added delta and pct_change tests for merged tracts.
cgroskopf authored
301 merged_pop_delta = merged_pop_2010 - merged_pop_2000
7eb4974 More testing.
cgroskopf authored
302 merged_pop_pct_change = float(merged_pop_delta) / merged_pop_2000
a28cd07 Added merged tract test from Paul.
cgroskopf authored
303
304 # Verify that the merged tract is correct
3f800a5 Merged tract test.
cgroskopf authored
305 merged_tract = self.geographies.find({ 'geoid': '15003011500' })
a28cd07 Added merged tract test from Paul.
cgroskopf authored
306 self.assertEqual(merged_tract.count(), 1)
307 merged_tract = merged_tract[0]
308
3f800a5 Merged tract test.
cgroskopf authored
309 self.assertEqual(len(merged_tract['xwalk']), 3)
310 self.assertEqual(merged_tract['xwalk']['15003008607']['POPPCT00'], 1.0)
311 self.assertEqual(merged_tract['xwalk']['15003008608']['POPPCT00'], 1.0)
312 self.assertEqual(merged_tract['xwalk']['15003008500']['POPPCT00'], 0.0)
a28cd07 Added merged tract test from Paul.
cgroskopf authored
313
3f800a5 Merged tract test.
cgroskopf authored
314 self.assertEqual(float(merged_tract['data']['2000']['P1']['P001001']), merged_pop_2000)
315 self.assertEqual(float(merged_tract['data']['2010']['P1']['P001001']), merged_pop_2010)
316 self.assertEqual(float(merged_tract['data']['delta']['P1']['P001001']), merged_pop_delta)
317 self.assertAlmostEqual(float(merged_tract['data']['pct_change']['P1']['P001001']), merged_pop_pct_change)
a243565 Added housing percentages to xwalk attr of geographies.
cgroskopf authored
318
3f800a5 Merged tract test.
cgroskopf authored
319 self.assertEqual(merged_tract['xwalk']['15003008607']['HUPCT00'], 1.0)
320 self.assertEqual(merged_tract['xwalk']['15003008608']['HUPCT00'], 1.0)
321 self.assertEqual(merged_tract['xwalk']['15003008500']['HUPCT00'], 0.0)
322
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
323 def test_tract_complex_merge(self):
324 # Verify state of 2010 status of tracts which contributed to the merged tract
325 tract1 = self.geographies.find({ 'geoid': '15001021300' })
326 self.assertEqual(tract1.count(), 1)
327
328 tract2 = self.geographies.find({ 'geoid': '15001021400' })
329 self.assertEqual(tract2.count(), 0)
330
331 tract3 = self.geographies.find({ 'geoid': '15001021503' })
332 self.assertEqual(tract3.count(), 0)
333
334 # Compute crosswalked values
1a7cb8d More tests pass.
cgroskopf authored
335 tract1_house_2000 = 2269
336 tract1_house_2000_pct = 0.0065
337 tract2_house_2000 = 1245
338 tract2_house_2000_pct = 0.9938
339 tract3_house_2000 = 2991
340 tract3_house_2000_pct = 0.0351
341 merged_house_2000 = int(sum([
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
342 tract1_house_2000 * tract1_house_2000_pct,
343 tract2_house_2000 * tract2_house_2000_pct,
344 tract3_house_2000 * tract3_house_2000_pct
1a7cb8d More tests pass.
cgroskopf authored
345 ]))
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
346 merged_house_2010 = 1586
347 merged_house_delta = merged_house_2010 - merged_house_2000
348 merged_house_pct_change = float(merged_house_delta) / merged_house_2000
349
350 # Verify that the merged tract is correct
351 merged_tract = self.geographies.find({ 'geoid': '15001021402' })
352 self.assertEqual(merged_tract.count(), 1)
353 merged_tract = merged_tract[0]
354
355 self.assertEqual(len(merged_tract['xwalk']), 3)
1a7cb8d More tests pass.
cgroskopf authored
356 self.assertAlmostEqual(merged_tract['xwalk']['15001021300']['HUPCT00'], tract1_house_2000_pct)
357 self.assertAlmostEqual(merged_tract['xwalk']['15001021400']['HUPCT00'], tract2_house_2000_pct)
358 self.assertAlmostEqual(merged_tract['xwalk']['15001021503']['HUPCT00'], tract3_house_2000_pct)
359
360 self.assertEqual(float(merged_tract['data']['2000']['H1']['H001001']), merged_house_2000)
361 self.assertEqual(float(merged_tract['data']['2010']['H1']['H001001']), merged_house_2010)
362 self.assertEqual(float(merged_tract['data']['delta']['H1']['H001001']), merged_house_delta)
363 self.assertAlmostEqual(float(merged_tract['data']['pct_change']['H1']['H001001']), merged_house_pct_change)
a28cd07 Added merged tract test from Paul.
cgroskopf authored
364
94a9ab6 @onyxfish Added split and merge tests for blocks.
onyxfish authored
365 class TestBlocks(unittest.TestCase):
366 def setUp(self):
367 self.geographies = utils.get_geography_collection()
368
38353fd @onyxfish Added total pop test for blocks.
onyxfish authored
369 def test_block_sum(self):
370 """
371 Verify that the total population of all blocks adds up to the expected amount.
372 """
373 blocks = self.geographies.find({ 'sumlev': config.SUMLEV_BLOCK })
374
375 pop_2010 = sum([int(block['data']['2010']['P1']['P001001']) for block in blocks])
376
377 self.assertEqual(pop_2010, 1360301)
378
94a9ab6 @onyxfish Added split and merge tests for blocks.
onyxfish authored
379 def test_block_split(self):
380 """
381 Verify that a split block is crosswalked correctly.
382 """
383 block1 = self.geographies.find({ 'geoid': '150010210051016' })
384 self.assertEqual(block1.count(), 1)
385 block1 = block1[0]
386
387 split_block_pop = 448
388 block1_land_pct = float(184458) / 587158 # AREALAND_INT / AREALAND_2000
389 block1_pop_2000 = int(block1_land_pct * split_block_pop)
390 block1_pop_2010 = 22
391 block1_pop_delta = block1_pop_2010 - block1_pop_2000
392 block1_pop_pct_change = float(block1_pop_delta) / block1_pop_2000
393
394 self.assertAlmostEqual(block1['xwalk']['150010210011337']['POPPCT00'], block1_land_pct, places=4)
395 self.assertAlmostEqual(block1['xwalk']['150010210011337']['HUPCT00'], block1_land_pct, places=4)
396 self.assertAlmostEqual(block1['data']['2000']['P1']['P001001'], block1_pop_2000)
397 self.assertAlmostEqual(float(block1['data']['2010']['P1']['P001001']), block1_pop_2010)
398 self.assertAlmostEqual(float(block1['data']['delta']['P1']['P001001']), block1_pop_delta)
399 self.assertAlmostEqual(float(block1['data']['pct_change']['P1']['P001001']), block1_pop_pct_change)
400
401 def test_block_merged(self):
402 """
403 Verify that a merged block is crosswalked correctly.
404 150010210011329 + 150010210011331 -> 150010210051009
405 """
406 # Compute crosswalked values
407 block1_pop_2000 = 12 # 150010210011329
408 block2_pop_2000 = 27 # 150010210011331
409 merged_pop_2000 = block1_pop_2000 + block2_pop_2000
410 merged_pop_2010 = 78
411 merged_pop_delta = merged_pop_2010 - merged_pop_2000
412 merged_pop_pct_change = float(merged_pop_delta) / merged_pop_2000
413
414 # Verify that the merged block is correct
415 merged_block = self.geographies.find({ 'geoid': '150010210051009' })
416 self.assertEqual(merged_block.count(), 1)
417 merged_block = merged_block[0]
418
419 self.assertEqual(len(merged_block['xwalk']), 2)
420 self.assertEqual(merged_block['xwalk']['150010210011329']['POPPCT00'], 1.0)
421 self.assertEqual(merged_block['xwalk']['150010210011331']['POPPCT00'], 1.0)
422
423 self.assertEqual(float(merged_block['data']['2000']['P1']['P001001']), merged_pop_2000)
424 self.assertEqual(float(merged_block['data']['2010']['P1']['P001001']), merged_pop_2010)
425 self.assertEqual(float(merged_block['data']['delta']['P1']['P001001']), merged_pop_delta)
426 self.assertAlmostEqual(float(merged_block['data']['pct_change']['P1']['P001001']), merged_pop_pct_change)
427
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
428 class TestFieldCrosswalk(unittest.TestCase):
429 def setUp(self):
7bc7b0d @JoeGermuska fix tests -- just broke the mongo connection management
JoeGermuska authored
430 self.geographies = utils.get_geography_collection()
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
431
432 def test_exact_same_name(self):
433 state = self.geographies.find_one({ 'geoid': '15' })
434
435 urban_and_rural_pop_2000 = 1211537
436 urban_and_rural_pop_2010 = 1360301
437 delta = urban_and_rural_pop_2010 - urban_and_rural_pop_2000
438 pct_change = float(urban_and_rural_pop_2010 - urban_and_rural_pop_2000) / urban_and_rural_pop_2000
439
440 self.assertEqual(float(state['data']['2000']['P2']['P002001']), urban_and_rural_pop_2000)
441 self.assertEqual(float(state['data']['2010']['P2']['P002001']), urban_and_rural_pop_2010)
442 self.assertEqual(float(state['data']['delta']['P2']['P002001']), delta)
443 self.assertAlmostEqual(float(state['data']['pct_change']['P2']['P002001']), pct_change)
444
445 def test_different_tables(self):
446 state = self.geographies.find_one({ 'geoid': '15' })
447
448 pacific_islanders_2000 = 113539
449 pacific_islanders_2010 = 135422
450 delta = pacific_islanders_2010 - pacific_islanders_2000
451 pct_change = float(pacific_islanders_2010 - pacific_islanders_2000) / pacific_islanders_2000
452
453 # 2000 field P007006
454 self.assertEqual(float(state['data']['2000']['P3']['P003006']), pacific_islanders_2000)
455 self.assertEqual(float(state['data']['2010']['P3']['P003006']), pacific_islanders_2010)
456 self.assertEqual(float(state['data']['delta']['P3']['P003006']), delta)
457 self.assertAlmostEqual(float(state['data']['pct_change']['P3']['P003006']), pct_change)
458
459 def test_different_everything(self):
3accf18 Added additional field mapping tests.
cgroskopf authored
460 state = self.geographies.find_one({ 'geoid': '15' })
461
462 unmarried_partner_households_2000 = 23516
463 unmarried_partner_households_2010 = 33068
464 delta = unmarried_partner_households_2010 - unmarried_partner_households_2000
465 pct_change = float(unmarried_partner_households_2010 - unmarried_partner_households_2000) / unmarried_partner_households_2000
466
467 # 2000 field PCT014002
468 self.assertEqual(float(state['data']['2000']['PCT15']['PCT015013']), unmarried_partner_households_2000)
469 self.assertEqual(float(state['data']['2010']['PCT15']['PCT015013']), unmarried_partner_households_2010)
470 self.assertEqual(float(state['data']['delta']['PCT15']['PCT015013']), delta)
471 self.assertAlmostEqual(float(state['data']['pct_change']['PCT15']['PCT015013']), pct_change)
640e9ab Tons of minor fixes to crosswalk and delta computations. New tests.
cgroskopf authored
472
42b0198 Added stub unit tests.
cgroskopf authored
473 class TestLabels(unittest.TestCase):
474 def setUp(self):
70eb7a3 @JoeGermuska push mongo collections back into dataprocessing/utils
JoeGermuska authored
475 self.labels = utils.get_label_collection()
7bc7b0d @JoeGermuska fix tests -- just broke the mongo connection management
JoeGermuska authored
476 self.geographies = utils.get_geography_collection()
34a1bcd Added more tests.
cgroskopf authored
477
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
478 def test_table_count(self):
7a81f53 Added more tests.
cgroskopf authored
479 labels = self.labels.find_one({ 'dataset': 'SF1' })
480
481 self.assertEqual(len(labels['tables']), 331)
59e6d5b Removed debug code. Stubbed additional tests.
cgroskopf authored
482
42b0198 Added stub unit tests.
cgroskopf authored
483 def test_table(self):
484 """
485 Header rows from input file:
c9ff7fc Labels tests.
cgroskopf authored
486 P12F.,,0,SEX BY AGE (SOME OTHER RACE ALONE) [49],,,,,,,,2000 SF1 P12F.
487 P12F.,,0,Universe: People who are Some Other Race alone,,,,,,,,
42b0198 Added stub unit tests.
cgroskopf authored
488 """
c9ff7fc Labels tests.
cgroskopf authored
489 table = self.labels.find_one({ 'dataset': 'SF1' })['tables']['P12F']
42b0198 Added stub unit tests.
cgroskopf authored
490
c9ff7fc Labels tests.
cgroskopf authored
491 self.assertEqual(table['name'], 'SEX BY AGE (SOME OTHER RACE ALONE)')
492 self.assertEqual(table['size'], 49)
493 self.assertEqual(table['universe'], 'People who are Some Other Race alone')
42b0198 Added stub unit tests.
cgroskopf authored
494
495 def test_label(self):
496 """
c9ff7fc Labels tests.
cgroskopf authored
497 P12F.,1,0,Total:,,,,,,,,
42b0198 Added stub unit tests.
cgroskopf authored
498 """
c9ff7fc Labels tests.
cgroskopf authored
499 table = self.labels.find_one({ 'dataset': 'SF1' })['tables']['P12F']
500 label = table['labels']['P012F001']
42b0198 Added stub unit tests.
cgroskopf authored
501
c9ff7fc Labels tests.
cgroskopf authored
502 self.assertEqual(label['text'], 'Total:')
503 self.assertEqual(label['parent'], None)
504 self.assertEqual(label['indent'], 0)
42b0198 Added stub unit tests.
cgroskopf authored
505
34a1bcd Added more tests.
cgroskopf authored
506 def test_labels_match_geographies(self):
507 """
508 Hawaii should have a key for every label.
509 Every label should have a key for Hawaii.
510 """
511 geo = self.geographies.find_one({ 'geoid': '15' })
512 labels = self.labels.find_one({ 'dataset': 'SF1' })
513
514 geo_tables = geo['data']['2010']
515 labels_tables = labels['tables']
516
517 self.assertEqual(sorted(geo_tables.keys()), sorted(labels_tables.keys()))
518
519 # Test table has labels
520 for table_name, geo_keys in geo_tables.items():
521 label_keys = labels_tables[table_name]['labels']
522
523 self.assertEqual(sorted(geo_keys.keys()), sorted(label_keys.keys()))
524
525 for table_name, label_data in labels_tables.items():
526 label_keys = label_data['labels']
527 geo_keys = geo_tables[table_name]
528
529 self.assertEqual(sorted(geo_keys.keys()), sorted(label_keys.keys()))
530
ccb79b4 Added table sizes test.
cgroskopf authored
531 def test_table_sizes(self):
532 """
533 Test that the tables documented size matches its actual label count.
534 """
535 labels_tables = self.labels.find_one({ 'dataset': 'SF1' })['tables']
536
537 for label_data in labels_tables.values():
538 self.assertEqual(label_data['size'], len(label_data['labels']))
539
42b0198 Added stub unit tests.
cgroskopf authored
540 if __name__ == '__main__':
541 unittest.main()
542
Something went wrong with that request. Please try again.