~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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Test the Distribution Source Package vocabulary."""

__metaclass__ = type

from canonical.launchpad.webapp.vocabulary import IHugeVocabulary
from canonical.testing.layers import DatabaseFunctionalLayer
from lp.registry.vocabularies import DistributionSourcePackageVocabulary
from lp.testing import TestCaseWithFactory


class TestDistributionSourcePackageVocabulary(TestCaseWithFactory):
    """Test that the vocabulary behaves as expected."""
    layer = DatabaseFunctionalLayer

    def test_provides_ihugevocabulary(self):
        vocabulary = DistributionSourcePackageVocabulary(
            self.factory.makeDistribution())
        self.assertProvides(vocabulary, IHugeVocabulary)

    def test_init_IDistribution(self):
        # When the context is adaptable to IDistribution, it also provides
        # the distribution.
        dsp = self.factory.makeDistributionSourcePackage(
            sourcepackagename='foo')
        vocabulary = DistributionSourcePackageVocabulary(dsp)
        self.assertEqual(dsp, vocabulary.context)
        self.assertEqual(dsp.distribution, vocabulary.distribution)

    def test_init_dsp_bugtask(self):
        # A dsp bugtask can be the context
        dsp = self.factory.makeDistributionSourcePackage(
            sourcepackagename='foo')
        bugtask = self.factory.makeBugTask(target=dsp)
        vocabulary = DistributionSourcePackageVocabulary(bugtask)
        self.assertEqual(bugtask, vocabulary.context)
        self.assertEqual(dsp.distribution, vocabulary.distribution)

    def test_init_dsp_question(self):
        # A dsp bugtask can be the context
        dsp = self.factory.makeDistributionSourcePackage(
            sourcepackagename='foo')
        question = self.factory.makeQuestion(
            target=dsp, owner=dsp.distribution.owner)
        vocabulary = DistributionSourcePackageVocabulary(question)
        self.assertEqual(question, vocabulary.context)
        self.assertEqual(dsp.distribution, vocabulary.distribution)

    def test_init_no_distribution(self):
        # The distribution is None if the context cannot be adapted to a
        # distribution.
        project = self.factory.makeProduct()
        vocabulary = DistributionSourcePackageVocabulary(project)
        self.assertEqual(project, vocabulary.context)
        self.assertEqual(None, vocabulary.distribution)

    def test_getDistributionAndPackageName_distro_and_package(self):
        # getDistributionAndPackageName() returns a tuple of distribution
        # and package name when the text contains both.
        new_distro = self.factory.makeDistribution(name='fnord')
        vocabulary = DistributionSourcePackageVocabulary(None)
        distribution, package_name = vocabulary.getDistributionAndPackageName(
            'fnord/pting')
        self.assertEqual(new_distro, distribution)
        self.assertEqual('pting', package_name)

    def test_getDistributionAndPackageName_default_distro_and_package(self):
        # getDistributionAndPackageName() returns a tuple of the default
        # distribution and package name when the text is just a package name.
        default_distro = self.factory.makeDistribution(name='fnord')
        vocabulary = DistributionSourcePackageVocabulary(default_distro)
        distribution, package_name = vocabulary.getDistributionAndPackageName(
            'pting')
        self.assertEqual(default_distro, distribution)
        self.assertEqual('pting', package_name)

    def test_getDistributionAndPackageName_bad_distro_and_package(self):
        # getDistributionAndPackageName() returns a tuple of the default
        # distribution and package name when the distro in the text cannot
        # be matched to a real distro.
        default_distro = self.factory.makeDistribution(name='fnord')
        vocabulary = DistributionSourcePackageVocabulary(default_distro)
        distribution, package_name = vocabulary.getDistributionAndPackageName(
            'misspelled/pting')
        self.assertEqual(default_distro, distribution)
        self.assertEqual('pting', package_name)

    def test_contains_true(self):
        # The vocabulary contains DSPs that have SPPH in the distro.
        spph = self.factory.makeSourcePackagePublishingHistory()
        dsp = spph.sourcepackagerelease.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(dsp)
        self.assertTrue(dsp in vocabulary)

    def test_contains_false(self):
        # The vocabulary does not contain DSPs without SPPH.
        spn = self.factory.makeSourcePackageName(name='foo')
        dsp = self.factory.makeDistributionSourcePackage(
            sourcepackagename=spn)
        vocabulary = DistributionSourcePackageVocabulary(dsp)
        self.assertFalse(dsp in vocabulary)

    def test_toTerm_raises_error(self):
        # An error is raised for DSP/SPNs without publishing history.
        dsp = self.factory.makeDistributionSourcePackage(
            sourcepackagename='foo')
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
        self.assertRaises(LookupError, vocabulary.toTerm, dsp.name)

    def test_toTerm_none_raises_error(self):
        # An error is raised for SPN does not exist.
        vocabulary = DistributionSourcePackageVocabulary(None)
        self.assertRaises(LookupError, vocabulary.toTerm, 'non-existant')

    def test_toTerm_spn_and_default_distribution(self):
        # The vocabulary's distribution is used when only a SPN is passed.
        spph = self.factory.makeSourcePackagePublishingHistory()
        dsp = spph.sourcepackagerelease.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
        term = vocabulary.toTerm(dsp.sourcepackagename)
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
        self.assertEqual(expected_token, term.token)
        self.assertEqual(dsp.sourcepackagename, term.value)

    def test_toTerm_spn_and_distribution(self):
        # The distribution is used with the spn if it is passed.
        spph = self.factory.makeSourcePackagePublishingHistory()
        dsp = spph.sourcepackagerelease.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(None)
        term = vocabulary.toTerm(dsp.sourcepackagename, dsp.distribution)
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
        self.assertEqual(expected_token, term.token)
        self.assertEqual(dsp.sourcepackagename, term.value)

    def test_toTerm_dsp(self):
        # The DSP's distribution is used when a DSP is passed.
        spph = self.factory.makeSourcePackagePublishingHistory()
        dsp = spph.sourcepackagerelease.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(dsp)
        term = vocabulary.toTerm(dsp)
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
        self.assertEqual(expected_token, term.token)
        self.assertEqual(dsp.sourcepackagename, term.value)

    def test_toTerm_unbuilt_title(self):
        # The DSP's distribution is used with the spn if it is passed.
        # Published, but unbuilt packages state the case in the term title.
        spph = self.factory.makeSourcePackagePublishingHistory()
        dsp = spph.sourcepackagerelease.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(dsp)
        term = vocabulary.toTerm(dsp)
        self.assertEqual('Not yet built.', term.title)

    def test_toTerm_built_single_binary_title(self):
        # The binary package name appears in the term's value.
        bpph = self.factory.makeBinaryPackagePublishingHistory()
        spr = bpph.binarypackagerelease.build.source_package_release
        dsp = self.factory.makeDistributionSourcePackage(
            sourcepackagename=spr.sourcepackagename,
            distribution=bpph.distroseries.distribution)
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
        term = vocabulary.toTerm(spr.sourcepackagename)
        self.assertEqual(bpph.binary_package_name, term.title)

    def test_toTerm_built_multiple_binary_title(self):
        # All of the binary package names appear in the term's value.
        spph = self.factory.makeSourcePackagePublishingHistory()
        spr = spph.sourcepackagerelease
        das = self.factory.makeDistroArchSeries(
            distroseries=spph.distroseries)
        expected_names = []
        for i in xrange(20):
            bpb = self.factory.makeBinaryPackageBuild(
                source_package_release=spr, distroarchseries=das)
            bpr = self.factory.makeBinaryPackageRelease(build=bpb)
            expected_names.append(bpr.name)
            self.factory.makeBinaryPackagePublishingHistory(
                binarypackagerelease=bpr, distroarchseries=das)
        dsp = spr.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
        term = vocabulary.toTerm(spr.sourcepackagename)
        self.assertEqual(', '.join(expected_names), term.title)

    def test_getTermByToken_error(self):
        # An error is raised if the token does not match a published DSP.
        dsp = self.factory.makeDistributionSourcePackage(
            sourcepackagename='foo')
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
        token = '%s/%s' % (dsp.distribution.name, dsp.name)
        self.assertRaises(LookupError, vocabulary.getTermByToken, token)

    def test_getTermByToken_token(self):
        # The term is return if it matches a published DSP.
        spph = self.factory.makeSourcePackagePublishingHistory()
        dsp = spph.sourcepackagerelease.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
        token = '%s/%s' % (dsp.distribution.name, dsp.name)
        term = vocabulary.getTermByToken(token)
        self.assertEqual(dsp.sourcepackagename, term.value)

    def test_searchForTerms_without_distribution(self):
        # An empty result set is return if the vocabulary has no distribution
        # and the search does not provide distribution information.
        spph = self.factory.makeSourcePackagePublishingHistory()
        dsp = spph.sourcepackagerelease.distrosourcepackage
        vocabulary = DistributionSourcePackageVocabulary(dsp.name)
        results = vocabulary.searchForTerms(dsp.name)
        self.assertIs(0, results.count())

    def test_searchForTerms_None(self):
        # Searching for nothing gets you that.
        vocabulary = DistributionSourcePackageVocabulary(
            self.factory.makeDistribution())
        results = vocabulary.searchForTerms()
        self.assertIs(0, results.count())

    def assertTermsEqual(self, expected, actual):
        # Assert two given terms are equal.
        self.assertEqual(expected.token, actual.token)
        self.assertEqual(expected.title, actual.title)
        self.assertEqual(expected.value, actual.value)

    def test_searchForTerms_published_source(self):
        # When we search for a source package name that is published, it is
        # returned.
        spph = self.factory.makeSourcePackagePublishingHistory()
        vocabulary = DistributionSourcePackageVocabulary(
            context=spph.distroseries.distribution)
        results = vocabulary.searchForTerms(query=spph.source_package_name)
        self.assertTermsEqual(
            vocabulary.toTerm(spph.source_package_name), list(results)[0])

    def test_searchForTerms_unpublished_source(self):
        # If the source package name isn't published in the distribution,
        # we get no results.
        spph = self.factory.makeSourcePackagePublishingHistory()
        vocabulary = DistributionSourcePackageVocabulary(
            context=self.factory.makeDistribution())
        results = vocabulary.searchForTerms(query=spph.source_package_name)
        self.assertEqual([], list(results))

    def test_searchForTerms_unpublished_binary(self):
        # If the binary package name isn't published in the distribution,
        # we get no results.
        bpph = self.factory.makeBinaryPackagePublishingHistory()
        vocabulary = DistributionSourcePackageVocabulary(
            context=self.factory.makeDistribution())
        results = vocabulary.searchForTerms(query=bpph.binary_package_name)
        self.assertEqual([], list(results))

    def test_searchForTerms_published_binary(self):
        # We can search for a binary package name, which returns the
        # relevant SPN.
        bpph = self.factory.makeBinaryPackagePublishingHistory()
        distribution = bpph.distroarchseries.distroseries.distribution
        vocabulary = DistributionSourcePackageVocabulary(
            context=distribution)
        spn = bpph.binarypackagerelease.build.source_package_release.name
        results = vocabulary.searchForTerms(query=bpph.binary_package_name)
        self.assertTermsEqual(vocabulary.toTerm(spn), list(results)[0])

    def test_searchForTerms_published_multiple_binaries(self):
        # Searching for a subset of a binary package name returns the SPN
        # that built the binary package.
        spn = self.factory.getOrMakeSourcePackageName('xorg')
        spr = self.factory.makeSourcePackageRelease(sourcepackagename=spn)
        das = self.factory.makeDistroArchSeries()
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagerelease=spr, distroseries=das.distroseries)
        for name in ('xorg-common', 'xorg-server', 'xorg-video-intel'):
            bpn = self.factory.getOrMakeBinaryPackageName(name)
            bpb = self.factory.makeBinaryPackageBuild(
                source_package_release=spr, distroarchseries=das)
            bpr = self.factory.makeBinaryPackageRelease(
                binarypackagename=bpn, build=bpb)
            self.factory.makeBinaryPackagePublishingHistory(
                binarypackagerelease=bpr, distroarchseries=das)
        vocabulary = DistributionSourcePackageVocabulary(
            context=das.distroseries.distribution)
        results = vocabulary.searchForTerms(query='xorg-se')
        self.assertTermsEqual(vocabulary.toTerm(spn), list(results)[0])