sqlalchemy.testing.expect_warnings

Here are the examples of the python api sqlalchemy.testing.expect_warnings taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

17 Examples 7

Example 1

Project: sqlalchemy Source File: test_compiler.py
    def test_no_cast_pre_4(self):
        self.assert_compile(
            cast(Column('foo', Integer), String),
            "CAST(foo AS CHAR)",
        )
        dialect = mysql.dialect()
        dialect.server_version_info = (3, 2, 3)
        with expect_warnings("Current MySQL version does not support CAST;"):
            self.assert_compile(
                cast(Column('foo', Integer), String),
                "foo",
                dialect=dialect
            )

Example 2

Project: sqlalchemy Source File: test_compiler.py
    def test_cast_grouped_expression_non_castable(self):
        with expect_warnings("Datatype FLOAT does not support CAST on MySQL;"):
            self.assert_compile(
                cast(sql.column('x') + sql.column('y'), Float),
                "(x + y)"
            )

Example 3

Project: sqlalchemy Source File: test_compiler.py
    def test_cast_grouped_expression_pre_4(self):
        dialect = mysql.dialect()
        dialect.server_version_info = (3, 2, 3)
        with expect_warnings("Current MySQL version does not support CAST;"):
            self.assert_compile(
                cast(sql.column('x') + sql.column('y'), Integer),
                "(x + y)",
                dialect=dialect
            )

Example 4

Project: sqlalchemy Source File: test_transaction.py
    def test_execution_options_ignored_mid_transaction(self):
        bind = mock.Mock()
        conn = mock.Mock(engine=bind)
        bind.contextual_connect = mock.Mock(return_value=conn)
        sess = Session(bind=bind)
        sess.execute("select 1")
        with expect_warnings(
                "Connection is already established for the "
                "given bind; execution_options ignored"):
            sess.connection(execution_options={'isolation_level': 'FOO'})

Example 5

Project: sqlalchemy Source File: test_insert.py
    def test_anticipate_no_pk_composite_pk(self):
        t = Table(
            't', MetaData(), Column('x', Integer, primary_key=True),
            Column('y', Integer, primary_key=True)
        )

        with expect_warnings(
            "Column 't.y' is marked as a member.*"
            "Note that as of SQLAlchemy 1.1,",
        ):
            self.assert_compile(
                t.insert(),
                "INSERT INTO t (x) VALUES (:x)",
                params={'x': 5},
            )

Example 6

Project: sqlalchemy Source File: test_insert.py
    def test_anticipate_no_pk_composite_pk_implicit_returning(self):
        t = Table(
            't', MetaData(), Column('x', Integer, primary_key=True),
            Column('y', Integer, primary_key=True)
        )
        d = postgresql.dialect()
        d.implicit_returning = True

        with expect_warnings(
            "Column 't.y' is marked as a member.*"
            "Note that as of SQLAlchemy 1.1,",
        ):
            self.assert_compile(
                t.insert(),
                "INSERT INTO t (x) VALUES (%(x)s)",
                params={"x": 5},
                dialect=d
            )

Example 7

Project: sqlalchemy Source File: test_insert.py
    def test_anticipate_no_pk_composite_pk_prefetch(self):
        t = Table(
            't', MetaData(), Column('x', Integer, primary_key=True),
            Column('y', Integer, primary_key=True)
        )
        d = postgresql.dialect()
        d.implicit_returning = False
        with expect_warnings(
            "Column 't.y' is marked as a member.*"
            "Note that as of SQLAlchemy 1.1,"
        ):
            self.assert_compile(
                t.insert(),
                "INSERT INTO t (x) VALUES (%(x)s)",
                params={'x': 5},
                dialect=d
            )

Example 8

Project: sqlalchemy Source File: test_insert.py
    def test_anticipate_no_pk_non_composite_pk(self):
        t = Table(
            't', MetaData(),
            Column('x', Integer, primary_key=True, autoincrement=False),
            Column('q', Integer)
        )
        with expect_warnings(
            "Column 't.x' is marked as a member.*"
            "may not store NULL.$"
        ):
            self.assert_compile(
                t.insert(),
                "INSERT INTO t (q) VALUES (:q)",
                params={"q": 5}
            )

Example 9

