changeset 3:49aae0c0293b

improved test coverage
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 14:48:31 -0700
parents 50ee13cddf58
children 8e130b7bfed9
files globalneighbors/distance.py tests/test_distance.py tests/test_grid.py tests/test_read.py tox.ini
diffstat 5 files changed, 57 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/globalneighbors/distance.py	Sat Jun 24 14:04:00 2017 -0700
+++ b/globalneighbors/distance.py	Sat Jun 24 14:48:31 2017 -0700
@@ -57,19 +57,21 @@
                         k=10,
                         lat_tol=1.,
                         lon_tol=1.,
-                        output=1000,
+                        output=None,
                         neighbors=None):
     """
     calculate `k` nearest neighbors for each location
+
+    locations -- dict of `geoid: (lat, lon)`
     """
     neighbors = neighbors or {}
-    items = locations.items()
+    items = locations.items()  # copy
     index = 0
     n_items = len(items)
     start = int(time.time())
     while items:
         index += 1
-        if not index % output:
+        if output and not index % output:
             # output status counter
             now = int(time.time())
             duration =  now - start
--- a/tests/test_distance.py	Sat Jun 24 14:04:00 2017 -0700
+++ b/tests/test_distance.py	Sat Jun 24 14:48:31 2017 -0700
@@ -10,6 +10,7 @@
 from globalneighbors import distance
 from globalneighbors.constants import Rearth
 from globalneighbors.locations import locations
+from globalneighbors.read import read_cities
 from globalneighbors.read import read_city_list
 from globalneighbors.schema import primary_key
 
@@ -17,6 +18,7 @@
 data = os.path.join(here, 'data')
 full_tsv_lines = 149092
 
+
 class DistanceTests(unittest.TestCase):
 
     # created with
@@ -28,6 +30,9 @@
     full_tsv = os.path.join(data, 'cities1000.txt')
     full_tsv_lines = 149092
 
+    # here's a smaller one
+    moderate_tsv = os.path.join(data, '10000cities.tsv')
+
     def test_haversine(self):
 
         # a simple canned case
@@ -102,6 +107,26 @@
             for i in range(1, len(distances)):
                 assert distances[i] >= distances[i-1]
 
+    def test_10000cities(self):
+        """a moderate size test"""
+
+        assert os.path.exists(self.moderate_tsv)
+        with open(self.moderate_tsv) as f:
+            cities = locations(read_cities(f))
+
+        # test over different values of # of neighbors
+        for k in (10, 100, 1000):
+            neighbors = distance.calculate_neighbors(cities,
+                                                     k=k)
+
+            # ensure you have no more neighbors than you ask for
+            assert max([len(value) for value in neighbors.values()]) <= k
+
+            # assert distances increase
+            for value in neighbors.values():
+                distances = [i[-1] for i in value]
+                assert distances == sorted(distances)
+
 
 if __name__ == '__main__':
     unittest.main()
--- a/tests/test_grid.py	Sat Jun 24 14:04:00 2017 -0700
+++ b/tests/test_grid.py	Sat Jun 24 14:48:31 2017 -0700
@@ -49,9 +49,19 @@
     def grid_locations(self, locations):
         """grid locations + test created grid"""
 
+        # create a grid
         grid = LatLonGrid(8, 8)
+
+        # add the items to it
         for geoid, (lat, lon) in locations.items():
             grid.add(geoid, lat, lon)
 
+        # iterate over the grid
+        n_locations = 0
+        for i in range(grid.n[0]):
+            for j in range(grid.n[1]):
+                n_locations += len(grid[(i,j)])
+        assert n_locations == len(locations)
+
 if __name__ == '__main__':
     unittest.main()
--- a/tests/test_read.py	Sat Jun 24 14:04:00 2017 -0700
+++ b/tests/test_read.py	Sat Jun 24 14:48:31 2017 -0700
@@ -7,7 +7,9 @@
 import os
 import unittest
 from globalneighbors import schema
+from globalneighbors.locations import locations
 from globalneighbors.read import read_tsv
+from globalneighbors.read import read_cities
 from globalneighbors.read import read_city_list
 
 
@@ -52,6 +54,19 @@
         """ensure we can read the cities as unicode"""
 
         cities = read_city_list(self.full_tsv)
+        for city in cities:
+            for field in schema.unicode_fields:
+                assert isinstance(city[field], unicode)
+
+    def test_iterative_locations(self):
+        """assert we can read into locations as a generator"""
+
+        with open(self.test_tsv) as f:
+            cities = locations(read_cities(f))
+        for geonameid, (lat, lon) in cities.items():
+            assert -90. <= lat <= 90.
+            assert -180. <= lon <= 180.
+            assert type(geonameid) == schema.types['geonameid']
 
 
 if __name__ == '__main__':
--- a/tox.ini	Sat Jun 24 14:04:00 2017 -0700
+++ b/tox.ini	Sat Jun 24 14:48:31 2017 -0700
@@ -2,5 +2,6 @@
 [tox]
 envlist=py27
 [testenv]
-commands=python setup.py test
+deps = pytest
+commands=pytest tests/