示例#1
0
    def test_on_conflict_do_update_exotic_targets_five(self):
        users = self.tables.users_xtra

        with testing.db.connect() as conn:
            self._exotic_targets_fixture(conn)
            # try bogus index
            i = insert(users)
            i = i.on_conflict_do_update(
                index_elements=self.bogus_index.columns,
                index_where=self.bogus_index.dialect_options["postgresql"]
                ["where"],
                set_=dict(name=i.excluded.name,
                          login_email=i.excluded.login_email),
            )

            assert_raises(
                exc.ProgrammingError,
                conn.execute,
                i,
                dict(
                    id=1,
                    name="namebogus",
                    login_email="*****@*****.**",
                    lets_index_this="bogus",
                ),
            )
示例#2
0
    def test_on_conflict_do_update_exotic_targets_five(self):
        users = self.tables.users_xtra

        with testing.db.connect() as conn:
            self._exotic_targets_fixture(conn)
            # try bogus index
            i = insert(users)
            i = i.on_conflict_do_update(
                index_elements=self.bogus_index.columns,
                index_where=self.bogus_index.dialect_options["postgresql"][
                    "where"
                ],
                set_=dict(
                    name=i.excluded.name, login_email=i.excluded.login_email
                ),
            )

            assert_raises(
                exc.ProgrammingError,
                conn.execute,
                i,
                dict(
                    id=1,
                    name="namebogus",
                    login_email="*****@*****.**",
                    lets_index_this="bogus",
                ),
            )
示例#3
0
    def test_from_only(self):
        m = MetaData()
        tbl1 = Table('testtbl1', m, Column('id', Integer))
        tbl2 = Table('testtbl2', m, Column('id', Integer))

        stmt = tbl1.select().with_hint(tbl1, 'ONLY', 'postgresql')
        expected = 'SELECT testtbl1.id FROM ONLY testtbl1'
        self.assert_compile(stmt, expected)

        talias1 = tbl1.alias('foo')
        stmt = talias1.select().with_hint(talias1, 'ONLY', 'postgresql')
        expected = 'SELECT foo.id FROM ONLY testtbl1 AS foo'
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2]).with_hint(tbl1, 'ONLY', 'postgresql')
        expected = ('SELECT testtbl1.id, testtbl2.id FROM ONLY testtbl1, '
                    'testtbl2')
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2]).with_hint(tbl2, 'ONLY', 'postgresql')
        expected = ('SELECT testtbl1.id, testtbl2.id FROM testtbl1, ONLY '
                    'testtbl2')
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2])
        stmt = stmt.with_hint(tbl1, 'ONLY', 'postgresql')
        stmt = stmt.with_hint(tbl2, 'ONLY', 'postgresql')
        expected = ('SELECT testtbl1.id, testtbl2.id FROM ONLY testtbl1, '
                    'ONLY testtbl2')
        self.assert_compile(stmt, expected)

        stmt = update(tbl1, values=dict(id=1))
        stmt = stmt.with_hint('ONLY', dialect_name='postgresql')
        expected = 'UPDATE ONLY testtbl1 SET id=%(id)s'
        self.assert_compile(stmt, expected)

        stmt = delete(tbl1).with_hint('ONLY',
                                      selectable=tbl1,
                                      dialect_name='postgresql')
        expected = 'DELETE FROM ONLY testtbl1'
        self.assert_compile(stmt, expected)

        tbl3 = Table('testtbl3', m, Column('id', Integer), schema='testschema')
        stmt = tbl3.select().with_hint(tbl3, 'ONLY', 'postgresql')
        expected = 'SELECT testschema.testtbl3.id FROM '\
            'ONLY testschema.testtbl3'
        self.assert_compile(stmt, expected)

        assert_raises(exc.CompileError,
                      tbl3.select().with_hint(tbl3, "FAKE",
                                              "postgresql").compile,
                      dialect=postgresql.dialect())
