Beispiel #1
0
    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_pluck_with_model_found(self):
        builder = Builder(self.get_mock_query_builder())

        model = {'name': 'foo'}
        builder.first = mock.MagicMock(return_value=model)

        self.assertEqual('foo', builder.pluck('name'))

        builder.first.assert_called_once_with(
            ['name']
        )
Beispiel #3
0
    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)
Beispiel #4
0
    def test_push_no_relations(self):
        model = 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': '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)

        self.assertTrue(model.push())
        self.assertEqual(1, model.id)
        self.assertTrue(model.exists)
Beispiel #5
0
    def _get_relation(self, parent=None):
        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('new_query').and_return(builder)
        related.should_receive('get_key_name').and_return('id')
        related.should_receive('get_table').and_return('relation')
        builder.should_receive('get_model').and_return(related)
        if parent is None:
            parent = OrmBelongsToModelStub()
        parent.foreign_key = 'foreign.value'

        return BelongsTo(builder, parent, 'foreign_key', 'id', 'relation')
Beispiel #6
0
    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.should_receive('new_query').and_return(builder)
        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 HasMany(builder, parent, 'table.foreign_key', 'id')
    def test_first_or_fail_raises_model_not_found_exception(self):
        model = self.get_mock_model()

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.first = mock.MagicMock(return_value=None)

        self.assertRaises(
            ModelNotFound,
            builder.first_or_fail,
            ['column']
        )

        builder.first.assert_called_once_with(
            ['column']
        )
    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)
    def test_chunk(self):
        builder = Builder(self.get_mock_query_builder())
        results = [['foo1', 'foo2'], ['foo3'], []]
        builder.for_page = mock.MagicMock(return_value=builder)
        builder.get = mock.MagicMock(side_effect=results)

        i = 0
        for result in builder.chunk(2):
            self.assertEqual(result, results[i])

            i += 1

        builder.for_page.assert_has_calls([
            mock.call(1, 2),
            mock.call(2, 2),
            mock.call(3, 2)
        ])
    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 _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_find_method(self):
        builder = Builder(self.get_mock_query_builder())
        builder.set_model(self.get_mock_model())
        builder.get_query().where = mock.MagicMock()
        builder.first = mock.MagicMock(return_value='baz')

        result = builder.find('bar', ['column'])

        builder.get_query().where.assert_called_once_with(
            'foo_table.foo', '=', 'bar'
        )
        self.assertEqual('baz', result)
Beispiel #13
0
    def test_push_one_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)

        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_get_models_hydrates_models(self):
        builder = Builder(self.get_mock_query_builder())
        records = [{
            'name': 'john', 'age': 26
        }, {
            'name': 'jane', 'age': 28
        }]

        builder.get_query().get = mock.MagicMock(return_value=records)
        model = self.get_mock_model()
        builder.set_model(model)
        model.get_connection_name = mock.MagicMock(return_value='foo_connection')
        model.hydrate = mock.MagicMock(return_value=Collection(['hydrated']))
        models = builder.get_models(['foo'])

        self.assertEqual(models.all(), ['hydrated'])

        model.get_table.assert_called_once_with()
        model.get_connection_name.assert_called_once_with()
        model.hydrate.assert_called_once_with(
            records, 'foo_connection'
        )
    def test_find_or_new_model_found(self):
        model = self.get_mock_model()
        model.find_or_new = mock.MagicMock(return_value='baz')

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.get_query().where = mock.MagicMock()
        builder.first = mock.MagicMock(return_value='baz')

        expected = model.find_or_new('bar', ['column'])
        result = builder.find('bar', ['column'])

        builder.get_query().where.assert_called_once_with(
            'foo_table.foo', '=', 'bar'
        )
        self.assertEqual(expected, result)
