]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/Tests/Unit/SummaryFile1Test.py
Added state, county, and tract fields to the Block class.
[dead/census-tools.git] / src / Tests / Unit / SummaryFile1Test.py
1 import Tests.Fixtures, SummaryFile1, GPS, unittest
2
3
4 class BlockTest(unittest.TestCase):
5
6 def testAverageDensityIsFloat(self):
7 """
8 We want to make sure no float->integer truncation
9 is taking place.
10 """
11
12 # Fill a GeoRecord with dummy values so that we
13 # can calculate its average population density.
14 gr = SummaryFile1.GeoRecord()
15 gr.state = '24'
16 gr.county = '001'
17 gr.tract = '123456'
18 gr.block = '1728'
19 gr.pop100 = '100'
20 gr.arealand = '40'
21 gr.areawatr = '40'
22 gr.intptlat = '0'
23 gr.intptlon = '0'
24
25 b = SummaryFile1.Block(gr)
26
27 # Should be 100/(40 + 40), which is 1.25 when
28 # intepreted as a float.
29 self.assertEqual(b.population_density(), 1.25)
30
31
32 class GeoRecordParserTest(unittest.TestCase):
33
34 def setUp(self):
35 self.grp = SummaryFile1.GeoRecordParser()
36
37
38 def testAllOfSubsetParsed(self):
39 fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/one_hundred_records.txt'
40 records = self.grp.parse_file(fixture_path)
41 self.assertEqual(len(records), 100)
42
43
44 def testSubsetOfBlocksParsed(self):
45 fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/one_hundred_records.txt'
46 blocks = self.grp.parse_blocks(fixture_path)
47 self.assertEqual(len(blocks), 62)
48
49
50 def testErrorParsingShortLines(self):
51 self.assertRaises(SummaryFile1.RecordError, self.grp.parse_line, "This is a short line.")
52
53
54 def testMdGeoRecordCount(self):
55 fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/mdgeo.uf1'
56 records = self.grp.parse_file(fixture_path)
57 self.assertEqual(len(records), 98763)
58
59
60 def testMdGeoBlockCount(self):
61 fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/mdgeo.uf1'
62 blocks = self.grp.parse_blocks(fixture_path)
63 self.assertEqual(len(blocks), 79128)
64
65
66 class SummaryFile1Test(unittest.TestCase):
67
68 def testEachBlockIsClosestToItself(self):
69 blocks_path = Tests.Fixtures.Path() + '/SummaryFile1/mdgeo.uf1'
70 grp = SummaryFile1.GeoRecordParser()
71 blocks = grp.parse_blocks(blocks_path)
72
73 # Only test 100 of these guys (or however many blocks there
74 # are in those 100 records.
75 fixtures_path = Tests.Fixtures.Path() + '/SummaryFile1/one_hundred_records.txt'
76 fixtures = grp.parse_blocks(fixtures_path)
77
78 for b in fixtures:
79 # It's probably unnecessary to copy the coordinates
80 # into a new instance here, but whatever.
81 b_coords = GPS.Coordinates()
82 b_coords.latitude = b.coordinates.latitude
83 b_coords.longitude = b.coordinates.longitude
84
85 closest_block = SummaryFile1.FindClosestBlock(blocks, b_coords)
86 self.assertEqual(b.block, closest_block.block)
87
88
89 def testEachBlockHasItsOwnAverageDensity(self):
90 geo_file_path = Tests.Fixtures.Path() + '/SummaryFile1/mdgeo.uf1'
91
92 # Only test 5 of these guys; they take longer.
93 grp = SummaryFile1.GeoRecordParser()
94 fixtures_path = Tests.Fixtures.Path() + '/SummaryFile1/five_blocks.txt'
95 fixtures = grp.parse_blocks(fixtures_path)
96
97 for b in fixtures:
98 # It's probably unnecessary to copy the coordinates
99 # into a new instance here, but whatever.
100 avg_density = SummaryFile1.FindAveragePopulationDensity(b.coordinates, geo_file_path)
101 self.assertEqual(b.population_density(), avg_density)
102
103
104 def suite():
105 suite = unittest.TestSuite()
106 suite.addTest(unittest.makeSuite(GeoRecordParserTest))
107 suite.addTest(unittest.makeSuite(SummaryFile1Test))
108 suite.addTest(unittest.makeSuite(BlockTest))
109 return suite
110