Exemplo n.º 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",
                ),
            )
Exemplo n.º 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",
                ),
            )
Exemplo n.º 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())
Exemplo n.º 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()
        )
Exemplo n.º 5
0
    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"
Exemplo n.º 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"
Exemplo n.º 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
Exemplo n.º 8
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
Exemplo n.º 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()
        )
Exemplo n.º 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'
         },
     )
Exemplo n.º 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"
         },
     )
Exemplo n.º 12
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)
Exemplo n.º 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
     )
Exemplo n.º 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")
Exemplo n.º 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"},
     )
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 21
0
 def test_get_view_names_empty(self):
     insp = inspect(testing.db)
     assert_raises(ValueError, insp.get_view_names, include=())
Exemplo n.º 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)