Esempio n. 1
0
    def test_insert_identity(self):
        Thing = self.classes.Thing

        s = fixture_session()

        t1, t2 = (Thing(foo=5), Thing(foo=10))

        s.add_all([t1, t2])

        with assert_engine(testing.db) as asserter:
            s.flush()
            eq_(t1.id, 1)
            eq_(t2.id, 2)

        asserter.assert_(
            Conditional(
                testing.db.dialect.implicit_returning,
                [
                    Conditional(
                        testing.db.dialect.insert_executemany_returning,
                        [
                            CompiledSQL(
                                "INSERT INTO test (foo) VALUES (%(foo)s) "
                                "RETURNING test.id",
                                [{"foo": 5}, {"foo": 10}],
                                dialect="postgresql",
                            ),
                        ],
                        [
                            CompiledSQL(
                                "INSERT INTO test (foo) VALUES (%(foo)s) "
                                "RETURNING test.id",
                                [{"foo": 5}],
                                dialect="postgresql",
                            ),
                            CompiledSQL(
                                "INSERT INTO test (foo) VALUES (%(foo)s) "
                                "RETURNING test.id",
                                [{"foo": 10}],
                                dialect="postgresql",
                            ),
                        ],
                    )
                ],
                [
                    CompiledSQL(
                        "INSERT INTO test (foo) VALUES (:foo)",
                        [{"foo": 5}],
                    ),
                    CompiledSQL(
                        "INSERT INTO test (foo) VALUES (:foo)",
                        [{"foo": 10}],
                    ),
                ],
            )
        )
Esempio n. 2
0
    def test_update_computed(self, eager):
        if eager:
            Thing = self.classes.Thing
        else:
            Thing = self.classes.ThingNoEager

        s = Session()

        t1, t2 = (Thing(id=1, foo=1), Thing(id=2, foo=2))

        s.add_all([t1, t2])
        s.flush()

        t1.foo = 5
        t2.foo = 6

        with assert_engine(testing.db) as asserter:
            s.flush()
            eq_(t1.bar, 5 + 42)
            eq_(t2.bar, 6 + 42)

        if eager and testing.db.dialect.implicit_returning:
            asserter.assert_(
                CompiledSQL(
                    "UPDATE test SET foo=%(foo)s "
                    "WHERE test.id = %(test_id)s "
                    "RETURNING test.bar",
                    [{
                        "foo": 5,
                        "test_id": 1
                    }],
                    dialect="postgresql",
                ),
                CompiledSQL(
                    "UPDATE test SET foo=%(foo)s "
                    "WHERE test.id = %(test_id)s "
                    "RETURNING test.bar",
                    [{
                        "foo": 6,
                        "test_id": 2
                    }],
                    dialect="postgresql",
                ),
            )
        elif eager:
            asserter.assert_(
                CompiledSQL(
                    "UPDATE test SET foo=:foo WHERE test.id = :test_id",
                    [{
                        "foo": 5,
                        "test_id": 1
                    }],
                ),
                CompiledSQL(
                    "UPDATE test SET foo=:foo WHERE test.id = :test_id",
                    [{
                        "foo": 6,
                        "test_id": 2
                    }],
                ),
                CompiledSQL(
                    "SELECT test.bar AS test_bar FROM test "
                    "WHERE test.id = :param_1",
                    [{
                        "param_1": 1
                    }],
                ),
                CompiledSQL(
                    "SELECT test.bar AS test_bar FROM test "
                    "WHERE test.id = :param_1",
                    [{
                        "param_1": 2
                    }],
                ),
            )
        else:
            asserter.assert_(
                CompiledSQL(
                    "UPDATE test SET foo=:foo WHERE test.id = :test_id",
                    [{
                        "foo": 5,
                        "test_id": 1
                    }, {
                        "foo": 6,
                        "test_id": 2
                    }],
                ),
                CompiledSQL(
                    "SELECT test.bar AS test_bar FROM test "
                    "WHERE test.id = :param_1",
                    [{
                        "param_1": 1
                    }],
                ),
                CompiledSQL(
                    "SELECT test.bar AS test_bar FROM test "
                    "WHERE test.id = :param_1",
                    [{
                        "param_1": 2
                    }],
                ),
            )
