def test_deprecated_append_ddl_listener_table(self): metadata, users, engine = self.metadata, self.users, self.engine canary = [] with testing.expect_deprecated(".* is deprecated .*"): users.append_ddl_listener( "before-create", lambda e, t, b: canary.append("mxyzptlk")) with testing.expect_deprecated(".* is deprecated .*"): users.append_ddl_listener( "after-create", lambda e, t, b: canary.append("klptzyxm")) with testing.expect_deprecated(".* is deprecated .*"): users.append_ddl_listener("before-drop", lambda e, t, b: canary.append("xyzzy")) with testing.expect_deprecated(".* is deprecated .*"): users.append_ddl_listener("after-drop", lambda e, t, b: canary.append("fnord")) metadata.create_all() assert "mxyzptlk" in canary assert "klptzyxm" in canary assert "xyzzy" not in canary assert "fnord" not in canary del engine.mock[:] canary[:] = [] metadata.drop_all() assert "mxyzptlk" not in canary assert "klptzyxm" not in canary assert "xyzzy" in canary assert "fnord" in canary
def test_enum_parse(self): with testing.expect_deprecated("Manually quoting ENUM value literals"): enum_table = Table( "mysql_enum", self.metadata, Column("e1", mysql.ENUM("'a'")), Column("e2", mysql.ENUM("''")), Column("e3", mysql.ENUM("a")), Column("e4", mysql.ENUM("")), Column("e5", mysql.ENUM("'a'", "''")), Column("e6", mysql.ENUM("''", "'a'")), Column("e7", mysql.ENUM("''", "'''a'''", "'b''b'", "''''")), ) for col in enum_table.c: self.assert_(repr(col)) enum_table.create() reflected = Table("mysql_enum", MetaData(testing.db), autoload=True) for t in enum_table, reflected: eq_(t.c.e1.type.enums, ["a"]) eq_(t.c.e2.type.enums, [""]) eq_(t.c.e3.type.enums, ["a"]) eq_(t.c.e4.type.enums, [""]) eq_(t.c.e5.type.enums, ["a", ""]) eq_(t.c.e6.type.enums, ["", "a"]) eq_(t.c.e7.type.enums, ["", "'a'", "b'b", "'"])
def test_threaded_deprecated_at_dialect_level(self): with testing.expect_deprecated( "The 'threaded' parameter to the cx_oracle dialect"): dialect = cx_oracle.dialect(threaded=False) arg, kw = dialect.create_connect_args( url.make_url("oracle+cx_oracle://scott:tiger@dsn")) eq_(kw["threaded"], False)
def test_legacy_typemap(self): table1 = table( "mytable", column("myid", Integer), column("name", String), column("description", String), ) with testing.expect_deprecated( "The text.typemap parameter is deprecated"): t = text( "select id, name from user", typemap=dict(id=Integer, name=String), ) stmt = select([table1.c.myid ]).select_from(table1.join(t, table1.c.myid == t.c.id)) compiled = stmt.compile() eq_( compiled._create_result_map(), { "myid": ( "myid", (table1.c.myid, "myid", "myid"), table1.c.myid.type, ) }, )
def test_deprecated_dialect_name_still_loads(self): dialects.registry.clear() with expect_deprecated( "The 'postgres' dialect name " "has been renamed to 'postgresql'" ): dialect = url.URL("postgres").get_dialect() is_(dialect, postgresql.dialect)
def test_filter_deprecated(self): cx = self.engine tbl = Table("t", MetaData(), Column("id", Integer)) target = cx.name assert DDL("")._should_execute_deprecated("x", tbl, cx) with testing.expect_deprecated(".* is deprecated .*"): assert DDL("", on=target)._should_execute_deprecated("x", tbl, cx) with testing.expect_deprecated(".* is deprecated .*"): assert not DDL("", on="bogus")._should_execute_deprecated( "x", tbl, cx) with testing.expect_deprecated(".* is deprecated .*"): assert DDL("", on=lambda d, x, y, z: True)._should_execute_deprecated( "x", tbl, cx) with testing.expect_deprecated(".* is deprecated .*"): assert DDL("", on=lambda d, x, y, z: z.engine.name != "bogus" )._should_execute_deprecated("x", tbl, cx)
def setup_mappers(cls): foo = cls.tables.foo mapper(Foo, foo) with testing.expect_deprecated( "The mapper.non_primary parameter is deprecated"): mapper(Foo, foo, non_primary=True, properties={"foo_bar": foo.c.data})
def test_append_listener(self): metadata, table = self.metadata, self.table def fn(*a): return None with testing.expect_deprecated(".* is deprecated .*"): table.append_ddl_listener("before-create", fn) with testing.expect_deprecated(".* is deprecated .*"): assert_raises(exc.InvalidRequestError, table.append_ddl_listener, "blah", fn) with testing.expect_deprecated(".* is deprecated .*"): metadata.append_ddl_listener("before-create", fn) with testing.expect_deprecated(".* is deprecated .*"): assert_raises( exc.InvalidRequestError, metadata.append_ddl_listener, "blah", fn, )
def test_ident_preparer_force(self): preparer = testing.db.dialect.identifier_preparer preparer.quote("hi") with testing.expect_deprecated( "The IdentifierPreparer.quote.force parameter is deprecated"): preparer.quote("hi", True) with testing.expect_deprecated( "The IdentifierPreparer.quote.force parameter is deprecated"): preparer.quote("hi", False) preparer.quote_schema("hi") with testing.expect_deprecated( "The IdentifierPreparer.quote_schema.force parameter is deprecated" ): preparer.quote_schema("hi", True) with testing.expect_deprecated( "The IdentifierPreparer.quote_schema.force parameter is deprecated" ): preparer.quote_schema("hi", True)
def test_unknown_mode(self): t = table("t", column("c")) with testing.expect_deprecated( "The select.for_update parameter is deprecated and " "will be removed in a future release."): assert_raises_message( exc.ArgumentError, "Unknown for_update argument: 'unknown_mode'", t.select, t.c.c == 7, for_update="unknown_mode", )
def _test_dialect_param_from_url(self, url_string, key, value): import cx_Oracle url_obj = url.make_url(url_string) dialect = cx_oracle.dialect(dbapi=cx_Oracle) with testing.expect_deprecated("cx_oracle dialect option %r should" % key): arg, kw = dialect.create_connect_args(url_obj) eq_(getattr(dialect, key), value) # test setting it on the dialect normally dialect = cx_oracle.dialect(dbapi=cx_Oracle, **{key: value}) eq_(getattr(dialect, key), value)
def test_sequence_ignore_nullability(self): metadata = MetaData() tbl = Table( "test", metadata, Column("id", Integer, Sequence("", start=5), nullable=True), ) with testing.expect_deprecated( "Use of Sequence with SQL Server in order to affect "): self.assert_compile( schema.CreateTable(tbl), "CREATE TABLE test (id INTEGER NOT NULL IDENTITY(5,1))", )
def test_sequence_start_0(self): metadata = MetaData() tbl = Table( "test", metadata, Column("id", Integer, Sequence("", 0), primary_key=True), ) with testing.expect_deprecated( "Use of Sequence with SQL Server in order to affect "): self.assert_compile( schema.CreateTable(tbl), "CREATE TABLE test (id INTEGER NOT NULL IDENTITY(0,1), " "PRIMARY KEY (id))", )
def _assert_legacy(self, leg, read=False, nowait=False): t = table("t", column("c")) with testing.expect_deprecated( "The select.for_update parameter is deprecated and " "will be removed in a future release."): s1 = select([t], for_update=leg) if leg is False: assert s1._for_update_arg is None assert s1.for_update is None else: eq_(s1._for_update_arg.read, read) eq_(s1._for_update_arg.nowait, nowait) eq_(s1.for_update, leg)
def test_table_useexisting(self): meta = self.metadata Table("t", meta, Column("x", Integer)) meta.create_all() with testing.expect_deprecated( "The Table.useexisting parameter is deprecated and " "will be removed in a future release."): Table("t", meta, useexisting=True, autoload_with=testing.db) with testing.expect_deprecated( "The Table.useexisting parameter is deprecated and " "will be removed in a future release."): assert_raises_message( exc.ArgumentError, "useexisting is synonymous with extend_existing.", Table, "t", meta, useexisting=True, extend_existing=True, autoload_with=testing.db, )
def test_join_condition_ignore_nonexistent_tables(self): m = MetaData() t1 = Table("t1", m, Column("id", Integer)) t2 = Table("t2", m, Column("id", Integer), Column("t1id", ForeignKey("t1.id"))) with testing.expect_deprecated( "The join_condition.ignore_nonexistent_tables " "parameter is deprecated"): join_cond = sql_util.join_condition(t1, t2, ignore_nonexistent_tables=True) t1t2 = t1.join(t2) assert t1t2.onclause.compare(join_cond)
def _set_fixture_one(self): with testing.expect_deprecated("Manually quoting SET value literals"): e1, e2 = mysql.SET("'a'", "'b'"), mysql.SET("'a'", "'b'") e4 = mysql.SET("'a'", "b") e5 = mysql.SET("'a'", "'b'", quoting="quoted") set_table = Table( "mysql_set", self.metadata, Column("e1", e1), Column("e2", e2, nullable=False), Column("e3", mysql.SET("a", "b")), Column("e4", e4), Column("e5", e5), ) return set_table
def test_legacy_bindparam(self): with testing.expect_deprecated( "The text.bindparams parameter is deprecated"): t = text( "select * from foo where lala=:bar and hoho=:whee", bindparams=[bindparam("bar", 4), bindparam("whee", 7)], ) self.assert_compile( t, "select * from foo where lala=:bar and hoho=:whee", checkparams={ "bar": 4, "whee": 7 }, )
def test_unicode_warnings_dialectlevel(self): unicodedata = self.data with testing.expect_deprecated( "The create_engine.convert_unicode parameter and " "corresponding dialect-level"): dialect = default.DefaultDialect(convert_unicode=True) dialect.supports_unicode_binds = False s = String() uni = s.dialect_impl(dialect).bind_processor(dialect) uni(util.b("x")) assert isinstance(uni(unicodedata), util.binary_type) eq_(uni(unicodedata), unicodedata.encode("utf-8"))
def test_case_sensitive(self): reg = functions._registry["_default"] cs_reg = functions._case_sensitive_registry["_default"] class MYFUNC(GenericFunction): type = DateTime assert isinstance(func.MYFUNC().type, DateTime) assert isinstance(func.MyFunc().type, DateTime) assert isinstance(func.mYfUnC().type, DateTime) assert isinstance(func.myfunc().type, DateTime) in_("myfunc", reg) not_in("MYFUNC", reg) not_in("MyFunc", reg) in_("myfunc", cs_reg) eq_(set(cs_reg["myfunc"].keys()), set(["MYFUNC"])) with testing.expect_deprecated( "GenericFunction 'MyFunc' is already registered with" " different letter case, so the previously registered function " "'MYFUNC' is switched into case-sensitive mode. " "GenericFunction objects will be fully case-insensitive in a " "future release.", regex=False, ): class MyFunc(GenericFunction): type = Integer assert isinstance(func.MYFUNC().type, DateTime) assert isinstance(func.MyFunc().type, Integer) with pytest.raises(AssertionError): assert isinstance(func.mYfUnC().type, Integer) with pytest.raises(AssertionError): assert isinstance(func.myfunc().type, Integer) eq_(reg["myfunc"], functions._CASE_SENSITIVE) not_in("MYFUNC", reg) not_in("MyFunc", reg) in_("myfunc", cs_reg) eq_(set(cs_reg["myfunc"].keys()), set(["MYFUNC", "MyFunc"]))
def test_ignoring_unicode_error(self): """checks String(unicode_error='ignore') is passed to underlying codec.""" unicodedata = self.data with testing.expect_deprecated( "The String.convert_unicode parameter is deprecated and " "will be removed in a future release.", "The String.unicode_errors parameter is deprecated and " "will be removed in a future release.", ): type_ = String(248, convert_unicode="force", unicode_error="ignore") dialect = default.DefaultDialect(encoding="ascii") proc = type_.result_processor(dialect, 10) utfdata = unicodedata.encode("utf8") eq_(proc(utfdata), unicodedata.encode("ascii", "ignore").decode())
def test_set_parse(self): with testing.expect_deprecated("Manually quoting SET value literals"): set_table = Table( "mysql_set", self.metadata, Column("e1", mysql.SET("'a'")), Column("e2", mysql.SET("''", retrieve_as_bitwise=True)), Column("e3", mysql.SET("a")), Column("e4", mysql.SET("", retrieve_as_bitwise=True)), Column("e5", mysql.SET("'a'", "''", retrieve_as_bitwise=True)), Column("e6", mysql.SET("''", "'a'", retrieve_as_bitwise=True)), Column( "e7", mysql.SET( "''", "'''a'''", "'b''b'", "''''", retrieve_as_bitwise=True, ), ), ) for col in set_table.c: self.assert_(repr(col)) set_table.create() # don't want any warnings on reflection reflected = Table("mysql_set", MetaData(testing.db), autoload=True) for t in set_table, reflected: eq_(t.c.e1.type.values, ("a", )) eq_(t.c.e2.type.values, ("", )) eq_(t.c.e3.type.values, ("a", )) eq_(t.c.e4.type.values, ("", )) eq_(t.c.e5.type.values, ("a", "")) eq_(t.c.e6.type.values, ("", "a")) eq_(t.c.e7.type.values, ("", "'a'", "b'b", "'"))
def expect_deprecated_opts(self): return expect_deprecated( "The psycopg2 use_batch_mode flag is superseded by " "executemany_mode='batch'" )
def test_select_autocommit(self): with testing.expect_deprecated( "The select.autocommit parameter is deprecated and " "will be removed in a future release."): select([column("x")], autocommit=True)
def test_enum(self): """Exercise the ENUM type.""" with testing.expect_deprecated("Manually quoting ENUM value literals"): e1, e2 = mysql.ENUM("'a'", "'b'"), mysql.ENUM("'a'", "'b'") e3 = mysql.ENUM("'a'", "'b'", strict=True) e4 = mysql.ENUM("'a'", "'b'", strict=True) enum_table = Table( "mysql_enum", self.metadata, Column("e1", e1), Column("e2", e2, nullable=False), Column( "e2generic", Enum("a", "b", validate_strings=True), nullable=False, ), Column("e3", e3), Column("e4", e4, nullable=False), Column("e5", mysql.ENUM("a", "b")), Column("e5generic", Enum("a", "b")), Column("e6", mysql.ENUM("'a'", "b")), Column( "e7", mysql.ENUM( EnumSetTest.SomeEnum, values_callable=EnumSetTest.get_enum_string_values, ), ), Column("e8", mysql.ENUM(EnumSetTest.SomeEnum)), ) eq_(colspec(enum_table.c.e1), "e1 ENUM('a','b')") eq_(colspec(enum_table.c.e2), "e2 ENUM('a','b') NOT NULL") eq_(colspec(enum_table.c.e2generic), "e2generic ENUM('a','b') NOT NULL") eq_(colspec(enum_table.c.e3), "e3 ENUM('a','b')") eq_(colspec(enum_table.c.e4), "e4 ENUM('a','b') NOT NULL") eq_(colspec(enum_table.c.e5), "e5 ENUM('a','b')") eq_(colspec(enum_table.c.e5generic), "e5generic ENUM('a','b')") eq_(colspec(enum_table.c.e6), "e6 ENUM('''a''','b')") eq_(colspec(enum_table.c.e7), "e7 ENUM('1','2','3','a','b')") eq_( colspec(enum_table.c.e8), "e8 ENUM('one','two','three','AMember','BMember')", ) enum_table.create() assert_raises( exc.DBAPIError, enum_table.insert().execute, e1=None, e2=None, e3=None, e4=None, ) assert enum_table.c.e2generic.type.validate_strings assert_raises( exc.StatementError, enum_table.insert().execute, e1="c", e2="c", e2generic="c", e3="c", e4="c", e5="c", e5generic="c", e6="c", e7="c", e8="c", ) enum_table.insert().execute() enum_table.insert().execute( e1="a", e2="a", e2generic="a", e3="a", e4="a", e5="a", e5generic="a", e6="'a'", e7="a", e8="AMember", ) enum_table.insert().execute( e1="b", e2="b", e2generic="b", e3="b", e4="b", e5="b", e5generic="b", e6="b", e7="b", e8="BMember", ) res = enum_table.select().execute().fetchall() expected = [ (None, "a", "a", None, "a", None, None, None, None, None), ( "a", "a", "a", "a", "a", "a", "a", "'a'", EnumSetTest.SomeEnum.AMember, EnumSetTest.SomeEnum.AMember, ), ( "b", "b", "b", "b", "b", "b", "b", "b", EnumSetTest.SomeEnum.BMember, EnumSetTest.SomeEnum.BMember, ), ] eq_(res, expected)
def test_engine_convert_unicode(self): with testing.expect_deprecated( "The create_engine.convert_unicode parameter and " "corresponding dialect-level"): create_engine("mysql://", convert_unicode=True, module=mock.Mock())
def test_string_convert_unicode_force(self): with testing.expect_deprecated( "The String.convert_unicode parameter is deprecated and " "will be removed in a future release."): String(convert_unicode="force")
def test_autocommit(self): with testing.expect_deprecated( "The text.autocommit parameter is deprecated"): text("select id, name from user", autocommit=True)
def test_select_for_update(self): with testing.expect_deprecated( "The select.for_update parameter is deprecated and " "will be removed in a future release."): select([column("x")], for_update=True)
def test_replace_function_case_sensitive(self): reg = functions._registry["_default"] cs_reg = functions._case_sensitive_registry["_default"] class replaceable_func(GenericFunction): type = Integer identifier = "REPLACEABLE_FUNC" assert isinstance(func.REPLACEABLE_FUNC().type, Integer) assert isinstance(func.Replaceable_Func().type, Integer) assert isinstance(func.RePlAcEaBlE_fUnC().type, Integer) assert isinstance(func.replaceable_func().type, Integer) in_("replaceable_func", reg) not_in("REPLACEABLE_FUNC", reg) not_in("Replaceable_Func", reg) in_("replaceable_func", cs_reg) eq_(set(cs_reg["replaceable_func"].keys()), set(["REPLACEABLE_FUNC"])) with testing.expect_deprecated( "GenericFunction 'Replaceable_Func' is already registered with" " different letter case, so the previously registered function " "'REPLACEABLE_FUNC' is switched into case-sensitive mode. " "GenericFunction objects will be fully case-insensitive in a " "future release.", regex=False, ): class Replaceable_Func(GenericFunction): type = DateTime identifier = "Replaceable_Func" assert isinstance(func.REPLACEABLE_FUNC().type, Integer) assert isinstance(func.Replaceable_Func().type, DateTime) assert isinstance(func.RePlAcEaBlE_fUnC().type, NullType) assert isinstance(func.replaceable_func().type, NullType) eq_(reg["replaceable_func"], functions._CASE_SENSITIVE) not_in("REPLACEABLE_FUNC", reg) not_in("Replaceable_Func", reg) in_("replaceable_func", cs_reg) eq_( set(cs_reg["replaceable_func"].keys()), set(["REPLACEABLE_FUNC", "Replaceable_Func"]), ) with testing.expect_warnings( "The GenericFunction 'REPLACEABLE_FUNC' is already registered and " "is going to be overriden.", regex=False, ): class replaceable_func_override(GenericFunction): type = DateTime identifier = "REPLACEABLE_FUNC" with testing.expect_deprecated( "GenericFunction(s) '['REPLACEABLE_FUNC', 'Replaceable_Func']' " "are already registered with different letter cases and might " "interact with 'replaceable_func'. GenericFunction objects will " "be fully case-insensitive in a future release.", regex=False, ): class replaceable_func_lowercase(GenericFunction): type = String identifier = "replaceable_func" with testing.expect_warnings( "The GenericFunction 'Replaceable_Func' is already registered and " "is going to be overriden.", regex=False, ): class Replaceable_Func_override(GenericFunction): type = Integer identifier = "Replaceable_Func" assert isinstance(func.REPLACEABLE_FUNC().type, DateTime) assert isinstance(func.Replaceable_Func().type, Integer) assert isinstance(func.RePlAcEaBlE_fUnC().type, NullType) assert isinstance(func.replaceable_func().type, String) eq_(reg["replaceable_func"], functions._CASE_SENSITIVE) not_in("REPLACEABLE_FUNC", reg) not_in("Replaceable_Func", reg) in_("replaceable_func", cs_reg) eq_( set(cs_reg["replaceable_func"].keys()), set(["REPLACEABLE_FUNC", "Replaceable_Func", "replaceable_func"]), )