<s> import arrow <EOL> from flexmock import flexmock , flexmock_teardown <EOL> from ... import OratorTestCase <EOL> from orator . query . builder import QueryBuilder <EOL> from orator . query . grammars import QueryGrammar <EOL> from orator . query . expression import QueryExpression <EOL> from orator . orm . builder import Builder <EOL> from orator . orm . model import Model <EOL> from orator . orm . relations import MorphOne , MorphMany <EOL> from orator . orm . collection import Collection <EOL> class OrmMorphTestCase ( OratorTestCase ) : <EOL> def tearDown ( self ) : <EOL> flexmock_teardown ( ) <EOL> def test_morph_one_sets_proper_constraints ( self ) : <EOL> self . _get_one_relation ( ) <EOL> def test_morph_one_eager_constraints_are_properly_added ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> relation . get_query ( ) . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , relation . get_parent ( ) . __class__ . __name__ ) <EOL> model1 = Model ( ) <EOL> model1 . id = <NUM_LIT:1> <EOL> model2 = Model ( ) <EOL> model2 . id = <NUM_LIT:2> <EOL> relation . add_eager_constraints ( [ model1 , model2 ] ) <EOL> def test_morph_many_sets_proper_constraints ( self ) : <EOL> self . _get_many_relation ( ) <EOL> def test_morph_many_eager_constraints_are_properly_added ( self ) : <EOL> relation = self . _get_many_relation ( ) <EOL> relation . get_query ( ) . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , relation . get_parent ( ) . __class__ . __name__ ) <EOL> model1 = Model ( ) <EOL> model1 . id = <NUM_LIT:1> <EOL> model2 = Model ( ) <EOL> model2 . id = <NUM_LIT:2> <EOL> relation . add_eager_constraints ( [ model1 , model2 ] ) <EOL> def test_create ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> created = flexmock ( Model ( ) ) <EOL> created . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> created . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , relation . get_parent ( ) . __class__ . __name__ ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:name>' : '<STR_LIT>' } ) . and_return ( created ) <EOL> created . should_receive ( '<STR_LIT>' ) . once ( ) . and_return ( True ) <EOL> self . assertEqual ( created , relation . create ( name = '<STR_LIT>' ) ) <EOL> def test_find_or_new_finds_model ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT:foo>' , [ '<STR_LIT:*>' ] ) . and_return ( model ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . find_or_new ( '<STR_LIT:foo>' ) . foo ) <EOL> def test_find_or_new_returns_new_model_with_morph_keys_set ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT:foo>' , [ '<STR_LIT:*>' ] ) . and_return ( None ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( model ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , relation . get_parent ( ) . __class__ . __name__ ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . find_or_new ( '<STR_LIT:foo>' ) . foo ) <EOL> def test_first_or_new_returns_first_model ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . and_return ( relation . get_query ( ) ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( model ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . first_or_new ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . foo ) <EOL> def test_first_or_new_returns_new_model_with_morph_keys_set ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . and_return ( relation . get_query ( ) ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( None ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( model ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , relation . get_parent ( ) . __class__ . __name__ ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . first_or_new ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . foo ) <EOL> def test_first_or_create_returns_first_model ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . and_return ( relation . get_query ( ) ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( model ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . first_or_create ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . foo ) <EOL> def test_first_or_create_creates_new_morph_model ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . and_return ( relation . get_query ( ) ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( None ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . and_return ( model ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , relation . get_parent ( ) . __class__ . __name__ ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . and_return ( ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . first_or_create ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . foo ) <EOL> def test_update_or_create_finds_first_model_and_updates ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . and_return ( relation . get_query ( ) ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( model ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . never ( ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:bar>' : '<STR_LIT>' } ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . and_return ( True ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . update_or_create ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , { '<STR_LIT:bar>' : '<STR_LIT>' } ) . foo ) <EOL> def test_update_or_create_finds_creates_new_morph_model ( self ) : <EOL> relation = self . _get_one_relation ( ) <EOL> model = flexmock ( ) <EOL> model . foo = '<STR_LIT:bar>' <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) . and_return ( relation . get_query ( ) ) <EOL> relation . get_query ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( None ) <EOL> relation . get_related ( ) . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( ) . and_return ( model ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , relation . get_parent ( ) . __class__ . __name__ ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( { '<STR_LIT:bar>' : '<STR_LIT>' } ) <EOL> model . should_receive ( '<STR_LIT>' ) . once ( ) . and_return ( True ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , relation . update_or_create ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , { '<STR_LIT:bar>' : '<STR_LIT>' } ) . foo ) <EOL> def _get_many_relation ( self ) : <EOL> flexmock ( Builder ) <EOL> query = flexmock ( QueryBuilder ( None , QueryGrammar ( ) , None ) ) <EOL> builder = Builder ( query ) <EOL> builder . should_receive ( '<STR_LIT>' ) . with_args ( '<STR_LIT>' , '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> related = flexmock ( Model ( ) ) <EOL> builder . should_receive ( '<STR_LIT>' ) . and_return ( related ) <EOL> parent = flexmock ( Model ( ) ) <EOL> parent . should_receive ( '<STR_LIT>' ) . with_args ( '<STR_LIT:id>' ) . and_return ( <NUM_LIT:1> ) <EOL> parent . should_receive ( '<STR_LIT>' ) . and_return ( parent . __class__ . __name__ ) <EOL> builder . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , parent . __class__ . __name__ ) <EOL> return MorphMany ( builder , parent , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> def _get_one_relation ( self ) : <EOL> flexmock ( Builder ) <EOL> query = flexmock ( QueryBuilder ( None , QueryGrammar ( ) , None ) ) <EOL> builder = Builder ( query ) <EOL> builder . should_receive ( '<STR_LIT>' ) . with_args ( '<STR_LIT>' , '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> related = flexmock ( Model ( ) ) <EOL> builder . should_receive ( '<STR_LIT>' ) . and_return ( related ) <EOL> parent = flexmock ( Model ( ) ) <EOL> parent . should_receive ( '<STR_LIT>' ) . with_args ( '<STR_LIT:id>' ) . and_return ( <NUM_LIT:1> ) <EOL> parent . should_receive ( '<STR_LIT>' ) . and_return ( parent . __class__ . __name__ ) <EOL> builder . should_receive ( '<STR_LIT>' ) . once ( ) . with_args ( '<STR_LIT>' , parent . __class__ . __name__ ) <EOL> return MorphOne ( builder , parent , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' ) </s>
