~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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for distroseries vocabularies in `lp.registry.vocabularies`."""

__metaclass__ = type

from datetime import (
    datetime,
    timedelta,
    )

from pytz import utc
from testtools.matchers import Equals
from zope.component import getUtility
from zope.schema.interfaces import IVocabularyFactory
from zope.security.proxy import removeSecurityProxy

from canonical.database.sqlbase import flush_database_caches
from canonical.launchpad.webapp.vocabulary import IHugeVocabulary
from canonical.testing.layers import DatabaseFunctionalLayer
from lp.registry.interfaces.distroseries import IDistroSeriesSet
from lp.registry.vocabularies import DistroSeriesDerivationVocabulary
from lp.testing import (
    StormStatementRecorder,
    TestCaseWithFactory,
    )
from lp.testing.matchers import HasQueryCount


class TestDistroSeriesDerivationVocabulary(TestCaseWithFactory):
    """Tests for `DistroSeriesDerivationVocabulary`."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestDistroSeriesDerivationVocabulary, self).setUp()
        self.all_distroseries = getUtility(IDistroSeriesSet).search()
        self.all_series_with_arch = [
            series for series in self.all_distroseries
            if series.architecturecount != 0]

    def test_registration(self):
        # DistroSeriesDerivationVocabulary is registered as a named
        # utility for IVocabularyFactory.
        self.assertEqual(
            getUtility(IVocabularyFactory, name="DistroSeriesDerivation"),
            DistroSeriesDerivationVocabulary)

    def test_interfaces(self):
        # DistroSeriesDerivationVocabulary instances provide
        # IVocabulary and IVocabularyTokenized.
        distroseries = self.factory.makeDistroSeries()
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        self.assertProvides(vocabulary, IHugeVocabulary)

    def test_distribution_with_non_derived_series(self):
        # Given a distribution with series, none of which are derived, the
        # vocabulary factory returns a vocabulary for all distroseries in all
        # distributions *except* the given distribution.
        distroseries = self.factory.makeDistroSeries()
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        expected_distroseries = (
            set(self.all_series_with_arch).difference(
                distroseries.distribution.series))
        observed_distroseries = set(term.value for term in vocabulary)
        self.assertEqual(expected_distroseries, observed_distroseries)

    def test_distribution_with_non_derived_series_no_arch(self):
        # Only the parents with DistroArchSeries configured in LP are
        # returned in the DistroSeriesDerivationVocabulary if no other
        # derived distroseries are present in the distribution.
        distroseries = self.factory.makeDistroSeries()
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        another_parent_no_arch = self.factory.makeDistroSeries()
        observed_distroseries = set(term.value for term in vocabulary)

        self.assertNotIn(another_parent_no_arch, observed_distroseries)

    def makeDistroSeriesWithDistroArch(self, *args, **kwargs):
        # Helper method to create a distroseries with a
        # DistroArchSeries.
        distroseries = self.factory.makeDistroSeries(*args, **kwargs)
        self.factory.makeDistroArchSeries(distroseries=distroseries)
        return distroseries

    def test_distribution_with_derived_series(self):
        # Given a distribution with series, one or more of which are derived,
        # the vocabulary factory returns a vocabulary for all distroseries of
        # the distribution from which the derived series have been
        # derived with distroarchseries setup in LP.
        parent_distroseries = self.makeDistroSeriesWithDistroArch()
        self.makeDistroSeriesWithDistroArch()
        distroseries = self.factory.makeDistroSeries()
        self.factory.makeDistroSeriesParent(
            derived_series=distroseries, parent_series=parent_distroseries)
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        expected_distroseries = set(parent_distroseries.distribution.series)
        observed_distroseries = set(term.value for term in vocabulary)
        self.assertContentEqual(expected_distroseries, observed_distroseries)

    def test_distribution_with_derived_series_no_arch(self):
        # Distroseries with no DistroArchSeries can be parents if this
        # is not the first derivation in the distribution.
        parent_distroseries = self.makeDistroSeriesWithDistroArch()
        another_parent_no_arch = self.factory.makeDistroSeries(
            distribution=parent_distroseries.distribution)
        distroseries = self.factory.makeDistroSeries()
        self.factory.makeDistroSeriesParent(
            derived_series=distroseries, parent_series=parent_distroseries)
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        observed_distroseries = set(term.value for term in vocabulary)

        self.assertIn(another_parent_no_arch, observed_distroseries)

    def test_distribution_with_derived_series_from_multiple_parents(self):
        # Given a distribution with series, one or more of which are derived
        # from multiple parents, the vocabulary factory returns a vocabulary
        # for all distroseries of the distribution*s* from which the derived
        # series have been derived.
        parent_distroseries = self.makeDistroSeriesWithDistroArch()
        another_parent_distroseries = self.makeDistroSeriesWithDistroArch()
        self.makeDistroSeriesWithDistroArch(
            distribution=parent_distroseries.distribution)
        distroseries = self.factory.makeDistroSeries()
        self.factory.makeDistroSeriesParent(
            derived_series=distroseries, parent_series=parent_distroseries)
        self.factory.makeDistroSeriesParent(
            derived_series=distroseries,
            parent_series=another_parent_distroseries)
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        expected_distroseries = set(
            parent_distroseries.distribution.series).union(
                set(another_parent_distroseries.distribution.series))
        observed_distroseries = set(term.value for term in vocabulary)
        self.assertEqual(expected_distroseries, observed_distroseries)

    def test_distribution_with_derived_series_of_self(self):
        # Given a distribution with series derived from other of its series
        # (which shouldn't happen), the vocabulary factory returns a
        # vocabulary for all distroseries in all distributions *except* the
        # given distribution.
        parent_distroseries = self.makeDistroSeriesWithDistroArch()
        distroseries = self.factory.makeDistroSeries(
            distribution=parent_distroseries.distribution)
        self.factory.makeDistroSeriesParent(
            derived_series=distroseries, parent_series=parent_distroseries)
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        expected_distroseries = (
            set(self.all_series_with_arch).difference(
                distroseries.distribution.series))
        observed_distroseries = set(term.value for term in vocabulary)
        self.assertEqual(expected_distroseries, observed_distroseries)

    def test_distroseries(self):
        # Given a distroseries, the vocabulary factory returns the vocabulary
        # the same as for its distribution.
        distroseries = self.makeDistroSeriesWithDistroArch()
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        expected_distroseries = (
            set(self.all_series_with_arch).difference(
                distroseries.distribution.series))
        observed_distroseries = set(term.value for term in vocabulary)
        self.assertEqual(expected_distroseries, observed_distroseries)

    def test_ordering(self):
        # The vocabulary is sorted by distribution display name then by the
        # date the distroseries was created, newest first.
        now = datetime.now(utc)
        two_days_ago = now - timedelta(2)
        six_days_ago = now - timedelta(7)

        aaa = self.factory.makeDistribution(displayname="aaa")
        aaa_series_older = self.makeDistroSeriesWithDistroArch(
            name="aaa-series-older", distribution=aaa)
        removeSecurityProxy(aaa_series_older).date_created = six_days_ago
        aaa_series_newer = self.makeDistroSeriesWithDistroArch(
            name="aaa-series-newer", distribution=aaa)
        removeSecurityProxy(aaa_series_newer).date_created = two_days_ago

        bbb = self.factory.makeDistribution(displayname="bbb")
        bbb_series_older = self.makeDistroSeriesWithDistroArch(
            name="bbb-series-older", distribution=bbb)
        removeSecurityProxy(bbb_series_older).date_created = six_days_ago
        bbb_series_newer = self.makeDistroSeriesWithDistroArch(
            name="bbb-series-newer", distribution=bbb)
        removeSecurityProxy(bbb_series_newer).date_created = two_days_ago

        ccc = self.factory.makeDistribution(displayname="ccc")
        ccc_series = self.makeDistroSeriesWithDistroArch(distribution=ccc)

        vocabulary = DistroSeriesDerivationVocabulary(ccc_series)
        expected_distroseries = [
            aaa_series_newer, aaa_series_older,
            bbb_series_newer, bbb_series_older]
        observed_distroseries = list(term.value for term in vocabulary)
        # observed_distroseries will contain distroseries from the sample
        # data, so we must only look at the set of distroseries we have
        # created.
        observed_distroseries = [
            series for series in observed_distroseries
            if series in expected_distroseries]
        self.assertEqual(expected_distroseries, observed_distroseries)

    def test_queries_for_distribution_with_non_derived_series(self):
        for index in range(10):
            self.factory.makeDistroSeries()
        distribution = self.factory.makeDistribution()
        distroseries = self.factory.makeDistroSeries(
            distribution=distribution)
        flush_database_caches()
        # Reload distroseries and distribution; these will reasonably already
        # be loaded before using the vocabulary.
        distroseries.distribution
        # Getting terms issues two queries: one to search for parent serieses
        # (of which there are none) and a second for all serieses.
        with StormStatementRecorder() as recorder:
            DistroSeriesDerivationVocabulary(distroseries).terms
            self.assertThat(recorder, HasQueryCount(Equals(2)))

    def test_queries_for_distribution_with_derived_series(self):
        for index in range(10):
            self.factory.makeDistroSeries()
        distribution = self.factory.makeDistribution()
        parent_distroseries = self.factory.makeDistroSeries()
        distroseries = self.factory.makeDistroSeries(
            distribution=distribution)
        self.factory.makeDistroSeriesParent(
            derived_series=distroseries, parent_series=parent_distroseries)
        flush_database_caches()
        # Reload distroseries and distribution; these will reasonably already
        # be loaded before using the vocabulary.
        distroseries.distribution
        # Getting terms issues 2 queries to find parent serieses.
        with StormStatementRecorder() as recorder:
            DistroSeriesDerivationVocabulary(distroseries).terms
            self.assertThat(recorder, HasQueryCount(Equals(2)))

    def test_no_duplicates(self):
        # No duplicates are present in the returned vocabulary.
        distroseries = self.makeDistroSeriesWithDistroArch()
        vocabulary = DistroSeriesDerivationVocabulary(distroseries)
        expected_distroseries = (
            set(self.all_series_with_arch).difference(
                distroseries.distribution.series))
        observed_distroseries = [term.value for term in vocabulary]

        self.assertContentEqual(
            expected_distroseries,
            observed_distroseries)