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)
import pgdb
import GPS
+import SummaryFile1
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.
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
+
--- /dev/null
+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
+