Beispiel #16
0
    def _get_one_relation(self):
        flexmock(Builder)
        query = flexmock(QueryBuilder(None, QueryGrammar(), None))
        builder = Builder(query)
        builder.should_receive('where').with_args('table.morph_id', '=', 1)
        related = flexmock(Model())
        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_morph_name').and_return(parent.__class__.__name__)
        builder.should_receive('where').once().with_args('table.morph_type', parent.__class__.__name__)

        return MorphOne(builder, parent, 'table.morph_type', 'table.morph_id', 'id')
    def test_find_or_new_model_not_found(self):
        model = self.get_mock_model()
        model.find_or_new = mock.MagicMock(return_value=self.get_mock_model())

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.get_query().where = mock.MagicMock()
        builder.first = mock.MagicMock(return_value=None)

        result = model.find_or_new('bar', ['column'])
        find_result = builder.find('bar', ['column'])

        builder.get_query().where.assert_called_once_with(
            'foo_table.foo', '=', 'bar'
        )
        self.assertIsNone(find_result)
        self.assertIsInstance(result, Model)
    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_find_with_many(self):
        model = self.get_mock_model()

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.get_query().where_in = mock.MagicMock()
        builder.get = mock.MagicMock(return_value='baz')

        result = builder.find([1, 2], ['column'])
        self.assertEqual('baz', result)

        builder.get_query().where_in.assert_called_once_with(
            'foo_table.foo', [1, 2]
        )

        builder.get.assert_called_once_with(
            ['column']
        )
Beispiel #20
0
    def test_find_or_fail_with_many_raises_model_not_found_exception(self):
        model = self.get_mock_model()

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.get_query().where_in = mock.MagicMock()
        builder.get = mock.MagicMock(return_value=Collection([1]))

        self.assertRaises(
            ModelNotFound,
            builder.find_or_fail,
            [1, 2],
            ['column']
        )

        builder.get_query().where_in.assert_called_once_with(
            'foo_table.foo', [1, 2]
        )

        builder.get.assert_called_once_with(
            ['column']
        )
Beispiel #21
0
    def test_find_or_fail_raises_model_not_found_exception(self):
        model = self.get_mock_model()

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.get_query().where = mock.MagicMock()
        builder.first = mock.MagicMock(return_value=None)

        self.assertRaises(
            ModelNotFound,
            builder.find_or_fail,
            'bar',
            ['column']
        )

        builder.get_query().where.assert_called_once_with(
            'foo_table.foo', '=', 'bar'
        )

        builder.first.assert_called_once_with(
            ['column']
        )
Beispiel #22
0
    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.should_receive('new_query').and_return(builder)
        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 HasMany(builder, parent, 'table.foreign_key', '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.should_receive("new_query").and_return(builder)
        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 HasMany(builder, parent, "table.foreign_key", "id")
Beispiel #24
0
    def _get_relation(self, parent=None):
        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('new_query').and_return(builder)
        related.should_receive('get_key_name').and_return('id')
        related.should_receive('get_table').and_return('relation')
        builder.should_receive('get_model').and_return(related)
        if parent is None:
            parent = OrmBelongsToModelStub()
        parent.foreign_key = 'foreign.value'

        return BelongsTo(builder, parent, 'foreign_key', 'id', 'relation')
    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)
Beispiel #26
0
    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 test_update_process_without_timestamps(self):
        query = flexmock(Builder)
        query.should_receive('where').once().with_args('id', 1)
        query.should_receive('update').once().with_args({'name': 'john'})

        model = flexmock(OrmModelStub())
        model.__timestamps__ = False
        model.new_query = mock.MagicMock(return_value=Builder(QueryBuilder(None, None, None)))
        model._update_timestamps = mock.MagicMock()

        events = flexmock(Event())
        model.__dispatcher__ = events
        model.should_receive('_fire_model_event').and_return(True)

        model.id = 1
        model.sync_original()
        model.name = 'john'
        model.set_exists(True)
        self.assertTrue(model.save())

        model.new_query.assert_called_once_with()
        self.assertFalse(model._update_timestamps.called)
