# HG changeset patch # User Jeff Hammel # Date 1498426158 25200 # Node ID 27925261c137753cb505662fe85fbbe424938e15 # Parent 94af113e498ac008fe00324bd00839435538d604 fix broken tests including an aggregious case where we add ourselves as a neighbor to ourself diff -r 94af113e498a -r 27925261c137 globalneighbors/distance.py --- 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 diff -r 94af113e498a -r 27925261c137 globalneighbors/templates/city.html --- 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 @@ {{ name }} + Home +

{{ name }}

+

{{latitude}},{{longitude}}

+
Population:
+
{{ population }}
diff -r 94af113e498a -r 27925261c137 globalneighbors/web.py --- 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): diff -r 94af113e498a -r 27925261c137 tests/test_bisect.py --- 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__': diff -r 94af113e498a -r 27925261c137 tests/test_distance.py --- 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"""