<s> import os <EOL> import unittest <EOL> from document import DjangoDocument <EOL> class Talk ( DjangoDocument ) : <EOL> structure = { '<STR_LIT>' : unicode } <EOL> class CrazyOne ( DjangoDocument ) : <EOL> class Meta : <EOL> verbose_name = u"<STR_LIT>" <EOL> structure = { '<STR_LIT:name>' : unicode } <EOL> class CrazyTwo ( DjangoDocument ) : <EOL> class Meta : <EOL> verbose_name = u"<STR_LIT>" <EOL> verbose_name_plural = u"<STR_LIT>" <EOL> structure = { '<STR_LIT>' : unicode } <EOL> class LighteningTalk ( Talk ) : <EOL> structure = { '<STR_LIT>' : bool } <EOL> default_values = { '<STR_LIT>' : True } <EOL> class DocumentTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> from shortcut import connection <EOL> connection . register ( [ Talk , CrazyOne , CrazyTwo , LighteningTalk ] ) <EOL> self . connection = connection <EOL> self . database = connection [ '<STR_LIT>' ] <EOL> def tearDown ( self ) : <EOL> self . connection . drop_database ( '<STR_LIT>' ) <EOL> def test_meta_creation ( self ) : <EOL> """<STR_LIT>""" <EOL> klass = Talk <EOL> self . assertTrue ( klass . _meta ) <EOL> self . assertFalse ( hasattr ( klass . _meta , '<STR_LIT>' ) ) <EOL> self . assertEqual ( klass . _meta . verbose_name , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . verbose_name_plural , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . app_label , u"<STR_LIT:__main__>" ) <EOL> self . assertEqual ( klass . _meta . model_name , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . pk . attname , '<STR_LIT>' ) <EOL> repr_ = repr ( klass . _meta ) <EOL> self . assertEqual ( repr_ . count ( '<STR_LIT>' ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( repr_ . count ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> def test_meta_creation_overwriting_verbose_name ( self ) : <EOL> klass = CrazyOne <EOL> self . assertTrue ( klass . _meta ) <EOL> self . assertEqual ( klass . _meta . verbose_name , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . verbose_name_plural , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . model_name , u"<STR_LIT>" ) <EOL> def test_meta_creation_overwriting_verbose_name_and_plural ( self ) : <EOL> klass = CrazyTwo <EOL> self . assertTrue ( klass . _meta ) <EOL> self . assertEqual ( klass . _meta . verbose_name , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . verbose_name_plural , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . model_name , u"<STR_LIT>" ) <EOL> def test_subclassed_document ( self ) : <EOL> klass = LighteningTalk <EOL> self . assertTrue ( klass . _meta ) <EOL> self . assertEqual ( klass . _meta . verbose_name , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . verbose_name_plural , u"<STR_LIT>" ) <EOL> self . assertEqual ( klass . _meta . model_name , u"<STR_LIT>" ) <EOL> def test_pk_shortcut ( self ) : <EOL> collection = self . database . talks <EOL> talk = collection . Talk ( ) <EOL> self . assertRaises ( KeyError , lambda t : t . pk , talk ) <EOL> talk [ '<STR_LIT>' ] = u"<STR_LIT>" <EOL> talk . save ( ) <EOL> self . assertTrue ( talk [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( talk . pk ) <EOL> self . assertTrue ( isinstance ( talk . pk , str ) ) <EOL> self . assertEqual ( talk . pk , str ( talk [ '<STR_LIT>' ] ) ) <EOL> def setter ( inst , forced_id ) : <EOL> inst . pk = forced_id <EOL> self . assertRaises ( ValueError , setter , talk , '<STR_LIT>' ) <EOL> def test_signals ( self ) : <EOL> _fired = [ ] <EOL> def trigger_pre_delete ( sender , instance , ** __ ) : <EOL> if sender is LighteningTalk : <EOL> if isinstance ( instance , LighteningTalk ) : <EOL> _fired . append ( '<STR_LIT>' ) <EOL> def trigger_post_delete ( sender , instance , ** __ ) : <EOL> if sender is LighteningTalk : <EOL> if isinstance ( instance , LighteningTalk ) : <EOL> _fired . append ( '<STR_LIT>' ) <EOL> def trigger_pre_save ( sender , instance , raw = None , ** __ ) : <EOL> if sender is LighteningTalk : <EOL> if isinstance ( instance , LighteningTalk ) : <EOL> if not getattr ( instance , '<STR_LIT>' , None ) : <EOL> _fired . append ( '<STR_LIT>' ) <EOL> def trigger_post_save ( sender , instance , raw = None , created = False , ** __ ) : <EOL> assert created in ( True , False ) , "<STR_LIT>" <EOL> if sender is LighteningTalk : <EOL> if isinstance ( instance , LighteningTalk ) : <EOL> if created : <EOL> _fired . append ( '<STR_LIT>' ) <EOL> else : <EOL> _fired . append ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in instance : <EOL> _fired . append ( '<STR_LIT>' ) <EOL> from django . db . models import signals <EOL> signals . pre_delete . connect ( trigger_pre_delete , sender = LighteningTalk ) <EOL> signals . post_delete . connect ( trigger_post_delete , sender = LighteningTalk ) <EOL> signals . pre_save . connect ( trigger_pre_save , sender = LighteningTalk ) <EOL> signals . post_save . connect ( trigger_post_save , sender = LighteningTalk ) <EOL> collection = self . database . talks <EOL> talk = collection . LighteningTalk ( ) <EOL> talk [ '<STR_LIT>' ] = u"<STR_LIT>" <EOL> talk . save ( ) <EOL> self . assertTrue ( '<STR_LIT>' in _fired ) <EOL> self . assertTrue ( '<STR_LIT>' in _fired ) <EOL> self . assertTrue ( '<STR_LIT>' in _fired ) <EOL> self . assertTrue ( '<STR_LIT>' not in _fired ) <EOL> talk . delete ( ) <EOL> self . assertTrue ( '<STR_LIT>' in _fired ) <EOL> self . assertTrue ( '<STR_LIT>' in _fired ) <EOL> talk [ '<STR_LIT>' ] = u"<STR_LIT>" <EOL> talk . save ( ) <EOL> self . assertTrue ( '<STR_LIT>' in _fired ) <EOL> class ShortcutTestCase ( unittest . TestCase ) : <EOL> def test_get_database ( self ) : <EOL> from shortcut import get_database , connection <EOL> db = get_database ( ) <EOL> self . assertEqual ( db . connection , connection ) <EOL> db = get_database ( connection ) <EOL> self . assertEqual ( db . connection , connection ) <EOL> def test_get_version ( self ) : <EOL> from shortcut import get_version <EOL> version = get_version ( ) <EOL> self . assertEqual ( <EOL> version , <EOL> open ( os . path . join ( os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' ) ) . read ( ) <EOL> ) <EOL> class MongoDBBaseTestCase ( unittest . TestCase ) : <EOL> def test_load_backend ( self ) : <EOL> try : <EOL> from django . db import connections <EOL> except ImportError : <EOL> return <EOL> self . assertTrue ( '<STR_LIT>' in connections ) <EOL> from django . db . utils import load_backend <EOL> backend = load_backend ( '<STR_LIT>' ) <EOL> self . assertTrue ( backend is not None ) <EOL> def test_database_wrapper ( self ) : <EOL> try : <EOL> from django . db import connections <EOL> except ImportError : <EOL> return <EOL> connection = connections [ '<STR_LIT>' ] <EOL> self . assertTrue ( hasattr ( connection , '<STR_LIT>' ) ) <EOL> self . assertTrue ( hasattr ( connection . connection , '<STR_LIT>' ) ) <EOL> def test_create_test_database ( self ) : <EOL> from django . conf import settings <EOL> try : <EOL> assert '<STR_LIT>' in settings . DATABASES <EOL> except AttributeError : <EOL> return <EOL> old_database_name = settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> assert '<STR_LIT>' not in old_database_name <EOL> from django . db import connections <EOL> connection = connections [ '<STR_LIT>' ] <EOL> connection . creation . create_test_db ( ) <EOL> test_database_name = settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . assertTrue ( '<STR_LIT>' in test_database_name ) <EOL> from mongokit import Connection <EOL> con = Connection ( ) <EOL> self . assertTrue ( test_database_name not in con . database_names ( ) ) <EOL> db = con [ settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> coll = db . test_collection_name <EOL> list ( coll . find ( ) ) <EOL> test_database_name = settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . assertTrue ( test_database_name in con . database_names ( ) ) <EOL> connection . creation . destroy_test_db ( old_database_name ) <EOL> self . assertTrue ( '<STR_LIT>' not in settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( test_database_name not in con . database_names ( ) ) <EOL> connection . close ( ) <EOL> def test_create_test_database_by_specific_bad_name ( self ) : <EOL> from django . conf import settings <EOL> try : <EOL> assert '<STR_LIT>' in settings . DATABASES <EOL> except AttributeError : <EOL> return <EOL> settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> from django . db import connections <EOL> connection = connections [ '<STR_LIT>' ] <EOL> self . assertRaises ( Exception , connection . creation . create_test_db ) <EOL> def test_create_test_database_by_specific_good_name ( self ) : <EOL> from django . conf import settings <EOL> try : <EOL> assert '<STR_LIT>' in settings . DATABASES <EOL> except AttributeError : <EOL> return <EOL> settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> old_database_name = settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> from django . db import connections <EOL> connection = connections [ '<STR_LIT>' ] <EOL> connection . creation . create_test_db ( ) <EOL> test_database_name = settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . assertTrue ( '<STR_LIT>' in test_database_name ) <EOL> from mongokit import Connection <EOL> con = Connection ( ) <EOL> self . assertTrue ( test_database_name not in con . database_names ( ) ) <EOL> db = con [ settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> coll = db . test_collection_name <EOL> list ( coll . find ( ) ) <EOL> test_database_name = settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . assertTrue ( test_database_name in con . database_names ( ) ) <EOL> connection . creation . destroy_test_db ( old_database_name ) <EOL> self . assertTrue ( '<STR_LIT>' not in <EOL> settings . DATABASES [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( test_database_name not in con . database_names ( ) ) <EOL> import datetime <EOL> from django_mongokit . forms import DocumentForm <EOL> from django_mongokit . forms import fields as mongokit_fields <EOL> from django import forms <EOL> class DetailedTalk ( DjangoDocument ) : <EOL> """<STR_LIT>""" <EOL> structure = { <EOL> '<STR_LIT>' : datetime . datetime , <EOL> '<STR_LIT>' : unicode , <EOL> '<STR_LIT>' : datetime . datetime , <EOL> '<STR_LIT>' : list , <EOL> '<STR_LIT>' : float , <EOL> } <EOL> default_values = { <EOL> '<STR_LIT>' : datetime . datetime . utcnow <EOL> } <EOL> required_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class BasicTalkForm ( DocumentForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> document = DetailedTalk <EOL> class BasicDocumentFormTest ( unittest . TestCase ) : <EOL> "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> from shortcut import connection <EOL> self . connection = connection <EOL> self . database = self . connection [ '<STR_LIT>' ] <EOL> self . now = datetime . datetime . utcnow ( ) <EOL> self . form = BasicTalkForm ( collection = self . database . test_collection ) <EOL> def tearDown ( self ) : <EOL> self . connection . drop_database ( '<STR_LIT>' ) <EOL> def test_all_fields_created ( self ) : <EOL> "<STR_LIT>" <EOL> self . assertEquals ( set ( self . form . fields . keys ( ) ) , <EOL> set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . __class__ , <EOL> forms . fields . DateTimeField ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . __class__ , <EOL> forms . fields . CharField ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . __class__ , <EOL> forms . fields . DateTimeField ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . __class__ , <EOL> mongokit_fields . JsonListField ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . __class__ , <EOL> forms . fields . FloatField ) <EOL> def test_required_set_correctly ( self ) : <EOL> "<STR_LIT>" <EOL> for field_name , field in self . form . fields . items ( ) : <EOL> if field_name in DetailedTalk . required_fields : <EOL> self . assertTrue ( <EOL> field . required , <EOL> "<STR_LIT>" % field_name <EOL> ) <EOL> else : <EOL> self . assertEquals ( <EOL> field . required , <EOL> False , <EOL> "<STR_LIT>" % field_name <EOL> ) <EOL> def test_initial_values_set_correctly ( self ) : <EOL> "<STR_LIT>" <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . initial . ctime ( ) , <EOL> self . now . ctime ( ) ) <EOL> def test_submit_with_good_values ( self ) : <EOL> "<STR_LIT>" <EOL> posted_form = BasicTalkForm ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , collection = self . database . test_collection ) <EOL> self . assertTrue ( posted_form . is_valid ( ) ) <EOL> obj = posted_form . save ( ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , datetime . datetime ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> def test_submit_form_with_invalid_json ( self ) : <EOL> "<STR_LIT>" <EOL> posted_form = BasicTalkForm ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , collection = self . database . test_collection ) <EOL> self . assertEquals ( posted_form . is_valid ( ) , False ) <EOL> self . assertTrue ( posted_form . errors [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( posted_form . errors [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . startswith ( <EOL> u'<STR_LIT>' ) ) <EOL> def test_submit_empty_form ( self ) : <EOL> "<STR_LIT>" <EOL> posted_form = BasicTalkForm ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , collection = self . database . test_collection ) <EOL> self . assertEquals ( posted_form . is_valid ( ) , False ) <EOL> self . assertEquals ( posted_form . errors . keys ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEquals ( posted_form . errors . values ( ) , [ <EOL> [ u'<STR_LIT>' ] , <EOL> [ u'<STR_LIT>' ] , <EOL> [ u'<STR_LIT>' ] ] ) <EOL> class DetailedTalkForm ( DocumentForm ) : <EOL> """<STR_LIT>""" <EOL> tags = forms . CharField ( max_length = <NUM_LIT> , required = True ) <EOL> def clean_tags ( self ) : <EOL> value = self . cleaned_data [ '<STR_LIT>' ] <EOL> return [ tag . strip ( ) for tag in value . split ( '<STR_LIT:U+002C>' ) ] <EOL> def clean_when ( self ) : <EOL> w = self . cleaned_data [ '<STR_LIT>' ] <EOL> when = datetime . datetime ( w . year , w . month , w . day , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> return when <EOL> class Meta : <EOL> document = DetailedTalk <EOL> fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class CustomizedDocumentFormTest ( unittest . TestCase ) : <EOL> "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> from shortcut import connection <EOL> self . connection = connection <EOL> self . database = self . connection [ '<STR_LIT>' ] <EOL> self . form = DetailedTalkForm ( collection = self . database . test_collection ) <EOL> def tearDown ( self ) : <EOL> self . connection . drop_database ( '<STR_LIT>' ) <EOL> def test_all_fields_created ( self ) : <EOL> "<STR_LIT>" <EOL> self . assertEquals ( self . form . fields . keys ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEquals ( [ fld . __class__ for fld in self . form . fields . values ( ) ] , <EOL> [ forms . fields . CharField , forms . fields . DateTimeField , <EOL> forms . fields . CharField , forms . fields . FloatField ] ) <EOL> def test_required_set_correctly ( self ) : <EOL> "<STR_LIT>" <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . required , True ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . required , True ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . required , True ) <EOL> self . assertEquals ( self . form . fields [ '<STR_LIT>' ] . required , True ) <EOL> def test_submit_form_with_correct_values ( self ) : <EOL> "<STR_LIT>" <EOL> posted_form = DetailedTalkForm ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , collection = self . database . test_collection ) <EOL> self . assertTrue ( posted_form . is_valid ( ) ) <EOL> obj = posted_form . save ( ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , datetime . datetime ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEquals ( obj [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
