sqlalchemy.testing.assertions.eq_

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

527 Examples 7

3 Source : assertions.py
with MIT License
from bkerler

def assert_compiled(element, assert_string, dialect=None):
    dialect = _get_dialect(dialect)
    eq_(
        text_type(element.compile(dialect=dialect)).
        replace("\n", "").replace("\t", ""),
        assert_string.replace("\n", "").replace("\t", "")
    )


_dialect_mods = {}

3 Source : test_on_duplicate.py
with Apache License 2.0
from gethue

    def test_on_duplicate_key_update(self):
        foos = self.tables.foos
        with testing.db.connect() as conn:
            conn.execute(insert(foos, dict(id=1, bar="b", baz="bz")))
            stmt = insert(foos).values(
                [dict(id=1, bar="ab"), dict(id=2, bar="b")]
            )
            stmt = stmt.on_duplicate_key_update(bar=stmt.inserted.bar)
            result = conn.execute(stmt)
            eq_(result.inserted_primary_key, [2])
            eq_(
                conn.execute(foos.select().where(foos.c.id == 1)).fetchall(),
                [(1, "ab", "bz", False)],
            )

    def test_on_duplicate_key_update_null(self):

3 Source : test_on_duplicate.py
with Apache License 2.0
from gethue

    def test_on_duplicate_key_update_null(self):
        foos = self.tables.foos
        with testing.db.connect() as conn:
            conn.execute(insert(foos, dict(id=1, bar="b", baz="bz")))
            stmt = insert(foos).values(
                [dict(id=1, bar="ab"), dict(id=2, bar="b")]
            )
            stmt = stmt.on_duplicate_key_update(updated_once=None)
            result = conn.execute(stmt)
            eq_(result.inserted_primary_key, [2])
            eq_(
                conn.execute(foos.select().where(foos.c.id == 1)).fetchall(),
                [(1, "b", "bz", None)],
            )

    def test_on_duplicate_key_update_expression(self):

3 Source : test_on_duplicate.py
with Apache License 2.0
from gethue

    def test_on_duplicate_key_update_expression(self):
        foos = self.tables.foos
        with testing.db.connect() as conn:
            conn.execute(insert(foos, dict(id=1, bar="b", baz="bz")))
            stmt = insert(foos).values(
                [dict(id=1, bar="ab"), dict(id=2, bar="b")]
            )
            stmt = stmt.on_duplicate_key_update(
                bar=func.concat(stmt.inserted.bar, "_foo")
            )
            result = conn.execute(stmt)
            eq_(result.inserted_primary_key, [2])
            eq_(
                conn.execute(foos.select().where(foos.c.id == 1)).fetchall(),
                [(1, "ab_foo", "bz", False)],
            )

    def test_on_duplicate_key_update_preserve_order(self):

3 Source : test_on_duplicate.py
with Apache License 2.0
from gethue

    def test_last_inserted_id(self):
        foos = self.tables.foos
        with testing.db.connect() as conn:
            stmt = insert(foos).values({"bar": "b", "baz": "bz"})
            result = conn.execute(
                stmt.on_duplicate_key_update(
                    bar=stmt.inserted.bar, baz="newbz"
                )
            )
            eq_(result.inserted_primary_key, [1])

            stmt = insert(foos).values({"id": 1, "bar": "b", "baz": "bz"})
            result = conn.execute(
                stmt.on_duplicate_key_update(
                    bar=stmt.inserted.bar, baz="newbz"
                )
            )
            eq_(result.inserted_primary_key, [1])

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_pg_dialect_use_native_unicode_from_config(self):
        config = {
            "sqlalchemy.url": testing.db.url,
            "sqlalchemy.use_native_unicode": "false",
        }

        e = engine_from_config(config, _initialize=False)
        eq_(e.dialect.use_native_unicode, False)

        config = {
            "sqlalchemy.url": testing.db.url,
            "sqlalchemy.use_native_unicode": "true",
        }

        e = engine_from_config(config, _initialize=False)
        eq_(e.dialect.use_native_unicode, True)

    def test_psycopg2_empty_connection_string(self):

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_psycopg2_empty_connection_string(self):
        dialect = psycopg2_dialect.dialect()
        u = url.make_url("postgresql://")
        cargs, cparams = dialect.create_connect_args(u)
        eq_(cargs, [""])
        eq_(cparams, {})

    def test_psycopg2_nonempty_connection_string(self):

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_psycopg2_nonempty_connection_string(self):
        dialect = psycopg2_dialect.dialect()
        u = url.make_url("postgresql://host")
        cargs, cparams = dialect.create_connect_args(u)
        eq_(cargs, [])
        eq_(cparams, {"host": "host"})

    def test_psycopg2_empty_connection_string_w_query_one(self):

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_psycopg2_empty_connection_string_w_query_one(self):
        dialect = psycopg2_dialect.dialect()
        u = url.make_url("postgresql:///?service=swh-log")
        cargs, cparams = dialect.create_connect_args(u)
        eq_(cargs, [])
        eq_(cparams, {"service": "swh-log"})

    def test_psycopg2_empty_connection_string_w_query_two(self):

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_psycopg2_empty_connection_string_w_query_two(self):
        dialect = psycopg2_dialect.dialect()
        u = url.make_url("postgresql:///?any_random_thing=yes")
        cargs, cparams = dialect.create_connect_args(u)
        eq_(cargs, [])
        eq_(cparams, {"any_random_thing": "yes"})

    def test_psycopg2_nonempty_connection_string_w_query(self):

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_psycopg2_nonempty_connection_string_w_query(self):
        dialect = psycopg2_dialect.dialect()
        u = url.make_url("postgresql://somehost/?any_random_thing=yes")
        cargs, cparams = dialect.create_connect_args(u)
        eq_(cargs, [])
        eq_(cparams, {"host": "somehost", "any_random_thing": "yes"})


