~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/registry/tests/test_dsp_vocabularies.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-07-28 17:34:34 UTC
  • mfrom: (13506.9.10 dsp-vocab-contracts)
  • Revision ID: launchpad@pqm.canonical.com-20110728173434-xm8jb9xmnhjuzb0i
[r=jcsackett][bug=817066] Fix DSP vocabs contract with
 IHugeVocabulary and its widgets. Added IDistribution adapters.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
 
14
14
class TestDistributionSourcePackageVocabulary(TestCaseWithFactory):
15
 
    """Test that the DistributionSourcePackageVocabulary behaves as
16
 
    expected."""
 
15
    """Test that the vocabulary behaves as expected."""
17
16
    layer = DatabaseFunctionalLayer
18
17
 
19
18
    def test_provides_ihugevocabulary(self):
21
20
            self.factory.makeDistribution())
22
21
        self.assertProvides(vocabulary, IHugeVocabulary)
23
22
 
24
 
    def test_toTerm_unbuilt_dsp(self):
25
 
        # If the source has no built binaries, the term's value contains a
26
 
        # string to that effect.
27
 
        dsp = self.factory.makeDistributionSourcePackage(
28
 
            sourcepackagename='foo')
29
 
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
30
 
        term = vocabulary.toTerm(dsp.name)
31
 
        self.assertEqual(dsp.sourcepackagename.name, term.title)
32
 
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
33
 
        self.assertEqual(expected_token, term.token)
34
 
        self.assertEqual('Not yet built.', term.value)
35
 
 
36
 
    def test_toTerm_built_single_binary(self):
 
23
    def test_init_IDistribution(self):
 
24
        # When the context is adaptable to IDistribution, it also provides
 
25
        # the distribution.
 
26
        dsp = self.factory.makeDistributionSourcePackage(
 
27
            sourcepackagename='foo')
 
28
        vocabulary = DistributionSourcePackageVocabulary(dsp)
 
29
        self.assertEqual(dsp, vocabulary.context)
 
30
        self.assertEqual(dsp.distribution, vocabulary.distribution)
 
31
 
 
32
    def test_init_dsp_bugtask(self):
 
33
        # A dsp bugtask can be the context
 
34
        dsp = self.factory.makeDistributionSourcePackage(
 
35
            sourcepackagename='foo')
 
36
        bugtask = self.factory.makeBugTask(target=dsp)
 
37
        vocabulary = DistributionSourcePackageVocabulary(bugtask)
 
38
        self.assertEqual(bugtask, vocabulary.context)
 
39
        self.assertEqual(dsp.distribution, vocabulary.distribution)
 
40
 
 
41
    def test_init_dsp_question(self):
 
42
        # A dsp bugtask can be the context
 
43
        dsp = self.factory.makeDistributionSourcePackage(
 
44
            sourcepackagename='foo')
 
45
        question = self.factory.makeQuestion(
 
46
            target=dsp, owner=dsp.distribution.owner)
 
47
        vocabulary = DistributionSourcePackageVocabulary(question)
 
48
        self.assertEqual(question, vocabulary.context)
 
49
        self.assertEqual(dsp.distribution, vocabulary.distribution)
 
50
 
 
51
    def test_init_no_distribution(self):
 
52
        # The distribution is None if the context cannot be adapted to a
 
53
        # distribution.
 
54
        project = self.factory.makeProduct()
 
55
        vocabulary = DistributionSourcePackageVocabulary(project)
 
56
        self.assertEqual(project, vocabulary.context)
 
57
        self.assertEqual(None, vocabulary.distribution)
 
58
 
 
59
    def test_getDistributionAndPackageName_distro_and_package(self):
 
60
        # getDistributionAndPackageName() returns a tuple of distribution
 
61
        # and package name when the text contains both.
 
62
        new_distro = self.factory.makeDistribution(name='fnord')
 
63
        vocabulary = DistributionSourcePackageVocabulary(None)
 
64
        distribution, package_name = vocabulary.getDistributionAndPackageName(
 
65
            'fnord/pting')
 
66
        self.assertEqual(new_distro, distribution)
 
67
        self.assertEqual('pting', package_name)
 
68
 
 
69
    def test_getDistributionAndPackageName_default_distro_and_package(self):
 
70
        # getDistributionAndPackageName() returns a tuple of the default
 
71
        # distribution and package name when the text is just a package name.
 
72
        default_distro = self.factory.makeDistribution(name='fnord')
 
73
        vocabulary = DistributionSourcePackageVocabulary(default_distro)
 
74
        distribution, package_name = vocabulary.getDistributionAndPackageName(
 
75
            'pting')
 