Beispiel #28
0
    def test_get_models_hydrates_models(self):
        builder = Builder(self.get_mock_query_builder())
        records = [{'name': 'john', 'age': 26}, {'name': 'jane', 'age': 28}]

        builder.get_query().get = mock.MagicMock(return_value=records)
        model = self.get_mock_model()
        builder.set_model(model)
        model.get_connection_name = mock.MagicMock(
            return_value='foo_connection')
        model.hydrate = mock.MagicMock(return_value=Collection(['hydrated']))
        models = builder.get_models(['foo'])

        self.assertEqual(models.all(), ['hydrated'])

        model.get_table.assert_called_once_with()
        model.get_connection_name.assert_called_once_with()
        model.hydrate.assert_called_once_with(records, 'foo_connection')
    def test_get_relation_properly_sets_nested_relationships(self):
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))
        model = flexmock(Model())
        relation = flexmock()
        model.set_relation('orders', relation)
        builder.set_model(model)
        relation_query = flexmock()
        relation.should_receive('get_query').and_return(relation_query)
        relation_query.should_receive('with_').once().with_args({'lines': None, 'lines.details': None})
        builder.set_eager_loads({
            'orders': None,
            'orders.lines': None,
            'orders.lines.details': None
        })

        relation = builder.get_relation('orders')
    def test_eager_load_accept_queries(self):
        model = OrmBuilderTestModelCloseRelated()
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))
        nop1 = OrmBuilderTestModelFarRelatedStub.where('id', 5)
        builder.set_eager_loads({'foo': nop1})
        relation = flexmock()
        relation.should_receive('add_eager_constraints').once().with_args(['models'])
        relation.should_receive('init_relation').once().with_args(['models'], 'foo').and_return(['models'])
        relation.should_receive('get_eager').once().and_return(['results'])
        relation.should_receive('match').once()\
            .with_args(['models'], ['results'], 'foo').and_return(['foo'])
        builder.should_receive('get_relation').once().with_args('foo').and_return(relation)
        relation.should_receive('merge_query').with_args(nop1).and_return(relation)

        results = builder.eager_load_relations(['models'])
        self.assertEqual(['foo'], results)
Beispiel #31
0
    def test_relationship_eager_load_process(self):
        proof = flexmock()
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))

        def callback(q):
            proof.foo = q

        builder.set_eager_loads({'orders': callback})
        relation = flexmock()
        relation.should_receive('add_eager_constraints').once().with_args(['models'])
        relation.should_receive('init_relation').once().with_args(['models'], 'orders').and_return(['models'])
        relation.should_receive('get_eager').once().and_return(['results'])
        relation.should_receive('get_query').once().and_return(relation)
        relation.should_receive('match').once()\
            .with_args(['models'], ['results'], 'orders').and_return(['models.matched'])
        builder.should_receive('get_relation').once().with_args('orders').and_return(relation)
        results = builder.eager_load_relations(['models'])

        self.assertEqual(['models.matched'], results)
        self.assertEqual(relation, proof.foo)
    def test_eager_load_accept_queries(self):
        model = OrmBuilderTestModelCloseRelated()
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))
        nop1 = OrmBuilderTestModelFarRelatedStub.where("id", 5)
        builder.set_eager_loads({"foo": nop1})
        relation = flexmock()
        relation.should_receive("add_eager_constraints").once().with_args(
            ["models"])
        relation.should_receive("init_relation").once().with_args(
            ["models"], "foo").and_return(["models"])
        relation.should_receive("get_eager").once().and_return(["results"])
        relation.should_receive("match").once().with_args(
            ["models"], ["results"], "foo").and_return(["foo"])
        builder.should_receive("get_relation").once().with_args(
            "foo").and_return(relation)
        relation.should_receive("merge_query").with_args(nop1).and_return(
            relation)

        results = builder.eager_load_relations(["models"])
        self.assertEqual(["foo"], results)
