コード例 #1
0
ファイル: test_model.py プロジェクト: jpsca/eloquent
    def test_push_many_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)

        flexmock(Builder)
        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'))
コード例 #2
0
ファイル: test_morph_to_many.py プロジェクト: jpsca/eloquent
    def _get_relation_arguments(self):
        parent = flexmock(Model())
        parent.should_receive('get_morph_class').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_class').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
コード例 #3
0
    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()
コード例 #4
0
    def _get_relation_arguments(self):
        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('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').once().with_args(
            'user_role', 'roles.id', '=', 'user_role.role_id')
        builder.should_receive('where').once().with_args(
            'user_role.user_id', '=', 1)

        return builder, parent, 'user_role', 'user_id', 'role_id', 'relation_id'
コード例 #5
0
    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)
コード例 #6
0
ファイル: test_model.py プロジェクト: jpsca/eloquent
    def test_push_no_relations(self):
        flexmock(Builder)
        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)