]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Added unit tests for the Census.Database class.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 28 Sep 2009 02:24:12 +0000 (22:24 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 28 Sep 2009 02:24:12 +0000 (22:24 -0400)
Created a new method in the Census.Database class for testing.
Updated the test script with the new suite.

bin/run_tests
src/Census.py
src/Tests/Unit/CensusTest.py [new file with mode: 0644]

index 742f7959c501b8381523acdba6b623fd5b27f25e..4c7a1a3a44b235347b8f6be60c966424f66a43f7 100755 (executable)
@@ -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)
index ae4d28d0de1a72bd631021cb6da270193850d991..a69ff58b9cb68fea0e8c398d5fce289f7954a87e 100644 (file)
@@ -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 (file)
index 0000000..3f56afb
--- /dev/null
@@ -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
+