Beispiel #33
0
    def test_eager_load_accept_queries(self):
        model = OrmBuilderTestModelCloseRelated()
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))
        nop1 = OrmBuilderTestModelFarRelatedStub.where('id', 5)
        builder.set_eager_loads({'foo': nop1})
        relation = flexmock()
        relation.should_receive('add_eager_constraints').once().with_args(
            ['models'])
        relation.should_receive('init_relation').once().with_args(
            ['models'], 'foo').and_return(['models'])
        relation.should_receive('get_eager').once().and_return(['results'])
        relation.should_receive('match').once()\
            .with_args(['models'], ['results'], 'foo').and_return(['foo'])
        builder.should_receive('get_relation').once().with_args(
            'foo').and_return(relation)
        relation.should_receive('merge_query').with_args(nop1).and_return(
            relation)

        results = builder.eager_load_relations(['models'])
        self.assertEqual(['foo'], results)
    def test_first(self):
        model = self.get_mock_model()

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.take = mock.MagicMock(return_value=builder)
        builder.get = mock.MagicMock(return_value=Collection(['bar']))

        result = builder.first()
        self.assertEqual('bar', result)

        builder.take.assert_called_once_with(
            1
        )

        builder.get.assert_called_once_with(
            ['*']
        )
Beispiel #35
0
    def test_update_process_does_not_override_timestamps(self):
        query = flexmock(Builder)
        query.should_receive('where').once().with_args('id', 1)
        query.should_receive('update').once().with_args({
            'created_at': 'foo',
            'updated_at': 'bar'
        })

        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.sync_original()
        model.created_at = 'foo'
        model.updated_at = 'bar'
        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_does_not_override_timestamps(self):
        query = flexmock(Builder)
        query.should_receive("where").once().with_args("id", 1)
        query.should_receive("update").once().with_args(
            {"created_at": "foo", "updated_at": "bar"}
        )

        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.created_at = "foo"
        model.updated_at = "bar"
        model.set_exists(True)
        self.assertTrue(model.save())

        model.new_query.assert_called_once_with()
        self.assertTrue(model._update_timestamps.called)
Beispiel #37
0
    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 = 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_find_or_fail_with_many_raises_model_not_found_exception(self):
        model = self.get_mock_model()

        builder = Builder(self.get_mock_query_builder())
        builder.set_model(model)
        builder.get_query().where_in = mock.MagicMock()
        builder.get = mock.MagicMock(return_value=Collection([1]))

        self.assertRaises(
            ModelNotFound,
            builder.find_or_fail,
            [1, 2],
            ['column']
        )

        builder.get_query().where_in.assert_called_once_with(
            'foo_table.foo', [1, 2]
        )

        builder.get.assert_called_once_with(
            ['column']
        )
    def test_get_relation_properly_sets_nested_relationships(self):
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))
        model = flexmock(Model())
        relation = flexmock()
        model.set_relation("orders", relation)
        builder.set_model(model)
        relation_query = flexmock()
        relation.should_receive("get_query").and_return(relation_query)
        relation_query.should_receive("with_").once().with_args({
            "lines":
            None,
            "lines.details":
            None
        })
        builder.set_eager_loads({
            "orders": None,
            "orders.lines": None,
            "orders.lines.details": None
        })

        relation = builder.get_relation("orders")
Beispiel #40
0
    def test_get_relation_properly_sets_nested_relationships(self):
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))
        model = flexmock(Model())
        relation = flexmock()
        model.set_relation('orders', relation)
        builder.set_model(model)
        relation_query = flexmock()
        relation.should_receive('get_query').and_return(relation_query)
        relation_query.should_receive('with_').once().with_args({
            'lines':
            None,
            'lines.details':
            None
        })
        builder.set_eager_loads({
            'orders': None,
            'orders.lines': None,
            'orders.lines.details': None
        })

        relation = builder.get_relation('orders')
    def test_relationship_eager_load_process(self):
        proof = flexmock()
        flexmock(Builder)
        builder = Builder(flexmock(QueryBuilder(None, None, None)))

        def callback(q):
            proof.foo = q

        builder.set_eager_loads({"orders": callback})
        relation = flexmock()
        relation.should_receive("add_eager_constraints").once().with_args(
            ["models"])
        relation.should_receive("init_relation").once().with_args(
            ["models"], "orders").and_return(["models"])
        relation.should_receive("get_eager").once().and_return(["results"])
        relation.should_receive("get_query").once().and_return(relation)
        relation.should_receive("match").once().with_args(
            ["models"], ["results"], "orders").and_return(["models.matched"])
        builder.should_receive("get_relation").once().with_args(
            "orders").and_return(relation)
        results = builder.eager_load_relations(["models"])

        self.assertEqual(["models.matched"], results)
        self.assertEqual(relation, proof.foo)
    def test_get_models_hydrates_models(self):
        builder = Builder(self.get_mock_query_builder())
        records = Collection([{
            "name": "john",
            "age": 26
        }, {
            "name": "jane",
            "age": 28
        }])

        builder.get_query().get = mock.MagicMock(return_value=records)
        model = self.get_mock_model()
        builder.set_model(model)
        model.get_connection_name = mock.MagicMock(
            return_value="foo_connection")
        model.hydrate = mock.MagicMock(return_value=Collection(["hydrated"]))
        models = builder.get_models(["foo"])

        self.assertEqual(models.all(), ["hydrated"])

        model.get_table.assert_called_once_with()
        model.get_connection_name.assert_called_once_with()
        model.hydrate.assert_called_once_with(records, "foo_connection")