Esempio n. 3
0
    def test_insert_computed(self, eager):
        if eager:
            Thing = self.classes.Thing
        else:
            Thing = self.classes.ThingNoEager

        s = Session()

        t1, t2 = (Thing(id=1, foo=5), Thing(id=2, foo=10))

        s.add_all([t1, t2])

        with assert_engine(testing.db) as asserter:
            s.flush()
            eq_(t1.bar, 5 + 42)
            eq_(t2.bar, 10 + 42)

        if eager and testing.db.dialect.implicit_returning:
            asserter.assert_(
                CompiledSQL(
                    "INSERT INTO test (id, foo) VALUES (%(id)s, %(foo)s) "
                    "RETURNING test.bar",
                    [{
                        "foo": 5,
                        "id": 1
                    }],
                    dialect="postgresql",
                ),
                CompiledSQL(
                    "INSERT INTO test (id, foo) VALUES (%(id)s, %(foo)s) "
                    "RETURNING test.bar",
                    [{
                        "foo": 10,
                        "id": 2
                    }],
                    dialect="postgresql",
                ),
            )
        else:
            asserter.assert_(
                CompiledSQL(
                    "INSERT INTO test (id, foo) VALUES (:id, :foo)",
                    [{
                        "foo": 5,
                        "id": 1
                    }, {
                        "foo": 10,
                        "id": 2
                    }],
                ),
                CompiledSQL(
                    "SELECT test.bar AS test_bar FROM test "
                    "WHERE test.id = :param_1",
                    [{
                        "param_1": 1
                    }],
                ),
                CompiledSQL(
                    "SELECT test.bar AS test_bar FROM test "
                    "WHERE test.id = :param_1",
                    [{
                        "param_1": 2
                    }],
                ),
            )
Esempio n. 4
0
    def test_subclass_loadattr(self, first_option, second_option,
                               second_argument, expect_load):
        Parent, Entity, SubEntity = self.classes("Parent", "Entity",
                                                 "SubEntity")

        stmt = select(Parent)

        will_lazyload = first_option in (defaultload, lazyload)

        if second_argument == "name":
            second_argument = SubEntity.name
            opt = first_option(Parent.entity.of_type(SubEntity))
        elif second_argument == "id":
            opt = first_option(Parent.entity)
            second_argument = Entity.id
        else:
            opt = first_option(Parent.entity)

        if second_option is None:
            sub_opt = opt
        elif second_option == "raise":
            sub_opt = opt.defer(second_argument, raiseload=True)
        else:
            sub_opt = getattr(opt, second_option)(second_argument)

        stmt = stmt.options(sub_opt)

        session = fixture_session()
        result = session.execute(stmt).scalars()

        parent_obj = result.first()

        entity_id = parent_obj.__dict__["entity_id"]

        with assertsql.assert_engine(testing.db) as asserter_:
            if expect_load:
                eq_(parent_obj.entity.name, "some name")
            else:
                with expect_raises_message(
                        exc.InvalidRequestError,
                        "'SubEntity.name' is not available due to raiseload=True",
                ):
                    parent_obj.entity.name

        expected = []

        if will_lazyload:
            expected.append(
                CompiledSQL(
                    "SELECT entity.id AS entity_id, "
                    "entity.type AS entity_type FROM entity "
                    "WHERE entity.id = :pk_1",
                    [{
                        "pk_1": entity_id
                    }],
                ))

        if second_option in ("load_only", None) or (
                second_option == "undefer"
                and first_option in (defaultload, lazyload)):
            # load will be a mapper optimized load for the name alone
            expected.append(
                CompiledSQL(
                    "SELECT sub_entity.name AS sub_entity_name "
                    "FROM sub_entity "
                    "WHERE :param_1 = sub_entity.id",
                    [{
                        "param_1": entity_id
                    }],
                ))
        elif second_option == "defer":
            # load will be a deferred load.  this is because the explicit
            # call to the deferred load put a deferred loader on the attribute
            expected.append(
                CompiledSQL(
                    "SELECT sub_entity.name AS sub_entity_name "
                    "FROM sub_entity "
                    "WHERE :param_1 = sub_entity.id",
                    [{
                        "param_1": entity_id
                    }],
                ))

        asserter_.assert_(*expected)