~launchpad-pqm/launchpad/devel

11168.3.1 by Jeroen Vermeulen
Test cruft and lint in Soyuz.
1
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
8687.15.34 by Karl Fogel
Add license header blocks to .py, .zcml, and .pt files that don't have it
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
4
"""Test model and set utilities used for publishing."""
5
6
from zope.component import getUtility
7675.687.70 by Michael Nelson
More changes to get test_publishing passing.
7
from zope.security.proxy import removeSecurityProxy
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
8
8857.2.2 by Michael Nelson
Updated tests after simplifying solution.
9
from canonical.database.constants import UTC_NOW
14185.1.9 by William Grant
Merge TestSPPHModel into test_publishing_models.
10
from canonical.launchpad.webapp.publisher import canonical_url
14185.1.2 by William Grant
Add SPPH.getFileByName, to get the changelog if it exists.
11
from canonical.testing.layers import (
12
    LaunchpadFunctionalLayer,
13
    LaunchpadZopelessLayer,
14
    )
15
from lp.app.errors import NotFoundError
11458.1.1 by Jelmer Vernooij
Move enums of buildmaster.
16
from lp.buildmaster.enums import BuildStatus
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
17
from lp.soyuz.interfaces.publishing import (
18
    IPublishingSet,
19
    PackagePublishingStatus,
20
    )
7675.687.55 by Michael Nelson
Added queueBuild to IPackageBuildDerived and updated test to ensure its provided by IBinaryPackageBuild.
21
from lp.soyuz.tests.test_binarypackagebuild import BaseTestCaseWithThreeBuilds
14185.1.2 by William Grant
Add SPPH.getFileByName, to get the changelog if it exists.
22
from lp.testing import TestCaseWithFactory
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
23
24
25
class TestPublishingSet(BaseTestCaseWithThreeBuilds):
26
    """Tests the IPublishingSet utility implementation."""
27
28
    layer = LaunchpadZopelessLayer
29
30
    def setUp(self):
31
        """Use `SoyuzTestPublisher` to publish some sources in archives."""
32
        super(TestPublishingSet, self).setUp()
33
8857.2.4 by Michael Nelson
Changes after bac's review.
34
        # Ensure all the builds have been built.
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
35
        for build in self.builds:
7675.687.70 by Michael Nelson
More changes to get test_publishing passing.
36
            removeSecurityProxy(build).status = BuildStatus.FULLYBUILT
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
37
        self.publishing_set = getUtility(IPublishingSet)
38
8857.2.3 by Michael Nelson
Updated name style for helper method on test case.
39
    def _getBuildsForResults(self, results):
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
40
        # The method returns (SPPH, Build) tuples, we just want the build.
41
        return [result[1] for result in results]
42
8857.2.2 by Michael Nelson
Updated tests after simplifying solution.
43
    def test_getUnpublishedBuildsForSources_none_published(self):
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
44
        # If no binaries have been published then all builds are.
45
        results = self.publishing_set.getUnpublishedBuildsForSources(
46
            self.sources)
8857.2.3 by Michael Nelson
Updated name style for helper method on test case.
47
        unpublished_builds = self._getBuildsForResults(results)
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
48
8857.2.4 by Michael Nelson
Changes after bac's review.
49
        self.assertContentEqual(self.builds, unpublished_builds)
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
50
51
    def test_getUnpublishedBuildsForSources_one_published(self):
52
        # If we publish a binary for a build, it is no longer returned.
53
        bpr = self.publisher.uploadBinaryForBuild(self.builds[0], 'gedit')
14185.1.8 by William Grant
Fix lint.
54
        self.publisher.publishBinaryInArchive(
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
55
            bpr, self.sources[0].archive,
56
            status=PackagePublishingStatus.PUBLISHED)
57
58
        results = self.publishing_set.getUnpublishedBuildsForSources(
59
            self.sources)
8857.2.3 by Michael Nelson
Updated name style for helper method on test case.
60
        unpublished_builds = self._getBuildsForResults(results)
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
61
8857.2.4 by Michael Nelson
Changes after bac's review.
62
        self.assertContentEqual(self.builds[1:3], unpublished_builds)
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
63
64
    def test_getUnpublishedBuildsForSources_with_cruft(self):
8857.2.2 by Michael Nelson
Updated tests after simplifying solution.
65
        # SourcePackages that has a superseded binary are still considered
66
        # 'published'.
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
67
8857.2.2 by Michael Nelson
Updated tests after simplifying solution.
68
        # Publish the binaries for gedit as superseded, explicitly setting
69
        # the date published.
70
        bpr = self.publisher.uploadBinaryForBuild(self.builds[0], 'gedit')
