Esempio n. 1
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_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 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 _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")
Esempio n. 5
0
    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'
Esempio n. 6
0
    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')
Esempio n. 7
0
    def test_timestamps_are_returned_as_objects_from_timestamps_and_datetime(self):
        model = Model()
        model.set_raw_attributes(
            {"created_at": datetime.datetime.utcnow(), "updated_at": time.time()}
        )

        self.assertIsInstance(model.created_at, Pendulum)
        self.assertIsInstance(model.updated_at, Pendulum)
Esempio n. 8
0
    def test_timestamps_are_returned_as_objects(self):
        model = Model()
        model.set_raw_attributes(
            {"created_at": "2015-03-24", "updated_at": "2015-03-24"}
        )

        self.assertIsInstance(model.created_at, Pendulum)
        self.assertIsInstance(model.updated_at, Pendulum)
Esempio n. 9
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 test_timestamps_are_returned_as_objects_from_timestamps_and_datetime(self):
        model = Model()
        model.set_raw_attributes({
            'created_at': datetime.datetime.utcnow(),
            'updated_at': time.time()
        })

        self.assertIsInstance(model.created_at, Arrow)
        self.assertIsInstance(model.updated_at, Arrow)
Esempio n. 11
0
    def test_timestamps_are_returned_as_objects_from_timestamps_and_datetime(self):
        model = Model()
        model.set_raw_attributes({
            'created_at': datetime.datetime.utcnow(),
            'updated_at': time.time()
        })

        self.assertIsInstance(model.created_at, Pendulum)
        self.assertIsInstance(model.updated_at, Pendulum)
    def test_timestamps_are_returned_as_objects(self):
        model = Model()
        model.set_raw_attributes({
            'created_at': '2015-03-24',
            'updated_at': '2015-03-24'
        })

        self.assertIsInstance(model.created_at, Arrow)
        self.assertIsInstance(model.updated_at, Arrow)
Esempio n. 13
0
    def test_timestamps_are_returned_as_objects(self):
        model = Model()
        model.set_raw_attributes({
            'created_at': '2015-03-24',
            'updated_at': '2015-03-24'
        })

        self.assertIsInstance(model.created_at, Pendulum)
        self.assertIsInstance(model.updated_at, Pendulum)
Esempio n. 14
0
    def test_to_dict_includes_default_formatted_timestamps(self):
        model = Model()
        model.set_raw_attributes(
            {"created_at": "2015-03-24", "updated_at": "2015-03-25"}
        )

        d = model.to_dict()

        self.assertEqual("2015-03-24T00:00:00+00:00", d["created_at"])
        self.assertEqual("2015-03-25T00:00:00+00:00", d["updated_at"])
Esempio n. 15
0
    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()
Esempio n. 16
0
    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()
Esempio n. 17
0
    def test_to_dict_includes_default_formatted_timestamps(self):
        model = Model()
        model.set_raw_attributes({
            'created_at': '2015-03-24',
            'updated_at': '2015-03-25'
        })

        d = model.to_dict()

        self.assertEqual('2015-03-24T00:00:00+00:00', d['created_at'])
        self.assertEqual('2015-03-25T00:00:00+00:00', d['updated_at'])
Esempio n. 18
0
    def test_morph_many_eager_constraints_are_properly_added(self):
        relation = self._get_many_relation()
        relation.get_query().get_query().should_receive('where_in').once().with_args('table.morph_id', [1, 2])
        relation.get_query().should_receive('where').once()\
            .with_args('table.morph_type', relation.get_parent().__class__.__name__)

        model1 = Model()
        model1.id = 1
        model2 = Model()
        model2.id = 2
        relation.add_eager_constraints([model1, model2])
Esempio n. 19
0
    def test_to_dict_includes_default_formatted_timestamps(self):
        model = Model()
        model.set_raw_attributes({
            'created_at': '2015-03-24',
            'updated_at': '2015-03-25'
        })

        d = model.to_dict()

        self.assertEqual('2015-03-24T00:00:00+00:00', d['created_at'])
        self.assertEqual('2015-03-25T00:00:00+00:00', d['updated_at'])
Esempio n. 20
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")
Esempio n. 21
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')
Esempio n. 22
0
    def test_associate_sets_foreign_key_and_type_on_model(self):
        parent = flexmock(Model())
        parent.should_receive('get_attribute').once().with_args('foreign_key').and_return('foreign.value')

        relation = self._get_relation_associate(parent)

        associate = flexmock(Model())
        associate.should_receive('get_key').once().and_return(1)
        associate.should_receive('get_morph_name').once().and_return('Model')

        parent.should_receive('set_attribute').once().with_args('foreign_key', 1)
        parent.should_receive('set_attribute').once().with_args('morph_type', 'Model')
        parent.should_receive('set_relation').once().with_args('relation', associate)

        relation.associate(associate)
Esempio n. 23
0
    def test_relation_is_properly_initialized(self):
        relation = self._get_relation()
        model = flexmock(Model())
        model.should_receive('set_relation').once().with_args('foo', None)
        models = relation.init_relation([model], 'foo')

        self.assertEqual([model], models)