76
        self.assertEqual(default_distro, distribution)
 
77
        self.assertEqual('pting', package_name)
 
78
 
 
79
    def test_getDistributionAndPackageName_bad_distro_and_package(self):
 
80
        # getDistributionAndPackageName() returns a tuple of the default
 
81
        # distribution and package name when the distro in the text cannot
 
82
        # be matched to a real distro.
 
83
        default_distro = self.factory.makeDistribution(name='fnord')
 
84
        vocabulary = DistributionSourcePackageVocabulary(default_distro)
 
85
        distribution, package_name = vocabulary.getDistributionAndPackageName(
 
86
            'misspelled/pting')
 
87
        self.assertEqual(default_distro, distribution)
 
88
        self.assertEqual('pting', package_name)
 
89
 
 
90
    def test_contains_true(self):
 
91
        # The vocabulary contains DSPs that have SPPH in the distro.
 
92
        spph = self.factory.makeSourcePackagePublishingHistory()
 
93
        dsp = spph.sourcepackagerelease.distrosourcepackage
 
94
        vocabulary = DistributionSourcePackageVocabulary(dsp)
 
95
        self.assertTrue(dsp in vocabulary)
 
96
 
 
97
    def test_contains_false(self):
 
98
        # The vocabulary does not contain DSPs without SPPH.
 
99
        spn = self.factory.makeSourcePackageName(name='foo')
 
100
        dsp = self.factory.makeDistributionSourcePackage(
 
101
            sourcepackagename=spn)
 
102
        vocabulary = DistributionSourcePackageVocabulary(dsp)
 
103
        self.assertFalse(dsp in vocabulary)
 
104
 
 
105
    def test_toTerm_raises_error(self):
 
106
        # An error is raised for DSP/SPNs without publishing history.
 
107
        dsp = self.factory.makeDistributionSourcePackage(
 
108
            sourcepackagename='foo')
 
109
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
 
110
        self.assertRaises(LookupError, vocabulary.toTerm, dsp.name)
 
111
 
 
112
    def test_toTerm_none_raises_error(self):
 
113
        # An error is raised for SPN does not exist.
 
114
        vocabulary = DistributionSourcePackageVocabulary(None)
 
115
        self.assertRaises(LookupError, vocabulary.toTerm, 'non-existant')
 
116
 
 
117
    def test_toTerm_spn_and_default_distribution(self):
 
118
        # The vocabulary's distribution is used when only a SPN is passed.
 
119
        spph = self.factory.makeSourcePackagePublishingHistory()
 
120
        dsp = spph.sourcepackagerelease.distrosourcepackage
 
121
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
 
122
        term = vocabulary.toTerm(dsp.sourcepackagename)
 
123
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
 
124
        self.assertEqual(expected_token, term.token)
 
125
        self.assertEqual(dsp.sourcepackagename, term.value)
 
126
 
 
127
    def test_toTerm_spn_and_distribution(self):
 
128
        # The distribution is used with the spn if it is passed.
 
129
        spph = self.factory.makeSourcePackagePublishingHistory()
 
130
        dsp = spph.sourcepackagerelease.distrosourcepackage
 
131
        vocabulary = DistributionSourcePackageVocabulary(None)
 
132
        term = vocabulary.toTerm(dsp.sourcepackagename, dsp.distribution)
 
133
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
 
134
        self.assertEqual(expected_token, term.token)
 
135
        self.assertEqual(dsp.sourcepackagename, term.value)
 
136
 
 
137
    def test_toTerm_dsp(self):
 
138
        # The DSP's distribution is used when a DSP is passed.
 
139
        spph = self.factory.makeSourcePackagePublishingHistory()
 
140
        dsp = spph.sourcepackagerelease.distrosourcepackage
 
141
        vocabulary = DistributionSourcePackageVocabulary(dsp)
 
142
        term = vocabulary.toTerm(dsp)
 
143
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
 
144
        self.assertEqual(expected_token, term.token)
 
145
        self.assertEqual(dsp.sourcepackagename, term.value)
 
146
 
 
147
    def test_toTerm_unbuilt_title(self):
 
148
        # The DSP's distribution is used with the spn if it is passed.
 
149
        # Published, but unbuilt packages state the case in the term title.
 
150
        spph = self.factory.makeSourcePackagePublishingHistory()
 
151
        dsp = spph.sourcepackagerelease.distrosourcepackage
 
152
        vocabulary = DistributionSourcePackageVocabulary(dsp)
 
153
        term = vocabulary.toTerm(dsp)
 
154
        self.assertEqual('Not yet built.', term.title)
 
