9
9
from zope.component import getUtility
10
10
from zope.security.proxy import removeSecurityProxy
12
from canonical.launchpad.interfaces.lpstorm import IStore
12
13
from canonical.testing.layers import (
13
14
DatabaseFunctionalLayer,
14
15
LaunchpadZopelessLayer,
16
17
from lp.registry.interfaces.distribution import IDistributionSet
18
from lp.registry.model.distributionsourcepackage import (
19
DistributionSourcePackage,
20
DistributionSourcePackageInDatabase,
17
22
from lp.registry.model.karma import KarmaTotalCache
18
23
from lp.soyuz.enums import PackagePublishingStatus
19
24
from lp.soyuz.tests.test_publishing import SoyuzTestPublisher
37
42
dsp = naked_distribution.getSourcePackage(name='pmount')
38
43
self.assertEqual(None, dsp.summary)
45
def test_ensure_spph_creates_a_dsp_in_db(self):
46
# The DSP.ensure() class method creates a persistent instance
47
# if one does not exist.
48
spph = self.factory.makeSourcePackagePublishingHistory()
49
spph_dsp = spph.sourcepackagerelease.distrosourcepackage
50
DistributionSourcePackage.ensure(spph)
51
new_dsp = DistributionSourcePackage._get(
52
spph_dsp.distribution, spph_dsp.sourcepackagename)
53
self.assertIsNot(None, new_dsp)
54
self.assertIsNot(spph_dsp, new_dsp)
55
self.assertEqual(spph_dsp.distribution, new_dsp.distribution)
57
spph_dsp.sourcepackagename, new_dsp.sourcepackagename)
59
def test_ensure_spph_dsp_in_db_exists(self):
60
# The DSP.ensure() class method does not create duplicate
61
# persistent instances; it skips the query to create the DSP.
62
store = IStore(DistributionSourcePackageInDatabase)
63
start_count = store.find(DistributionSourcePackageInDatabase).count()
64
spph = self.factory.makeSourcePackagePublishingHistory()
65
DistributionSourcePackage.ensure(spph)
66
new_count = store.find(DistributionSourcePackageInDatabase).count()
67
self.assertEqual(start_count + 1, new_count)
68
final_count = store.find(DistributionSourcePackageInDatabase).count()
69
self.assertEqual(new_count, final_count)
71
def test_ensure_spph_does_not_create_dsp_in_db_non_primary_archive(self):
72
# The DSP.ensure() class method creates a persistent instance
73
# if one does not exist.
74
archive = self.factory.makeArchive()
75
spph = self.factory.makeSourcePackagePublishingHistory(
77
spph_dsp = spph.sourcepackagerelease.distrosourcepackage
78
DistributionSourcePackage.ensure(spph)
79
new_dsp = DistributionSourcePackage._get(
80
spph_dsp.distribution, spph_dsp.sourcepackagename)
81
self.assertIs(None, new_dsp)
83
def test_ensure_suitesourcepackage_creates_a_dsp_in_db(self):
84
# The DSP.ensure() class method creates a persistent instance
85
# if one does not exist.
86
sourcepackage = self.factory.makeSourcePackage()
87
DistributionSourcePackage.ensure(sourcepackage=sourcepackage)
88
new_dsp = DistributionSourcePackage._get(
89
sourcepackage.distribution, sourcepackage.sourcepackagename)
90
self.assertIsNot(None, new_dsp)
91
self.assertEqual(sourcepackage.distribution, new_dsp.distribution)
93
sourcepackage.sourcepackagename, new_dsp.sourcepackagename)
95
def test_delete_without_dsp_in_db(self):
96
# Calling delete() on a DSP without persistence returns False.
97
dsp = self.factory.makeDistributionSourcePackage()
98
self.assertFalse(dsp.delete())
100
def test_delete_with_dsp_in_db_with_history(self):
101
# Calling delete() on a persistent DSP with SPPH returns False.
102
# Once a package is uploaded, it cannot be deleted.
103
spph = self.factory.makeSourcePackagePublishingHistory()
104
dsp = spph.sourcepackagerelease.distrosourcepackage
105
DistributionSourcePackage.ensure(spph=spph)
107
self.assertFalse(dsp.delete())
109
def test_delete_with_dsp_in_db_without_history(self):
110
# Calling delete() on a persistent DSP without SPPH returns True.
111
# A package without history was a mistake.
112
sp = self.factory.makeSourcePackage()
113
DistributionSourcePackage.ensure(sourcepackage=sp)
115
dsp = sp.distribution_sourcepackage
116
self.assertTrue(dsp.delete())
41
119
class TestDistributionSourcePackageFindRelatedArchives(TestCaseWithFactory):