Esempio n. 24
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')
Esempio n. 25
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")
Esempio n. 26
0
    def test_relation_is_properly_initialized(self):
        relation = self._get_relation()
        model = flexmock(Model())
        model.should_receive("set_relation").once().with_args(
            "foo", Collection)
        models = relation.init_relation([model], "foo")

        self.assertEqual([model], models)
Esempio n. 27
0
    def test_update_retrieve_model_and_updates(self):
        relation = self._get_relation()
        mock = flexmock(Model())
        mock.should_receive('fill').once().with_args({'foo': 'bar'}).and_return(mock)
        mock.should_receive('save').once().and_return(True)
        relation.get_query().should_receive('first').once().and_return(mock)

        self.assertTrue(relation.update({'foo': 'bar'}))
Esempio n. 28
0
    def test_relation_is_properly_initialized(self):
        relation = self._get_relation()
        model = flexmock(Model())
        relation.get_related().should_receive('new_collection').replace_with(lambda l=None: Collection(l or []))
        model.should_receive('set_relation').once().with_args('foo', Collection)
        models = relation.init_relation([model], 'foo')

        self.assertEqual([model], models)
Esempio n. 29
0
    def test_touch_method_updates_related_timestamps(self):
        builder = flexmock(Builder, get_model=None, where=None)
        parent = Model()
        parent = flexmock(parent)
        parent.should_receive('get_attribute').with_args('id').and_return(1)
        related = Model()
        related = flexmock(related)
        builder.should_receive('get_model').and_return(related)
        builder.should_receive('where')
        relation = HasOne(Builder(QueryBuilder(None, None, None)), parent, 'foreign_key', 'id')
        related.should_receive('get_table').and_return('table')
        related.should_receive('get_updated_at_column').and_return('updated_at')
        now = arrow.get()
        related.should_receive('fresh_timestamp').and_return(now)
        builder.should_receive('update').once().with_args({'updated_at': now})

        relation.touch()
Esempio n. 30
0
    def test_save_method_set_foreign_key_on_model(self):
        relation = self._get_relation()
        mock_model = flexmock(Model(), save=lambda: True)
        mock_model.should_receive('save').once().and_return(True)
        result = relation.save(mock_model)

        attributes = result.get_attributes()
        self.assertEqual(1, attributes['foreign_key'])
Esempio n. 31
0
    def test_create_properly_creates_new_model(self):
        relation = self._get_relation()
        created = flexmock(Model(), save=lambda: True, set_attribute=lambda: None)
        created.should_receive('save').once().and_return(True)
        relation.get_related().should_receive('new_instance').once().with_args({'name': 'john'}).and_return(created)
        created.should_receive('set_attribute').with_args('foreign_key', 1)

        self.assertEqual(created, relation.create(name='john'))
Esempio n. 32
0
    def test_associate_sets_foreign_key_and_type_on_model(self):
        parent = flexmock(Model())
        parent.should_receive("get_attribute").once().with_args(
            "foreign_key").and_return("foreign.value")

        relation = self._get_relation_associate(parent)

        associate = flexmock(Model())
        associate.should_receive("get_key").once().and_return(1)
        associate.should_receive("get_morph_name").once().and_return("Model")

        parent.should_receive("set_attribute").once().with_args(
            "foreign_key", 1)
        parent.should_receive("set_attribute").once().with_args(
            "morph_type", "Model")
        parent.should_receive("set_relation").once().with_args(
            "relation", associate)

        relation.associate(associate)
Esempio n. 33
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,
        )
Esempio n. 34
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')
Esempio n. 35
0
    def test_associate_sets_foreign_key_on_model(self):
        parent = Model()
        parent.foreign_key = 'foreign.value'
        parent.get_attribute = mock.MagicMock(return_value='foreign.value')
        parent.set_attribute = mock.MagicMock()
        parent.set_relation = mock.MagicMock()
        relation = self._get_relation(parent)
        associate = flexmock(Model())
        associate.should_receive('get_attribute').once().with_args('id').and_return(1)

        relation.associate(associate)

        parent.get_attribute.assert_has_calls([
            mock.call('foreign_key'),
            mock.call('foreign_key')
        ])
        parent.set_attribute.assert_has_calls([
            mock.call('foreign_key', 1)
        ])
        parent.set_relation.assert_called_once_with('relation', associate)
Esempio n. 36
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")
Esempio n. 37
0
    def test_timestamps_return_none_if_set_to_none(self):
        model = Model()
        model.unguard()

        timestamps = {
            'created_at': datetime.datetime.now(),
            'updated_at': datetime.datetime.now()
        }

        instance = model.new_instance(timestamps)
        instance.created_at = None

        self.assertIsNone(instance.created_at)

        model.reguard()
Esempio n. 38
0
    def test_timestamps_are_returned_as_objects_on_create(self):
        model = Model()
        model.unguard()

        timestamps = {
            'created_at': datetime.datetime.now(),
            'updated_at': datetime.datetime.now()
        }

        instance = model.new_instance(timestamps)

        self.assertIsInstance(instance.created_at, Pendulum)
        self.assertIsInstance(instance.updated_at, Pendulum)

        model.reguard()