def test_compare_clauselist_assoc_different_operator(self): l1 = and_(self.a.c.x == self.b.c.y, self.a.c.y == self.b.c.z) l2 = or_(self.a.c.y == self.b.c.z, self.a.c.x == self.b.c.y) is_false(l1.compare(l2))
def test_compare_binds(self): b1 = bindparam("foo", type_=Integer()) b2 = bindparam("foo", type_=Integer()) b3 = bindparam("bar", type_=Integer()) b4 = bindparam("foo", type_=String()) def c1(): return 5 def c2(): return 6 b5 = bindparam("foo", type_=Integer(), callable_=c1) b6 = bindparam("foo", type_=Integer(), callable_=c2) b7 = bindparam("foo", type_=Integer(), callable_=c1) b8 = bindparam("foo", type_=Integer, value=5) b9 = bindparam("foo", type_=Integer, value=6) is_false(b1.compare(b5)) is_true(b5.compare(b7)) is_false(b5.compare(b6)) is_true(b1.compare(b2)) # currently not comparing "key", as we often have to compare # anonymous names. however we should really check for that is_true(b1.compare(b3)) is_false(b1.compare(b4)) is_false(b1.compare(b8)) is_false(b8.compare(b9)) is_true(b8.compare(b8))
def test_compare_clauselist_assoc_different_operator(self): l1 = and_(table_c.c.x == table_d.c.y, table_c.c.y == table_d.c.z) l2 = or_(table_c.c.y == table_d.c.z, table_c.c.x == table_d.c.y) is_false(l1.compare(l2))
def test_compare_col_identity(self): stmt1 = ( select([table_a.c.a, table_b.c.b]) .where(table_a.c.a == table_b.c.b) .alias() ) stmt1_c = ( select([table_a.c.a, table_b.c.b]) .where(table_a.c.a == table_b.c.b) .alias() ) stmt2 = union(select([table_a]), select([table_b])) stmt3 = select([table_b]) equivalents = {table_a.c.a: [table_b.c.a]} is_false( stmt1.compare(stmt2, use_proxies=True, equivalents=equivalents) ) is_true( stmt1.compare(stmt1_c, use_proxies=True, equivalents=equivalents) ) is_true( (table_a.c.a == table_b.c.b).compare( stmt1.c.a == stmt1.c.b, use_proxies=True, equivalents=equivalents, ) )
def test_anno_w_fixed_table(self, decl_base): users = Table( "users", decl_base.metadata, Column("id", Integer, primary_key=True), Column("name", String(50), nullable=False), Column("data", String(50)), Column("x", Integer), Column("y", Integer), Column("created_at", DateTime), ) class User(decl_base): __table__ = users id: Mapped[int] name: Mapped[str] data: Mapped[Optional[str]] x: Mapped[int] y: Mapped[int] created_at: Mapped[datetime.datetime] self.assert_compile( select(User), "SELECT users.id, users.name, users.data, users.x, " "users.y, users.created_at FROM users", ) eq_(User.__mapper__.primary_key, (User.__table__.c.id,)) is_false(User.__table__.c.id.nullable) is_false(User.__table__.c.name.nullable) is_true(User.__table__.c.data.nullable) assert isinstance(User.__table__.c.created_at.type, DateTime)
def test_bind_create_drop_constructor_bound(self): for bind in (testing.db, testing.db.connect()): if isinstance(bind, engine.Connection): bind.begin() try: for args in (([bind], {}), ([], {"bind": bind})): with testing.expect_deprecated_20( "The MetaData.bind argument is deprecated "): metadata = MetaData(*args[0], **args[1]) table = Table("test_table", metadata, Column("foo", Integer)) assert metadata.bind is table.bind is bind with testing.expect_deprecated_20( "The ``bind`` argument for schema methods " "that invoke SQL"): metadata.create_all() is_true(inspect(bind).has_table(table.name)) with testing.expect_deprecated_20( "The ``bind`` argument for schema methods " "that invoke SQL"): metadata.drop_all() with testing.expect_deprecated_20( "The ``bind`` argument for schema methods " "that invoke SQL"): table.create() with testing.expect_deprecated_20( "The ``bind`` argument for schema methods " "that invoke SQL"): table.drop() is_false(inspect(bind).has_table(table.name)) finally: if isinstance(bind, engine.Connection): bind.close()
def test_collection_class_uselist(self, decl_base): class A(decl_base): __tablename__ = "a" id: Mapped[int] = mapped_column(primary_key=True) data: Mapped[str] = mapped_column() bs_list: Mapped[List["B"]] = relationship(viewonly=True) bs_set: Mapped[Set["B"]] = relationship(viewonly=True) bs_list_warg: Mapped[List["B"]] = relationship("B", viewonly=True) bs_set_warg: Mapped[Set["B"]] = relationship("B", viewonly=True) class B(decl_base): __tablename__ = "b" id: Mapped[int] = mapped_column(Integer, primary_key=True) a_id: Mapped[int] = mapped_column(ForeignKey("a.id")) a: Mapped["A"] = relationship(viewonly=True) a_warg: Mapped["A"] = relationship("A", viewonly=True) is_(A.__mapper__.attrs["bs_list"].collection_class, list) is_(A.__mapper__.attrs["bs_set"].collection_class, set) is_(A.__mapper__.attrs["bs_list_warg"].collection_class, list) is_(A.__mapper__.attrs["bs_set_warg"].collection_class, set) is_true(A.__mapper__.attrs["bs_list"].uselist) is_true(A.__mapper__.attrs["bs_set"].uselist) is_true(A.__mapper__.attrs["bs_list_warg"].uselist) is_true(A.__mapper__.attrs["bs_set_warg"].uselist) is_false(B.__mapper__.attrs["a"].uselist) is_false(B.__mapper__.attrs["a_warg"].uselist)
def test_compare_labels(self): for fixtures_, compare_values in [ (self.fixtures, True), (self.dont_compare_values_fixtures, False), ]: for fixture in fixtures_: case_a = fixture() case_b = fixture() for a, b in itertools.combinations_with_replacement( range(len(case_a)), 2): if a == b: is_true( case_a[a].compare( case_b[b], compare_annotations=True, compare_values=compare_values, ), "%r != %r" % (case_a[a], case_b[b]), ) else: is_false( case_a[a].compare( case_b[b], compare_annotations=True, compare_values=compare_values, ), "%r == %r" % (case_a[a], case_b[b]), )
def test_compare_col_identity(self): stmt1 = ( select([table_a.c.a, table_b.c.b]) .where(table_a.c.a == table_b.c.b) .alias() ) stmt1_c = ( select([table_a.c.a, table_b.c.b]) .where(table_a.c.a == table_b.c.b) .alias() ) stmt2 = union(select([table_a]), select([table_b])) equivalents = {table_a.c.a: [table_b.c.a]} is_false( stmt1.compare(stmt2, use_proxies=True, equivalents=equivalents) ) is_true( stmt1.compare(stmt1_c, use_proxies=True, equivalents=equivalents) ) is_true( (table_a.c.a == table_b.c.b).compare( stmt1.c.a == stmt1.c.b, use_proxies=True, equivalents=equivalents, ) )
def test_annotated_types_as_keys(self, decl_base: Type[DeclarativeBase]): """neat!!!""" str50 = Annotated[str, 50] str30 = Annotated[str, 30] opt_str50 = Optional[str50] opt_str30 = Optional[str30] decl_base.registry.update_type_annotation_map( {str50: String(50), str30: String(30)} ) class MyClass(decl_base): __tablename__ = "my_table" id: Mapped[str50] = mapped_column(primary_key=True) data_one: Mapped[str30] data_two: Mapped[opt_str30] data_three: Mapped[str50] data_four: Mapped[opt_str50] data_five: Mapped[str] data_six: Mapped[Optional[str]] eq_(MyClass.__table__.c.data_one.type.length, 30) is_false(MyClass.__table__.c.data_one.nullable) eq_(MyClass.__table__.c.data_two.type.length, 30) is_true(MyClass.__table__.c.data_two.nullable) eq_(MyClass.__table__.c.data_three.type.length, 50)
async def test_engine_eq_ne(self, async_engine): e2 = _async_engine.AsyncEngine(async_engine.sync_engine) e3 = testing.engines.testing_engine(asyncio=True) eq_(async_engine, e2) ne_(async_engine, e3) is_false(async_engine == None)
async def test_engine_eq_ne(self, async_engine): e2 = _async_engine.AsyncEngine(async_engine.sync_engine) e3 = engines.testing_engine(asyncio=True, transfer_staticpool=True) eq_(async_engine, e2) ne_(async_engine, e3) is_false(async_engine == None)
def test_engine_has_table(self): with testing.expect_deprecated( r"The Engine.has_table\(\) method is deprecated"): is_false(testing.db.has_table("dont_exist")) with testing.expect_deprecated( r"The Engine.has_table\(\) method is deprecated"): is_true(testing.db.has_table("user"))
def test_compare_comparison_non_commutative_inverses(self): l1 = table_c.c.x >= table_d.c.y l2 = table_d.c.y < table_c.c.x l3 = table_d.c.y <= table_c.c.x # we're not doing this kind of commutativity right now. is_false(l1.compare(l2)) is_false(l1.compare(l3))
def test_compare_comparison_associative(self): l1 = table_c.c.x == table_d.c.y l2 = table_d.c.y == table_c.c.x l3 = table_c.c.x == table_d.c.z is_true(l1.compare(l1)) is_true(l1.compare(l2)) is_false(l1.compare(l3))
def test_compare_clauselist_not_assoc_different_operator(self): l1 = ClauseList( self.a.c.x, self.a.c.y, self.b.c.y, operator=operators.sub) l2 = ClauseList( self.a.c.x, self.a.c.y, self.b.c.y, operator=operators.div) is_false(l1.compare(l2))
def test_exists(self): dont_exist = Table("dont_exist", MetaData()) with testing.expect_deprecated( r"The Table.exists\(\) method is deprecated"): is_false(dont_exist.exists(testing.db)) user = self.tables.user with testing.expect_deprecated( r"The Table.exists\(\) method is deprecated"): is_true(user.exists(testing.db))
def test_compare_clauselist_not_associative(self): l1 = ClauseList( self.a.c.x, self.a.c.y, self.b.c.y, operator=operators.sub) l2 = ClauseList( self.b.c.y, self.a.c.x, self.a.c.y, operator=operators.sub) is_true(l1.compare(l1)) is_false(l1.compare(l2))
async def test_transaction_eq_ne(self, async_engine): async with async_engine.connect() as conn: t1 = await conn.begin() t2 = _async_engine.AsyncTransaction._from_existing_transaction( conn, t1._proxied) eq_(t1, t2) is_false(t1 == None)
def test_dont_use_get_pj_is_different(self): mapper(self.classes.A, self.tables.a) m_b = mapper(self.classes.B, self.tables.b_sameorder, properties={ 'a': relationship(self.classes.A, primaryjoin=and_( self.tables.a.c.id1 == self.tables.b_sameorder.c.a_id1, self.tables.a.c.id2 == 12 )) }) configure_mappers() is_false(m_b.relationships.a.strategy.use_get)
def test_compare_clauselist_associative(self): l1 = and_(self.a.c.x == self.b.c.y, self.a.c.y == self.b.c.z) l2 = and_(self.a.c.y == self.b.c.z, self.a.c.x == self.b.c.y) l3 = and_(self.a.c.x == self.b.c.z, self.a.c.y == self.b.c.y) is_true(l1.compare(l1)) is_true(l1.compare(l2)) is_false(l1.compare(l3))
def test_compare_clauselist_associative(self): l1 = and_(table_c.c.x == table_d.c.y, table_c.c.y == table_d.c.z) l2 = and_(table_c.c.y == table_d.c.z, table_c.c.x == table_d.c.y) l3 = and_(table_c.c.x == table_d.c.z, table_c.c.y == table_d.c.y) is_true(l1.compare(l1)) is_true(l1.compare(l2)) is_false(l1.compare(l3))
def test_create_drop_explicit(self): metadata = MetaData() table = Table("test_table", metadata, Column("foo", Integer)) for bind in (testing.db, testing.db.connect()): for args in [([], {"bind": bind}), ([bind], {})]: metadata.create_all(*args[0], **args[1]) is_true(inspect(bind).has_table(table.name)) metadata.drop_all(*args[0], **args[1]) table.create(*args[0], **args[1]) table.drop(*args[0], **args[1]) is_false(inspect(bind).has_table(table.name))
async def test_transaction_eq_ne(self, async_engine): async with async_engine.connect() as conn: t1 = await conn.begin() t2 = _async_engine.AsyncTransaction._regenerate_proxy_for_target( t1._proxied) eq_(t1, t2) is_false(t1 == None)
async def test_connection_eq_ne(self, async_engine): async with async_engine.connect() as conn: c2 = _async_engine.AsyncConnection(async_engine, conn.sync_connection) eq_(conn, c2) async with async_engine.connect() as c3: ne_(conn, c3) is_false(conn == None)
def test_contains_mapping(self): keyed_tuple = self._fixture(["x", "y"], ["a", "b"])._mapping is_false("x" in keyed_tuple) is_false("z" in keyed_tuple) is_true("z" not in keyed_tuple) is_true("x" not in keyed_tuple) # we do keys is_true("a" in keyed_tuple) is_true("b" in keyed_tuple)
def test_construct_lhs(self, decl_base): class User(decl_base): __tablename__ = "users" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column() data: Mapped[Optional[str]] = mapped_column() self.assert_compile( select(User), "SELECT users.id, users.name, users.data FROM users") eq_(User.__mapper__.primary_key, (User.__table__.c.id, )) is_false(User.__table__.c.id.nullable) is_false(User.__table__.c.name.nullable) is_true(User.__table__.c.data.nullable)
def test_comparison(self): common_url = ( "dbtype://*****:*****@[2001:da8:2004:1000:202:116:160:90]:80/database?foo=bar") other_url = "dbtype://*****:*****@host/" url1 = url.make_url(common_url) url2 = url.make_url(common_url) url3 = url.make_url(other_url) is_true(url1 == url2) is_false(url1 != url2) is_true(url1 != url3) is_false(url1 == url3)
def test_compare_tables(self): is_true(table_a.compare(table_a_2)) # the "proxy" version compares schema tables on metadata identity is_false(table_a.compare(table_a_2, use_proxies=True)) # same for lower case tables since it compares lower case columns # using proxies, which makes it very unlikely to have multiple # table() objects with columns that compare equally is_false( table("a", column("x", Integer), column("q", String)).compare( table("a", column("x", Integer), column("q", String)), use_proxies=True, ))
def test_compare_tables(self): is_true(table_a.compare(table_a_2)) # the "proxy" version compares schema tables on metadata identity is_false(table_a.compare(table_a_2, use_proxies=True)) # same for lower case tables since it compares lower case columns # using proxies, which makes it very unlikely to have multiple # table() objects with columns that compare equally is_false( table("a", column("x", Integer), column("q", String)).compare( table("a", column("x", Integer), column("q", String)), use_proxies=True, ) )
def test_all_present(self): need = set( cls for cls in class_hierarchy(ClauseElement) if issubclass(cls, (ColumnElement, Selectable)) and "__init__" in cls.__dict__ and not issubclass(cls, (Annotated)) and "orm" not in cls.__module__ and "crud" not in cls.__module__ and "dialects" not in cls.__module__ # TODO: dialects? ).difference({ColumnElement, UnaryExpression}) for fixture in self.fixtures: case_a = fixture() for elem in case_a: for mro in type(elem).__mro__: need.discard(mro) is_false(bool(need), "%d Remaining classes: %r" % (len(need), need))
def test_component_set(self, component): common_url = ( "dbtype://*****:*****@[2001:da8:2004:1000:202:116:160:90]:80/database?foo=bar") url1 = url.make_url(common_url) url2 = url.make_url(common_url) url3 = url2.set(**{component: "new_changed_value"}) is_true(url1 != url3) is_false(url1 == url3) url4 = url3.set(**{component: getattr(url1, component)}) is_true(url4 == url1) is_false(url4 != url1)
def test_all_present(self): need = set( cls for cls in class_hierarchy(ClauseElement) if issubclass(cls, (ColumnElement, Selectable)) and "__init__" in cls.__dict__ and not issubclass(cls, (Annotated)) and "orm" not in cls.__module__ and "compiler" not in cls.__module__ and "crud" not in cls.__module__ and "dialects" not in cls.__module__ # TODO: dialects? ).difference({ColumnElement, UnaryExpression}) for fixture in self.fixtures: case_a = fixture() for elem in case_a: for mro in type(elem).__mro__: need.discard(mro) is_false(bool(need), "%d Remaining classes: %r" % (len(need), need))
def test_comparison(self): components = ( "drivername", "username", "password", "host", "database", "query", "port", ) common_url = ( "dbtype://*****:*****@[2001:da8:2004:1000:202:116:160:90]:80/database?foo=bar" ) other_url = "dbtype://*****:*****@host/" url1 = url.make_url(common_url) url2 = url.make_url(common_url) url3 = url.make_url(other_url) is_true(url1 == url2) is_false(url1 != url2) is_true(url1 != url3) is_false(url1 == url3) for curr_component in components: setattr(url2, curr_component, "new_changed_value") is_true(url1 != url2) is_false(url1 == url2) setattr(url2, curr_component, getattr(url1, curr_component))
def test_compare(self): for fixture in self.fixtures: case_a = fixture() case_b = fixture() for a, b in itertools.combinations_with_replacement( range(len(case_a)), 2 ): if a == b: is_true( case_a[a].compare( case_b[b], arbitrary_expression=True ), "%r != %r" % (case_a[a], case_b[b]), ) else: is_false( case_a[a].compare( case_b[b], arbitrary_expression=True ), "%r == %r" % (case_a[a], case_b[b]), )
def test_compare_labels(self): is_true(column("q").label(None).compare(column("q").label(None))) is_false(column("q").label("foo").compare(column("q").label(None))) is_false(column("q").label(None).compare(column("q").label("foo"))) is_false(column("q").label("foo").compare(column("q").label("bar"))) is_true(column("q").label("foo").compare(column("q").label("foo")))