def test_plain_aliased_compound(self): Company = _poly_fixtures.Company Person = _poly_fixtures.Person Engineer = _poly_fixtures.Engineer cmapper = inspect(Company) emapper = inspect(Engineer) c_alias = aliased(Company) p_alias = aliased(Person) c_alias = inspect(c_alias) p_alias = inspect(p_alias) p1 = PathRegistry.coerce( (c_alias, cmapper.attrs.employees, p_alias, emapper.attrs.machines) ) # plain AliasedClass - the path keeps that AliasedClass directly # as is in the path eq_( p1.path, ( c_alias, cmapper.attrs.employees, p_alias, emapper.attrs.machines, ), )
def test_double_w_ac_against_subquery(self): ( users, orders, User, Address, Order, addresses, Item, items, order_items, ) = ( self.tables.users, self.tables.orders, self.classes.User, self.classes.Address, self.classes.Order, self.tables.addresses, self.classes.Item, self.tables.items, self.tables.order_items, ) mapper(Address, addresses) mapper( Order, orders, properties={ "items": relationship( Item, secondary=order_items, lazy="select", order_by=items.c.id, ) }, ) mapper(Item, items) open_mapper = aliased( Order, select([orders]).where(orders.c.isopen == 1).alias()) closed_mapper = aliased( Order, select([orders]).where(orders.c.isopen == 0).alias()) mapper( User, users, properties=dict( addresses=relationship(Address, lazy=True), open_orders=relationship(open_mapper, lazy="select"), closed_orders=relationship(closed_mapper, lazy="select"), ), ) self._run_double_test()
def test_tuple_labeling(self): sess = create_session() # test pickle + all the protocols ! for pickled in False, -1, 0, 1, 2: for row in sess.query(User, Address).join(User.addresses).all(): if pickled is not False: row = pickle.loads(pickle.dumps(row, pickled)) eq_(list(row.keys()), ["User", "Address"]) eq_(row.User, row[0]) eq_(row.Address, row[1]) for row in sess.query(User.name, User.id.label("foobar")): if pickled is not False: row = pickle.loads(pickle.dumps(row, pickled)) eq_(list(row.keys()), ["name", "foobar"]) eq_(row.name, row[0]) eq_(row.foobar, row[1]) for row in sess.query(User).values( User.name, User.id.label("foobar") ): if pickled is not False: row = pickle.loads(pickle.dumps(row, pickled)) eq_(list(row.keys()), ["name", "foobar"]) eq_(row.name, row[0]) eq_(row.foobar, row[1]) oalias = aliased(Order) for row in sess.query(User, oalias).join(User.orders).all(): if pickled is not False: row = pickle.loads(pickle.dumps(row, pickled)) eq_(list(row.keys()), ["User"]) eq_(row.User, row[0]) oalias = aliased(Order, name="orders") for row in ( sess.query(User, oalias).join(oalias, User.orders).all() ): if pickled is not False: row = pickle.loads(pickle.dumps(row, pickled)) eq_(list(row.keys()), ["User", "orders"]) eq_(row.User, row[0]) eq_(row.orders, row[1]) # test here that first col is not labeled, only # one name in keys, matches correctly for row in sess.query(User.name + "hoho", User.name): eq_(list(row.keys()), ["name"]) eq_(row[0], row.name + "hoho") if pickled is not False: ret = sess.query(User, Address).join(User.addresses).all() pickle.loads(pickle.dumps(ret, pickled))
def test_parententity_vs_parentmapper(self): class Point(object): pass self._fixture(Point, properties={"x_syn": synonym("x")}) pa = aliased(Point) is_(Point.x_syn._parententity, inspect(Point)) is_(Point.x._parententity, inspect(Point)) is_(Point.x_syn._parentmapper, inspect(Point)) is_(Point.x._parentmapper, inspect(Point)) is_( Point.x_syn.__clause_element__()._annotations["parententity"], inspect(Point), ) is_( Point.x.__clause_element__()._annotations["parententity"], inspect(Point), ) is_( Point.x_syn.__clause_element__()._annotations["parentmapper"], inspect(Point), ) is_( Point.x.__clause_element__()._annotations["parentmapper"], inspect(Point), ) pa = aliased(Point) is_(pa.x_syn._parententity, inspect(pa)) is_(pa.x._parententity, inspect(pa)) is_(pa.x_syn._parentmapper, inspect(Point)) is_(pa.x._parentmapper, inspect(Point)) is_( pa.x_syn.__clause_element__()._annotations["parententity"], inspect(pa), ) is_( pa.x.__clause_element__()._annotations["parententity"], inspect(pa) ) is_( pa.x_syn.__clause_element__()._annotations["parentmapper"], inspect(Point), ) is_( pa.x.__clause_element__()._annotations["parentmapper"], inspect(Point), )
def setup_classes(cls): Base = cls.DeclarativeBasic class A(ComparableEntity, Base): __tablename__ = "a" id = Column(Integer, primary_key=True) b_id = Column(ForeignKey("b.id")) class B(ComparableEntity, Base): __tablename__ = "b" id = Column(Integer, primary_key=True) class C(ComparableEntity, Base): __tablename__ = "c" id = Column(Integer, primary_key=True) a_id = Column(ForeignKey("a.id")) class D(ComparableEntity, Base): __tablename__ = "d" id = Column(Integer, primary_key=True) c_id = Column(ForeignKey("c.id")) b_id = Column(ForeignKey("b.id")) # 1. set up the join() as a variable, so we can refer # to it in the mapping multiple times. j = join(B, D, D.b_id == B.id).join(C, C.id == D.c_id) # 2. Create an AliasedClass to B B_viacd = aliased(B, j, flat=True) A.b = relationship(B_viacd, primaryjoin=A.b_id == j.c.b_id)
def test_meta_getattr_three(self): class MetaPoint(type): def __getattr__(cls, key): @hybrid_property def double_x(me): return me.x * 2 if key == "double_x": return double_x.__get__(None, cls) raise AttributeError(key) class Point(compat.with_metaclass(MetaPoint)): pass self._fixture(Point) alias = aliased(Point) eq_(str(Point.double_x.__clause_element__()), "point.x * :x_1") eq_(str(alias.double_x.__clause_element__()), "point_1.x * :x_1") sess = Session() self.assert_compile( sess.query(alias).filter(alias.double_x > Point.x), "SELECT point_1.id AS point_1_id, point_1.x AS point_1_x, " "point_1.y AS point_1_y FROM point AS point_1, point " "WHERE point_1.x * :x_1 > point.x", )
def test_hybrid_descriptor_two(self): class Point(object): def __init__(self, x, y): self.x, self.y = x, y @hybrid_property def double_x(self): return self.x * 2 self._fixture(Point) alias = aliased(Point) eq_(str(Point.double_x), "Point.double_x") eq_(str(alias.double_x), "AliasedClass_Point.double_x") eq_(str(Point.double_x.__clause_element__()), "point.x * :x_1") eq_(str(alias.double_x.__clause_element__()), "point_1.x * :x_1") sess = Session() self.assert_compile( sess.query(alias).filter(alias.double_x > Point.x), "SELECT point_1.id AS point_1_id, point_1.x AS point_1_x, " "point_1.y AS point_1_y FROM point AS point_1, point " "WHERE point_1.x * :x_1 > point.x", )
def setup_classes(cls): Base = cls.DeclarativeBasic class A(Base): __tablename__ = "a" id = Column(Integer, primary_key=True) class B(Base): __tablename__ = "b" id = Column(Integer, primary_key=True) a_id = Column(ForeignKey("a.id")) cs = relationship("C") class C(Base): __tablename__ = "c" id = Column(Integer, primary_key=True) b_id = Column(ForeignKey("b.id")) partition = select([ B, func.row_number().over(order_by=B.id, partition_by=B.a_id).label("index"), ]).alias() partitioned_b = aliased(B, alias=partition) A.partitioned_bs = relationship( partitioned_b, primaryjoin=and_(partitioned_b.a_id == A.id, partition.c.index < 10), )
def test_eq(self): umapper = inspect(self.classes.User) amapper = inspect(self.classes.Address) u_alias = inspect(aliased(self.classes.User)) p1 = PathRegistry.coerce((umapper, umapper.attrs.addresses)) p2 = PathRegistry.coerce((umapper, umapper.attrs.addresses)) p3 = PathRegistry.coerce((umapper, umapper.attrs.name)) p4 = PathRegistry.coerce((u_alias, umapper.attrs.addresses)) p5 = PathRegistry.coerce((umapper, umapper.attrs.addresses, amapper)) p6 = PathRegistry.coerce( (amapper, amapper.attrs.user, umapper, umapper.attrs.addresses) ) p7 = PathRegistry.coerce( ( amapper, amapper.attrs.user, umapper, umapper.attrs.addresses, amapper, amapper.attrs.email_address, ) ) is_(p1 == p2, True) is_(p1 == p3, False) is_(p1 == p4, False) is_(p1 == p5, False) is_(p6 == p7, False) is_(p6 == p7.parent.parent, True) is_(p1 != p2, False) is_(p1 != p3, True) is_(p1 != p4, True) is_(p1 != p5, True)
def test_aliased_query(self): A = self._fixture() sess = Session() self.assert_compile( sess.query(aliased(A).value), "SELECT a_1.value AS a_1_value FROM a AS a_1", )
def test_descriptors(self): class descriptor(object): def __init__(self, fn): self.fn = fn def __get__(self, obj, owner): if obj is not None: return self.fn(obj, obj) else: return self def method(self): return "method" class Point(object): center = (0, 0) @descriptor def thing(self, arg): return arg.center self._fixture(Point) alias = aliased(Point) assert Point.thing != (0, 0) assert Point().thing == (0, 0) assert Point.thing.method() == "method" assert alias.thing != (0, 0) assert alias.thing.method() == "method"
def test_aliased_query_col(self): A = self._fixture() sess = Session() self.assert_compile( sess.query(aliased(A).value(5)), "SELECT foo(a_1.value, :foo_1) + :foo_2 AS anon_1 FROM a AS a_1", )
def test_not_instantiatable(self): class Point(object): pass self._fixture(Point) alias = aliased(Point) assert_raises(TypeError, alias)
def test_aliased_class(self): Address = self.classes.Address ualias = aliased(Address) insp = inspect(ualias) is_(insp.mapper, inspect(Address)) is_(insp.selectable, ualias._aliased_insp.selectable) assert not insp.is_selectable assert insp.is_aliased_class
def test_aliased_filter(self): A = self._fixture() sess = Session() self.assert_compile( sess.query(aliased(A)).filter_by(value="foo"), "SELECT a_1.value AS a_1_value, a_1.id AS a_1_id " "FROM a AS a_1 WHERE upper(a_1.value) = upper(:upper_1)", )
def test_aliased_query(self): A = self._fixture() sess = Session() self.assert_compile( sess.query(aliased(A)).filter_by(value="foo"), "SELECT a_1.value AS a_1_value, a_1.id AS a_1_id " "FROM a AS a_1 WHERE foo(a_1.value) + bar(a_1.value) = :param_1", )
def test_nonpoly_oftype_aliased_subclass_onroot(self): Engineer = _poly_fixtures.Engineer eng_alias = aliased(Engineer) ea_insp = inspect(eng_alias) p1 = PathRegistry.coerce((ea_insp, ea_insp.mapper.attrs.paperwork)) eq_(p1.path, (ea_insp, ea_insp.mapper.attrs.paperwork)) eq_(p1.natural_path, (ea_insp, ea_insp.mapper.attrs.paperwork))
def test_aliased_query(self): A = self._fixture() sess = Session() a1 = aliased(A) self.assert_compile( sess.query(a1).filter(a1.value(5) == "foo"), "SELECT a_1.value AS a_1_value, a_1.id AS a_1_id " "FROM a AS a_1 WHERE foo(a_1.value, :foo_1) + :foo_2 = :param_1", )
def test_instancemethod(self): class Point(object): def zero(self): self.x, self.y = 0, 0 self._fixture(Point) alias = aliased(Point) assert Point.zero assert getattr(alias, "zero")
def test_is_instance(self): User = self.classes.User u1 = User(name="ed") insp = inspect(u1) assert insp.is_instance insp = inspect(User) assert not insp.is_instance insp = inspect(aliased(User)) assert not insp.is_instance
def test_a_to_d_aliased(self): A, D = self.classes("A", "D") a1 = aliased(A) # aliased, uses adaption therefore expensive @profiling.function_call_count(times=50, warmup=1) def go(): orm_join(a1, D, a1.d) go()
def test_column_adapter_lookup(self): User, Address = self.classes("User", "Address") u1 = aliased(User) @assert_cycles() def go(): adapter = sql_util.ColumnAdapter(inspect(u1).selectable) adapter.columns[User.id] go()
def test_a_to_b_aliased(self): A, B = self.classes("A", "B") a1 = aliased(A) # uses aliasing, therefore adaption which is expensive @profiling.function_call_count(times=50, warmup=1) def go(): orm_join(a1, B, a1.b) go()
def test_twolevel_subqueryload_wsubclass_mapper_term(self): DataContainer, SubJob = self.classes.DataContainer, self.classes.SubJob s = Session(testing.db) sj_alias = aliased(SubJob) q = s.query(DataContainer).options( subqueryload(DataContainer.jobs.of_type(sj_alias)).subqueryload( sj_alias.widget)) def go(): eq_(q.all(), self._dc_fixture()) self.assert_sql_count(testing.db, go, 3)
def test_query_aliased(self): A, C, B = (self.classes.A, self.classes.C, self.classes.B) sess = Session() b1, b2 = B(data="b1"), B(data="b2") a1 = A(c=C("a1b1", b1)) a2 = A(c=C("a2b1", b2)) sess.add_all([a1, a2]) sess.commit() ae = aliased(A) eq_(sess.query(ae).filter(ae.c == C("a2b1", b2)).one(), a2)
def test_classmethod(self): class Point(object): @classmethod def max_x(cls): return 100 self._fixture(Point) alias = aliased(Point) assert Point.max_x assert alias.max_x assert Point.max_x() == alias.max_x() == 100
def test_plain_aliased(self): Person = _poly_fixtures.Person Engineer = _poly_fixtures.Engineer emapper = inspect(Engineer) p_alias = aliased(Person) p_alias = inspect(p_alias) p1 = PathRegistry.coerce((p_alias, emapper.attrs.machines)) # plain AliasedClass - the path keeps that AliasedClass directly # as is in the path eq_(p1.path, (p_alias, emapper.attrs.machines))
def test_meta_getattr_one(self): class MetaPoint(type): def __getattr__(cls, key): if key == "x_syn": return cls.x raise AttributeError(key) class Point(compat.with_metaclass(MetaPoint)): pass self._fixture(Point) alias = aliased(Point) eq_(str(Point.x_syn), "Point.x") eq_(str(alias.x_syn), "AliasedClass_Point.x") # from __clause_element__() perspective, Point.x_syn # and Point.x return the same thing, so that's good eq_(str(Point.x.__clause_element__()), "point.x") eq_(str(Point.x_syn.__clause_element__()), "point.x") # same for the alias eq_(str(alias.x + 1), "point_1.x + :x_1") eq_(str(alias.x_syn + 1), "point_1.x + :x_1") is_(Point.x_syn.__clause_element__(), Point.x.__clause_element__()) eq_(str(alias.x_syn == alias.x), "point_1.x = point_1.x") a2 = aliased(Point) eq_(str(a2.x_syn == alias.x), "point_1.x = point_2.x") sess = Session() self.assert_compile( sess.query(alias).filter(alias.x_syn > Point.x), "SELECT point_1.id AS point_1_id, point_1.x AS point_1_x, " "point_1.y AS point_1_y FROM point AS point_1, point " "WHERE point_1.x > point.x", )
def test_comparator_aliased(self): Graph, Edge, Point = ( self.classes.Graph, self.classes.Edge, self.classes.Point, ) sess = self._fixture() g = sess.query(Graph).first() ea = aliased(Edge) assert (sess.query(ea).filter(ea.start != Point(3, 4)).first() is g.edges[1])
def test_order_by_aliased(self): self._fixture(False) Edge = self.classes.Edge s = Session() ea = aliased(Edge) self.assert_compile( s.query(ea).order_by(ea.start, ea.end), "SELECT edge_1.id AS edge_1_id, edge_1.x1 AS edge_1_x1, " "edge_1.y1 AS edge_1_y1, edge_1.x2 AS edge_1_x2, " "edge_1.y2 AS edge_1_y2 " "FROM edge AS edge_1 ORDER BY edge_1.x1, edge_1.y1, " "edge_1.x2, edge_1.y2", )