예제 #1
0
    def test_bind_through_execute(self, statement, expected_get_bind_args,
                                  expected_engine_name):
        users, Address, addresses, User = (
            self.tables.users,
            self.classes.Address,
            self.tables.addresses,
            self.classes.User,
        )

        mapper(User, users, properties={"addresses": relationship(Address)})
        mapper(Address, addresses)

        e1 = engines.testing_engine()
        e2 = engines.testing_engine()
        e3 = engines.testing_engine()

        canary = mock.Mock()

        class GetBindSession(Session):
            def _connection_for_bind(self, bind, **kw):
                canary._connection_for_bind(bind, **kw)
                return mock.Mock()

            def get_bind(self, **kw):
                canary.get_bind(**kw)
                return Session.get_bind(self, **kw)

        sess = GetBindSession(e3, future=True)
        sess.bind_mapper(User, e1)
        sess.bind_mapper(Address, e2)

        lambda_args = dict(
            session=sess,
            User=User,
            Address=Address,
            e1=e1,
            e2=e2,
            e3=e3,
            addresses=addresses,
        )
        statement = testing.resolve_lambda(statement, **lambda_args)

        expected_get_bind_args = testing.resolve_lambda(
            expected_get_bind_args, **lambda_args)

        engine = {"e1": e1, "e2": e2, "e3": e3}[expected_engine_name]

        with mock.patch(
                "sqlalchemy.orm.context.ORMCompileState.orm_setup_cursor_result"
        ):
            sess.execute(statement)

        eq_(
            canary.mock_calls,
            [
                mock.call.get_bind(**expected_get_bind_args),
                mock.call._connection_for_bind(engine, close_with_result=True),
            ],
        )
        sess.close()
예제 #2
0
    def test_with_polymorphic_join_exec_contains_eager_two(
        self, contains_eager_option
    ):
        sess = Session()

        wp = with_polymorphic(Person, [Engineer, Manager], aliased=True)
        contains_eager_option = testing.resolve_lambda(
            contains_eager_option, Company=Company, wp=wp
        )
        q = (
            sess.query(Company)
            .join(Company.employees.of_type(wp))
            .order_by(Company.company_id, wp.person_id)
            .options(contains_eager_option)
        )

        def go():
            eq_(q.all(), [self.c1, self.c2])

        self.assert_sql_count(testing.db, go, 1)

        self.assert_compile(
            q,
            self._test_with_polymorphic_join_exec_contains_eager_two_result(),
        )
예제 #3
0
    def test_get_bind(self, testcase, expected):
        users, Address, addresses, User = (
            self.tables.users,
            self.classes.Address,
            self.tables.addresses,
            self.classes.User,
        )

        mapper(User, users, properties={"addresses": relationship(Address)})
        mapper(Address, addresses)

        e1 = engines.testing_engine()
        e2 = engines.testing_engine()
        e3 = engines.testing_engine()

        testcase = testing.resolve_lambda(
            testcase,
            User=User,
            Address=Address,
            e1=e1,
            e2=e2,
            e3=e3,
            addresses=addresses,
        )

        sess = Session(e3)
        sess.bind_mapper(User, e1)
        sess.bind_mapper(Address, e2)

        engine = {"e1": e1, "e2": e2, "e3": e3}[expected]
        conn = sess.connection(**testcase)
        is_(conn.engine, engine)

        sess.close()
예제 #4
0
    def test_when_dicts(self, test_case, expected):
        t = table("test", column("col1"))

        when_dict, value, else_ = testing.resolve_lambda(test_case, t=t)

        self.assert_compile(
            case(when_dict, value=value, else_=else_), expected
        )
예제 #5
0
    def test_unbound_options(self, test_case):
        sess, User, Address, Dingaling = self._option_test_fixture()

        opt = testing.resolve_lambda(test_case, User=User, Address=Address)
        opt2 = pickle.loads(pickle.dumps(opt))
        eq_(opt.path, opt2.path)

        u1 = sess.query(User).options(opt).first()
        pickle.loads(pickle.dumps(u1))
예제 #6
0
    def test_join_entity_arg(self, plain_fixture, test_case):
        User, Address = plain_fixture

        s = Session(testing.db, future=True)

        stmt = testing.resolve_lambda(test_case, **locals())
        self.assert_compile(
            stmt,
            "SELECT users.id AS users_id, users.name AS users_name "
            "FROM users JOIN addresses ON users.id = addresses.user_id",
        )
예제 #7
0
    def test_string_ops(self, expr):
        User = self.classes.User

        test_expr = testing.resolve_lambda(expr, User=User)
        eval_eq(
            test_expr,
            testcases=[
                (User(name="named"), True),
                (User(name="othername"), False),
            ],
        )
예제 #8
0
    def test_strict_binds(self, expr, compiled, kw):
        """test the 'strict' compiler binds."""

        from sqlalchemy.dialects.mssql.base import MSSQLStrictCompiler

        mxodbc_dialect = mxodbc.dialect()
        mxodbc_dialect.statement_compiler = MSSQLStrictCompiler

        t = table("sometable", column("foo"))

        expr = testing.resolve_lambda(expr, t=t)
        self.assert_compile(expr, compiled, dialect=mxodbc_dialect, **kw)
예제 #9
0
    def test_bound_options(self, test_case):
        sess, User, Address, Dingaling = self._option_test_fixture()

        opt = testing.resolve_lambda(test_case, User=User, Address=Address)

        opt2 = pickle.loads(pickle.dumps(opt))
        eq_(opt.path, opt2.path)
        for v1, v2 in zip(opt.context, opt2.context):
            eq_(v1.local_opts, v2.local_opts)

        u1 = sess.query(User).options(opt).first()
        pickle.loads(pickle.dumps(u1))
예제 #10
0
    def test_when_dicts(self, test_case, expected):
        t = table("test", column("col1"))

        whens, value, else_ = testing.resolve_lambda(test_case, t=t)

        def _case_args(whens, value=None, else_=None):
            kw = {}
            if value is not None:
                kw["value"] = value
            if else_ is not None:
                kw["else_"] = else_

            return case(whens, **kw)

            # note: 1.3 also does not allow this form
            # case([whens], **kw)

        self.assert_compile(
            _case_args(whens=whens, value=value, else_=else_),
            expected,
        )
예제 #11
0
    def test_no_star_orm_combinations(self, exprtype, testcase):
        """test for #8235"""
        User = self.classes.User

        if exprtype == "plain":
            star = "*"
        elif exprtype == "text":
            star = text("*")
        elif exprtype == "literal_column":
            star = literal_column("*")
        else:
            assert False

        args = testing.resolve_lambda(testcase, User=User, star=star)
        stmt = select(*args).select_from(User)

        s = fixture_session()

        with expect_raises_message(
                exc.CompileError,
                r"Can't generate ORM query that includes multiple expressions "
                r"at the same time as '\*';",
        ):
            s.execute(stmt)