changeset 14:27925261c137

fix broken tests including an aggregious case where we add ourselves as a neighbor to ourself
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 14:29:18 -0700
parents 94af113e498a
children 21095c9006e5
files globalneighbors/distance.py globalneighbors/templates/city.html globalneighbors/web.py tests/test_bisect.py tests/test_distance.py
diffstat 5 files changed, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/globalneighbors/distance.py	Sun Jun 25 14:04:49 2017 -0700
+++ b/globalneighbors/distance.py	Sun Jun 25 14:29:18 2017 -0700
@@ -147,10 +147,13 @@
             new_distance = haversine(*args, r=Rearth)
 
             # insert in order
-            for i in (id1, id2):
-                distances = neighbors.setdefault(i, [])
-
-                insert_distance_bisect(distances, i, new_distance, k)
+            ids = (id1, id2)
+            for i in (0, 1):
+                distances = neighbors.setdefault(ids[i], [])
+                insert_distance_bisect(distances,
+                                       ids[i-1],
+                                       new_distance,
+                                       k)
 
     return neighbors
 
--- a/globalneighbors/templates/city.html	Sun Jun 25 14:04:49 2017 -0700
+++ b/globalneighbors/templates/city.html	Sun Jun 25 14:29:18 2017 -0700
@@ -4,10 +4,16 @@
     <title>{{ name }}</title>
   </head>
   <body>
+    <a href="/"
+       title="back to Global Niehgbors">Home</a>
+
     <h1>{{ name }}</h1>
 
+    <h2 title="location">{{latitude}},{{longitude}}</h2>
+
     <dl>
       <dt>Population:</dt>
+      <dd>{{ population }}</dd>
     </dl>
   </body>
 </html>
--- a/globalneighbors/web.py	Sun Jun 25 14:04:49 2017 -0700
+++ b/globalneighbors/web.py	Sun Jun 25 14:29:18 2017 -0700
@@ -63,7 +63,7 @@
         retval = [{"name": i[name],
                   "geonameid": i['geonameid']}
                    for i in cities]
-    return sorted(retval)[:limit]
+    return sorted(retval, key=lambda x: x['name'])[:limit]
 
 
 class Handler(object):
--- a/tests/test_bisect.py	Sun Jun 25 14:04:49 2017 -0700
+++ b/tests/test_bisect.py	Sun Jun 25 14:29:18 2017 -0700
@@ -25,6 +25,8 @@
             assert len(_distances) == k
             ordered = [value[-1] for value in _distances]
             assert sorted(ordered) == ordered
+            keys = [value[0] for value in _distances]
+            assert len(set(keys)) == len(_distances)
 
 
 if __name__ == '__main__':
--- a/tests/test_distance.py	Sun Jun 25 14:04:49 2017 -0700
+++ b/tests/test_distance.py	Sun Jun 25 14:29:18 2017 -0700
@@ -124,9 +124,12 @@
             assert max([len(value) for value in neighbors.values()]) <= k
 
             # assert distances increase
-            for value in neighbors.values():
+            for key, value in neighbors.items():
                 distances = [i[-1] for i in value]
                 assert distances == sorted(distances)
+                neighbors = [i[0] for i in value]
+                assert key not in neighbors
+                assert len(set(neighbors)) == len(neighbors)
 
     def test_insert_distances(self):
         """test insert distances algorithm"""