class ExecuteManyMode(object):

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_client_encoding(self):
        c = testing.db.connect()
        current_encoding = c.execute("show client_encoding").fetchone()[0]
        c.close()

        # attempt to use an encoding that's not
        # already set
        if current_encoding == "UTF8":
            test_encoding = "LATIN1"
        else:
            test_encoding = "UTF8"

        e = engines.testing_engine(options={"client_encoding": test_encoding})
        c = e.connect()
        new_encoding = c.execute("show client_encoding").fetchone()[0]
        eq_(new_encoding, test_encoding)

    @testing.requires.psycopg2_or_pg8000_compatibility

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_extract(self):
        fivedaysago = testing.db.scalar(
            select([func.now()])
        ) - datetime.timedelta(days=5)
        for field, exp in (
            ("year", fivedaysago.year),
            ("month", fivedaysago.month),
            ("day", fivedaysago.day),
        ):
            r = testing.db.execute(
                select(
                    [extract(field, func.now() + datetime.timedelta(days=-5))]
                )
            ).scalar()
            eq_(r, exp)

    @testing.provide_metadata

3 Source : test_dialect.py
with Apache License 2.0
from gethue

    def test_quoted_name_bindparam_ok(self):
        from sqlalchemy.sql.elements import quoted_name

        with testing.db.connect() as conn:
            eq_(
                conn.scalar(
                    select(
                        [
                            cast(
                                literal(quoted_name("some_name", False)),
                                String,
                            )
                        ]
                    )
                ),
                "some_name",
            )

    def test_preexecute_passivedefault(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_foreign_table_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("test_foreigntable", metadata, autoload=True)
        eq_(
            set(table.columns.keys()),
            set(["id", "data"]),
            "Columns of reflected foreign table didn't equal expected columns",
        )

    def test_get_foreign_table_names(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_get_foreign_table_names(self):
        inspector = inspect(testing.db)
        with testing.db.connect():
            ft_names = inspector.get_foreign_table_names()
            eq_(ft_names, ["test_foreigntable"])

    def test_get_table_names_no_foreign(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_get_table_names_no_foreign(self):
        inspector = inspect(testing.db)
        with testing.db.connect():
            names = inspector.get_table_names()
            eq_(names, ["testtable"])


class PartitionedReflectionTest(fixtures.TablesTest, AssertsExecutionResults):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_reflect_index(self):
        idx = inspect(testing.db).get_indexes("data_values")
        eq_(
            idx, [{"column_names": ["q"], "name": "my_index", "unique": False}]
        )

    @testing.only_on("postgresql >= 11")

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_reflect_index_from_partition(self):
        idx = inspect(testing.db).get_indexes("data_values_4_10")
        # note the name appears to be generated by PG, currently
        # 'data_values_4_10_q_idx'
        eq_(idx, [{"column_names": ["q"], "name": mock.ANY, "unique": False}])


class MaterializedViewReflectionTest(

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_mview_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("test_mview", metadata, autoload=True)
        eq_(
            set(table.columns.keys()),
            set(["id", "data"]),
            "Columns of reflected mview didn't equal expected columns",
        )

    def test_mview_select(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_mview_select(self):
        metadata = MetaData(testing.db)
        table = Table("test_mview", metadata, autoload=True)
        eq_(table.select().execute().fetchall(), [(89, "d1")])

    def test_get_view_names(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_get_view_names_plain(self):
        insp = inspect(testing.db)
        eq_(
            set(insp.get_view_names(include=("plain",))), set(["test_regview"])
        )

    def test_get_view_names_plain_string(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_get_view_names_materialized(self):
        insp = inspect(testing.db)
        eq_(
            set(insp.get_view_names(include=("materialized",))),
            set(["test_mview"]),
        )

    def test_get_view_names_reflection_cache_ok(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_get_view_names_reflection_cache_ok(self):
        insp = inspect(testing.db)
        eq_(
            set(insp.get_view_names(include=("plain",))), set(["test_regview"])
        )
        eq_(
            set(insp.get_view_names(include=("materialized",))),
            set(["test_mview"]),
        )
        eq_(set(insp.get_view_names()), set(["test_regview", "test_mview"]))

    def test_get_view_names_empty(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_get_view_definition(self):
        insp = inspect(testing.db)
        eq_(
            re.sub(
                r"[\n\t ]+",
                " ",
                insp.get_view_definition("test_mview").strip(),
            ),
            "SELECT testtable.id, testtable.data FROM testtable;",
        )


class DomainReflectionTest(fixtures.TestBase, AssertsExecutionResults):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_table_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("testtable", metadata, autoload=True)
        eq_(
            set(table.columns.keys()),
            set(["question", "answer"]),
            "Columns of reflected table didn't equal expected columns",
        )
        assert isinstance(table.c.answer.type, Integer)

    def test_domain_is_reflected(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_domain_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("testtable", metadata, autoload=True)
        eq_(
            str(table.columns.answer.server_default.arg),
            "42",
            "Reflected default value didn't equal expected value",
        )
        assert (
            not table.columns.answer.nullable
        ), "Expected reflected column to not be nullable."

    def test_enum_domain_is_reflected(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_enum_domain_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("enum_test", metadata, autoload=True)
        eq_(table.c.data.type.enums, ["test"])

    def test_array_domain_is_reflected(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_array_domain_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("array_test", metadata, autoload=True)
        eq_(table.c.data.type.__class__, ARRAY)
        eq_(table.c.data.type.item_type.__class__, INTEGER)

    def test_quoted_remote_schema_domain_is_reflected(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_quoted_remote_schema_domain_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("quote_test", metadata, autoload=True)
        eq_(table.c.data.type.__class__, INTEGER)

    def test_table_is_reflected_test_schema(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_table_is_reflected_test_schema(self):
        metadata = MetaData(testing.db)
        table = Table(
            "testtable", metadata, autoload=True, schema="test_schema"
        )
        eq_(
            set(table.columns.keys()),
            set(["question", "answer", "anything"]),
            "Columns of reflected table didn't equal expected columns",
        )
        assert isinstance(table.c.anything.type, Integer)

    def test_schema_domain_is_reflected(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_schema_domain_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table(
            "testtable", metadata, autoload=True, schema="test_schema"
        )
        eq_(
            str(table.columns.answer.server_default.arg),
            "0",
            "Reflected default value didn't equal expected value",
        )
        assert (
            table.columns.answer.nullable
        ), "Expected reflected column to be nullable."

    def test_crosschema_domain_is_reflected(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_crosschema_domain_is_reflected(self):
        metadata = MetaData(testing.db)
        table = Table("crosschema", metadata, autoload=True)
        eq_(
            str(table.columns.answer.server_default.arg),
            "0",
            "Reflected default value didn't equal expected value",
        )
        assert (
            table.columns.answer.nullable
        ), "Expected reflected column to be nullable."

    def test_unknown_types(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_reflected_primary_key_order(self):
        meta1 = self.metadata
        subject = Table(
            "subject",
            meta1,
            Column("p1", Integer, primary_key=True),
            Column("p2", Integer, primary_key=True),
            PrimaryKeyConstraint("p2", "p1"),
        )
        meta1.create_all()
        meta2 = MetaData(testing.db)
        subject = Table("subject", meta2, autoload=True)
        eq_(subject.primary_key.columns.keys(), ["p2", "p1"])

    @testing.provide_metadata

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_reflect_default_over_128_chars(self):
        Table(
            "t",
            self.metadata,
            Column("x", String(200), server_default="abcd" * 40),
        ).create(testing.db)

        m = MetaData()
        t = Table("t", m, autoload=True, autoload_with=testing.db)
        eq_(
            t.c.x.server_default.arg.text,
            "'%s'::character varying" % ("abcd" * 40),
        )

    @testing.fails_if("postgresql   <   8.1", "schema name leaks in, not sure")

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_altered_type_autoincrement_pk_reflection(self):
        metadata = self.metadata
        Table(
            "t",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("x", Integer),
        )
        metadata.create_all()
        testing.db.connect().execution_options(autocommit=True).execute(
            "alter table t alter column id type varchar(50)"
        )
        m2 = MetaData(testing.db)
        t2 = Table("t", m2, autoload=True)
        eq_(t2.c.id.autoincrement, False)
        eq_(t2.c.x.autoincrement, False)

    @testing.provide_metadata

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_renamed_pk_reflection(self):
        metadata = self.metadata
        Table("t", metadata, Column("id", Integer, primary_key=True))
        metadata.create_all()
        testing.db.connect().execution_options(autocommit=True).execute(
            "alter table t rename id to t_id"
        )
        m2 = MetaData(testing.db)
        t2 = Table("t", m2, autoload=True)
        eq_([c.name for c in t2.primary_key], ["t_id"])

    @testing.provide_metadata

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_inspect_enums(self):
        enum_type = postgresql.ENUM(
            "cat", "dog", "rat", name="pet", metadata=self.metadata
        )
        enum_type.create(testing.db)
        inspector = reflection.Inspector.from_engine(testing.db)
        eq_(
            inspector.get_enums(),
            [
                {
                    "visible": True,
                    "labels": ["cat", "dog", "rat"],
                    "name": "pet",
                    "schema": "public",
                }
            ],
        )

    @testing.provide_metadata

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_inspect_enum_empty(self):
        enum_type = postgresql.ENUM(name="empty", metadata=self.metadata)
        enum_type.create(testing.db)
        inspector = reflection.Inspector.from_engine(testing.db)

        eq_(
            inspector.get_enums(),
            [
                {
                    "visible": True,
                    "labels": [],
                    "name": "empty",
                    "schema": "public",
                }
            ],
        )

    @testing.provide_metadata

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_inspect_enum_empty_from_table(self):
        Table(
            "t", self.metadata, Column("x", postgresql.ENUM(name="empty"))
        ).create(testing.db)

        t = Table("t", MetaData(testing.db), autoload_with=testing.db)
        eq_(t.c.x.type.enums, [])

    @testing.provide_metadata

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def _assert_reflected(self, dialect):
        for sch, args in [
            ("my_custom_type", (None, None)),
            ("my_custom_type()", (None, None)),
            ("my_custom_type(ARG1)", ("ARG1", None)),
            ("my_custom_type(ARG1, ARG2)", ("ARG1", "ARG2")),
        ]:
            column_info = dialect._get_column_info(
                "colname", sch, None, False, {}, {}, "public", None, ""
            )
            assert isinstance(column_info["type"], self.CustomType)
            eq_(column_info["type"].arg1, args[0])
            eq_(column_info["type"].arg2, args[1])

    def test_clslevel(self):

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def _test_interval_symbol(self, sym):
        t = Table(
            "i_test",
            self.metadata,
            Column("id", Integer, primary_key=True),
            Column("data1", INTERVAL(fields=sym)),
        )
        t.create(testing.db)

        columns = {
            rec["name"]: rec
            for rec in inspect(testing.db).get_columns("i_test")
        }
        assert isinstance(columns["data1"]["type"], INTERVAL)
        eq_(columns["data1"]["type"].fields, sym.lower())
        eq_(columns["data1"]["type"].precision, None)

    @testing.provide_metadata

3 Source : test_reflection.py
with Apache License 2.0
from gethue

    def test_interval_precision(self):
        t = Table(
            "i_test",
            self.metadata,
            Column("id", Integer, primary_key=True),
            Column("data1", INTERVAL(precision=6)),
        )
        t.create(testing.db)

        columns = {
            rec["name"]: rec
            for rec in inspect(testing.db).get_columns("i_test")
        }
        assert isinstance(columns["data1"]["type"], INTERVAL)
        eq_(columns["data1"]["type"].fields, None)
        eq_(columns["data1"]["type"].precision, 6)

3 Source : test_types.py
with Apache License 2.0
from gethue

    def test_arrays_pg(self):
        metadata = self.metadata
        t1 = Table(
            "t",
            metadata,
            Column("x", postgresql.ARRAY(Float)),
            Column("y", postgresql.ARRAY(REAL)),
            Column("z", postgresql.ARRAY(postgresql.DOUBLE_PRECISION)),
            Column("q", postgresql.ARRAY(Numeric)),
        )
        metadata.create_all()
        t1.insert().execute(x=[5], y=[5], z=[6], q=[decimal.Decimal("6.4")])
        row = t1.select().execute().first()
        eq_(row, ([5], [5], [6], [decimal.Decimal("6.4")]))

    @testing.fails_on(

3 Source : test_types.py
with Apache License 2.0
from gethue

    def test_arrays_base(self):
        metadata = self.metadata
        t1 = Table(
            "t",
            metadata,
            Column("x", sqltypes.ARRAY(Float)),
            Column("y", sqltypes.ARRAY(REAL)),
            Column("z", sqltypes.ARRAY(postgresql.DOUBLE_PRECISION)),
            Column("q", sqltypes.ARRAY(Numeric)),
        )
        metadata.create_all()
        t1.insert().execute(x=[5], y=[5], z=[6], q=[decimal.Decimal("6.4")])
        row = t1.select().execute().first()
        eq_(row, ([5], [5], [6], [decimal.Decimal("6.4")]))


class EnumTest(fixtures.TestBase, AssertsExecutionResults):

3 Source : test_types.py
with Apache License 2.0
from gethue

    def test_cast_path(self):
        eq_(
            self._scalar(cast("pg_catalog.pg_class", postgresql.REGCLASS)),
            "pg_class",
        )

    def test_cast_oid(self):

3 Source : test_types.py
with Apache License 2.0
from gethue

    def test_cast_oid(self):
        regclass = cast("pg_class", postgresql.REGCLASS)
        oid = self._scalar(cast(regclass, postgresql.OID))
        assert isinstance(oid, int)
        eq_(self._scalar(cast(oid, postgresql.REGCLASS)), "pg_class")

    def test_cast_whereclause(self):

3 Source : test_types.py
with Apache License 2.0
from gethue

    def test_array_index_map_dimensions(self):
        col = column("x", postgresql.ARRAY(Integer, dimensions=3))
        is_(col[5].type._type_affinity, ARRAY)
        assert isinstance(col[5].type, postgresql.ARRAY)
        eq_(col[5].type.dimensions, 2)
        is_(col[5][6].type._type_affinity, ARRAY)
        assert isinstance(col[5][6].type, postgresql.ARRAY)
        eq_(col[5][6].type.dimensions, 1)
        is_(col[5][6][7].type._type_affinity, Integer)

    def test_array_getitem_single_type(self):

3 Source : test_types.py
with Apache License 2.0
from gethue

    def test_array_agg(self):
        values_table = Table("values", self.metadata, Column("value", Integer))
        self.metadata.create_all(testing.db)
        testing.db.execute(
            values_table.insert(), [{"value": i} for i in range(1, 10)]
        )

        stmt = select([func.array_agg(values_table.c.value)])
        eq_(testing.db.execute(stmt).scalar(), list(range(1, 10)))

        stmt = select([func.array_agg(values_table.c.value)[3]])
        eq_(testing.db.execute(stmt).scalar(), 3)

        stmt = select([func.array_agg(values_table.c.value)[2:4]])
        eq_(testing.db.execute(stmt).scalar(), [2, 3, 4])

    def test_array_index_slice_exprs(self):

3 Source : test_types.py
with Apache License 2.0
from gethue

    def test_any_all_exprs_array(self):
        stmt = select(
            [
                3
                == any_(
                    func.array_cat(
                        array([1, 2, 3]),
                        array([4, 5, 6]),
                        type_=self.ARRAY(Integer),
                    )
                )
            ]
        )
        eq_(testing.db.execute(stmt).scalar(), True)

    def test_insert_array(self):

See More Examples