def test_push_one_relation(self): flexmock(Builder) related1 = flexmock(Model()) query = flexmock(QueryBuilder(MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor())) builder = Builder(query) builder.get_query().should_receive('insert_get_id').once().with_args({'name': 'related1'}, 'id').and_return(2) related1.should_receive('new_query').once().and_return(builder) related1.should_receive('_update_timestamps').once() related1.name = 'related1' related1.set_exists(False) model = flexmock(Model()) model.should_receive('resolve_connection').and_return(MockConnection().prepare_mock()) query = flexmock(QueryBuilder(MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor())) builder = Builder(query) builder.get_query().should_receive('insert_get_id').once().with_args({'name': 'john'}, 'id').and_return(1) model.should_receive('new_query').once().and_return(builder) model.should_receive('_update_timestamps').once() model.name = 'john' model.set_exists(False) model.set_relation('relation_one', related1) self.assertTrue(model.push()) self.assertEqual(1, model.id) self.assertTrue(model.exists) self.assertEqual(2, model.relation_one.id) self.assertTrue(model.relation_one.exists) self.assertEqual(2, related1.id) self.assertTrue(related1.exists)
def test_insert_process(self): query = flexmock(Builder) model = OrmModelStub() query_builder = flexmock(QueryBuilder) query_builder.should_receive('insert_get_id').once().with_args({'name': 'john'}, 'id').and_return(1) model.new_query = mock.MagicMock(return_value=Builder(QueryBuilder(None, None, None))) model._update_timestamps = mock.MagicMock() events = flexmock(Event()) model.__dispatcher__ = events events.should_receive('fire').once()\ .with_args('saving: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('creating: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('created: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('saved: %s' % model.__class__.__name__, model)\ .and_return(True) model.name = 'john' model.set_exists(False) self.assertTrue(model.save()) self.assertEqual(1, model.id) self.assertTrue(model.exists) self.assertTrue(model._update_timestamps.called) model = OrmModelStub() query_builder.should_receive('insert').once().with_args({'name': 'john'}) model.new_query = mock.MagicMock(return_value=Builder(QueryBuilder(None, None, None))) model._update_timestamps = mock.MagicMock() model.set_incrementing(False) events = flexmock(Event()) model.__dispatcher__ = events events.should_receive('fire').once()\ .with_args('saving: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('creating: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('created: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('saved: %s' % model.__class__.__name__, model)\ .and_return(True) model.name = 'john' model.set_exists(False) self.assertTrue(model.save()) self.assertFalse(hasattr(model, 'id')) self.assertTrue(model.exists) self.assertTrue(model._update_timestamps.called)
def test_push_many_relation(self): related1 = flexmock(Model()) query = flexmock( QueryBuilder( MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor() ) ) builder = Builder(query) builder.get_query().should_receive("insert_get_id").once().with_args( {"name": "related1"}, "id" ).and_return(2) related1.should_receive("new_query").once().and_return(builder) related1.should_receive("_update_timestamps").once() related1.name = "related1" related1.set_exists(False) related2 = flexmock(Model()) query = flexmock( QueryBuilder( MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor() ) ) builder = Builder(query) builder.get_query().should_receive("insert_get_id").once().with_args( {"name": "related2"}, "id" ).and_return(3) related2.should_receive("new_query").once().and_return(builder) related2.should_receive("_update_timestamps").once() related2.name = "related2" related2.set_exists(False) model = flexmock(Model()) model.should_receive("resolve_connection").and_return( MockConnection().prepare_mock() ) query = flexmock( QueryBuilder( MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor() ) ) builder = Builder(query) builder.get_query().should_receive("insert_get_id").once().with_args( {"name": "john"}, "id" ).and_return(1) model.should_receive("new_query").once().and_return(builder) model.should_receive("_update_timestamps").once() model.name = "john" model.set_exists(False) model.set_relation("relation_many", Collection([related1, related2])) self.assertTrue(model.push()) self.assertEqual(1, model.id) self.assertTrue(model.exists) self.assertEqual(2, len(model.relation_many)) self.assertEqual([2, 3], model.relation_many.lists("id"))
def test_models_are_properly_pulled_and_matched(self): relation = self._get_relation() one = flexmock(Model()) one.morph_type = "morph_type_1" one.foreign_key = "foreign_key_1" two = flexmock(Model()) two.morph_type = "morph_type_1" two.foreign_key = "foreign_key_1" three = flexmock(Model()) three.morph_type = "morph_type_2" three.foreign_key = "foreign_key_2" relation.add_eager_constraints([one, two, three]) first_query = flexmock( Builder(flexmock(QueryBuilder(None, QueryGrammar(), None)))) second_query = flexmock( Builder(flexmock(QueryBuilder(None, QueryGrammar(), None)))) first_model = flexmock(Model()) second_model = flexmock(Model()) relation.should_receive("_create_model_by_type").once().with_args( "morph_type_1").and_return(first_model) relation.should_receive("_create_model_by_type").once().with_args( "morph_type_2").and_return(second_model) first_model.should_receive("get_key_name").and_return("id") second_model.should_receive("get_key_name").and_return("id") first_model.should_receive("new_query").once().and_return(first_query) second_model.should_receive("new_query").once().and_return( second_query) first_query.get_query().should_receive("where_in").once().with_args( "id", ["foreign_key_1"]).and_return(first_query) result_one = flexmock() first_query.should_receive("get").and_return( Collection.make([result_one])) result_one.should_receive("get_key").and_return("foreign_key_1") second_query.get_query().should_receive("where_in").once().with_args( "id", ["foreign_key_2"]).and_return(second_query) result_two = flexmock() second_query.should_receive("get").and_return( Collection.make([result_two])) result_two.should_receive("get_key").and_return("foreign_key_2") one.should_receive("set_relation").once().with_args( "relation", result_one) two.should_receive("set_relation").once().with_args( "relation", result_one) three.should_receive("set_relation").once().with_args( "relation", result_two) relation.get_eager()
def test_models_are_properly_pulled_and_matched(self): relation = self._get_relation() one = flexmock(Model()) one.morph_type = 'morph_type_1' one.foreign_key = 'foreign_key_1' two = flexmock(Model()) two.morph_type = 'morph_type_1' two.foreign_key = 'foreign_key_1' three = flexmock(Model()) three.morph_type = 'morph_type_2' three.foreign_key = 'foreign_key_2' relation.add_eager_constraints([one, two, three]) first_query = flexmock( Builder(flexmock(QueryBuilder(None, QueryGrammar(), None)))) second_query = flexmock( Builder(flexmock(QueryBuilder(None, QueryGrammar(), None)))) first_model = flexmock(Model()) second_model = flexmock(Model()) relation.should_receive('_create_model_by_type').once().with_args( 'morph_type_1').and_return(first_model) relation.should_receive('_create_model_by_type').once().with_args( 'morph_type_2').and_return(second_model) first_model.should_receive('get_key_name').and_return('id') second_model.should_receive('get_key_name').and_return('id') first_model.should_receive('new_query').once().and_return(first_query) second_model.should_receive('new_query').once().and_return( second_query) first_query.get_query().should_receive('where_in').once()\ .with_args('id', ['foreign_key_1']).and_return(first_query) result_one = flexmock() first_query.should_receive('get').and_return( Collection.make([result_one])) result_one.should_receive('get_key').and_return('foreign_key_1') second_query.get_query().should_receive('where_in').once()\ .with_args('id', ['foreign_key_2']).and_return(second_query) result_two = flexmock() second_query.should_receive('get').and_return( Collection.make([result_two])) result_two.should_receive('get_key').and_return('foreign_key_2') one.should_receive('set_relation').once().with_args( 'relation', result_one) two.should_receive('set_relation').once().with_args( 'relation', result_one) three.should_receive('set_relation').once().with_args( 'relation', result_two) relation.get_eager()
def test_relation_count_query_can_be_built(self): relation = self._get_relation() query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.get_query().should_receive('select').once() relation.get_parent().should_receive('get_table').and_return('table') builder.should_receive('where').once().with_args('table.foreign_key', '=', QueryExpression) parent_query = flexmock(QueryBuilder(None, None, None)) relation.get_query().should_receive('get_query').and_return(parent_query) grammar = flexmock() parent_query.should_receive('get_grammar').once().and_return(grammar) grammar.should_receive('wrap').once().with_args('table.id') relation.get_relation_count_query(builder, builder)
def _get_relation_arguments(self): flexmock(Model).should_receive("_boot_columns").and_return(["name"]) parent = flexmock(Model()) parent.should_receive("get_key").and_return(1) parent.should_receive("get_created_at_column").and_return("created_at") parent.should_receive("get_updated_at_column").and_return("updated_at") query = flexmock( QueryBuilder(MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor())) flexmock(Builder) builder = Builder(query) builder.should_receive("get_query").and_return(query) related = flexmock(Model()) builder.set_model(related) builder.should_receive("get_model").and_return(related) related.should_receive("new_query").and_return(builder) related.should_receive("get_key_name").and_return("id") related.should_receive("get_table").and_return("roles") related.should_receive("new_pivot").replace_with( lambda *args: Pivot(*args)) builder.get_query().should_receive("join").at_least().once().with_args( "user_role", "roles.id", "=", "user_role.role_id") builder.should_receive("where").at_least().once().with_args( "user_role.user_id", "=", 1) return builder, parent, "user_role", "user_id", "role_id", "relation_id"
def _get_relation(self): flexmock(Builder) query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.should_receive('where').with_args('table.foreign_key', '=', 1) related = flexmock(Model()) related_query = QueryBuilder(None, QueryGrammar(), None) related.should_receive('new_query').and_return(Builder(related_query)) builder.should_receive('get_model').and_return(related) parent = flexmock(Model()) parent.should_receive('get_attribute').with_args('id').and_return(1) parent.should_receive('get_created_at_column').and_return('created_at') parent.should_receive('get_updated_at_column').and_return('updated_at') parent.should_receive('new_query').and_return(builder) return HasOne(builder, parent, 'table.foreign_key', 'id')
def _get_relation(self): flexmock(Builder) query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.get_query().should_receive('join').at_least().once().with_args('users', 'users.id', '=', 'posts.user_id') builder.should_receive('where').with_args('users.country_id', '=', 1) country = flexmock(Model()) country.should_receive('get_key').and_return(1) country.should_receive('get_foreign_key').and_return('country_id') user = flexmock(Model()) user.should_receive('get_table').and_return('users') user.should_receive('get_qualified_key_name').and_return('users.id') post = flexmock(Model()) post.should_receive('get_table').and_return('posts') builder.should_receive('get_model').and_return(post) post.should_receive('new_query').and_return(builder) user.should_receive('get_key').and_return(1) user.should_receive('get_created_at_column').and_return('created_at') user.should_receive('get_updated_at_column').and_return('updated_at') parent = flexmock(Model()) parent.should_receive('get_attribute').with_args('id').and_return(1) parent.should_receive('get_created_at_column').and_return('created_at') parent.should_receive('get_updated_at_column').and_return('updated_at') parent.should_receive('new_query').and_return(builder) return HasManyThrough(builder, country, user, 'country_id', 'user_id')
def test_update_process_uses_old_primary_key(self): query = flexmock(Builder) query.should_receive("where").once().with_args("id", 1) query.should_receive("update").once().with_args({"id": 2, "name": "john"}) model = models.OrmModelStub() model.new_query = mock.MagicMock( return_value=Builder(QueryBuilder(None, None, None)) ) model._update_timestamps = mock.MagicMock() events = flexmock(Event()) model.__dispatcher__ = events events.should_receive("fire").once().with_args( "saving: %s" % model.__class__.__name__, model ).and_return(True) events.should_receive("fire").once().with_args( "updating: %s" % model.__class__.__name__, model ).and_return(True) events.should_receive("fire").once().with_args( "updated: %s" % model.__class__.__name__, model ).and_return(True) events.should_receive("fire").once().with_args( "saved: %s" % model.__class__.__name__, model ).and_return(True) model.id = 1 model.sync_original() model.id = 2 model.name = "john" model.set_exists(True) self.assertTrue(model.save()) model.new_query.assert_called_once_with() self.assertTrue(model._update_timestamps.called)
def test_update_process(self): query = flexmock(Builder) query.should_receive('where').once().with_args('id', 1) query.should_receive('update').once().with_args({'name': 'john'}) model = OrmModelStub() model.new_query = mock.MagicMock( return_value=Builder(QueryBuilder(None, None, None))) model._update_timestamps = mock.MagicMock() events = flexmock(Event()) model.__dispatcher__ = events events.should_receive('fire').once()\ .with_args('saving: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('updating: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('updated: %s' % model.__class__.__name__, model)\ .and_return(True) events.should_receive('fire').once()\ .with_args('saved: %s' % model.__class__.__name__, model)\ .and_return(True) model.id = 1 model.foo = 'bar' model.sync_original() model.name = 'john' model.set_exists(True) self.assertTrue(model.save()) model.new_query.assert_called_once_with() model._update_timestamps.assert_called_once_with()
def _get_relation_arguments(self): parent = flexmock(Model()) parent.should_receive('get_morph_name').and_return( parent.__class__.__name__) parent.should_receive('get_key').and_return(1) parent.should_receive('get_created_at_column').and_return('created_at') parent.should_receive('get_updated_at_column').and_return('updated_at') query = flexmock( QueryBuilder(MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor())) flexmock(Builder) builder = Builder(query) builder.should_receive('get_query').and_return(query) related = flexmock(Model()) builder.set_model(related) builder.should_receive('get_model').and_return(related) related.should_receive('get_key_name').and_return('id') related.should_receive('get_table').and_return('tags') related.should_receive('get_morph_name').and_return( parent.__class__.__name__) builder.get_query().should_receive('join').once().with_args( 'taggables', 'tags.id', '=', 'taggables.tag_id') builder.should_receive('where').once().with_args( 'taggables.taggable_id', '=', 1) builder.should_receive('where').once().with_args( 'taggables.taggable_type', parent.__class__.__name__) return builder, parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'relation_name', False
def _get_relation(self): flexmock(Builder) query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.should_receive("where").with_args("table.foreign_key", "=", 1) related = flexmock(Model()) related_query = QueryBuilder(None, QueryGrammar(), None) related.should_receive("new_query").and_return(Builder(related_query)) builder.should_receive("get_model").and_return(related) parent = flexmock(Model()) parent.should_receive("get_attribute").with_args("id").and_return(1) parent.should_receive("get_created_at_column").and_return("created_at") parent.should_receive("get_updated_at_column").and_return("updated_at") parent.should_receive("new_query").and_return(builder) return HasOne(builder, parent, "table.foreign_key", "id")
def test_relation_count_query_can_be_built(self): relation = self._get_relation() query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.get_query().should_receive("select").once() relation.get_parent().should_receive("get_table").and_return("table") builder.should_receive("where").once().with_args( "table.foreign_key", "=", QueryExpression) parent_query = flexmock(QueryBuilder(None, None, None)) relation.get_query().should_receive("get_query").and_return( parent_query) grammar = flexmock() parent_query.should_receive("get_grammar").once().and_return(grammar) grammar.should_receive("wrap").once().with_args("table.id") relation.get_relation_count_query(builder, builder)
def _get_relation(self): flexmock(Builder) query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.get_query().should_receive("join").at_least().once().with_args( "users", "users.id", "=", "posts.user_id") builder.should_receive("where").with_args("users.country_id", "=", 1) country = flexmock(Model()) country.should_receive("get_key").and_return(1) country.should_receive("get_foreign_key").and_return("country_id") user = flexmock(Model()) user.should_receive("get_table").and_return("users") user.should_receive("get_qualified_key_name").and_return("users.id") post = flexmock(Model()) post.should_receive("get_table").and_return("posts") builder.should_receive("get_model").and_return(post) post.should_receive("new_query").and_return(builder) user.should_receive("get_key").and_return(1) user.should_receive("get_created_at_column").and_return("created_at") user.should_receive("get_updated_at_column").and_return("updated_at") parent = flexmock(Model()) parent.should_receive("get_attribute").with_args("id").and_return(1) parent.should_receive("get_created_at_column").and_return("created_at") parent.should_receive("get_updated_at_column").and_return("updated_at") parent.should_receive("new_query").and_return(builder) return HasManyThrough(builder, country, user, "country_id", "user_id")
def test_timestamps_can_be_retrieved_properly(self): model1 = OrmBelongsToManyModelStub() model1.fill(name='john', pivot_user_id=1, pivot_role_id=2) model2 = OrmBelongsToManyModelStub() model2.fill(name='jane', pivot_user_id=3, pivot_role_id=4) models = [model1, model2] base_builder = flexmock( Builder( QueryBuilder(MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor()))) relation = self._get_relation().with_timestamps() relation.get_parent().should_receive('get_connection_name').and_return( 'foo.connection') relation.get_query().get_query().should_receive('add_select').once()\ .with_args( 'roles.*', 'user_role.user_id AS pivot_user_id', 'user_role.role_id AS pivot_role_id', 'user_role.created_at AS pivot_created_at', 'user_role.updated_at AS pivot_updated_at' )\ .and_return(relation.get_query()) relation.get_query().should_receive('get_models').once().and_return( models) relation.get_query().should_receive( 'eager_load_relations').once().with_args(models).and_return(models) relation.get_related().should_receive('new_collection').replace_with( lambda l: Collection(l)) relation.get_query().should_receive('get_query').once().and_return( base_builder) results = relation.get()
def _get_relation_arguments(self): flexmock(Model).should_receive('_boot_columns').and_return(['name']) parent = flexmock(Model()) parent.should_receive('get_key').and_return(1) parent.should_receive('get_created_at_column').and_return('created_at') parent.should_receive('get_updated_at_column').and_return('updated_at') query = flexmock( QueryBuilder(MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor())) flexmock(Builder) builder = Builder(query) builder.should_receive('get_query').and_return(query) related = flexmock(Model()) builder.set_model(related) builder.should_receive('get_model').and_return(related) related.should_receive('new_query').and_return(builder) related.should_receive('get_key_name').and_return('id') related.should_receive('get_table').and_return('roles') related.should_receive('new_pivot').replace_with( lambda *args: Pivot(*args)) builder.get_query().should_receive('join').at_least().once().with_args( 'user_role', 'roles.id', '=', 'user_role.role_id') builder.should_receive('where').at_least().once().with_args( 'user_role.user_id', '=', 1) return builder, parent, 'user_role', 'user_id', 'role_id', 'relation_id'
def new_query(self): mock = flexmock(Builder) mock_query = flexmock(QueryBuilder) mock_query.should_receive('use_write_connection').once().and_return(flexmock) mock.should_receive('find').once().with_args(1).and_return('foo') return Builder(QueryBuilder(None, None, None))
def new_query(self): mock = flexmock(Builder) model = flexmock() mock_query = flexmock(QueryBuilder) mock_query.should_receive('where_in').once().with_args('id', [1, 2, 3]).and_return(flexmock) mock.should_receive('get').once().and_return([model]) model.should_receive('delete').once() return Builder(QueryBuilder(None, None, None))
def new_query(self): mock = flexmock(Builder) model = flexmock() mock_query = flexmock(QueryBuilder) mock_query.should_receive("where_in").once().with_args( "id", [1, 2, 3]).and_return(flexmock) mock.should_receive("get").once().and_return([model]) model.should_receive("delete").once() return Builder(QueryBuilder(None, None, None))
def test_eager_load_relations_load_top_level_relationships(self): flexmock(Builder) builder = Builder(flexmock(QueryBuilder(None, None, None))) nop1 = lambda: None nop2 = lambda: None builder.set_eager_loads({'foo': nop1, 'foo.bar': nop2}) builder.should_receive('_load_relation').with_args(['models'], 'foo', nop1).and_return(['foo']) results = builder.eager_load_relations(['models']) self.assertEqual(['foo'], results)
def test_eager_load_relations_load_top_level_relationships(self): flexmock(Builder) builder = Builder(flexmock(QueryBuilder(None, None, None))) nop1 = lambda: None nop2 = lambda: None builder.set_eager_loads({"foo": nop1, "foo.bar": nop2}) builder.should_receive("_load_relation").with_args( ["models"], "foo", nop1).and_return(["foo"]) results = builder.eager_load_relations(["models"]) self.assertEqual(["foo"], results)
def _get_relation_associate(self, parent): flexmock(Builder) query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.should_receive('where').with_args('relation.id', '=', 'foreign.value') related = flexmock(Model()) related.should_receive('get_key').and_return(1) related.should_receive('get_table').and_return('relation') builder.should_receive('get_model').and_return(related) return MorphTo(builder, parent, 'foreign_key', 'id', 'morph_type', 'relation')
def test_models_are_properly_hydrated(self): model1 = OrmBelongsToManyModelStub() model1.fill(name="john", pivot_user_id=1, pivot_role_id=2) model2 = OrmBelongsToManyModelStub() model2.fill(name="jane", pivot_user_id=3, pivot_role_id=4) models = [model1, model2] base_builder = flexmock( Builder( QueryBuilder(MockConnection().prepare_mock(), QueryGrammar(), QueryProcessor()))) relation = self._get_relation() relation.get_parent().should_receive("get_connection_name").and_return( "foo.connection") relation.get_query().get_query().should_receive( "add_select").once().with_args(*[ "roles.*", "user_role.user_id AS pivot_user_id", "user_role.role_id AS pivot_role_id", ]).and_return(relation.get_query()) relation.get_query().should_receive("get_models").once().and_return( models) relation.get_query().should_receive( "eager_load_relations").once().with_args(models).and_return(models) relation.get_related().should_receive("new_collection").replace_with( lambda l: Collection(l)) relation.get_query().should_receive("get_query").once().and_return( base_builder) results = relation.get() self.assertIsInstance(results, Collection) # Make sure the foreign keys were set on the pivot models self.assertEqual("user_id", results[0].pivot.get_foreign_key()) self.assertEqual("role_id", results[0].pivot.get_other_key()) self.assertEqual("john", results[0].name) self.assertEqual(1, results[0].pivot.user_id) self.assertEqual(2, results[0].pivot.role_id) self.assertEqual("foo.connection", results[0].pivot.get_connection_name()) self.assertEqual("jane", results[1].name) self.assertEqual(3, results[1].pivot.user_id) self.assertEqual(4, results[1].pivot.role_id) self.assertEqual("foo.connection", results[1].pivot.get_connection_name()) self.assertEqual("user_role", results[0].pivot.get_table()) self.assertTrue(results[0].pivot.exists)
def test_delete_properly_deletes_model(self): model = models.OrmModelStub() builder = flexmock(Builder(QueryBuilder(None, None, None))) builder.should_receive("where").once().with_args("id", 1).and_return(builder) builder.should_receive("delete").once() model.new_query = mock.MagicMock(return_value=builder) model.touch_owners = mock.MagicMock() model.set_exists(True) model.id = 1 model.delete() self.assertTrue(model.touch_owners.called)
def _get_relation_associate(self, parent): flexmock(Builder) query = flexmock(QueryBuilder(None, QueryGrammar(), None)) builder = Builder(query) builder.should_receive("where").with_args("relation.id", "=", "foreign.value") related = flexmock(Model()) related.should_receive("get_key").and_return(1) related.should_receive("get_table").and_return("relation") builder.should_receive("get_model").and_return(related) return MorphTo(builder, parent, "foreign_key", "id", "morph_type", "relation")
def test_creating_with_only_updated_at_column(self): query_builder = flexmock(QueryBuilder) query_builder.should_receive('insert_get_id').once().with_args( { 'name': 'john' }, 'id').and_return(1) model = flexmock(OrmModelUpdatedAt()) model.should_receive('new_query').and_return( Builder(QueryBuilder(None, None, None))) model.should_receive('set_created_at').never() model.should_receive('set_updated_at').once() model.name = 'john' model.save()
def test_creating_with_only_updated_at_column(self): query_builder = flexmock(QueryBuilder) query_builder.should_receive("insert_get_id").once().with_args( {"name": "john"}, "id" ).and_return(1) model = flexmock(models.OrmModelUpdatedAt()) model.should_receive("new_query").and_return( Builder(QueryBuilder(None, None, None)) ) model.should_receive("set_created_at").never() model.should_receive("set_updated_at").once() model.name = "john" model.save()
def test_updating_with_only_updated_at_column(self): query = flexmock(Builder) query.should_receive('where').once().with_args('id', 1) query.should_receive('update').once().with_args({'name': 'john'}) model = flexmock(OrmModelUpdatedAt()) model.id = 1 model.sync_original() model.set_exists(True) model.should_receive('new_query').and_return( Builder(QueryBuilder(None, None, None))) model.should_receive('set_created_at').never() model.should_receive('set_updated_at').once() model.name = 'john' model.save()
def test_macros_are_called_on_builder(self): builder = Builder( QueryBuilder(flexmock(Connection), flexmock(QueryGrammar), flexmock(QueryProcessor))) def foo_bar(builder): builder.foobar = True return builder builder.macro("foo_bar", foo_bar) result = builder.foo_bar() self.assertEqual(result, builder) self.assertTrue(builder.foobar)