Project: sqlalchemy Source File: test_insert.py
    def test_anticipate_no_pk_non_composite_pk_implicit_returning(self):
        t = Table(
            't', MetaData(),
            Column('x', Integer, primary_key=True, autoincrement=False),
            Column('q', Integer)
        )
        d = postgresql.dialect()
        d.implicit_returning = True
        with expect_warnings(
            "Column 't.x' is marked as a member.*"
            "may not store NULL.$",
        ):
            self.assert_compile(
                t.insert(),
                "INSERT INTO t (q) VALUES (%(q)s)",
                params={"q": 5},
                dialect=d
            )

Example 10

Project: sqlalchemy Source File: test_insert.py
    def test_anticipate_no_pk_non_composite_pk_prefetch(self):
        t = Table(
            't', MetaData(),
            Column('x', Integer, primary_key=True, autoincrement=False),
            Column('q', Integer)
        )
        d = postgresql.dialect()
        d.implicit_returning = False

        with expect_warnings(
            "Column 't.x' is marked as a member.*"
            "may not store NULL.$"
        ):
            self.assert_compile(
                t.insert(),
                "INSERT INTO t (q) VALUES (%(q)s)",
                params={"q": 5},
                dialect=d
            )

Example 11

Project: sqlalchemy Source File: test_text.py
Function: test
    def _test(self, fn, arg, offending_clause, expected):
        with expect_warnings("Textual "):
            stmt = fn(arg)
            self.assert_compile(stmt, expected)

        assert_raises_message(
            exc.SAWarning,
            r"Textual (?:SQL|column|SQL FROM) expression %(stmt)r should be "
            r"explicitly declared (?:with|as) text\(%(stmt)r\)" % {
                "stmt": util.ellipses_string(offending_clause),
            },
            fn, arg
        )

Example 12

Project: sqlalchemy Source File: test_text.py
Function: test_warning
    def _test_warning(self, stmt, offending_clause, expected):
        with expect_warnings(
                "Can't resolve label reference %r;" % offending_clause):
            self.assert_compile(
                stmt,
                expected
            )
        assert_raises_message(
            exc.SAWarning,
            "Can't resolve label reference %r; converting to text" %
            offending_clause,
            stmt.compile
        )

Example 13

Project: sqlalchemy Source File: test_compiler.py
    def test_unsupported_cast_literal_bind(self):
        expr = cast(column('foo', Integer) + 5, Float)

        with expect_warnings(
            "Datatype FLOAT does not support CAST on MySQL;"
        ):
            self.assert_compile(
                expr,
                "(foo + 5)",
                literal_binds=True
            )

        dialect = mysql.MySQLDialect()
        dialect.server_version_info = (3, 9, 8)
        with expect_warnings(
            "Current MySQL version does not support CAST"
        ):
            eq_(
                str(expr.compile(
                    dialect=dialect,
                    compile_kwargs={"literal_binds": True})),
                "(foo + 5)"
            )

Example 14

Project: sqlalchemy Source File: test_compiler.py
    def test_unsupported_casts(self):

        t = sql.table('t', sql.column('col'))
        m = mysql

        specs = [
            (m.MSBit, "t.col"),

            (FLOAT, "t.col"),
            (Float, "t.col"),
            (m.MSFloat, "t.col"),
            (m.MSDouble, "t.col"),
            (m.MSReal, "t.col"),

            (m.MSYear, "t.col"),
            (m.MSYear(2), "t.col"),

            (Boolean, "t.col"),
            (BOOLEAN, "t.col"),

            (m.MSEnum, "t.col"),
            (m.MSEnum("1", "2"), "t.col"),
            (m.MSSet, "t.col"),
            (m.MSSet("1", "2"), "t.col"),
        ]

        for type_, expected in specs:
            with expect_warnings(
                "Datatype .* does not support CAST on MySQL;"
            ):
                self.assert_compile(cast(t.c.col, type_), expected)

Example 15

