def test_fk_cant_drop_cycled_unnamed(self): metadata = MetaData() Table("a", metadata, Column('id', Integer, primary_key=True), Column('bid', Integer), ForeignKeyConstraint(["bid"], ["b.id"])) Table("b", metadata, Column('id', Integer, primary_key=True), Column("aid", Integer), ForeignKeyConstraint(["aid"], ["a.id"])) metadata.create_all(testing.db) if testing.db.dialect.supports_alter: assert_raises_message( exc.CircularDependencyError, "Can't sort tables for DROP; an unresolvable foreign key " "dependency exists between tables: a, b. Please ensure " "that the ForeignKey and ForeignKeyConstraint objects " "involved in the cycle have names so that they can be " "dropped using DROP CONSTRAINT.", metadata.drop_all, testing.db) else: with expect_warnings("Can't sort tables for DROP; an unresolvable " "foreign key dependency "): with self.sql_execution_asserter() as asserter: metadata.drop_all(testing.db, checkfirst=False) asserter.assert_( AllOf(CompiledSQL("DROP TABLE a"), CompiledSQL("DROP TABLE b")))
def test_fk_cant_drop_cycled_unnamed(self): metadata = MetaData() Table( "a", metadata, Column("id", Integer, primary_key=True), Column("bid", Integer), ForeignKeyConstraint(["bid"], ["b.id"]), ) Table( "b", metadata, Column("id", Integer, primary_key=True), Column("aid", Integer), ForeignKeyConstraint(["aid"], ["a.id"]), ) metadata.create_all(testing.db) if testing.db.dialect.supports_alter: assert_raises_message( exc.CircularDependencyError, "Can't sort tables for DROP; an unresolvable foreign key " "dependency exists between tables: a, b. Please ensure " "that the ForeignKey and ForeignKeyConstraint objects " "involved in the cycle have names so that they can be " "dropped using DROP CONSTRAINT.", metadata.drop_all, testing.db, ) else: with expect_warnings("Can't sort tables for DROP; an unresolvable " "foreign key dependency "): with self.sql_execution_asserter() as asserter: metadata.drop_all(testing.db, checkfirst=False) asserter.assert_(AllOf(CompiledSQL("DROP TABLE a"), CompiledSQL("DROP TABLE b")))
def _assert_sql(self, element, legacy_sql, modern_sql=None): dialect = mssql.dialect() with assertions.expect_warnings( "legacy_schema_aliasing flag is defaulted to True.*"): self.assert_compile(element, legacy_sql, dialect=dialect) dialect = mssql.dialect(legacy_schema_aliasing=False) self.assert_compile(element, modern_sql or "foob", dialect=dialect)
def _assert_cyclic_constraint_no_alter(self, metadata, auto=False, sqlite_warning=False): table_assertions = [] if auto: table_assertions.append( DialectSQL("CREATE TABLE b (" "id INTEGER NOT NULL, " "aid INTEGER, " "PRIMARY KEY (id), " "CONSTRAINT bfk FOREIGN KEY(aid) REFERENCES a (id)" ")")) table_assertions.append( DialectSQL("CREATE TABLE a (" "id INTEGER NOT NULL, " "bid INTEGER, " "PRIMARY KEY (id), " "FOREIGN KEY(bid) REFERENCES b (id)" ")")) else: table_assertions.append( DialectSQL("CREATE TABLE b (" "id INTEGER NOT NULL, " "aid INTEGER, " "PRIMARY KEY (id), " "CONSTRAINT bfk FOREIGN KEY(aid) REFERENCES a (id)" ")")) table_assertions.append( DialectSQL("CREATE TABLE a (" "id INTEGER NOT NULL, " "bid INTEGER, " "PRIMARY KEY (id), " "FOREIGN KEY(bid) REFERENCES b (id)" ")")) assertions = [AllOf(*table_assertions)] with self.sql_execution_asserter() as asserter: metadata.create_all(testing.db, checkfirst=False) asserter.assert_(*assertions) assertions = [ AllOf(CompiledSQL("DROP TABLE a"), CompiledSQL("DROP TABLE b")) ] if sqlite_warning: with expect_warnings("Can't sort tables for DROP; "): with self.sql_execution_asserter() as asserter: metadata.drop_all(testing.db, checkfirst=False), else: with self.sql_execution_asserter() as asserter: metadata.drop_all(testing.db, checkfirst=False), asserter.assert_(*assertions)
def test_more_cols_than_sql(self): c1, c2, c3, c4 = column('q'), column('p'), column('r'), column('d') stmt = text("select a, b from text1").columns(c1, c2, c3, c4) with assertions.expect_warnings( r"Number of columns in textual SQL \(4\) is " "smaller than number of columns requested \(2\)"): result = testing.db.execute(stmt) row = result.first() eq_(row[c2], "b1") assert_raises_message(exc.NoSuchColumnError, "in row for column 'r'", lambda: row[c3])
def _assert_sql(self, element, legacy_sql, modern_sql=None): dialect = mssql.dialect() with assertions.expect_warnings( "legacy_schema_aliasing flag is defaulted to True.*"): self.assert_compile( element, legacy_sql, dialect=dialect ) dialect = mssql.dialect(legacy_schema_aliasing=False) self.assert_compile( element, modern_sql or "foob", dialect=dialect )
def test_more_cols_than_sql(self): c1, c2, c3, c4 = column('q'), column('p'), column('r'), column('d') stmt = text("select a, b from text1").columns(c1, c2, c3, c4) with assertions.expect_warnings( r"Number of columns in textual SQL \(4\) is " "smaller than number of columns requested \(2\)"): result = testing.db.execute(stmt) row = result.first() eq_(row[c2], "b1") assert_raises_message( exc.NoSuchColumnError, "in row for column 'r'", lambda: row[c3] )
def test_same_module_same_name(self): base = registry() f1 = MockClass(base, "foo.bar.Foo") f2 = MockClass(base, "foo.bar.Foo") clsregistry.add_class("Foo", f1, base._class_registry) gc_collect() with expect_warnings( "This declarative base already contains a class with the " "same class name and module name as foo.bar.Foo, and " "will be replaced in the string-lookup table."): clsregistry.add_class( "Foo", f2, base._class_registry, )
def test_cycle_unnamed_fks(self): metadata = MetaData(testing.db) Table("a", metadata, Column("id", Integer, primary_key=True), Column("bid", Integer, ForeignKey("b.id"))) Table("b", metadata, Column("id", Integer, primary_key=True), Column("aid", Integer, ForeignKey("a.id"))) assertions = [ AllOf( CompiledSQL("CREATE TABLE b (" "id INTEGER NOT NULL, " "aid INTEGER, " "PRIMARY KEY (id)" ")"), CompiledSQL("CREATE TABLE a (" "id INTEGER NOT NULL, " "bid INTEGER, " "PRIMARY KEY (id)" ")"), ), AllOf( CompiledSQL("ALTER TABLE b ADD " "FOREIGN KEY(aid) REFERENCES a (id)"), CompiledSQL("ALTER TABLE a ADD " "FOREIGN KEY(bid) REFERENCES b (id)"), ), ] with self.sql_execution_asserter() as asserter: metadata.create_all(checkfirst=False) if testing.db.dialect.supports_alter: asserter.assert_(*assertions) assert_raises_message( exc.CircularDependencyError, "Can't sort tables for DROP; an unresolvable foreign key " "dependency exists between tables: a, b. " "Please ensure that the " "ForeignKey and ForeignKeyConstraint objects involved in the " "cycle have names so that they can be dropped using " "DROP CONSTRAINT.", metadata.drop_all, checkfirst=False, ) else: with expect_warnings( "Can't sort tables for DROP; an unresolvable " "foreign key dependency exists between tables" ): with self.sql_execution_asserter() as asserter: metadata.drop_all(checkfirst=False) asserter.assert_(AllOf(CompiledSQL("DROP TABLE b"), CompiledSQL("DROP TABLE a")))
def test_replace_function_case_insensitive(self): 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) with 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' assert isinstance(func.Replaceable_Func().type, DateTime) assert isinstance(func.RePlAcEaBlE_fUnC().type, DateTime) assert isinstance(func.replaceable_func().type, DateTime)
def test_replace_function_case_insensitive(self): 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) with 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' assert isinstance(func.Replaceable_Func().type, DateTime) assert isinstance(func.RePlAcEaBlE_fUnC().type, DateTime) assert isinstance(func.replaceable_func().type, DateTime)
def test_cycle_unnamed_fks(self): metadata = MetaData() Table( "a", metadata, Column("id", Integer, primary_key=True), Column("bid", Integer, ForeignKey("b.id")), ) Table( "b", metadata, Column("id", Integer, primary_key=True), Column("aid", Integer, ForeignKey("a.id")), ) assertions = [ AllOf( CompiledSQL("CREATE TABLE b (" "id INTEGER NOT NULL, " "aid INTEGER, " "PRIMARY KEY (id)" ")"), CompiledSQL("CREATE TABLE a (" "id INTEGER NOT NULL, " "bid INTEGER, " "PRIMARY KEY (id)" ")"), ), AllOf( CompiledSQL("ALTER TABLE b ADD " "FOREIGN KEY(aid) REFERENCES a (id)"), CompiledSQL("ALTER TABLE a ADD " "FOREIGN KEY(bid) REFERENCES b (id)"), ), ] with self.sql_execution_asserter() as asserter: metadata.create_all(testing.db, checkfirst=False) if testing.db.dialect.supports_alter: asserter.assert_(*assertions) assert_raises_message( exc.CircularDependencyError, "Can't sort tables for DROP; an unresolvable foreign key " "dependency exists between tables: a, b. " "Please ensure that the " "ForeignKey and ForeignKeyConstraint objects involved in the " "cycle have names so that they can be dropped using " "DROP CONSTRAINT.", metadata.drop_all, testing.db, checkfirst=False, ) else: with expect_warnings( "Can't sort tables for DROP; an unresolvable " "foreign key dependency exists between tables"): with self.sql_execution_asserter() as asserter: metadata.drop_all(testing.db, checkfirst=False) asserter.assert_( AllOf(CompiledSQL("DROP TABLE b"), CompiledSQL("DROP TABLE a")))
def _assert_cyclic_constraint_no_alter( self, metadata, auto=False, sqlite_warning=False): table_assertions = [] if auto: table_assertions.append( DialectSQL( 'CREATE TABLE b (' 'id INTEGER NOT NULL, ' 'aid INTEGER, ' 'PRIMARY KEY (id), ' 'CONSTRAINT bfk FOREIGN KEY(aid) REFERENCES a (id)' ')' ) ) table_assertions.append( DialectSQL( 'CREATE TABLE a (' 'id INTEGER NOT NULL, ' 'bid INTEGER, ' 'PRIMARY KEY (id), ' 'FOREIGN KEY(bid) REFERENCES b (id)' ')' ) ) else: table_assertions.append( DialectSQL( 'CREATE TABLE b (' 'id INTEGER NOT NULL, ' 'aid INTEGER, ' 'PRIMARY KEY (id), ' 'CONSTRAINT bfk FOREIGN KEY(aid) REFERENCES a (id)' ')' ) ) table_assertions.append( DialectSQL( 'CREATE TABLE a (' 'id INTEGER NOT NULL, ' 'bid INTEGER, ' 'PRIMARY KEY (id), ' 'FOREIGN KEY(bid) REFERENCES b (id)' ')' ) ) assertions = [AllOf(*table_assertions)] with self.sql_execution_asserter() as asserter: metadata.create_all(checkfirst=False) asserter.assert_(*assertions) assertions = [AllOf( CompiledSQL("DROP TABLE a"), CompiledSQL("DROP TABLE b") )] if sqlite_warning: with expect_warnings("Can't sort tables for DROP; "): with self.sql_execution_asserter() as asserter: metadata.drop_all(checkfirst=False), else: with self.sql_execution_asserter() as asserter: metadata.drop_all(checkfirst=False), asserter.assert_(*assertions)