Beispiel #43
0
    def new_query(self):
        mock = flexmock(Builder)
        mock.should_receive('find').once().with_args([1, 2],
                                                     ['*']).and_return('foo')

        return Builder(QueryBuilder(None, None, None))
Beispiel #44
0
    def new_query(self):
        flexmock(Builder).should_receive('find').once().with_args(
            1, ['*']).and_return('foo')

        return Builder(None)
Beispiel #45
0
    def new_query(self):
        mock = flexmock(Builder(None))
        mock.should_receive('with_').once().with_args('foo',
                                                      'bar').and_return('foo')

        return mock
Beispiel #46
0
    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_builder(self):
     return Builder(self.get_mock_query_builder())
    def test_get_does_not_eager_relations_when_no_results_are_returned(self):
        flexmock(Builder)
        builder = Builder(self.get_mock_query_builder())
        builder.should_receive('get_models').with_args(['foo']).and_return(['bar'])
        builder.should_receive('eager_load_relations').with_args(['bar']).and_return([])
        builder.set_model(self.get_mock_model())
        builder.get_model().new_collection = mock.MagicMock(return_value=Collection([]))

        results = builder.get(['foo'])
        self.assertEqual([], results.all())

        builder.get_model().new_collection.assert_called_with([])
    def test_pluck_with_model_not_found(self):
        builder = Builder(self.get_mock_query_builder())

        builder.first = mock.MagicMock(return_value=None)

        self.assertIsNone(builder.pluck("name"))
Beispiel #50
0
    def test_get_loads_models_and_hydrates_eager_relations(self):
        flexmock(Builder)
        builder = Builder(self.get_mock_query_builder())
        builder.should_receive('get_models').with_args(['foo']).and_return(['bar'])
        builder.should_receive('eager_load_relations').with_args(['bar']).and_return(['bar', 'baz'])
        builder.set_model(self.get_mock_model())
        builder.get_model().new_collection = mock.MagicMock(return_value=Collection(['bar', 'baz']))

        results = builder.get(['foo'])
        self.assertEqual(['bar', 'baz'], results.all())

        builder.get_model().new_collection.assert_called_with(['bar', 'baz'])
    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'
Beispiel #52
0
    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 _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 test_get_does_not_eager_relations_when_no_results_are_returned(self):
        flexmock(Builder)
        builder = Builder(self.get_mock_query_builder())
        builder.should_receive("get_models").with_args(["foo"
                                                        ]).and_return(["bar"])
        builder.should_receive("eager_load_relations").with_args(
            ["bar"]).and_return([])
        builder.set_model(self.get_mock_model())
        builder.get_model().new_collection = mock.MagicMock(
            return_value=Collection([]))

        results = builder.get(["foo"])
        self.assertEqual([], results.all())

        builder.get_model().new_collection.assert_called_with([])
    def test_pluck_with_model_not_found(self):
        builder = Builder(self.get_mock_query_builder())

        builder.first = mock.MagicMock(return_value=None)

        self.assertIsNone(builder.pluck('name'))
Beispiel #56
0
    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 _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'