def test_mixed_entities_one(self):
        sess = create_session()

        expected = [
            (Engineer(
                status=u'regular engineer',
                engineer_name=u'dilbert',
                name=u'dilbert',
                company_id=1,
                primary_language=u'java',
                person_id=1,
                type=u'engineer'),
            u'MegaCorp, Inc.'),
            (Engineer(
                status=u'regular engineer',
                engineer_name=u'wally',
                name=u'wally',
                company_id=1,
                primary_language=u'c++',
                person_id=2,
                type=u'engineer'),
            u'MegaCorp, Inc.'),
            (Engineer(
                status=u'elbonian engineer',
                engineer_name=u'vlad',
                name=u'vlad',
                company_id=2,
                primary_language=u'cobol',
                person_id=5,
                type=u'engineer'),
            u'Elbonia, Inc.')]
        eq_(sess.query(Engineer, Company.name)
                .join(Company.employees)
                .filter(Person.type == 'engineer').all(),
            expected)
 def test_multi_join(self):
     sess = create_session()
     e = aliased(Person)
     c = aliased(Company)
     q = sess.query(Company, Person, c, e)\
             .join(Person, Company.employees)\
             .join(e, c.employees)\
             .filter(Person.name == 'dilbert')\
             .filter(e.name == 'wally')
     eq_(q.count(), 1)
     eq_(q.all(), [
         (
             Company(company_id=1, name=u'MegaCorp, Inc.'),
             Engineer(
                 status=u'regular engineer',
                 engineer_name=u'dilbert',
                 name=u'dilbert',
                 company_id=1,
                 primary_language=u'java',
                 person_id=1,
                 type=u'engineer'),
             Company(company_id=1, name=u'MegaCorp, Inc.'),
             Engineer(
                 status=u'regular engineer',
                 engineer_name=u'wally',
                 name=u'wally',
                 company_id=1,
                 primary_language=u'c++',
                 person_id=2,
                 type=u'engineer')
         )
     ])
    def test_joinedload_on_subclass(self):
        sess = create_session()
        expected = [
            Engineer(
                name="dilbert",
                engineer_name="dilbert",
                primary_language="java",
                status="regular engineer",
                machines=[
                    Machine(name="IBM ThinkPad"),
                    Machine(name="IPhone")])]

        def go():
            # test load People with joinedload to engineers + machines
            eq_(sess.query(Person)
                    .with_polymorphic('*')
                    .options(joinedload(Engineer.machines))
                    .filter(Person.name == 'dilbert').all(),
              expected)
        self.assert_sql_count(testing.db, go, 1)

        sess = create_session()
        def go():
            # test load People with subqueryload to engineers + machines
            eq_(sess.query(Person)
                    .with_polymorphic('*')
                    .options(subqueryload(Engineer.machines))
                    .filter(Person.name == 'dilbert').all(),
              expected)
        self.assert_sql_count(testing.db, go, 2)
 def test_get_one(self):
     """
     For all mappers, ensure the primary key has been calculated as
     just the "person_id" column.
     """
     sess = create_session()
     eq_(sess.query(Person).get(e1.person_id),
         Engineer(name="dilbert", primary_language="java"))
 def test_mixed_entities_sixteen(self):
     sess = create_session()
     expected = [(Engineer(status=u'elbonian engineer',
                           engineer_name=u'vlad',
                           name=u'vlad',
                           primary_language=u'cobol'), u'Elbonia, Inc.')]
     eq_(
         sess.query(Person, Company.name).join(Company.employees).filter(
             Company.name == 'Elbonia, Inc.').all(), expected)
 def test_mixed_entities_three(self):
     sess = create_session()
     palias = aliased(Person)
     expected = [(Engineer(status=u'elbonian engineer',
                           engineer_name=u'vlad',
                           name=u'vlad',
                           primary_language=u'cobol'), u'Elbonia, Inc.',
                  Engineer(status=u'regular engineer',
                           engineer_name=u'dilbert',
                           name=u'dilbert',
                           company_id=1,
                           primary_language=u'java',
                           person_id=1,
                           type=u'engineer'))]
     eq_(
         sess.query(Person, Company.name,
                    palias).join(Company.employees).filter(
                        Company.name == 'Elbonia, Inc.').filter(
                            palias.name == 'dilbert').all(), expected)
 def test_get_two(self):
     sess = create_session()
     eq_(sess.query(Engineer).get(e1.person_id),
         Engineer(name="dilbert", primary_language="java"))
 def test_filter_on_subclass_three(self):
     sess = create_session()
     eq_(sess.query(Engineer)
             .filter(Engineer.person_id == e1.person_id).first(),
         Engineer(name="dilbert"))
 def test_filter_on_subclass_two(self):
     sess = create_session()
     eq_(sess.query(Engineer).first(), Engineer(name="dilbert"))
示例#10
0
 def test_filter_on_subclass_one(self):
     sess = create_session()
     eq_(sess.query(Engineer).all()[0], Engineer(name="dilbert"))
    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)