import unittest import Tests.Fixtures import SummaryFile1 import GPS import StringUtils class BlockTest(unittest.TestCase): def setUp(self): self.grp = SummaryFile1.GeoRecordParser() def testOneAverageDensityIsFloat(self): """ We want to make sure no float->integer truncation is taking place. """ # Fill a GeoRecord with dummy values so that we # can calculate its average population density. gr = SummaryFile1.GeoRecord() gr.state = '24' gr.county = '001' gr.tract = '123456' gr.block = '1728' gr.pop100 = '100' gr.arealand = '40' gr.areawatr = '40' gr.intptlat = '0' gr.intptlon = '0' b = SummaryFile1.Block(gr) # Should be 100/(40 + 40), which is 1.25 when # intepreted as a float. self.assertEqual(b.population_density(), 1.25) def testAllAverageDensitiesAreFloat(self): """ Test every GeoRecord in the Maryland file, and make sure all of the average densities can be parsed as floats. """ fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/mdgeo.uf1' blocks = self.grp.parse_blocks(fixture_path) for b in blocks: self.assertTrue(StringUtils.is_float(b.population_density())) class GeoRecordParserTest(unittest.TestCase): def setUp(self): self.grp = SummaryFile1.GeoRecordParser() def testAllOfSubsetParsed(self): fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/one_hundred_records.txt' records = self.grp.parse_file(fixture_path) self.assertEqual(len(records), 100) def testSubsetOfBlocksParsed(self): fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/one_hundred_records.txt' blocks = self.grp.parse_blocks(fixture_path) self.assertEqual(len(blocks), 62) def testErrorParsingShortLines(self): self.assertRaises(SummaryFile1.RecordError, self.grp.parse_line, "This is a short line.") def testMdGeoRecordCount(self): fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/mdgeo.uf1' records = self.grp.parse_file(fixture_path) self.assertEqual(len(records), 1000) def testMdGeoBlockCount(self): fixture_path = Tests.Fixtures.Path() + '/SummaryFile1/mdgeo.uf1' blocks = self.grp.parse_blocks(fixture_path) self.assertEqual(len(blocks), 910) def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(GeoRecordParserTest)) suite.addTest(unittest.makeSuite(BlockTest)) return suite