<s> import sys <EOL> sys . path [ <NUM_LIT:0> : <NUM_LIT:0> ] = [ "<STR_LIT>" ] <EOL> import unittest <EOL> from mongoengine import * <EOL> from mongoengine . connection import get_db <EOL> __all__ = ( "<STR_LIT>" , ) <EOL> class GeoFieldTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> connect ( db = '<STR_LIT>' ) <EOL> self . db = get_db ( ) <EOL> def _test_for_expected_error ( self , Cls , loc , expected ) : <EOL> try : <EOL> Cls ( loc = loc ) . validate ( ) <EOL> self . fail ( '<STR_LIT>' . format ( loc ) ) <EOL> except ValidationError as e : <EOL> self . assertEqual ( expected , e . to_dict ( ) [ '<STR_LIT>' ] ) <EOL> def test_geopoint_validation ( self ) : <EOL> class Location ( Document ) : <EOL> loc = GeoPointField ( ) <EOL> invalid_coords = [ { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } , <NUM_LIT:5> , "<STR_LIT:a>" ] <EOL> expected = '<STR_LIT>' <EOL> for coord in invalid_coords : <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> invalid_coords = [ [ ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> invalid_coords = [ [ { } , { } ] , ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> def test_point_validation ( self ) : <EOL> class Location ( Document ) : <EOL> loc = PointField ( ) <EOL> invalid_coords = { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ ] } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ <NUM_LIT:5> , "<STR_LIT:a>" ] <EOL> expected = "<STR_LIT>" <EOL> for coord in invalid_coords : <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> invalid_coords = [ [ ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> invalid_coords = [ [ { } , { } ] , ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> Location ( loc = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) . validate ( ) <EOL> Location ( loc = { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> <NUM_LIT> , <EOL> <NUM_LIT> <EOL> ] } ) . validate ( ) <EOL> def test_linestring_validation ( self ) : <EOL> class Location ( Document ) : <EOL> loc = LineStringField ( ) <EOL> invalid_coords = { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ ] ] } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] } <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ <NUM_LIT:5> , "<STR_LIT:a>" ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ <NUM_LIT:1> ] ] <EOL> expected = "<STR_LIT>" % repr ( invalid_coords [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> expected = "<STR_LIT>" % repr ( invalid_coords [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ { } , { } ] ] , [ ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ] ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> Location ( loc = [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ) . validate ( ) <EOL> def test_polygon_validation ( self ) : <EOL> class Location ( Document ) : <EOL> loc = PolygonField ( ) <EOL> invalid_coords = { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ ] ] } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ] } <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ <NUM_LIT:5> , "<STR_LIT:a>" ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ { } , { } ] ] , [ ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> Location ( loc = [ [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ] ) . validate ( ) <EOL> def test_multipoint_validation ( self ) : <EOL> class Location ( Document ) : <EOL> loc = MultiPointField ( ) <EOL> invalid_coords = { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ ] ] } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] } <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ <NUM_LIT:1> ] ] , [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> invalid_coords = [ [ [ { } , { } ] ] , [ ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ] ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> Location ( loc = [ [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ) . validate ( ) <EOL> Location ( loc = { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] <EOL> ] } ) . validate ( ) <EOL> def test_multilinestring_validation ( self ) : <EOL> class Location ( Document ) : <EOL> loc = MultiLineStringField ( ) <EOL> invalid_coords = { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ ] ] } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ] } <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ <NUM_LIT:5> , "<STR_LIT:a>" ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ <NUM_LIT:1> ] ] ] <EOL> expected = "<STR_LIT>" % repr ( invalid_coords [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ] <EOL> expected = "<STR_LIT>" % repr ( invalid_coords [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ [ { } , { } ] ] ] , [ [ ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ] ] ] <EOL> for coord in invalid_coords : <EOL> expected = "<STR_LIT>" % repr ( coord [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . _test_for_expected_error ( Location , coord , expected ) <EOL> Location ( loc = [ [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ] ) . validate ( ) <EOL> def test_multipolygon_validation ( self ) : <EOL> class Location ( Document ) : <EOL> loc = MultiPolygonField ( ) <EOL> invalid_coords = { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ ] ] } <EOL> expected = '<STR_LIT>' <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ] ] } <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ [ <NUM_LIT:5> , "<STR_LIT:a>" ] ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ [ ] ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ [ { } , { } ] ] ] , [ [ ( "<STR_LIT:a>" , "<STR_LIT:b>" ) ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> invalid_coords = [ [ [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] ] ] <EOL> expected = "<STR_LIT>" <EOL> self . _test_for_expected_error ( Location , invalid_coords , expected ) <EOL> Location ( loc = [ [ [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:6> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ] ] ) . validate ( ) <EOL> def test_indexes_geopoint ( self ) : <EOL> """<STR_LIT>""" <EOL> class Event ( Document ) : <EOL> title = StringField ( ) <EOL> location = GeoPointField ( ) <EOL> geo_indicies = Event . _geo_indices ( ) <EOL> self . assertEqual ( geo_indicies , [ { '<STR_LIT>' : [ ( '<STR_LIT:location>' , '<STR_LIT>' ) ] } ] ) <EOL> def test_geopoint_embedded_indexes ( self ) : <EOL> """<STR_LIT>""" <EOL> class Venue ( EmbeddedDocument ) : <EOL> location = GeoPointField ( ) <EOL> name = StringField ( ) <EOL> class Event ( Document ) : <EOL> title = StringField ( ) <EOL> venue = EmbeddedDocumentField ( Venue ) <EOL> geo_indicies = Event . _geo_indices ( ) <EOL> self . assertEqual ( geo_indicies , [ { '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } ] ) <EOL> def test_indexes_2dsphere ( self ) : <EOL> """<STR_LIT>""" <EOL> class Event ( Document ) : <EOL> title = StringField ( ) <EOL> point = PointField ( ) <EOL> line = LineStringField ( ) <EOL> polygon = PolygonField ( ) <EOL> geo_indicies = Event . _geo_indices ( ) <EOL> self . assertTrue ( { '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } in geo_indicies ) <EOL> self . assertTrue ( { '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } in geo_indicies ) <EOL> self . assertTrue ( { '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } in geo_indicies ) <EOL> def test_indexes_2dsphere_embedded ( self ) : <EOL> """<STR_LIT>""" <EOL> class Venue ( EmbeddedDocument ) : <EOL> name = StringField ( ) <EOL> point = PointField ( ) <EOL> line = LineStringField ( ) <EOL> polygon = PolygonField ( ) <EOL> class Event ( Document ) : <EOL> title = StringField ( ) <EOL> venue = EmbeddedDocumentField ( Venue ) <EOL> geo_indicies = Event . _geo_indices ( ) <EOL> self . assertTrue ( { '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } in geo_indicies ) <EOL> self . assertTrue ( { '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } in geo_indicies ) <EOL> self . assertTrue ( { '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] } in geo_indicies ) <EOL> def test_geo_indexes_recursion ( self ) : <EOL> class Location ( Document ) : <EOL> name = StringField ( ) <EOL> location = GeoPointField ( ) <EOL> class Parent ( Document ) : <EOL> name = StringField ( ) <EOL> location = ReferenceField ( Location ) <EOL> Location . drop_collection ( ) <EOL> Parent . drop_collection ( ) <EOL> Parent ( name = '<STR_LIT>' ) . save ( ) <EOL> info = Parent . _get_collection ( ) . index_information ( ) <EOL> self . assertFalse ( '<STR_LIT>' in info ) <EOL> info = Location . _get_collection ( ) . index_information ( ) <EOL> self . assertTrue ( '<STR_LIT>' in info ) <EOL> self . assertEqual ( len ( Parent . _geo_indices ( ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( Location . _geo_indices ( ) ) , <NUM_LIT:1> ) <EOL> def test_geo_indexes_auto_index ( self ) : <EOL> class Log ( Document ) : <EOL> location = PointField ( auto_index = False ) <EOL> datetime = DateTimeField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : [ [ ( "<STR_LIT:location>" , "<STR_LIT>" ) , ( "<STR_LIT>" , <NUM_LIT:1> ) ] ] <EOL> } <EOL> self . assertEqual ( [ ] , Log . _geo_indices ( ) ) <EOL> Log . drop_collection ( ) <EOL> Log . ensure_indexes ( ) <EOL> info = Log . _get_collection ( ) . index_information ( ) <EOL> self . assertEqual ( info [ "<STR_LIT>" ] [ "<STR_LIT:key>" ] , <EOL> [ ( '<STR_LIT:location>' , '<STR_LIT>' ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] ) <EOL> class Log ( Document ) : <EOL> location = PointField ( auto_index = False ) <EOL> datetime = DateTimeField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : [ ( "<STR_LIT:location>" , "<STR_LIT>" ) , ( "<STR_LIT>" , <NUM_LIT:1> ) ] } <EOL> ] <EOL> } <EOL> self . assertEqual ( [ ] , Log . _geo_indices ( ) ) <EOL> Log . drop_collection ( ) <EOL> Log . ensure_indexes ( ) <EOL> info = Log . _get_collection ( ) . index_information ( ) <EOL> self . assertEqual ( info [ "<STR_LIT>" ] [ "<STR_LIT:key>" ] , <EOL> [ ( '<STR_LIT:location>' , '<STR_LIT>' ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
