From e29f3e5cc471e3cb60f641fcf3a3577c87896f36 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 27 Sep 2009 22:24:12 -0400 Subject: [PATCH] Added unit tests for the Census.Database class. Created a new method in the Census.Database class for testing. Updated the test script with the new suite. --- bin/run_tests | 2 ++ src/Census.py | 28 ++++++++++++++++++++++- src/Tests/Unit/CensusTest.py | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/Tests/Unit/CensusTest.py diff --git a/bin/run_tests b/bin/run_tests index 742f795..4c7a1a3 100755 --- a/bin/run_tests +++ b/bin/run_tests @@ -5,10 +5,12 @@ import sys, os, site site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') import unittest +from Tests.Unit import CensusTest from Tests.Unit import SummaryFile1Test from Tests.Unit import StringUtilsTest suite = unittest.TestSuite() +suite.addTest(CensusTest.suite()) suite.addTest(SummaryFile1Test.suite()) suite.addTest(StringUtilsTest.suite()) unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/src/Census.py b/src/Census.py index ae4d28d..a69ff58 100644 --- a/src/Census.py +++ b/src/Census.py @@ -1,6 +1,7 @@ import pgdb import GPS +import SummaryFile1 class Database: @@ -20,7 +21,7 @@ class Database: def __del__(self): self.connection.close() - + def find_average_population_density(self, coords): """ Find the average population density at a set of GPS coordinates. @@ -110,3 +111,28 @@ class Database: else: return None + + + def get_block_geometry_as_wkt(self, blkidfp00): + """ + Find the geometry of a (uniquely-identified) block, in + Well-Known Text format. + """ + cursor = self.connection.cursor() + + query = """ + SELECT ST_AsText(tiger.the_geom) + FROM tiger + WHERE tiger.blkidfp00 = %s; + """ + sql_params = (blkidfp00,) + + cursor.execute(query, sql_params) + rows = cursor.fetchall() + cursor.close() + + if len(rows) > 0: + return rows[0][0] + else: + return None + diff --git a/src/Tests/Unit/CensusTest.py b/src/Tests/Unit/CensusTest.py new file mode 100644 index 0000000..3f56afb --- /dev/null +++ b/src/Tests/Unit/CensusTest.py @@ -0,0 +1,43 @@ +import unittest + +import Census +import Configuration.Defaults +import GPS +import SummaryFile1 +import Tests.Fixtures + + +class DatabaseTest(unittest.TestCase): + + def setUp(self): + self.cdb = Census.Database(Configuration.Defaults.DATABASE_HOST, + Configuration.Defaults.DATABASE_NAME, + Configuration.Defaults.DATABASE_USERNAME, + Configuration.Defaults.SRID) + + + def testEachBlockContainsItsOwnPopulation(self): + # These calculations are slightly off due to a discrepancy + # between the WKB/WKT format calculations. It would appear + # that converting from WKB to WKT and then back loses some + # information. + # + # The error is 1/100th of a person. Should be pretty safe. + error_threshold = 0.01 + + grp = SummaryFile1.GeoRecordParser() + fixtures_path = Tests.Fixtures.Path() + '/SummaryFile1/one_hundred_records.txt' + fixtures = grp.parse_blocks(fixtures_path) + + for b in fixtures: + block_boundary = self.cdb.get_block_geometry_as_wkt(b.tiger_blkidfp00()) + contained_population = self.cdb.find_contained_population(block_boundary) + error = abs(b.pop100 - contained_population) + self.assertTrue(error <= error_threshold) + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(DatabaseTest)) + return suite + -- 2.44.2