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