示例#4
0
    def test_from_only(self):
        m = MetaData()
        tbl1 = Table('testtbl1', m, Column('id', Integer))
        tbl2 = Table('testtbl2', m, Column('id', Integer))

        stmt = tbl1.select().with_hint(tbl1, 'ONLY', 'postgresql')
        expected = 'SELECT testtbl1.id FROM ONLY testtbl1'
        self.assert_compile(stmt, expected)

        talias1 = tbl1.alias('foo')
        stmt = talias1.select().with_hint(talias1, 'ONLY', 'postgresql')
        expected = 'SELECT foo.id FROM ONLY testtbl1 AS foo'
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2]).with_hint(tbl1, 'ONLY', 'postgresql')
        expected = ('SELECT testtbl1.id, testtbl2.id FROM ONLY testtbl1, '
                    'testtbl2')
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2]).with_hint(tbl2, 'ONLY', 'postgresql')
        expected = ('SELECT testtbl1.id, testtbl2.id FROM testtbl1, ONLY '
                    'testtbl2')
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2])
        stmt = stmt.with_hint(tbl1, 'ONLY', 'postgresql')
        stmt = stmt.with_hint(tbl2, 'ONLY', 'postgresql')
        expected = ('SELECT testtbl1.id, testtbl2.id FROM ONLY testtbl1, '
                    'ONLY testtbl2')
        self.assert_compile(stmt, expected)

        stmt = update(tbl1, values=dict(id=1))
        stmt = stmt.with_hint('ONLY', dialect_name='postgresql')
        expected = 'UPDATE ONLY testtbl1 SET id=%(id)s'
        self.assert_compile(stmt, expected)

        stmt = delete(tbl1).with_hint(
            'ONLY', selectable=tbl1, dialect_name='postgresql')
        expected = 'DELETE FROM ONLY testtbl1'
        self.assert_compile(stmt, expected)

        tbl3 = Table('testtbl3', m, Column('id', Integer), schema='testschema')
        stmt = tbl3.select().with_hint(tbl3, 'ONLY', 'postgresql')
        expected = 'SELECT testschema.testtbl3.id FROM '\
            'ONLY testschema.testtbl3'
        self.assert_compile(stmt, expected)

        assert_raises(
            exc.CompileError,
            tbl3.select().with_hint(tbl3, "FAKE", "postgresql").compile,
            dialect=postgresql.dialect()
        )
    def test_cursor_close_w_failed_rowproc(self):
        User = self.classes.User
        s = fixture_session()

        q = s.query(User)

        ctx = q._compile_context()
        cursor = mock.Mock()
        ctx.compile_state._entities = [
            mock.Mock(row_processor=mock.Mock(side_effect=Exception("boom")))
        ]
        assert_raises(Exception, loading.instances, cursor, ctx)
        assert cursor.close.called, "Cursor wasn't closed"
示例#6
0
    def test_cursor_close_w_failed_rowproc(self):
        User = self.classes.User
        s = Session()

        q = s.query(User)

        ctx = q._compile_context()
        cursor = mock.Mock()
        q._entities = [
            mock.Mock(row_processor=mock.Mock(side_effect=Exception("boom")))
        ]
        assert_raises(Exception, list, loading.instances(q, cursor, ctx))
        assert cursor.close.called, "Cursor wasn't closed"
示例#7
0
    def test_unknown_types(self):
        from sqlalchemy.databases import postgresql
        ischema_names = postgresql.PGDialect.ischema_names
        postgresql.PGDialect.ischema_names = {}
        try:
            m2 = MetaData(testing.db)
            assert_raises(exc.SAWarning, Table, 'testtable', m2, autoload=True)

            @testing.emits_warning('Did not recognize type')
            def warns():
                m3 = MetaData(testing.db)
                t3 = Table('testtable', m3, autoload=True)
                assert t3.c.answer.type.__class__ == sa.types.NullType
        finally:
            postgresql.PGDialect.ischema_names = ischema_names
    def test_unknown_types(self):
        from sqlalchemy.databases import postgresql
        ischema_names = postgresql.PGDialect.ischema_names
        postgresql.PGDialect.ischema_names = {}
        try:
            m2 = MetaData(testing.db)
            assert_raises(exc.SAWarning, Table, 'testtable', m2,
                          autoload=True)

            @testing.emits_warning('Did not recognize type')
            def warns():
                m3 = MetaData(testing.db)
                t3 = Table('testtable', m3, autoload=True)
                assert t3.c.answer.type.__class__ == sa.types.NullType
        finally:
            postgresql.PGDialect.ischema_names = ischema_names
示例#9
0
    def test_from_only(self):
        m = MetaData()
        tbl1 = Table("testtbl1", m, Column("id", Integer))
        tbl2 = Table("testtbl2", m, Column("id", Integer))

        stmt = tbl1.select().with_hint(tbl1, "ONLY", "postgresql")
        expected = "SELECT testtbl1.id FROM ONLY testtbl1"
        self.assert_compile(stmt, expected)

        talias1 = tbl1.alias("foo")
        stmt = talias1.select().with_hint(talias1, "ONLY", "postgresql")
        expected = "SELECT foo.id FROM ONLY testtbl1 AS foo"
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2]).with_hint(tbl1, "ONLY", "postgresql")
        expected = "SELECT testtbl1.id, testtbl2.id FROM ONLY testtbl1, " "testtbl2"
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2]).with_hint(tbl2, "ONLY", "postgresql")
        expected = "SELECT testtbl1.id, testtbl2.id FROM testtbl1, ONLY " "testtbl2"
        self.assert_compile(stmt, expected)

        stmt = select([tbl1, tbl2])
        stmt = stmt.with_hint(tbl1, "ONLY", "postgresql")
        stmt = stmt.with_hint(tbl2, "ONLY", "postgresql")
        expected = "SELECT testtbl1.id, testtbl2.id FROM ONLY testtbl1, " "ONLY testtbl2"
        self.assert_compile(stmt, expected)

        stmt = update(tbl1, values=dict(id=1))
        stmt = stmt.with_hint("ONLY", dialect_name="postgresql")
        expected = "UPDATE ONLY testtbl1 SET id=%(id)s"
        self.assert_compile(stmt, expected)

        stmt = delete(tbl1).with_hint("ONLY", selectable=tbl1, dialect_name="postgresql")
        expected = "DELETE FROM ONLY testtbl1"
        self.assert_compile(stmt, expected)

        tbl3 = Table("testtbl3", m, Column("id", Integer), schema="testschema")
        stmt = tbl3.select().with_hint(tbl3, "ONLY", "postgresql")
        expected = "SELECT testschema.testtbl3.id FROM " "ONLY testschema.testtbl3"
        self.assert_compile(stmt, expected)

        assert_raises(
            exc.CompileError, tbl3.select().with_hint(tbl3, "FAKE", "postgresql").compile, dialect=postgresql.dialect()
        )
