~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
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests that the PillarName materialized view is being maintained correctly.
"""

__metaclass__ = type

import unittest

from lp.services.database.sqlbase import cursor
from lp.testing.layers import LaunchpadZopelessLayer


class PillarNameTriggersTestCase(unittest.TestCase):
    layer = LaunchpadZopelessLayer

    def setUp(self):
        LaunchpadZopelessLayer.switchDbUser('testadmin')

    def testDistributionTable(self):
        cur = cursor()

        # Ensure our sample data is valid and that each Distribution.name
        # has a corresponding entry in PillarName.name
        cur.execute("""
            SELECT COUNT(*)
            FROM Distribution FULL OUTER JOIN PillarName
                ON Distribution.id = PillarName.distribution
            WHERE Distribution.name != PillarName.name
            """)
        self.failUnlessEqual(cur.fetchone()[0], 0)

        def is_in_sync(name):
            cur.execute("""
                SELECT COUNT(*)
                FROM Distribution, PillarName
                WHERE Distribution.id = PillarName.distribution
                    AND Distribution.name = PillarName.name
                    AND PillarName.product IS NULL
                    AND PillarName.project IS NULL
                    AND Distribution.name = %(name)s
                """, dict(name=name))
            return cur.fetchone()[0] == 1

        # Inserting a new Distribution will populate PillarName
        cur.execute("""
            INSERT INTO Distribution (
                name, description, domainname, owner, registrant,
                displayname, summary, title, members, mirror_admin
                )
                VALUES (
                    'whatever', 'whatever', 'whatever', 1, 1,
                    'whatever', 'whatever', 'whatever', 1, 1
                    )
            """)
        self.failUnless(is_in_sync('whatever'))

        # Updating the Distribution.name will propogate changes to PillarName
        cur.execute("""
            UPDATE Distribution SET name='whatever2' where name='whatever'
            """)
        self.failUnless(is_in_sync('whatever2'))

        # Updating other fields won't do any harm.
        cur.execute("""
            UPDATE Distribution SET description='whatever2'
            WHERE name='whatever2'
            """)
        self.failUnless(is_in_sync('whatever2'))

        # Deleting a Distribution removes the corresponding entry in
        # PillarName
        cur.execute("DELETE FROM Distribution WHERE name='whatever2'")
        cur.execute("SELECT COUNT(*) FROM PillarName WHERE name='whatever2'")
        self.failUnlessEqual(cur.fetchone()[0], 0)

    def testProductTable(self):
        cur = cursor()

        # Ensure our sample data is valid and that each Product.name
        # has a corresponding entry in PillarName.name
        cur.execute("""
            SELECT COUNT(*)
            FROM Product FULL OUTER JOIN PillarName
                ON Product.id = PillarName.product
            WHERE Product.name != PillarName.name
            """)
        self.failUnlessEqual(cur.fetchone()[0], 0)

        def is_in_sync(name):
            cur.execute("""
                SELECT COUNT(*)
                FROM Product, PillarName
                WHERE Product.id = PillarName.product
                    AND Product.name = PillarName.name
                    AND PillarName.distribution IS NULL
                    AND PillarName.project IS NULL
                    AND Product.name = %(name)s
                """, dict(name=name))
            return cur.fetchone()[0] == 1

        # Inserting a new Product will populate PillarName
        cur.execute("""
            INSERT INTO Product (
                owner, registrant, name, displayname, title, summary)
            VALUES (
                1, 1, 'whatever', 'whatever', 'whatever', 'whatever'
                )
            """)
        self.failUnless(is_in_sync('whatever'))

        # Updating the Product.name will propogate changes to PillarName
        cur.execute("""
            UPDATE Product SET name='whatever2' where name='whatever'
            """)
        self.failUnless(is_in_sync('whatever2'))

        # Updating other fields won't do any harm.
        cur.execute("""
            UPDATE Product SET summary='whatever2'
            WHERE name='whatever2'
            """)
        self.failUnless(is_in_sync('whatever2'))

        # Deleting a Product removes the corresponding entry in PillarName
        cur.execute("DELETE FROM Product WHERE name='whatever2'")
        cur.execute("SELECT COUNT(*) FROM PillarName WHERE name='whatever2'")
        self.failUnlessEqual(cur.fetchone()[0], 0)

    def testProjectTable(self):
        cur = cursor()

        # Ensure our sample data is valid and that each Project.name
        # has a corresponding entry in PillarName.name
        cur.execute("""
            SELECT COUNT(*)
            FROM Project FULL OUTER JOIN PillarName
                ON Project.id = PillarName.project
            WHERE Project.name != PillarName.name
            """)
        self.failUnlessEqual(cur.fetchone()[0], 0)

        def is_in_sync(name):
            cur.execute("""
                SELECT COUNT(*)
                FROM Project, PillarName
                WHERE Project.id = PillarName.project
                    AND Project.name = PillarName.name
                    AND PillarName.product IS NULL
                    AND PillarName.distribution IS NULL
                    AND Project.name = %(name)s
                """, dict(name=name))
            return cur.fetchone()[0] == 1

        # Inserting a new ProjectGroup will populate PillarName
        cur.execute("""
            INSERT INTO Project (
                name, owner, registrant, displayname, title, summary,
                description
                )
                VALUES (
                    'whatever', 1, 1, 'whatever', 'whatever',
                    'whatever', 'whatever'
                    )
            """)
        self.failUnless(is_in_sync('whatever'))

        # Updating the ProjectGroup.name will propogate changes to PillarName
        cur.execute("""
            UPDATE Project SET name='whatever2' where name='whatever'
            """)
        self.failUnless(is_in_sync('whatever2'))

        # Updating other fields won't do any harm.
        cur.execute("""
            UPDATE Project SET description='whatever2'
            WHERE name='whatever2'
            """)
        self.failUnless(is_in_sync('whatever2'))

        # Deleting a ProjectGroup removes the corresponding entry in
        # PillarName.
        cur.execute("DELETE FROM Project WHERE name='whatever2'")
        cur.execute("SELECT COUNT(*) FROM PillarName WHERE name='whatever2'")
        self.failUnlessEqual(cur.fetchone()[0], 0)