<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 NULLIFY , PULL <EOL> from mongoengine . connection import get_db <EOL> __all__ = ( "<STR_LIT>" , ) <EOL> class ClassMethodsTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> connect ( db = '<STR_LIT>' ) <EOL> self . db = get_db ( ) <EOL> class Person ( Document ) : <EOL> name = StringField ( ) <EOL> age = IntField ( ) <EOL> non_field = True <EOL> meta = { "<STR_LIT>" : True } <EOL> self . Person = Person <EOL> def tearDown ( self ) : <EOL> for collection in self . db . collection_names ( ) : <EOL> if '<STR_LIT>' in collection : <EOL> continue <EOL> self . db . drop_collection ( collection ) <EOL> def test_definition ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:name>' ] , <EOL> sorted ( self . Person . _fields . keys ( ) ) ) <EOL> self . assertEqual ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> sorted ( [ x . __class__ . __name__ for x in <EOL> self . Person . _fields . values ( ) ] ) ) <EOL> def test_get_db ( self ) : <EOL> """<STR_LIT>""" <EOL> db = self . Person . _get_db ( ) <EOL> self . assertEqual ( self . db , db ) <EOL> def test_get_collection_name ( self ) : <EOL> """<STR_LIT>""" <EOL> collection_name = '<STR_LIT>' <EOL> self . assertEqual ( collection_name , self . Person . _get_collection_name ( ) ) <EOL> def test_get_collection ( self ) : <EOL> """<STR_LIT>""" <EOL> collection_name = '<STR_LIT>' <EOL> collection = self . Person . _get_collection ( ) <EOL> self . assertEqual ( self . db [ collection_name ] , collection ) <EOL> def test_drop_collection ( self ) : <EOL> """<STR_LIT>""" <EOL> collection_name = '<STR_LIT>' <EOL> self . Person ( name = '<STR_LIT>' ) . save ( ) <EOL> self . assertTrue ( collection_name in self . db . collection_names ( ) ) <EOL> self . Person . drop_collection ( ) <EOL> self . assertFalse ( collection_name in self . db . collection_names ( ) ) <EOL> def test_register_delete_rule ( self ) : <EOL> """<STR_LIT>""" <EOL> class Job ( Document ) : <EOL> employee = ReferenceField ( self . Person ) <EOL> self . assertEqual ( self . Person . _meta . get ( '<STR_LIT>' ) , None ) <EOL> self . Person . register_delete_rule ( Job , '<STR_LIT>' , NULLIFY ) <EOL> self . assertEqual ( self . Person . _meta [ '<STR_LIT>' ] , <EOL> { ( Job , '<STR_LIT>' ) : NULLIFY } ) <EOL> def test_compare_indexes ( self ) : <EOL> """<STR_LIT>""" <EOL> class BlogPost ( Document ) : <EOL> author = StringField ( ) <EOL> title = StringField ( ) <EOL> description = StringField ( ) <EOL> tags = StringField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT:title>' ) ] <EOL> } <EOL> BlogPost . drop_collection ( ) <EOL> BlogPost . ensure_indexes ( ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } ) <EOL> BlogPost . ensure_index ( [ '<STR_LIT>' , '<STR_LIT:description>' ] ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT:description>' , <NUM_LIT:1> ) ] ] } ) <EOL> BlogPost . _get_collection ( ) . drop_index ( '<STR_LIT>' ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } ) <EOL> BlogPost . _get_collection ( ) . drop_index ( '<STR_LIT>' ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT:title>' , <NUM_LIT:1> ) ] ] , '<STR_LIT>' : [ ] } ) <EOL> def test_compare_indexes_inheritance ( self ) : <EOL> """<STR_LIT>""" <EOL> class BlogPost ( Document ) : <EOL> author = StringField ( ) <EOL> title = StringField ( ) <EOL> description = StringField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : True <EOL> } <EOL> class BlogPostWithTags ( BlogPost ) : <EOL> tags = StringField ( ) <EOL> tag_list = ListField ( StringField ( ) ) <EOL> meta = { <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } <EOL> BlogPost . drop_collection ( ) <EOL> BlogPost . ensure_indexes ( ) <EOL> BlogPostWithTags . ensure_indexes ( ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } ) <EOL> BlogPostWithTags . ensure_index ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] ] } ) <EOL> BlogPostWithTags . _get_collection ( ) . drop_index ( '<STR_LIT>' ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } ) <EOL> BlogPostWithTags . _get_collection ( ) . drop_index ( '<STR_LIT>' ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] ] , '<STR_LIT>' : [ ] } ) <EOL> def test_compare_indexes_multiple_subclasses ( self ) : <EOL> """<STR_LIT>""" <EOL> class BlogPost ( Document ) : <EOL> author = StringField ( ) <EOL> title = StringField ( ) <EOL> description = StringField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : True <EOL> } <EOL> class BlogPostWithTags ( BlogPost ) : <EOL> tags = StringField ( ) <EOL> tag_list = ListField ( StringField ( ) ) <EOL> meta = { <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } <EOL> class BlogPostWithCustomField ( BlogPost ) : <EOL> custom = DictField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } <EOL> BlogPost . ensure_indexes ( ) <EOL> BlogPostWithTags . ensure_indexes ( ) <EOL> BlogPostWithCustomField . ensure_indexes ( ) <EOL> self . assertEqual ( BlogPost . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } ) <EOL> self . assertEqual ( BlogPostWithTags . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } ) <EOL> self . assertEqual ( BlogPostWithCustomField . compare_indexes ( ) , { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } ) <EOL> def test_list_indexes_inheritance ( self ) : <EOL> """<STR_LIT>""" <EOL> class BlogPost ( Document ) : <EOL> author = StringField ( ) <EOL> title = StringField ( ) <EOL> description = StringField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : True <EOL> } <EOL> class BlogPostWithTags ( BlogPost ) : <EOL> tags = StringField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } <EOL> class BlogPostWithTagsAndExtraText ( BlogPostWithTags ) : <EOL> extra_text = StringField ( ) <EOL> meta = { <EOL> '<STR_LIT>' : [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } <EOL> BlogPost . drop_collection ( ) <EOL> BlogPost . ensure_indexes ( ) <EOL> BlogPostWithTags . ensure_indexes ( ) <EOL> BlogPostWithTagsAndExtraText . ensure_indexes ( ) <EOL> self . assertEqual ( BlogPost . list_indexes ( ) , <EOL> BlogPostWithTags . list_indexes ( ) ) <EOL> self . assertEqual ( BlogPost . list_indexes ( ) , <EOL> BlogPostWithTagsAndExtraText . list_indexes ( ) ) <EOL> self . assertEqual ( BlogPost . list_indexes ( ) , <EOL> [ [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] , <EOL> [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] , <EOL> [ ( u'<STR_LIT>' , <NUM_LIT:1> ) ] , [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] ] ) <EOL> def test_register_delete_rule_inherited ( self ) : <EOL> class Vaccine ( Document ) : <EOL> name = StringField ( required = True ) <EOL> meta = { "<STR_LIT>" : [ "<STR_LIT:name>" ] } <EOL> class Animal ( Document ) : <EOL> family = StringField ( required = True ) <EOL> vaccine_made = ListField ( ReferenceField ( "<STR_LIT>" , reverse_delete_rule = PULL ) ) <EOL> meta = { "<STR_LIT>" : True , "<STR_LIT>" : [ "<STR_LIT>" ] } <EOL> class Cat ( Animal ) : <EOL> name = StringField ( required = True ) <EOL> self . assertEqual ( Vaccine . _meta [ '<STR_LIT>' ] [ ( Animal , '<STR_LIT>' ) ] , PULL ) <EOL> self . assertEqual ( Vaccine . _meta [ '<STR_LIT>' ] [ ( Cat , '<STR_LIT>' ) ] , PULL ) <EOL> def test_collection_naming ( self ) : <EOL> """<STR_LIT>""" <EOL> class DefaultNamingTest ( Document ) : <EOL> pass <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> DefaultNamingTest . _get_collection_name ( ) ) <EOL> class CustomNamingTest ( Document ) : <EOL> meta = { '<STR_LIT>' : '<STR_LIT>' } <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> CustomNamingTest . _get_collection_name ( ) ) <EOL> class DynamicNamingTest ( Document ) : <EOL> meta = { '<STR_LIT>' : lambda c : "<STR_LIT>" } <EOL> self . assertEqual ( '<STR_LIT>' , DynamicNamingTest . _get_collection_name ( ) ) <EOL> class BaseDocument ( Document ) : <EOL> meta = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : lambda c : c . __name__ . lower ( ) <EOL> } <EOL> class OldNamingConvention ( BaseDocument ) : <EOL> pass <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> OldNamingConvention . _get_collection_name ( ) ) <EOL> class InheritedAbstractNamingTest ( BaseDocument ) : <EOL> meta = { '<STR_LIT>' : '<STR_LIT>' } <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> InheritedAbstractNamingTest . _get_collection_name ( ) ) <EOL> class BaseMixin ( object ) : <EOL> meta = { <EOL> '<STR_LIT>' : lambda c : c . __name__ . lower ( ) <EOL> } <EOL> class OldMixinNamingConvention ( Document , BaseMixin ) : <EOL> pass <EOL> self . assertEqual ( '<STR_LIT>' , <EOL> OldMixinNamingConvention . _get_collection_name ( ) ) <EOL> class BaseMixin ( object ) : <EOL> meta = { <EOL> '<STR_LIT>' : lambda c : c . __name__ . lower ( ) <EOL> } <EOL> class BaseDocument ( Document , BaseMixin ) : <EOL> meta = { '<STR_LIT>' : True } <EOL> class MyDocument ( BaseDocument ) : <EOL> pass <EOL> self . assertEqual ( '<STR_LIT>' , MyDocument . _get_collection_name ( ) ) <EOL> def test_custom_collection_name_operations ( self ) : <EOL> """<STR_LIT>""" <EOL> collection_name = '<STR_LIT>' <EOL> class Person ( Document ) : <EOL> name = StringField ( ) <EOL> meta = { '<STR_LIT>' : collection_name } <EOL> Person ( name = "<STR_LIT>" ) . save ( ) <EOL> self . assertTrue ( collection_name in self . db . collection_names ( ) ) <EOL> user_obj = self . db [ collection_name ] . find_one ( ) <EOL> self . assertEqual ( user_obj [ '<STR_LIT:name>' ] , "<STR_LIT>" ) <EOL> user_obj = Person . objects [ <NUM_LIT:0> ] <EOL> self . assertEqual ( user_obj . name , "<STR_LIT>" ) <EOL> Person . drop_collection ( ) <EOL> self . assertFalse ( collection_name in self . db . collection_names ( ) ) <EOL> def test_collection_name_and_primary ( self ) : <EOL> """<STR_LIT>""" <EOL> class Person ( Document ) : <EOL> name = StringField ( primary_key = True ) <EOL> meta = { '<STR_LIT>' : '<STR_LIT>' } <EOL> Person ( name = "<STR_LIT>" ) . save ( ) <EOL> user_obj = Person . objects . first ( ) <EOL> self . assertEqual ( user_obj . name , "<STR_LIT>" ) <EOL> Person . drop_collection ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
