'Ensure that spherical geospatial queries are working.'

@needs_mongodb_v3
def test_spherical_geospatial_operators(self):

    class Point(Document, ):
        location = GeoPointField()
    Point.drop_collection()
    north_point = Point(location=[(-122), 38]).save()
    south_point = Point(location=[(-122), 37]).save()
    earth_radius = 6378.009
    points = Point.objects(location__near_sphere=[(-122), 37.5])
    self.assertEqual(points.count(), 2)
    points = Point.objects(location__within_spherical_distance=[[(-122), 37.5], (60 / earth_radius)])
    self.assertEqual(points.count(), 2)
    points = Point.objects(location__near_sphere=[(-122), 37.5], location__max_distance=(60 / earth_radius))
    self.assertEqual(points.count(), 2)
    points = Point.objects(location__near_sphere=[(-122), 37.8], location__max_distance=(60 / earth_radius))
    close_point = points.first()
    self.assertEqual(points.count(), 1)
    points = Point.objects(location__near_sphere=[(-122), 37.8], location__min_distance=(60 / earth_radius))
    self.assertEqual(points.count(), 1)
    far_point = points.first()
    self.assertNotEqual(close_point, far_point)
    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)
    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)
    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)
