~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to test/orm/test_loading.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from . import _fixtures
 
2
from sqlalchemy.orm import loading, Session, aliased
 
3
from sqlalchemy.testing.assertions import eq_
 
4
from sqlalchemy.util import KeyedTuple
 
5
 
 
6
# class InstancesTest(_fixtures.FixtureTest):
 
7
# class GetFromIdentityTest(_fixtures.FixtureTest):
 
8
# class LoadOnIdentTest(_fixtures.FixtureTest):
 
9
# class InstanceProcessorTest(_fixture.FixtureTest):
 
10
 
 
11
class MergeResultTest(_fixtures.FixtureTest):
 
12
    run_setup_mappers = 'once'
 
13
    run_inserts = 'once'
 
14
    run_deletes = None
 
15
 
 
16
    @classmethod
 
17
    def setup_mappers(cls):
 
18
        cls._setup_stock_mapping()
 
19
 
 
20
    def _fixture(self):
 
21
        User = self.classes.User
 
22
 
 
23
        s = Session()
 
24
        u1, u2, u3, u4 = User(id=1, name='u1'), User(id=2, name='u2'), \
 
25
                            User(id=7, name='u3'), User(id=8, name='u4')
 
26
        s.query(User).filter(User.id.in_([7, 8])).all()
 
27
        s.close()
 
28
        return s, [u1, u2, u3, u4]
 
29
 
 
30
    def test_single_entity(self):
 
31
        s, (u1, u2, u3, u4) = self._fixture()
 
32
        User = self.classes.User
 
33
 
 
34
        q = s.query(User)
 
35
        collection = [u1, u2, u3, u4]
 
36
        it = loading.merge_result(
 
37
            q,
 
38
            collection
 
39
        )
 
40
        eq_(
 
41
            [x.id for x in it],
 
42
            [1, 2, 7, 8]
 
43
        )
 
44
 
 
45
    def test_single_column(self):
 
46
        User = self.classes.User
 
47
 
 
48
        s = Session()
 
49
 
 
50
        q = s.query(User.id)
 
51
        collection = [(1, ), (2, ), (7, ), (8, )]
 
52
        it = loading.merge_result(
 
53
            q,
 
54
            collection
 
55
        )
 
56
        eq_(
 
57
            list(it),
 
58
            [(1, ), (2, ), (7, ), (8, )]
 
59
        )
 
60
 
 
61
    def test_entity_col_mix_plain_tuple(self):
 
62
        s, (u1, u2, u3, u4) = self._fixture()
 
63
        User = self.classes.User
 
64
 
 
65
        q = s.query(User, User.id)
 
66
        collection = [(u1, 1), (u2, 2), (u3, 7), (u4, 8)]
 
67
        it = loading.merge_result(
 
68
            q,
 
69
            collection
 
70
        )
 
71
        it = list(it)
 
72
        eq_(
 
73
            [(x.id, y) for x, y in it],
 
74
            [(1, 1), (2, 2), (7, 7), (8, 8)]
 
75
        )
 
76
        eq_(it[0].keys(), ['User', 'id'])
 
77
 
 
78
    def test_entity_col_mix_keyed_tuple(self):
 
79
        s, (u1, u2, u3, u4) = self._fixture()
 
80
        User = self.classes.User
 
81
 
 
82
        q = s.query(User, User.id)
 
83
        kt = lambda *x: KeyedTuple(x, ['User', 'id'])
 
84
        collection = [kt(u1, 1), kt(u2, 2), kt(u3, 7), kt(u4, 8)]
 
85
        it = loading.merge_result(
 
86
            q,
 
87
            collection
 
88
        )
 
89
        it = list(it)
 
90
        eq_(
 
91
            [(x.id, y) for x, y in it],
 
92
            [(1, 1), (2, 2), (7, 7), (8, 8)]
 
93
        )
 
94
        eq_(it[0].keys(), ['User', 'id'])
 
95
 
 
96
    def test_none_entity(self):
 
97
        s, (u1, u2, u3, u4) = self._fixture()
 
98
        User = self.classes.User
 
99
 
 
100
        ua = aliased(User)
 
101
        q = s.query(User, ua)
 
102
        kt = lambda *x: KeyedTuple(x, ['User', 'useralias'])
 
103
        collection = [kt(u1, u2), kt(u1, None), kt(u2, u3)]
 
104
        it = loading.merge_result(
 
105
            q,
 
106
            collection
 
107
        )
 
108
        eq_(
 
109
            [
 
110
                (x and x.id or None, y and y.id or None)
 
111
                for x, y in it
 
112
            ],
 
113
            [(u1.id, u2.id), (u1.id, None), (u2.id, u3.id)]
 
114
        )
 
115
 
 
116