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