Project: sqlalchemy Source File: test_query.py
    def test_foreignkey_missing_insert(self):
        Table(
            't1', self.metadata,
            Column('id', Integer, primary_key=True))
        t2 = Table(
            't2',
            self.metadata,
            Column(
                'id',
                Integer,
                ForeignKey('t1.id'),
                primary_key=True))
        self.metadata.create_all()

        # want to ensure that "null value in column "id" violates not-
        # null constraint" is raised (IntegrityError on psycoopg2, but
        # ProgrammingError on pg8000), and not "ProgrammingError:
        # (ProgrammingError) relationship "t2_id_seq" does not exist".
        # the latter corresponds to autoincrement behavior, which is not
        # the case here due to the foreign key.

        for eng in [
            engines.testing_engine(options={'implicit_returning': False}),
            engines.testing_engine(options={'implicit_returning': True})
        ]:
            with expect_warnings(
                ".*has no Python-side or server-side default.*"
            ):
                assert_raises(
                    (exc.IntegrityError, exc.ProgrammingError),
                    eng.execute, t2.insert()
                )

Example 16

Project: sqlalchemy Source File: test_query.py
    def _assert_data_noautoincrement(self, table):
        engine = \
            engines.testing_engine(options={'implicit_returning': False})

        with engine.connect() as conn:
            conn.execute(table.insert(), {'id': 30, 'data': 'd1'})

            with expect_warnings(
                    ".*has no Python-side or server-side default.*",
            ):
                assert_raises(
                    (exc.IntegrityError, exc.ProgrammingError),
                    conn.execute, table.insert(), {'data': 'd2'})
            with expect_warnings(
                    ".*has no Python-side or server-side default.*",
            ):
                assert_raises(
                    (exc.IntegrityError, exc.ProgrammingError),
                    conn.execute, table.insert(), {'data': 'd2'},
                    {'data': 'd3'})
            with expect_warnings(
                    ".*has no Python-side or server-side default.*",
            ):
                assert_raises(
                    (exc.IntegrityError, exc.ProgrammingError),
                    conn.execute, table.insert(), {'data': 'd2'})
            with expect_warnings(
                    ".*has no Python-side or server-side default.*",
            ):
                assert_raises(
                    (exc.IntegrityError, exc.ProgrammingError),
                    conn.execute, table.insert(), {'data': 'd2'},
                    {'data': 'd3'})

            conn.execute(
                table.insert(),
                {'id': 31, 'data': 'd2'}, {'id': 32, 'data': 'd3'})
            conn.execute(table.insert(inline=True), {'id': 33, 'data': 'd4'})
            eq_(conn.execute(table.select()).fetchall(), [
                (30, 'd1'),
                (31, 'd2'),
                (32, 'd3'),
                (33, 'd4')])
            conn.execute(table.delete())

        # test the same series of events using a reflected version of
        # the table

        m2 = MetaData(engine)
        table = Table(table.name, m2, autoload=True)
        with engine.connect() as conn:
            conn.execute(table.insert(), {'id': 30, 'data': 'd1'})

            with expect_warnings(
                    ".*has no Python-side or server-side default.*",
            ):
                assert_raises(
                    (exc.IntegrityError, exc.ProgrammingError),
                    conn.execute, table.insert(), {'data': 'd2'})
            with expect_warnings(
                    ".*has no Python-side or server-side default.*",
            ):
                assert_raises(
                    (exc.IntegrityError, exc.ProgrammingError),
                    conn.execute, table.insert(), {'data': 'd2'},
                    {'data': 'd3'})
            conn.execute(
                table.insert(),
                {'id': 31, 'data': 'd2'}, {'id': 32, 'data': 'd3'})
            conn.execute(table.insert(inline=True), {'id': 33, 'data': 'd4'})
            eq_(conn.execute(table.select()).fetchall(), [
                (30, 'd1'),
                (31, 'd2'),
                (32, 'd3'),
                (33, 'd4')])

Example 17

Project: sqlalchemy Source File: test_transaction.py
    @testing.requires.python2
    @testing.requires.savepoints_w_release
    def test_report_primary_error_when_rollback_fails(self):
        User, users = self.classes.User, self.tables.users

        mapper(User, users)

        session = Session(testing.db)

        with expect_warnings(".*during handling of a previous exception.*"):
            session.begin_nested()
            savepoint = session.\
                connection()._Connection__transaction._savepoint

            # force the savepoint to disappear
            session.connection().dialect.do_release_savepoint(
                session.connection(), savepoint
            )

            # now do a broken flush
            session.add_all([User(id=1), User(id=1)])

            assert_raises_message(
                sa_exc.DBAPIError,
                "ROLLBACK TO SAVEPOINT ",
                session.flush
            )