示例#10
0
 def test_bad_args(self):
     assert_raises(
         ValueError,
         insert(self.tables.foos, values={}).on_duplicate_key_update)
     assert_raises(
         exc.ArgumentError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
         {
             'id': 1,
             'bar': 'b'
         },
         id=1,
         bar='b',
     )
     assert_raises(
         exc.ArgumentError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
         {
             'id': 1,
             'bar': 'b'
         },
         {
             'id': 2,
             'bar': 'baz'
         },
     )
示例#11
0
 def test_bad_args(self):
     assert_raises(
         ValueError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
     )
     assert_raises(
         exc.ArgumentError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
         {
             "id": 1,
             "bar": "b"
         },
         id=1,
         bar="b",
     )
     assert_raises(
         exc.ArgumentError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
         {
             "id": 1,
             "bar": "b"
         },
         {
             "id": 2,
             "bar": "baz"
         },
     )
 def test_bad_args(self):
     assert_raises(ValueError,
                   insert(self.tables.users).on_conflict_do_nothing,
                   constraint='id',
                   index_elements=['id'])
     assert_raises(ValueError,
                   insert(self.tables.users).on_conflict_do_update,
                   constraint='id',
                   index_elements=['id'])
     assert_raises(ValueError,
                   insert(self.tables.users).on_conflict_do_update,
                   constraint='id')
     assert_raises(ValueError,
                   insert(self.tables.users).on_conflict_do_update)
示例#13
0
 def test_bad_args(self):
     assert_raises(
         ValueError,
         insert(self.tables.users).on_conflict_do_nothing,
         constraint='id', index_elements=['id']
     )
     assert_raises(
         ValueError,
         insert(self.tables.users).on_conflict_do_update,
         constraint='id', index_elements=['id']
     )
     assert_raises(
         ValueError,
         insert(self.tables.users).on_conflict_do_update, constraint='id'
     )
     assert_raises(
         ValueError,
         insert(self.tables.users).on_conflict_do_update
     )
示例#14
0
    def test_error_code(self, metadata, connection):
        t = Table("t", metadata, Column("id", Integer, primary_key=True))
        t.create(connection)

        errmsg = assert_raises(
            exc.IntegrityError,
            connection.execute,
            t.insert(),
            [{"id": 1}, {"id": 1}],
        )

        if testing.against("postgresql+pg8000"):
            # TODO: is there another way we're supposed to see this?
            eq_(errmsg.orig.args[0]["C"], "23505")
        else:
            eq_(errmsg.orig.pgcode, "23505")

        if testing.against("postgresql+asyncpg"):
            eq_(errmsg.orig.sqlstate, "23505")
示例#15
0
 def test_bad_args(self):
     assert_raises(
         ValueError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
     )
     assert_raises(
         exc.ArgumentError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
         {"id": 1, "bar": "b"},
         id=1,
         bar="b",
     )
     assert_raises(
         exc.ArgumentError,
         insert(self.tables.foos, values={}).on_duplicate_key_update,
         {"id": 1, "bar": "b"},
         {"id": 2, "bar": "baz"},
     )
示例#16
0
 def test_numeric_raise(self):
     stmt = text("select cast('hi' as char) as hi", typemap={"hi": Numeric})
     assert_raises(exc.InvalidRequestError, testing.db.execute, stmt)
示例#17
0
 def test_numeric_raise(self):
     stmt = text("select cast('hi' as char) as hi", typemap={'hi': Numeric})
     assert_raises(exc.InvalidRequestError, testing.db.execute, stmt)
示例#18
0
 def test_bad_args(self):
     assert_raises(
         ValueError,
         insert(self.tables.foos, values={}).on_duplicate_key_update
     )
 def test_bad_args(self):
     assert_raises(
         ValueError,
         insert(self.tables.foos, values={}).on_duplicate_key_update)
示例#20
0
 def test_numeric_raise(self):
     stmt = text("select cast('hi' as char) as hi").columns(hi=Numeric)
     assert_raises(exc.InvalidRequestError, testing.db.execute, stmt)
示例#21
0
 def test_get_view_names_empty(self):
     insp = inspect(testing.db)
     assert_raises(ValueError, insp.get_view_names, include=())
示例#22
0
 def test_numeric_raise(self, connection):
     stmt = text("select cast('hi' as char) as hi").columns(hi=Numeric)
     assert_raises(exc.InvalidRequestError, connection.execute, stmt)