Ejemplo n.º 1
0
 def test_get_three(self):
     sess = create_session()
     eq_(sess.query(Manager).get(b1.person_id),
         Boss(name="pointy haired boss", golf_swing="fore"))
Ejemplo n.º 2
0
 def test_filter_on_subclass_six(self):
     sess = create_session()
     eq_(sess.query(Boss)
             .filter(Boss.person_id == b1.person_id).one(),
         Boss(name="pointy haired boss"))
    def test_relationship_to_polymorphic(self):
        expected = [
            Company(name="MegaCorp, Inc.",
                    employees=[
                        Engineer(name="dilbert",
                                 engineer_name="dilbert",
                                 primary_language="java",
                                 status="regular engineer",
                                 machines=[
                                     Machine(name="IBM ThinkPad"),
                                     Machine(name="IPhone")
                                 ]),
                        Engineer(name="wally",
                                 engineer_name="wally",
                                 primary_language="c++",
                                 status="regular engineer"),
                        Boss(name="pointy haired boss",
                             golf_swing="fore",
                             manager_name="pointy",
                             status="da boss"),
                        Manager(name="dogbert",
                                manager_name="dogbert",
                                status="regular manager"),
                    ]),
            Company(name="Elbonia, Inc.",
                    employees=[
                        Engineer(name="vlad",
                                 engineer_name="vlad",
                                 primary_language="cobol",
                                 status="elbonian engineer")
                    ])
        ]

        sess = create_session()

        def go():
            # test load Companies with lazy load to 'employees'
            eq_(sess.query(Company).all(), expected)

        count = {'': 9, 'Polymorphic': 4}.get(self.select_type, 5)
        self.assert_sql_count(testing.db, go, count)

        sess = create_session()

        def go():
            # currently, it doesn't matter if we say Company.employees,
            # or Company.employees.of_type(Engineer).  joinedloader
            # doesn't pick up on the "of_type()" as of yet.
            eq_(
                sess.query(Company).options(
                    joinedload_all(Company.employees.of_type(Engineer),
                                   Engineer.machines)).all(), expected)

        # in the case of select_type='', the joinedload
        # doesn't take in this case; it joinedloads company->people,
        # then a load for each of 5 rows, then lazyload of "machines"
        count = {'': 7, 'Polymorphic': 1}.get(self.select_type, 2)
        self.assert_sql_count(testing.db, go, count)

        sess = create_session()

        def go():
            eq_(
                sess.query(Company).options(
                    subqueryload_all(Company.employees.of_type(Engineer),
                                     Engineer.machines)).all(), expected)

        count = {
            '': 8,
            'Joins': 4,
            'Unions': 4,
            'Polymorphic': 3,
            'AliasedJoins': 4
        }[self.select_type]
        self.assert_sql_count(testing.db, go, count)