<s> """<STR_LIT>""" <EOL> import pickle <EOL> from . google_imports import namespace_manager <EOL> from . google_imports import datastore_types <EOL> from . google_test_imports import unittest <EOL> from . import polymodel <EOL> from . import model <EOL> from . import query <EOL> from . import test_utils <EOL> PolyModel = polymodel . PolyModel <EOL> class PolyModelTests ( test_utils . NDBTest ) : <EOL> def setUp ( self ) : <EOL> super ( PolyModelTests , self ) . setUp ( ) <EOL> the_module = polymodel <EOL> def testBasics ( self ) : <EOL> class Shoe ( PolyModel ) : <EOL> color = model . StringProperty ( ) <EOL> class Moccasin ( Shoe ) : <EOL> leather = model . StringProperty ( ) <EOL> class Sneaker ( Shoe ) : <EOL> pump = model . BooleanProperty ( ) <EOL> self . assertEqual ( Shoe . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( Shoe . _class_key ( ) , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( Moccasin . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( Moccasin . _class_key ( ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( Sneaker . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( Sneaker . _class_key ( ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> s_key = model . Key ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . assertEqual ( Shoe ( ) . put ( ) , s_key ) <EOL> s = s_key . get ( ) <EOL> self . assertEqual ( s . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( s . _class_key ( ) , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( s . class_ , [ '<STR_LIT>' ] ) <EOL> m_key = model . Key ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> self . assertEqual ( Moccasin ( color = '<STR_LIT>' , leather = '<STR_LIT>' ) . put ( ) , <EOL> m_key ) <EOL> m = m_key . get ( ) <EOL> self . assertEqual ( m . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( m . class_ , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> snkr_key = model . Key ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> self . assertEqual ( Sneaker ( color = '<STR_LIT>' , pump = False ) . put ( ) , snkr_key ) <EOL> snkr = snkr_key . get ( ) <EOL> self . assertEqual ( snkr . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( snkr . class_ , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( Shoe . query ( ) . fetch ( ) , [ s , m , snkr ] ) <EOL> self . assertEqual ( Shoe . query ( Sneaker . pump == False ) . fetch ( ) , [ snkr ] ) <EOL> self . assertEqual ( Moccasin . query ( ) . fetch ( ) , [ m ] ) <EOL> self . assertEqual ( Sneaker . query ( ) . fetch ( ) , [ snkr ] ) <EOL> def testBlobKeyProperty ( self ) : <EOL> class MyModel ( PolyModel ) : <EOL> pass <EOL> class MyDerivedModel ( MyModel ) : <EOL> image = model . BlobKeyProperty ( ) <EOL> test_blobkey = datastore_types . BlobKey ( '<STR_LIT>' ) <EOL> m = MyDerivedModel ( ) <EOL> m . image = test_blobkey <EOL> m . put ( ) <EOL> m = m . key . get ( ) <EOL> m . image = test_blobkey <EOL> m . put ( ) <EOL> self . assertTrue ( isinstance ( m . image , datastore_types . BlobKey ) ) <EOL> self . assertEqual ( str ( m . image ) , str ( test_blobkey ) ) <EOL> def testClassKeyProperty ( self ) : <EOL> class Animal ( PolyModel ) : <EOL> pass <EOL> class Dog ( Animal ) : <EOL> pass <EOL> fido = Dog ( ) <EOL> self . assertEqual ( fido . class_ , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertRaises ( TypeError , setattr , fido , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def testPolyExpando ( self ) : <EOL> class Animal ( PolyModel , model . Expando ) : <EOL> pass <EOL> class Mammal ( Animal ) : <EOL> pass <EOL> cat = Mammal ( name = '<STR_LIT>' , naps = <NUM_LIT> , sound = '<STR_LIT>' ) <EOL> cat1 = cat . put ( ) . get ( ) <EOL> self . assertFalse ( cat1 is cat ) <EOL> self . assertEqual ( cat1 , cat ) <EOL> self . assertEqual ( cat1 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( cat1 . naps , <NUM_LIT> ) <EOL> self . assertEqual ( cat1 . sound , '<STR_LIT>' ) <EOL> def testExpandoPoly ( self ) : <EOL> class Animal ( model . Expando , PolyModel ) : <EOL> pass <EOL> class Mammal ( Animal ) : <EOL> pass <EOL> cat = Mammal ( name = '<STR_LIT>' , naps = <NUM_LIT> , sound = '<STR_LIT>' ) <EOL> cat1 = cat . put ( ) . get ( ) <EOL> self . assertFalse ( cat1 is cat ) <EOL> self . assertEqual ( cat1 , cat ) <EOL> self . assertEqual ( cat1 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( cat1 . naps , <NUM_LIT> ) <EOL> self . assertEqual ( cat1 . sound , '<STR_LIT>' ) <EOL> def testInheritance ( self ) : <EOL> class NamedThing ( model . Model ) : <EOL> name = model . StringProperty ( ) <EOL> class Animal ( PolyModel , NamedThing ) : <EOL> legs = model . IntegerProperty ( default = <NUM_LIT:4> ) <EOL> class Canine ( Animal ) : <EOL> pass <EOL> class Dog ( Canine ) : <EOL> breed = model . StringProperty ( default = '<STR_LIT>' ) <EOL> class Wolf ( Canine ) : <EOL> mythical = model . BooleanProperty ( default = False ) <EOL> class Feline ( Animal ) : <EOL> sound = model . StringProperty ( ) <EOL> class Cat ( Feline ) : <EOL> naps = model . IntegerProperty ( ) <EOL> class Panther ( Feline ) : <EOL> pass <EOL> class Monster ( Dog , Cat ) : <EOL> ancestry = model . StringProperty ( ) <EOL> class Ghoul ( Monster , model . Expando ) : <EOL> pass <EOL> k9 = Canine ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( k9 . legs , <NUM_LIT:4> ) <EOL> self . assertEqual ( k9 . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( k9 . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( k9 . _class_key ( ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> tom = Cat ( name = '<STR_LIT>' , naps = <NUM_LIT:12> , sound = '<STR_LIT>' ) <EOL> self . assertTrue ( isinstance ( tom , Cat ) ) <EOL> self . assertTrue ( isinstance ( tom , Feline ) ) <EOL> self . assertTrue ( isinstance ( tom , Animal ) ) <EOL> self . assertTrue ( isinstance ( tom , PolyModel ) ) <EOL> self . assertEqual ( tom . naps , <NUM_LIT:12> ) <EOL> self . assertEqual ( tom . sound , '<STR_LIT>' ) <EOL> self . assertEqual ( tom . legs , <NUM_LIT:4> ) <EOL> self . assertEqual ( tom . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( tom . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( tom . _class_key ( ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> fido = Wolf ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( fido . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( fido . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( fido . _class_key ( ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertRaises ( AttributeError , lambda : fido . breed ) <EOL> scary = Ghoul ( name = '<STR_LIT>' , book = '<STR_LIT>' ) <EOL> self . assertEqual ( scary . ancestry , None ) <EOL> self . assertEqual ( scary . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( scary . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( scary . _class_key ( ) , [ '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> k91 = k9 . put ( ) . get ( ) <EOL> self . assertTrue ( isinstance ( k9 , Canine ) ) <EOL> self . assertEqual ( k9 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( k9 . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( k9 . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( k9 . _class_key ( ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( isinstance ( k91 , Canine ) ) <EOL> self . assertEqual ( k91 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( k91 . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( k91 . _class_name ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( k91 . _class_key ( ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( k91 , k9 ) <EOL> tom1 = tom . put ( ) . get ( ) <EOL> self . assertEqual ( tom1 , tom ) <EOL> fido1 = fido . put ( ) . get ( ) <EOL> self . assertEqual ( fido1 , fido ) <EOL> scary1 = scary . put ( ) . get ( ) <EOL> self . assertEqual ( scary1 , scary ) <EOL> self . assertEqual ( scary1 . book , '<STR_LIT>' ) <EOL> def testPickling ( self ) : <EOL> global Animal , Dog <EOL> class Animal ( PolyModel ) : <EOL> name = model . StringProperty ( ) <EOL> class Dog ( Animal ) : <EOL> breed = model . StringProperty ( ) <EOL> for proto in <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> : <EOL> fido = Dog ( name = '<STR_LIT>' , breed = '<STR_LIT>' ) <EOL> s = pickle . dumps ( fido , proto ) <EOL> fido1 = pickle . loads ( s ) <EOL> self . assertEqual ( fido1 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( fido1 . breed , '<STR_LIT>' ) <EOL> self . assertEqual ( fido1 . class_ , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( fido , fido1 ) <EOL> def testClassNameOverride ( self ) : <EOL> class Animal ( PolyModel ) : <EOL> pass <EOL> class Feline ( Animal ) : <EOL> pass <EOL> class Cat ( Feline ) : <EOL> @ classmethod <EOL> def _class_name ( cls ) : <EOL> return '<STR_LIT>' <EOL> tom = Cat ( ) <EOL> self . assertEqual ( tom . class_ , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> tom . put ( ) <EOL> self . assertEqual ( Cat . query ( ) . fetch ( ) , [ tom ] ) <EOL> def testEdgeCases ( self ) : <EOL> self . assertEqual ( PolyModel . _get_kind ( ) , '<STR_LIT>' ) <EOL> def testMixins ( self ) : <EOL> class Mixin ( object ) : <EOL> pass <EOL> class Entity ( polymodel . PolyModel ) : <EOL> pass <EOL> class ChildEntity ( Entity ) : <EOL> pass <EOL> class RightMixinEntity ( Entity , Mixin ) : <EOL> pass <EOL> class LeftMixinEntity ( Mixin , Entity ) : <EOL> pass <EOL> self . assertEqual ( Entity . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( ChildEntity . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( RightMixinEntity . _get_kind ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( LeftMixinEntity . _get_kind ( ) , '<STR_LIT>' ) <EOL> def testGql ( self ) : <EOL> class A ( polymodel . PolyModel ) : <EOL> pass <EOL> class B ( A ) : <EOL> pass <EOL> class C ( A ) : <EOL> pass <EOL> b = B ( ) <EOL> b . put ( ) <EOL> c = C ( ) <EOL> c . put ( ) <EOL> self . assertEqual ( query . gql ( '<STR_LIT>' ) . fetch ( ) , [ b , c ] ) <EOL> self . assertEqual ( B . gql ( '<STR_LIT>' ) . fetch ( ) , [ b ] ) <EOL> self . assertEqual ( query . gql ( '<STR_LIT>' ) . fetch ( ) , [ b ] ) <EOL> def testQueryFilter ( self ) : <EOL> class Animal ( PolyModel ) : <EOL> pass <EOL> class Cat ( Animal ) : <EOL> pass <EOL> self . assertEqual ( Animal . query ( ) . filters , None ) <EOL> self . assertNotEqual ( Cat . query ( ) . filters , None ) <EOL> TOM_PB = """<STR_LIT>""" <EOL> class CompatibilityTests ( test_utils . NDBTest ) : <EOL> def testCompatibility ( self ) : <EOL> class Animal ( PolyModel ) : <EOL> name = model . StringProperty ( ) <EOL> class Feline ( Animal ) : <EOL> whiskers = model . BooleanProperty ( ) <EOL> class Cat ( Feline ) : <EOL> purr = model . StringProperty ( ) <EOL> tom = Cat ( name = '<STR_LIT>' , purr = '<STR_LIT>' , whiskers = True ) <EOL> tom . _prepare_for_put ( ) <EOL> self . assertEqual ( str ( tom . _to_pb ( ) ) , TOM_PB ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