71
        bpphs = self.publisher.publishBinaryInArchive(
72
            bpr, self.sources[0].archive,
73
            status=PackagePublishingStatus.SUPERSEDED)
74
        for bpph in bpphs:
7659.7.5 by Julian Edwards
Fix lots of broken stuff
75
            bpph.datepublished = UTC_NOW
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
76
77
        results = self.publishing_set.getUnpublishedBuildsForSources(
78
            self.sources)
8857.2.3 by Michael Nelson
Updated name style for helper method on test case.
79
        unpublished_builds = self._getBuildsForResults(results)
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
80
81
        # The original gedit build should not be included in the results as,
8857.2.2 by Michael Nelson
Updated tests after simplifying solution.
82
        # even though it is no longer published.
8857.2.4 by Michael Nelson
Changes after bac's review.
83
        self.assertContentEqual(self.builds[1:3], unpublished_builds)
8857.2.1 by Michael Nelson
Initiall work to ignore cruft for build statuses.
84
7675.444.8 by Muharem Hrnjadovic
Gavin's review comments
85
    def test_getChangesFileLFA(self):
7675.444.7 by Muharem Hrnjadovic
Added unit test for IPublishingSet.getChangesFileLFA().
86
        # The getChangesFileLFA() method finds the right LFAs.
87
        lfas = (
88
            self.publishing_set.getChangesFileLFA(hist.sourcepackagerelease)
89
            for hist in self.sources)
90
        urls = [lfa.http_url for lfa in lfas]
3691.57.67 by Stuart Bishop
Fix tests with hardcoded Librarian ports to not depend on domain or port
91
        self.assert_(urls[0].endswith('/94/gedit_666_source.changes'))
92
        self.assert_(urls[1].endswith('/96/firefox_666_source.changes'))
93
        self.assert_(urls[2].endswith(
94
            '/98/getting-things-gnome_666_source.changes'))
14185.1.2 by William Grant
Add SPPH.getFileByName, to get the changelog if it exists.
95
96
97
class TestSourcePackagePublishingHistory(TestCaseWithFactory):
98
99
    layer = LaunchpadFunctionalLayer
100
14185.1.9 by William Grant
Merge TestSPPHModel into test_publishing_models.
101
    def test_ancestry(self):
102
        """Ancestry can be traversed."""
103
        ancestor = self.factory.makeSourcePackagePublishingHistory()
104
        spph = self.factory.makeSourcePackagePublishingHistory(
105
            ancestor=ancestor)
106
        self.assertEquals(spph.ancestor.displayname, ancestor.displayname)
107
108
    def test_changelogUrl_missing(self):
109
        spr = self.factory.makeSourcePackageRelease(changelog=None)
110
        spph = self.factory.makeSourcePackagePublishingHistory(
111
            sourcepackagerelease=spr)
112
        self.assertEqual(None, spph.changelogUrl())
113
114
    def test_changelogUrl(self):
115
        spr = self.factory.makeSourcePackageRelease(
116
            changelog=self.factory.makeChangelog('foo', ['1.0']))
117
        spph = self.factory.makeSourcePackagePublishingHistory(
118
            sourcepackagerelease=spr)
119
        self.assertEqual(
120
            canonical_url(spph) + '/+files/%s' % spr.changelog.filename,
121
            spph.changelogUrl())
122
14185.1.2 by William Grant
Add SPPH.getFileByName, to get the changelog if it exists.
123
    def test_getFileByName_changelog(self):
124
        spr = self.factory.makeSourcePackageRelease(
125
            changelog=self.factory.makeLibraryFileAlias(filename='changelog'))
126
        spph = self.factory.makeSourcePackagePublishingHistory(
127
            sourcepackagerelease=spr)
128
        self.assertEqual(spr.changelog, spph.getFileByName('changelog'))
129
14185.1.3 by William Grant
Fix tests.
130
    def test_getFileByName_changelog_absent(self):
14185.1.2 by William Grant
Add SPPH.getFileByName, to get the changelog if it exists.
131
        spr = self.factory.makeSourcePackageRelease(changelog=None)
132
        spph = self.factory.makeSourcePackagePublishingHistory(
133
            sourcepackagerelease=spr)
14185.1.3 by William Grant
Fix tests.
134
        self.assertRaises(NotFoundError, spph.getFileByName, 'changelog')
14185.1.2 by William Grant
Add SPPH.getFileByName, to get the changelog if it exists.
135
14185.1.3 by William Grant
Fix tests.
136
    def test_getFileByName_unhandled_name(self):
14185.1.2 by William Grant
Add SPPH.getFileByName, to get the changelog if it exists.
137
        spph = self.factory.makeSourcePackagePublishingHistory()
14185.1.3 by William Grant
Fix tests.
138
        self.assertRaises(NotFoundError, spph.getFileByName, 'not-changelog')