def test_primary_eager_aliasing_three(self): # assert the JOINs don't over JOIN sess = fixture_session() def go(): with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person) .with_polymorphic("*") .order_by(Person.person_id) .options(joinedload(Engineer.machines))[1:3], all_employees[1:3], ) self.assert_sql_count(testing.db, go, 3) with expect_deprecated_20(with_polymorphic_dep): eq_( sess.scalar( select(func.count("*")).select_from( sess.query(Person) .with_polymorphic("*") .options(joinedload(Engineer.machines)) .order_by(Person.person_id) .limit(2) .offset(1) .subquery() ) ), 2, )
def test_query_wpoly_single_inh_subclass(self): Boss = self.classes.Boss poly = self._with_poly_fixture() s = fixture_session() with expect_deprecated_20(with_polymorphic_dep): q = s.query(Boss).with_polymorphic(Boss, poly) self.assert_compile( q, "SELECT anon_1.employee_id AS anon_1_employee_id, " "anon_1.employee_name AS anon_1_employee_name, " "anon_1.employee_type AS anon_1_employee_type, " "anon_1.manager_manager_data AS anon_1_manager_manager_data " "FROM (SELECT employee.id AS employee_id, employee.type " "AS employee_type, employee.name AS employee_name, " "manager.manager_data AS manager_manager_data, " "NULL AS engineer_info, NULL AS manager_id FROM employee " "JOIN manager ON employee.id = manager.id " "UNION ALL SELECT employee.id AS employee_id, " "employee.type AS employee_type, employee.name AS employee_name, " "NULL AS manager_data, " "engineer.engineer_info AS engineer_engineer_info, " "engineer.manager_id AS engineer_manager_id " "FROM employee JOIN engineer ON employee.id = engineer.id) " "AS anon_1 WHERE anon_1.employee_type IN (__[POSTCOMPILE_type_1])", )
def test_of_type_aliased_fromjoinpoint(self): Company, Employee, Engineer = ( self.classes.Company, self.classes.Employee, self.classes.Engineer, ) companies, employees = self.tables.companies, self.tables.employees self.mapper_registry.map_imperatively( Company, companies, properties={"employee": relationship(Employee)} ) self.mapper_registry.map_imperatively( Employee, employees, polymorphic_on=employees.c.type ) self.mapper_registry.map_imperatively( Engineer, inherits=Employee, polymorphic_identity="engineer" ) sess = fixture_session() with expect_deprecated_20(aliased_jp_dep): self.assert_compile( sess.query(Company).outerjoin( Company.employee.of_type(Engineer), aliased=True, from_joinpoint=True, ), "SELECT companies.company_id AS companies_company_id, " "companies.name AS companies_name FROM companies " "LEFT OUTER JOIN employees AS employees_1 ON " "companies.company_id = employees_1.company_id " "AND employees_1.type IN (__[POSTCOMPILE_type_1])", )
def go(): with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic(Engineer).order_by( Person.person_id).all(), self._emps_wo_relationships_fixture(), )
def go(): with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic(Engineer).filter( Engineer.primary_language == "java").all(), self._emps_wo_relationships_fixture()[0:1], )
def go(): # NOTE: subqueryload is broken for this case, first found # when cartesian product detection was added. with expect_deprecated_20(with_polymorphic_dep): for a in (session.query(A).with_polymorphic([B, C]).options( selectinload(B.related), selectinload(C.related))): eq_(a.related, [d])
def go(): # limit the polymorphic join down to just "Person", # overriding select_table with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic(Person).all(), self._emps_wo_relationships_fixture(), )
def go(): with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic("*").order_by( Person.person_id).options(joinedload( Engineer.machines))[1:3], all_employees[1:3], )
def go(): with expect_deprecated_20(with_polymorphic_dep): for a in ( session.query(A) .with_polymorphic([B, C]) .options(joinedload(B.related), joinedload(C.related)) ): eq_(a.related, [d])
def test_join_from_with_polymorphic_nonaliased_one(self): sess = fixture_session() with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic(Manager).order_by( Person.person_id).join(Person.paperwork).filter( Paperwork.description.like("%review%")).all(), [b1, m1], )
def go(): # test load People with joinedload to engineers + machines with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic("*").options( joinedload(Engineer.machines)).filter( Person.name == "dilbert").all(), expected, )
def test_join_to_polymorphic_flag_aliased(self): sess = fixture_session() with expect_deprecated_20(aliased_jp_dep): eq_( sess.query(Company).join( Company.employees, aliased=True).filter(Person.name == "vlad").one(), c2, )
def test_join_from_with_polymorphic_flag_aliased_one(self): sess = fixture_session() with expect_deprecated_20(aliased_jp_dep, with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic(Manager).join( Person.paperwork, aliased=True).filter( Paperwork.description.like("%review%")).all(), [b1, m1], )
def test_join_from_polymorphic_flag_aliased_two(self): sess = fixture_session() with expect_deprecated_20(aliased_jp_dep): eq_( sess.query(Person).order_by(Person.person_id).join( Person.paperwork, aliased=True).filter( Paperwork.description.like("%#2%")).all(), [e1, m1], )
def test_with_polymorphic_seven(self): sess = fixture_session() # compare to entities without related collections to prevent # additional lazy SQL from firing on loaded entities with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic("*").order_by( Person.person_id).all(), self._emps_wo_relationships_fixture(), )
def test_join_from_with_polymorphic_nonaliased_three(self): sess = fixture_session() with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic([ Manager, Engineer ]).order_by(Person.person_id).join(Person.paperwork).filter( Person.name.like("%dog%")).filter( Paperwork.description.like("%#2%")).all(), [m1], )
def test_join_from_with_polymorphic_explicit_aliased_one(self): sess = fixture_session() pa = aliased(Paperwork) with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic(Manager).join( pa, Person.paperwork).filter( pa.description.like("%review%")).all(), [b1, m1], )
def test_with_polymorphic_six(self): sess = fixture_session() with expect_deprecated_20(with_polymorphic_dep): assert_raises( sa_exc.InvalidRequestError, sess.query(Person).with_polymorphic, Paperwork, ) with expect_deprecated_20(with_polymorphic_dep): assert_raises( sa_exc.InvalidRequestError, sess.query(Engineer).with_polymorphic, Boss, ) with expect_deprecated_20(with_polymorphic_dep): assert_raises( sa_exc.InvalidRequestError, sess.query(Engineer).with_polymorphic, Person, )
def test_join_from_with_polymorphic_explicit_aliased_two(self): sess = fixture_session() pa = aliased(Paperwork) with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic( [Manager, Engineer]).order_by(Person.person_id).join( pa, Person.paperwork).filter( pa.description.like("%#2%")).all(), [e1, m1], )
def test_polymorphic_any_flag_alias_two(self): sess = fixture_session() # test that the aliasing on "Person" does not bleed into the # EXISTS clause generated by any() any_ = Company.employees.any(Person.name == "wally") with expect_deprecated_20(aliased_jp_dep): eq_( sess.query(Company).join( Company.employees, aliased=True).filter( Person.name == "dilbert").filter(any_).all(), [c1], )
def go(): with expect_deprecated_20(with_polymorphic_dep): eq_( sess.query(Person).with_polymorphic("*").order_by( Person.person_id).all(), [ Person(name="p1"), Employee(name="e1", employee_data="ed1"), Manager(name="m1", employee_data="ed2", manager_data="md1"), ], )
def test_without_default_polymorphic(self): Employee, Engineer, Manager = self.classes( "Employee", "Engineer", "Manager" ) (Hacker,) = self.classes("Hacker") (employees_table,) = self.tables("employees") engineers_table, managers_table = self.tables("engineers", "managers") (hackers_table,) = self.tables("hackers") pjoin = polymorphic_union( { "employee": employees_table, "manager": managers_table, "engineer": engineers_table, "hacker": hackers_table, }, "type", "pjoin", ) pjoin2 = polymorphic_union( {"engineer": engineers_table, "hacker": hackers_table}, "type", "pjoin2", ) employee_mapper = self.mapper_registry.map_imperatively( Employee, employees_table, polymorphic_identity="employee" ) self.mapper_registry.map_imperatively( Manager, managers_table, inherits=employee_mapper, concrete=True, polymorphic_identity="manager", ) engineer_mapper = self.mapper_registry.map_imperatively( Engineer, engineers_table, inherits=employee_mapper, concrete=True, polymorphic_identity="engineer", ) self.mapper_registry.map_imperatively( Hacker, hackers_table, inherits=engineer_mapper, concrete=True, polymorphic_identity="hacker", ) session = fixture_session() jdoe = Employee("Jdoe") tom = Manager("Tom", "knows how to manage things") jerry = Engineer("Jerry", "knows how to program") hacker = Hacker("Kurt", "Badass", "knows how to hack") session.add_all((jdoe, tom, jerry, hacker)) session.flush() with expect_deprecated_20(with_polymorphic_dep): eq_( len( session.connection() .execute( session.query(Employee) .with_polymorphic("*", pjoin, pjoin.c.type) .statement ) .fetchall() ), 4, ) eq_(session.get(Employee, jdoe.employee_id), jdoe) eq_(session.get(Engineer, jerry.employee_id), jerry) with expect_deprecated_20(with_polymorphic_dep): eq_( set( [ repr(x) for x in session.query(Employee).with_polymorphic( "*", pjoin, pjoin.c.type ) ] ), set( [ "Employee Jdoe", "Engineer Jerry knows how to program", "Manager Tom knows how to manage things", "Hacker Kurt 'Badass' knows how to hack", ] ), ) eq_( set([repr(x) for x in session.query(Manager)]), set(["Manager Tom knows how to manage things"]), ) with expect_deprecated_20(with_polymorphic_dep): eq_( set( [ repr(x) for x in session.query(Engineer).with_polymorphic( "*", pjoin2, pjoin2.c.type ) ] ), set( [ "Engineer Jerry knows how to program", "Hacker Kurt 'Badass' knows how to hack", ] ), ) eq_( set([repr(x) for x in session.query(Hacker)]), set(["Hacker Kurt 'Badass' knows how to hack"]), ) # test adaption of the column by wrapping the query in a # subquery with testing.expect_deprecated( r"The Query.from_self\(\) method", with_polymorphic_dep ): eq_( len( session.connection() .execute( session.query(Engineer) .with_polymorphic("*", pjoin2, pjoin2.c.type) .from_self() .statement ) .fetchall() ), 2, ) with testing.expect_deprecated( r"The Query.from_self\(\) method", with_polymorphic_dep ): eq_( set( [ repr(x) for x in session.query(Engineer) .with_polymorphic("*", pjoin2, pjoin2.c.type) .from_self() ] ), set( [ "Engineer Jerry knows how to program", "Hacker Kurt 'Badass' knows how to hack", ] ), )