155
 
 
156
    def test_toTerm_built_single_binary_title(self):
37
157
        # The binary package name appears in the term's value.
38
158
        bpph = self.factory.makeBinaryPackagePublishingHistory()
39
159
        spr = bpph.binarypackagerelease.build.source_package_release
42
162
            distribution=bpph.distroseries.distribution)
43
163
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
44
164
        term = vocabulary.toTerm(spr.sourcepackagename)
45
 
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
46
 
        self.assertEqual(expected_token, term.token)
47
 
        self.assertEqual(bpph.binary_package_name, term.value)
 
165
        self.assertEqual(bpph.binary_package_name, term.title)
48
166
 
49
 
    def test_toTerm_built_multiple_binary(self):
 
167
    def test_toTerm_built_multiple_binary_title(self):
50
168
        # All of the binary package names appear in the term's value.
51
169
        spph = self.factory.makeSourcePackagePublishingHistory()
52
170
        spr = spph.sourcepackagerelease
63
181
        dsp = spr.distrosourcepackage
64
182
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
65
183
        term = vocabulary.toTerm(spr.sourcepackagename)
66
 
        expected_token = '%s/%s' % (dsp.distribution.name, dsp.name)
67
 
        self.assertEqual(expected_token, term.token)
68
 
        self.assertEqual(', '.join(expected_names), term.value)
 
184
        self.assertEqual(', '.join(expected_names), term.title)
 
185
 
 
186
    def test_getTermByToken_error(self):
 
187
        # An error is raised if the token does not match a published DSP.
 
188
        dsp = self.factory.makeDistributionSourcePackage(
 
189
            sourcepackagename='foo')
 
190
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
 
191
        token = '%s/%s' % (dsp.distribution.name, dsp.name)
 
192
        self.assertRaises(LookupError, vocabulary.getTermByToken, token)
 
193
 
 
194
    def test_getTermByToken_token(self):
 
195
        # The term is return if it matches a published DSP.
 
196
        spph = self.factory.makeSourcePackagePublishingHistory()
 
197
        dsp = spph.sourcepackagerelease.distrosourcepackage
 
198
        vocabulary = DistributionSourcePackageVocabulary(dsp.distribution)
 
199
        token = '%s/%s' % (dsp.distribution.name, dsp.name)
 
200
        term = vocabulary.getTermByToken(token)
 
201
        self.assertEqual(dsp.sourcepackagename, term.value)
 
202
 
 
203
    def test_searchForTerms_without_distribution(self):
 
204
        # An empty result set is return if the vocabulary has no distribution
 
205
        # and the search does not provide distribution information.
 
206
        spph = self.factory.makeSourcePackagePublishingHistory()
 
207
        dsp = spph.sourcepackagerelease.distrosourcepackage
 
208
        vocabulary = DistributionSourcePackageVocabulary(dsp.name)
 
209
        results = vocabulary.searchForTerms(dsp.name)
 
210
        self.assertIs(0, results.count())
69
211
 
70
212
    def test_searchForTerms_None(self):
71
213
        # Searching for nothing gets you that.
72
214
        vocabulary = DistributionSourcePackageVocabulary(
73
215
            self.factory.makeDistribution())
74
216
        results = vocabulary.searchForTerms()
75
 
        self.assertIs(None, results)
 
217
        self.assertIs(0, results.count())
76
218
 
77
219
    def assertTermsEqual(self, expected, actual):
78
220
        # Assert two given terms are equal.
125
267
        spn = self.factory.getOrMakeSourcePackageName('xorg')
126
268
        spr = self.factory.makeSourcePackageRelease(sourcepackagename=spn)
127
269
        das = self.factory.makeDistroArchSeries()
128
 
        spph = self.factory.makeSourcePackagePublishingHistory(
 
270
        self.factory.makeSourcePackagePublishingHistory(
129
271
            sourcepackagerelease=spr, distroseries=das.distroseries)
130
272
        for name in ('xorg-common', 'xorg-server', 'xorg-video-intel'):
131
273
            bpn = self.factory.getOrMakeBinaryPackageName(name)
133
275
                source_package_release=spr, distroarchseries=das)
134
276
            bpr = self.factory.makeBinaryPackageRelease(
135
277
                binarypackagename=bpn, build=bpb)
136
 
            bpph = self.factory.makeBinaryPackagePublishingHistory(
 
278
            self.factory.makeBinaryPackagePublishingHistory(
137
279
                binarypackagerelease=bpr, distroarchseries=das)
138
280
        vocabulary = DistributionSourcePackageVocabulary(
139
281
            context=das.distroseries.distribution)