~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for `Collection`."""

__metaclass__ = type

from storm.locals import (
    Int,
    Storm,
    )
from zope.component import getUtility

from lp.services.database.collection import Collection
from lp.services.webapp.interfaces import (
    IStoreSelector,
    MAIN_STORE,
    MASTER_FLAVOR,
    )
from lp.testing import TestCaseWithFactory
from lp.testing.fakemethod import FakeMethod
from lp.testing.layers import ZopelessDatabaseLayer


def get_store():
    return getUtility(IStoreSelector).get(MAIN_STORE, MASTER_FLAVOR)


class FakeStore:
    find = FakeMethod(result=[])

    def using(self, *args):
        return self


def make_table(range_start, range_end, table_name=None):
    """Create a temporary table and a storm class for it."""
    assert range_start < range_end, "Invalid range."
    if table_name is None:
        table_name = "TestTable"
    get_store().execute("""
       CREATE TEMP TABLE %s AS
       SELECT generate_series AS id
       FROM generate_series(%d, %d)
       """ % (table_name, range_start, range_end - 1))

    class TestTable(Storm):
        """A test class/table generated on the fly for testing purposes."""
        __storm_table__ = table_name
        id = Int(primary=True)

        def __init__(self, id):
            self.id = id

        def __eq__(self, other):
            return self.id == other.id

    return TestTable


def get_ids(testtable_objects):
    """Helper to unpack ids from a sequence of TestTable objects."""
    return [obj.id for obj in testtable_objects]


class CollectionTest(TestCaseWithFactory):

    layer = ZopelessDatabaseLayer

    def test_make_table(self):
        TestTable = make_table(1, 5)
        result = get_store().find(TestTable).order_by(TestTable.id)
        self.assertEqual(range(1, 5), get_ids(result))

    def test_select_one(self):
        TestTable = make_table(1, 5)
        collection = Collection(TestTable.id == 1, tables=TestTable)
        result = collection.select(TestTable)
        self.assertEqual([1], get_ids(result))

    def test_select_all(self):
        TestTable = make_table(1, 3)
        collection = Collection(tables=TestTable)
        result = collection.select(TestTable)
        self.assertContentEqual([1, 2], get_ids(result))

    def test_select_condition(self):
        TestTable = make_table(1, 5)
        collection = Collection(TestTable.id > 2, tables=TestTable)
        result = collection.select(TestTable)
        self.assertContentEqual([3, 4], get_ids(result))

    def test_select_conditions(self):
        TestTable = make_table(1, 5)
        collection = Collection(
            TestTable.id > 2, TestTable.id < 4, tables=TestTable)
        result = collection.select(TestTable)
        self.assertContentEqual([3], get_ids(result))

    def test_select_column(self):
        TestTable = make_table(1, 3)
        collection = Collection(tables=TestTable)
        result = collection.select(TestTable.id)
        self.assertContentEqual([1, 2], list(result))

    def test_copy_collection(self):
        TestTable = make_table(1, 3)
        collection = Collection(tables=TestTable)
        copied_collection = Collection(collection)
        result = copied_collection.select(TestTable)
        self.assertContentEqual([1, 2], get_ids(result))

    def test_restrict_collection(self):
        TestTable = make_table(1, 3)
        collection = Collection(tables=TestTable)
        copied_collection = Collection(collection, TestTable.id < 2)
        result = copied_collection.select(TestTable)
        self.assertContentEqual([1], get_ids(result))

    def test_add_tables(self):
        # The list of tables to select from carries across copies.
        TestTable1 = make_table(1, 2, 'TestTable1')
        TestTable2 = make_table(2, 3, 'TestTable2')
        collection = Collection(tables=TestTable1)
        collection = Collection(collection, tables=TestTable2)
        result = collection.select(TestTable1.id, TestTable2.id)
        self.assertEqual([(1, 2)], list(result))

    def test_add_tables_and_conditions(self):
        TestTable1 = make_table(1, 2, 'TestTable1')
        TestTable2 = make_table(2, 3, 'TestTable2')
        collection = Collection(TestTable1.id == 1, tables=TestTable1)
        collection = Collection(
            collection, TestTable2.id == 2, tables=TestTable2)
        result = collection.select(TestTable1.id, TestTable2.id)
        self.assertEqual([(1, 2)], list(result))

    def test_select_join(self):
        TestTable1 = make_table(1, 2, 'TestTable1')
        TestTable2 = make_table(2, 3, 'TestTable2')
        collection = Collection(tables=(TestTable1, TestTable2))
        result = collection.select(TestTable1, TestTable2)
        self.assertEqual(
            [(TestTable1(id=1), TestTable2(id=2))], list(result))

    def test_select_join_column(self):
        TestTable1 = make_table(1, 2, 'TestTable1')
        TestTable2 = make_table(2, 3, 'TestTable2')
        collection = Collection(tables=(TestTable1, TestTable2))
        result = collection.select(TestTable1.id, TestTable2.id)
        self.assertEqual([(1, 2)], list(result))

    def test_select_partial_join(self):
        TestTable1 = make_table(1, 2, 'TestTable1')
        TestTable2 = make_table(2, 3, 'TestTable2')
        collection = Collection(
            TestTable2.id == TestTable1.id + 1,
            tables=(TestTable1, TestTable2))
        result = collection.select(TestTable1.id)
        self.assertEqual([1], list(result))

    def test_joinInner(self):
        TestTable1 = make_table(1, 3, 'TestTable1')
        TestTable2 = make_table(2, 4, 'TestTable2')

        # Add a join table to the collection.
        collection = Collection(tables=TestTable1).joinInner(
            TestTable2, TestTable2.id == TestTable1.id)
        result = collection.select(TestTable1.id, TestTable2.id)
        self.assertContentEqual([(2, 2)], list(result))

    def test_joinOuter(self):
        TestTable1 = make_table(1, 3, 'TestTable1')
        TestTable2 = make_table(2, 4, 'TestTable2')

        # Add an outer-join table to the collection.
        collection = Collection(tables=TestTable1).joinOuter(
            TestTable2, TestTable2.id == TestTable1.id)
        result = collection.select(TestTable1.id, TestTable2.id)
        self.assertContentEqual([(1, None), (2, 2)], list(result))

    def test_select_store(self):
        TestTable = make_table(1, 2)
        collection = Collection(tables=TestTable)

        store = FakeStore()
        self.assertNotEqual(store, collection.store)

        collection_with_store = collection.use(store)
        self.assertEqual(store, collection_with_store.store)

        self.assertEqual([], collection_with_store.select(TestTable))