<s> import sys <EOL> sys . path [ <NUM_LIT:0> : <NUM_LIT:0> ] = [ "<STR_LIT>" ] <EOL> import unittest <EOL> from mongoengine import * <EOL> from mongoengine . queryset import Q <EOL> from mongoengine . queryset import transform <EOL> __all__ = ( "<STR_LIT>" , ) <EOL> class TransformTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> connect ( db = '<STR_LIT>' ) <EOL> def test_transform_query ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( transform . query ( name = '<STR_LIT:test>' , age = <NUM_LIT:30> ) , <EOL> { '<STR_LIT:name>' : '<STR_LIT:test>' , '<STR_LIT>' : <NUM_LIT:30> } ) <EOL> self . assertEqual ( transform . query ( age__lt = <NUM_LIT:30> ) , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:30> } } ) <EOL> self . assertEqual ( transform . query ( age__gt = <NUM_LIT:20> , age__lt = <NUM_LIT:50> ) , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:20> , '<STR_LIT>' : <NUM_LIT:50> } } ) <EOL> self . assertEqual ( transform . query ( age = <NUM_LIT:20> , age__gt = <NUM_LIT:50> ) , <EOL> { '<STR_LIT>' : [ { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:50> } } , { '<STR_LIT>' : <NUM_LIT:20> } ] } ) <EOL> self . assertEqual ( transform . query ( friend__age__gte = <NUM_LIT:30> ) , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:30> } } ) <EOL> self . assertEqual ( transform . query ( name__exists = True ) , <EOL> { '<STR_LIT:name>' : { '<STR_LIT>' : True } } ) <EOL> def test_transform_update ( self ) : <EOL> class DicDoc ( Document ) : <EOL> dictField = DictField ( ) <EOL> class Doc ( Document ) : <EOL> pass <EOL> DicDoc . drop_collection ( ) <EOL> Doc . drop_collection ( ) <EOL> doc = Doc ( ) . save ( ) <EOL> dic_doc = DicDoc ( ) . save ( ) <EOL> for k , v in ( ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> update = transform . update ( DicDoc , ** { "<STR_LIT>" % k : doc } ) <EOL> self . assertTrue ( isinstance ( update [ v ] [ "<STR_LIT>" ] , dict ) ) <EOL> update = transform . update ( DicDoc , unset__dictField__test = doc ) <EOL> self . assertEqual ( update [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> update = transform . update ( DicDoc , pull__dictField__test = doc ) <EOL> self . assertTrue ( isinstance ( update [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:test>" ] , dict ) ) <EOL> def test_query_field_name ( self ) : <EOL> """<STR_LIT>""" <EOL> class Comment ( EmbeddedDocument ) : <EOL> content = StringField ( db_field = '<STR_LIT>' ) <EOL> class BlogPost ( Document ) : <EOL> title = StringField ( db_field = '<STR_LIT>' ) <EOL> comments = ListField ( EmbeddedDocumentField ( Comment ) , <EOL> db_field = '<STR_LIT>' ) <EOL> BlogPost . drop_collection ( ) <EOL> data = { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : [ Comment ( content = '<STR_LIT:test>' ) ] } <EOL> post = BlogPost ( ** data ) <EOL> post . save ( ) <EOL> self . assertTrue ( '<STR_LIT>' in <EOL> BlogPost . objects ( title = data [ '<STR_LIT:title>' ] ) . _query ) <EOL> self . assertFalse ( '<STR_LIT:title>' in <EOL> BlogPost . objects ( title = data [ '<STR_LIT:title>' ] ) . _query ) <EOL> self . assertEqual ( BlogPost . objects ( title = data [ '<STR_LIT:title>' ] ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT>' in BlogPost . objects ( pk = post . id ) . _query ) <EOL> self . assertEqual ( BlogPost . objects ( pk = post . id ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT>' in <EOL> BlogPost . objects ( comments__content = '<STR_LIT:test>' ) . _query ) <EOL> self . assertEqual ( BlogPost . objects ( comments__content = '<STR_LIT:test>' ) . count ( ) , <NUM_LIT:1> ) <EOL> BlogPost . drop_collection ( ) <EOL> def test_query_pk_field_name ( self ) : <EOL> """<STR_LIT>""" <EOL> class BlogPost ( Document ) : <EOL> title = StringField ( primary_key = True , db_field = '<STR_LIT>' ) <EOL> BlogPost . drop_collection ( ) <EOL> data = { '<STR_LIT:title>' : '<STR_LIT>' } <EOL> post = BlogPost ( ** data ) <EOL> post . save ( ) <EOL> self . assertTrue ( '<STR_LIT>' in BlogPost . objects ( pk = data [ '<STR_LIT:title>' ] ) . _query ) <EOL> self . assertTrue ( '<STR_LIT>' in BlogPost . objects ( title = data [ '<STR_LIT:title>' ] ) . _query ) <EOL> self . assertEqual ( BlogPost . objects ( pk = data [ '<STR_LIT:title>' ] ) . count ( ) , <NUM_LIT:1> ) <EOL> BlogPost . drop_collection ( ) <EOL> def test_chaining ( self ) : <EOL> class A ( Document ) : <EOL> pass <EOL> class B ( Document ) : <EOL> a = ReferenceField ( A ) <EOL> A . drop_collection ( ) <EOL> B . drop_collection ( ) <EOL> a1 = A ( ) . save ( ) <EOL> a2 = A ( ) . save ( ) <EOL> B ( a = a1 ) . save ( ) <EOL> q1 = B . objects . filter ( a__in = [ a1 , a2 ] , a = a1 ) . _query <EOL> q2 = B . objects . filter ( a__in = [ a1 , a2 ] ) <EOL> q2 = q2 . filter ( a = a1 ) . _query <EOL> self . assertEqual ( q1 , q2 ) <EOL> def test_raw_query_and_Q_objects ( self ) : <EOL> """<STR_LIT>""" <EOL> class Foo ( Document ) : <EOL> name = StringField ( ) <EOL> a = StringField ( ) <EOL> b = StringField ( ) <EOL> c = StringField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : False <EOL> } <EOL> query = Foo . objects ( __raw__ = { '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT:bar>' } ] } ) . _query <EOL> self . assertEqual ( query , { '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT:bar>' } ] } ) <EOL> q1 = { '<STR_LIT>' : [ { '<STR_LIT:a>' : <NUM_LIT:1> } , { '<STR_LIT:b>' : <NUM_LIT:1> } ] } <EOL> query = Foo . objects ( Q ( __raw__ = q1 ) & Q ( c = <NUM_LIT:1> ) ) . _query <EOL> self . assertEqual ( query , { '<STR_LIT>' : [ { '<STR_LIT:a>' : <NUM_LIT:1> } , { '<STR_LIT:b>' : <NUM_LIT:1> } ] , '<STR_LIT:c>' : <NUM_LIT:1> } ) <EOL> def test_raw_and_merging ( self ) : <EOL> class Doc ( Document ) : <EOL> meta = { '<STR_LIT>' : False } <EOL> raw_query = Doc . objects ( __raw__ = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT:yes>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> } ) . _query <EOL> expected = { '<STR_LIT>' : False , '<STR_LIT>' : '<STR_LIT:yes>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ] } <EOL> self . assertEqual ( expected , raw_query ) <EOL> def test_geojson_PointField ( self ) : <EOL> class Location ( Document ) : <EOL> loc = PointField ( ) <EOL> update = transform . update ( Location , set__loc = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertEqual ( update , { '<STR_LIT>' : { '<STR_LIT>' : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> ] } } } ) <EOL> update = transform . update ( Location , set__loc = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> ] } ) <EOL> self . assertEqual ( update , { '<STR_LIT>' : { '<STR_LIT>' : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:2> ] } } } ) <EOL> def test_geojson_LineStringField ( self ) : <EOL> class Location ( Document ) : <EOL> line = LineStringField ( ) <EOL> update = transform . update ( Location , set__line = [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] ) <EOL> self . assertEqual ( update , { '<STR_LIT>' : { '<STR_LIT>' : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] } } } ) <EOL> update = transform . update ( Location , set__line = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] } ) <EOL> self . assertEqual ( update , { '<STR_LIT>' : { '<STR_LIT>' : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:2> ] ] } } } ) <EOL> def test_geojson_PolygonField ( self ) : <EOL> class Location ( Document ) : <EOL> poly = PolygonField ( ) <EOL> update = transform . update ( Location , set__poly = [ [ [ <NUM_LIT> , <NUM_LIT:5> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:5> ] ] ] ) <EOL> self . assertEqual ( update , { '<STR_LIT>' : { '<STR_LIT>' : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ [ <NUM_LIT> , <NUM_LIT:5> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:5> ] ] ] } } } ) <EOL> update = transform . update ( Location , set__poly = { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ [ <NUM_LIT> , <NUM_LIT:5> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:5> ] ] ] } ) <EOL> self . assertEqual ( update , { '<STR_LIT>' : { '<STR_LIT>' : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ [ [ <NUM_LIT> , <NUM_LIT:5> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:6> ] , [ <NUM_LIT> , <NUM_LIT:5> ] ] ] } } } ) <EOL> def test_type ( self ) : <EOL> class Doc ( Document ) : <EOL> df = DynamicField ( ) <EOL> Doc ( df = True ) . save ( ) <EOL> Doc ( df = <NUM_LIT:7> ) . save ( ) <EOL> Doc ( df = "<STR_LIT>" ) . save ( ) <EOL> self . assertEqual ( Doc . objects ( df__type = <NUM_LIT:1> ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Doc . objects ( df__type = <NUM_LIT:8> ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Doc . objects ( df__type = <NUM_LIT:2> ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Doc . objects ( df__type = <NUM_LIT:16> ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_last_field_name_like_operator ( self ) : <EOL> class EmbeddedItem ( EmbeddedDocument ) : <EOL> type = StringField ( ) <EOL> name = StringField ( ) <EOL> class Doc ( Document ) : <EOL> item = EmbeddedDocumentField ( EmbeddedItem ) <EOL> Doc . drop_collection ( ) <EOL> doc = Doc ( item = EmbeddedItem ( type = "<STR_LIT>" , name = "<STR_LIT>" ) ) <EOL> doc . save ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , Doc . objects ( item__type__ = "<STR_LIT>" ) . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , Doc . objects ( item__name__ = "<STR_LIT>" ) . count ( ) ) <EOL> def test_understandable_error_raised ( self ) : <EOL> class Event ( Document ) : <EOL> title = StringField ( ) <EOL> location = GeoPointField ( ) <EOL> box = [ ( <NUM_LIT> , - <NUM_LIT> ) , ( <NUM_LIT> , - <NUM_LIT> ) ] <EOL> events = Event . objects ( location__within = box ) <EOL> self . assertRaises ( InvalidQueryError , lambda : events . count ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
