ran unittest2pytest

This commit is contained in:
Bastien Gérard
2019-08-30 16:13:30 +03:00
parent aa6ff8c84a
commit ac25f4b98b
46 changed files with 4247 additions and 4428 deletions

View File

@@ -48,14 +48,14 @@ class TestGeoQueries(MongoDBTestCase):
# note that "near" will show the san francisco event, too,
# although it sorts to last.
events = self.Event.objects(location__near=[-87.67892, 41.9120459])
self.assertEqual(events.count(), 3)
self.assertEqual(list(events), [event1, event3, event2])
assert events.count() == 3
assert list(events) == [event1, event3, event2]
# ensure ordering is respected by "near"
events = self.Event.objects(location__near=[-87.67892, 41.9120459])
events = events.order_by("-date")
self.assertEqual(events.count(), 3)
self.assertEqual(list(events), [event3, event1, event2])
assert events.count() == 3
assert list(events) == [event3, event1, event2]
def test_near_and_max_distance(self):
"""Ensure the "max_distance" operator works alongside the "near"
@@ -66,8 +66,8 @@ class TestGeoQueries(MongoDBTestCase):
# find events within 10 degrees of san francisco
point = [-122.415579, 37.7566023]
events = self.Event.objects(location__near=point, location__max_distance=10)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0], event2)
assert events.count() == 1
assert events[0] == event2
def test_near_and_min_distance(self):
"""Ensure the "min_distance" operator works alongside the "near"
@@ -78,7 +78,7 @@ class TestGeoQueries(MongoDBTestCase):
# find events at least 10 degrees away of san francisco
point = [-122.415579, 37.7566023]
events = self.Event.objects(location__near=point, location__min_distance=10)
self.assertEqual(events.count(), 2)
assert events.count() == 2
def test_within_distance(self):
"""Make sure the "within_distance" operator works."""
@@ -87,29 +87,29 @@ class TestGeoQueries(MongoDBTestCase):
# find events within 5 degrees of pitchfork office, chicago
point_and_distance = [[-87.67892, 41.9120459], 5]
events = self.Event.objects(location__within_distance=point_and_distance)
self.assertEqual(events.count(), 2)
assert events.count() == 2
events = list(events)
self.assertNotIn(event2, events)
self.assertIn(event1, events)
self.assertIn(event3, events)
assert event2 not in events
assert event1 in events
assert event3 in events
# find events within 10 degrees of san francisco
point_and_distance = [[-122.415579, 37.7566023], 10]
events = self.Event.objects(location__within_distance=point_and_distance)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0], event2)
assert events.count() == 1
assert events[0] == event2
# find events within 1 degree of greenpoint, broolyn, nyc, ny
point_and_distance = [[-73.9509714, 40.7237134], 1]
events = self.Event.objects(location__within_distance=point_and_distance)
self.assertEqual(events.count(), 0)
assert events.count() == 0
# ensure ordering is respected by "within_distance"
point_and_distance = [[-87.67892, 41.9120459], 10]
events = self.Event.objects(location__within_distance=point_and_distance)
events = events.order_by("-date")
self.assertEqual(events.count(), 2)
self.assertEqual(events[0], event3)
assert events.count() == 2
assert events[0] == event3
def test_within_box(self):
"""Ensure the "within_box" operator works."""
@@ -118,8 +118,8 @@ class TestGeoQueries(MongoDBTestCase):
# check that within_box works
box = [(-125.0, 35.0), (-100.0, 40.0)]
events = self.Event.objects(location__within_box=box)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0].id, event2.id)
assert events.count() == 1
assert events[0].id == event2.id
def test_within_polygon(self):
"""Ensure the "within_polygon" operator works."""
@@ -133,8 +133,8 @@ class TestGeoQueries(MongoDBTestCase):
(-87.656164, 41.898061),
]
events = self.Event.objects(location__within_polygon=polygon)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0].id, event1.id)
assert events.count() == 1
assert events[0].id == event1.id
polygon2 = [
(-1.742249, 54.033586),
@@ -142,7 +142,7 @@ class TestGeoQueries(MongoDBTestCase):
(-4.40094, 53.389881),
]
events = self.Event.objects(location__within_polygon=polygon2)
self.assertEqual(events.count(), 0)
assert events.count() == 0
def test_2dsphere_near(self):
"""Make sure the "near" operator works with a PointField, which
@@ -154,14 +154,14 @@ class TestGeoQueries(MongoDBTestCase):
# note that "near" will show the san francisco event, too,
# although it sorts to last.
events = self.Event.objects(location__near=[-87.67892, 41.9120459])
self.assertEqual(events.count(), 3)
self.assertEqual(list(events), [event1, event3, event2])
assert events.count() == 3
assert list(events) == [event1, event3, event2]
# ensure ordering is respected by "near"
events = self.Event.objects(location__near=[-87.67892, 41.9120459])
events = events.order_by("-date")
self.assertEqual(events.count(), 3)
self.assertEqual(list(events), [event3, event1, event2])
assert events.count() == 3
assert list(events) == [event3, event1, event2]
def test_2dsphere_near_and_max_distance(self):
"""Ensure the "max_distance" operator works alongside the "near"
@@ -172,21 +172,21 @@ class TestGeoQueries(MongoDBTestCase):
# find events within 10km of san francisco
point = [-122.415579, 37.7566023]
events = self.Event.objects(location__near=point, location__max_distance=10000)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0], event2)
assert events.count() == 1
assert events[0] == event2
# find events within 1km of greenpoint, broolyn, nyc, ny
events = self.Event.objects(
location__near=[-73.9509714, 40.7237134], location__max_distance=1000
)
self.assertEqual(events.count(), 0)
assert events.count() == 0
# ensure ordering is respected by "near"
events = self.Event.objects(
location__near=[-87.67892, 41.9120459], location__max_distance=10000
).order_by("-date")
self.assertEqual(events.count(), 2)
self.assertEqual(events[0], event3)
assert events.count() == 2
assert events[0] == event3
def test_2dsphere_geo_within_box(self):
"""Ensure the "geo_within_box" operator works with a 2dsphere
@@ -197,8 +197,8 @@ class TestGeoQueries(MongoDBTestCase):
# check that within_box works
box = [(-125.0, 35.0), (-100.0, 40.0)]
events = self.Event.objects(location__geo_within_box=box)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0].id, event2.id)
assert events.count() == 1
assert events[0].id == event2.id
def test_2dsphere_geo_within_polygon(self):
"""Ensure the "geo_within_polygon" operator works with a
@@ -214,8 +214,8 @@ class TestGeoQueries(MongoDBTestCase):
(-87.656164, 41.898061),
]
events = self.Event.objects(location__geo_within_polygon=polygon)
self.assertEqual(events.count(), 1)
self.assertEqual(events[0].id, event1.id)
assert events.count() == 1
assert events[0].id == event1.id
polygon2 = [
(-1.742249, 54.033586),
@@ -223,7 +223,7 @@ class TestGeoQueries(MongoDBTestCase):
(-4.40094, 53.389881),
]
events = self.Event.objects(location__geo_within_polygon=polygon2)
self.assertEqual(events.count(), 0)
assert events.count() == 0
def test_2dsphere_near_and_min_max_distance(self):
"""Ensure "min_distace" and "max_distance" operators work well
@@ -237,15 +237,15 @@ class TestGeoQueries(MongoDBTestCase):
location__min_distance=1000,
location__max_distance=10000,
).order_by("-date")
self.assertEqual(events.count(), 1)
self.assertEqual(events[0], event3)
assert events.count() == 1
assert events[0] == event3
# ensure ordering is respected by "near" with "min_distance"
events = self.Event.objects(
location__near=[-87.67892, 41.9120459], location__min_distance=10000
).order_by("-date")
self.assertEqual(events.count(), 1)
self.assertEqual(events[0], event2)
assert events.count() == 1
assert events[0] == event2
def test_2dsphere_geo_within_center(self):
"""Make sure the "geo_within_center" operator works with a
@@ -256,11 +256,11 @@ class TestGeoQueries(MongoDBTestCase):
# find events within 5 degrees of pitchfork office, chicago
point_and_distance = [[-87.67892, 41.9120459], 2]
events = self.Event.objects(location__geo_within_center=point_and_distance)
self.assertEqual(events.count(), 2)
assert events.count() == 2
events = list(events)
self.assertNotIn(event2, events)
self.assertIn(event1, events)
self.assertIn(event3, events)
assert event2 not in events
assert event1 in events
assert event3 in events
def _test_embedded(self, point_field_class):
"""Helper test method ensuring given point field class works
@@ -290,8 +290,8 @@ class TestGeoQueries(MongoDBTestCase):
# note that "near" will show the san francisco event, too,
# although it sorts to last.
events = Event.objects(venue__location__near=[-87.67892, 41.9120459])
self.assertEqual(events.count(), 3)
self.assertEqual(list(events), [event1, event3, event2])
assert events.count() == 3
assert list(events) == [event1, event3, event2]
def test_geo_spatial_embedded(self):
"""Make sure GeoPointField works properly in an embedded document."""
@@ -319,55 +319,55 @@ class TestGeoQueries(MongoDBTestCase):
# Finds both points because they are within 60 km of the reference
# point equidistant between them.
points = Point.objects(location__near_sphere=[-122, 37.5])
self.assertEqual(points.count(), 2)
assert points.count() == 2
# Same behavior for _within_spherical_distance
points = Point.objects(
location__within_spherical_distance=[[-122, 37.5], 60 / earth_radius]
)
self.assertEqual(points.count(), 2)
assert points.count() == 2
points = Point.objects(
location__near_sphere=[-122, 37.5], location__max_distance=60 / earth_radius
)
self.assertEqual(points.count(), 2)
assert points.count() == 2
# Test query works with max_distance, being farer from one point
points = Point.objects(
location__near_sphere=[-122, 37.8], location__max_distance=60 / earth_radius
)
close_point = points.first()
self.assertEqual(points.count(), 1)
assert points.count() == 1
# Test query works with min_distance, being farer from one point
points = Point.objects(
location__near_sphere=[-122, 37.8], location__min_distance=60 / earth_radius
)
self.assertEqual(points.count(), 1)
assert points.count() == 1
far_point = points.first()
self.assertNotEqual(close_point, far_point)
assert close_point != far_point
# Finds both points, but orders the north point first because it's
# closer to the reference point to the north.
points = Point.objects(location__near_sphere=[-122, 38.5])
self.assertEqual(points.count(), 2)
self.assertEqual(points[0].id, north_point.id)
self.assertEqual(points[1].id, south_point.id)
assert points.count() == 2
assert points[0].id == north_point.id
assert points[1].id == south_point.id
# Finds both points, but orders the south point first because it's
# closer to the reference point to the south.
points = Point.objects(location__near_sphere=[-122, 36.5])
self.assertEqual(points.count(), 2)
self.assertEqual(points[0].id, south_point.id)
self.assertEqual(points[1].id, north_point.id)
assert points.count() == 2
assert points[0].id == south_point.id
assert points[1].id == north_point.id
# Finds only one point because only the first point is within 60km of
# the reference point to the south.
points = Point.objects(
location__within_spherical_distance=[[-122, 36.5], 60 / earth_radius]
)
self.assertEqual(points.count(), 1)
self.assertEqual(points[0].id, south_point.id)
assert points.count() == 1
assert points[0].id == south_point.id
def test_linestring(self):
class Road(Document):
@@ -381,13 +381,13 @@ class TestGeoQueries(MongoDBTestCase):
# near
point = {"type": "Point", "coordinates": [40, 5]}
roads = Road.objects.filter(line__near=point["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__near=point).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__near={"$geometry": point}).count()
self.assertEqual(1, roads)
assert 1 == roads
# Within
polygon = {
@@ -395,37 +395,37 @@ class TestGeoQueries(MongoDBTestCase):
"coordinates": [[[40, 5], [40, 6], [41, 6], [41, 5], [40, 5]]],
}
roads = Road.objects.filter(line__geo_within=polygon["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__geo_within=polygon).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__geo_within={"$geometry": polygon}).count()
self.assertEqual(1, roads)
assert 1 == roads
# Intersects
line = {"type": "LineString", "coordinates": [[40, 5], [40, 6]]}
roads = Road.objects.filter(line__geo_intersects=line["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__geo_intersects=line).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__geo_intersects={"$geometry": line}).count()
self.assertEqual(1, roads)
assert 1 == roads
polygon = {
"type": "Polygon",
"coordinates": [[[40, 5], [40, 6], [41, 6], [41, 5], [40, 5]]],
}
roads = Road.objects.filter(line__geo_intersects=polygon["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__geo_intersects=polygon).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(line__geo_intersects={"$geometry": polygon}).count()
self.assertEqual(1, roads)
assert 1 == roads
def test_polygon(self):
class Road(Document):
@@ -439,13 +439,13 @@ class TestGeoQueries(MongoDBTestCase):
# near
point = {"type": "Point", "coordinates": [40, 5]}
roads = Road.objects.filter(poly__near=point["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__near=point).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__near={"$geometry": point}).count()
self.assertEqual(1, roads)
assert 1 == roads
# Within
polygon = {
@@ -453,37 +453,37 @@ class TestGeoQueries(MongoDBTestCase):
"coordinates": [[[40, 5], [40, 6], [41, 6], [41, 5], [40, 5]]],
}
roads = Road.objects.filter(poly__geo_within=polygon["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__geo_within=polygon).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__geo_within={"$geometry": polygon}).count()
self.assertEqual(1, roads)
assert 1 == roads
# Intersects
line = {"type": "LineString", "coordinates": [[40, 5], [41, 6]]}
roads = Road.objects.filter(poly__geo_intersects=line["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__geo_intersects=line).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__geo_intersects={"$geometry": line}).count()
self.assertEqual(1, roads)
assert 1 == roads
polygon = {
"type": "Polygon",
"coordinates": [[[40, 5], [40, 6], [41, 6], [41, 5], [40, 5]]],
}
roads = Road.objects.filter(poly__geo_intersects=polygon["coordinates"]).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__geo_intersects=polygon).count()
self.assertEqual(1, roads)
assert 1 == roads
roads = Road.objects.filter(poly__geo_intersects={"$geometry": polygon}).count()
self.assertEqual(1, roads)
assert 1 == roads
def test_aspymongo_with_only(self):
"""Ensure as_pymongo works with only"""
@@ -495,13 +495,10 @@ class TestGeoQueries(MongoDBTestCase):
p = Place(location=[24.946861267089844, 60.16311983618494])
p.save()
qs = Place.objects().only("location")
self.assertDictEqual(
qs.as_pymongo()[0]["location"],
{
u"type": u"Point",
u"coordinates": [24.946861267089844, 60.16311983618494],
},
)
assert qs.as_pymongo()[0]["location"] == {
u"type": u"Point",
u"coordinates": [24.946861267089844, 60.16311983618494],
}
def test_2dsphere_point_sets_correctly(self):
class Location(Document):
@@ -511,11 +508,11 @@ class TestGeoQueries(MongoDBTestCase):
Location(loc=[1, 2]).save()
loc = Location.objects.as_pymongo()[0]
self.assertEqual(loc["loc"], {"type": "Point", "coordinates": [1, 2]})
assert loc["loc"] == {"type": "Point", "coordinates": [1, 2]}
Location.objects.update(set__loc=[2, 1])
loc = Location.objects.as_pymongo()[0]
self.assertEqual(loc["loc"], {"type": "Point", "coordinates": [2, 1]})
assert loc["loc"] == {"type": "Point", "coordinates": [2, 1]}
def test_2dsphere_linestring_sets_correctly(self):
class Location(Document):
@@ -525,15 +522,11 @@ class TestGeoQueries(MongoDBTestCase):
Location(line=[[1, 2], [2, 2]]).save()
loc = Location.objects.as_pymongo()[0]
self.assertEqual(
loc["line"], {"type": "LineString", "coordinates": [[1, 2], [2, 2]]}
)
assert loc["line"] == {"type": "LineString", "coordinates": [[1, 2], [2, 2]]}
Location.objects.update(set__line=[[2, 1], [1, 2]])
loc = Location.objects.as_pymongo()[0]
self.assertEqual(
loc["line"], {"type": "LineString", "coordinates": [[2, 1], [1, 2]]}
)
assert loc["line"] == {"type": "LineString", "coordinates": [[2, 1], [1, 2]]}
def test_geojson_PolygonField(self):
class Location(Document):
@@ -543,17 +536,17 @@ class TestGeoQueries(MongoDBTestCase):
Location(poly=[[[40, 5], [40, 6], [41, 6], [40, 5]]]).save()
loc = Location.objects.as_pymongo()[0]
self.assertEqual(
loc["poly"],
{"type": "Polygon", "coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]]},
)
assert loc["poly"] == {
"type": "Polygon",
"coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]],
}
Location.objects.update(set__poly=[[[40, 4], [40, 6], [41, 6], [40, 4]]])
loc = Location.objects.as_pymongo()[0]
self.assertEqual(
loc["poly"],
{"type": "Polygon", "coordinates": [[[40, 4], [40, 6], [41, 6], [40, 4]]]},
)
assert loc["poly"] == {
"type": "Polygon",
"coordinates": [[[40, 4], [40, 6], [41, 6], [40, 4]]],
}
if __name__ == "__main__":