~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Ian Booth
  • Date: 2011-04-19 15:10:57 UTC
  • mfrom: (12868 devel)
  • mto: This revision was merged to the branch mainline in revision 12983.
  • Revision ID: ian.booth@canonical.com-20110419151057-he56y6k29c4zeiyk
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
from storm.store import Store
10
10
import transaction
11
11
from zope.component import getUtility
12
 
from zope.security.interfaces import Unauthorized
13
12
from zope.security.proxy import removeSecurityProxy
14
13
 
15
14
from canonical.launchpad.webapp.authorization import check_permission
30
29
    IDistroSeriesDifference,
31
30
    IDistroSeriesDifferenceSource,
32
31
    )
 
32
from lp.registry.interfaces.pocket import PackagePublishingPocket
 
33
from lp.registry.model.distroseriesdifference import (
 
34
    most_recent_comments,
 
35
    most_recent_publications,
 
36
    )
33
37
from lp.services.propertycache import get_property_cache
34
38
from lp.soyuz.enums import PackageDiffStatus
35
39
from lp.soyuz.interfaces.publishing import PackagePublishingStatus
301
305
        ds_diff = self.factory.makeDistroSeriesDifference(
302
306
            source_package_name_str="foonew")
303
307
 
304
 
        with person_logged_in(ds_diff.owner):
 
308
        person = self.factory.makePerson()
 
309
        with person_logged_in(person):
305
310
            dsd_comment = ds_diff.addComment(
306
 
                ds_diff.owner, "Wait until version 2.1")
 
311
                person, "Wait until version 2.1")
307
312
 
308
313
        self.assertEqual(ds_diff, dsd_comment.distro_series_difference)
309
314
 
312
317
        # most recent comment first.
313
318
        ds_diff = self.factory.makeDistroSeriesDifference()
314
319
 
315
 
        with person_logged_in(ds_diff.owner):
 
320
        person = self.factory.makePerson()
 
321
        with person_logged_in(person):
316
322
            dsd_comment = ds_diff.addComment(
317
 
                ds_diff.owner, "Wait until version 2.1")
 
323
                person, "Wait until version 2.1")
318
324
            dsd_comment_2 = ds_diff.addComment(
319
 
                ds_diff.owner, "Wait until version 2.1")
 
325
                person, "Wait until version 2.1")
320
326
 
321
327
        self.assertEqual(
322
328
            [dsd_comment_2, dsd_comment], list(ds_diff.getComments()))
323
329
 
324
 
    def test_addComment_not_public(self):
325
 
        # Comments cannot be added with launchpad.View.
 
330
    def test_latest_comment(self):
 
331
        # latest_comment is a property containing the most recent comment.
326
332
        ds_diff = self.factory.makeDistroSeriesDifference()
327
 
        person = self.factory.makePerson()
328
 
 
329
 
        with person_logged_in(person):
330
 
            self.assertTrue(check_permission('launchpad.View', ds_diff))
331
 
            self.assertFalse(check_permission('launchpad.Edit', ds_diff))
332
 
            self.assertRaises(Unauthorized, getattr, ds_diff, 'addComment')
 
333
 
 
334
        with person_logged_in(ds_diff.owner):
 
335
            comments = [
 
336
                ds_diff.addComment(
 
337
                    ds_diff.owner, "Wait until version 2.1"),
 
338
                ds_diff.addComment(
 
339
                    ds_diff.owner, "Wait until version 2.1"),
 
340
                ]
 
341
 
 
342
        self.assertEqual(comments[-1], ds_diff.latest_comment)
333
343
 
334
344
    def test_addComment_for_owners(self):
335
345
        # Comments can be added by any of the owners of the derived
336
346
        # series.
337
347
        ds_diff = self.factory.makeDistroSeriesDifference()
338
348
 
339
 
        with person_logged_in(ds_diff.owner):
 
349
        person = self.factory.makePerson()
 
350
        with person_logged_in(person):
340
351
            self.assertTrue(check_permission('launchpad.Edit', ds_diff))
341
352
            diff_comment = ds_diff.addComment(
342
353
                ds_diff.derived_series.owner, "Boo")
371
382
            sorted([packageset.name for packageset in packagesets]),
372
383
            [packageset.name for packageset in ds_diff.getPackageSets()])
373
384
 
374
 
    def test_blacklist_not_public(self):
375
 
        # Differences cannot be blacklisted without edit access.
376
 
        ds_diff = self.factory.makeDistroSeriesDifference()
377
 
        person = self.factory.makePerson()
378
 
 
379
 
        with person_logged_in(person):
380
 
            self.assertTrue(check_permission('launchpad.View', ds_diff))
381
 
            self.assertFalse(check_permission('launchpad.Edit', ds_diff))
382
 
            self.assertRaises(Unauthorized, getattr, ds_diff, 'blacklist')
383
 
 
384
385
    def test_blacklist_default(self):
385
386
        # By default the current version is blacklisted.
386
387
        ds_diff = self.factory.makeDistroSeriesDifference()
387
388
 
388
 
        with person_logged_in(ds_diff.owner):
 
389
        with person_logged_in(self.factory.makePerson()):
389
390
            ds_diff.blacklist()
390
391
 
391
392
        self.assertEqual(
396
397
        # All versions are blacklisted with the all=True param.
397
398
        ds_diff = self.factory.makeDistroSeriesDifference()
398
399
 
399
 
        with person_logged_in(ds_diff.owner):
 
400
        with person_logged_in(self.factory.makePerson()):
400
401
            ds_diff.blacklist(all=True)
401
402
 
402
403
        self.assertEqual(
408
409
        ds_diff = self.factory.makeDistroSeriesDifference(
409
410
            status=DistroSeriesDifferenceStatus.BLACKLISTED_CURRENT)
410
411
 
411
 
        with person_logged_in(ds_diff.owner):
 
412
        with person_logged_in(self.factory.makePerson()):
412
413
            ds_diff.unblacklist()
413
414
 
414
415
        self.assertEqual(
429
430
            status=PackagePublishingStatus.PENDING,
430
431
            version='1.0')
431
432
 
432
 
        with person_logged_in(ds_diff.owner):
 
433
        with person_logged_in(self.factory.makePerson()):
433
434
            ds_diff.unblacklist()
434
435
 
435
436
        self.assertEqual(
624
625
                'parent': parent_changelog,
625
626
            })
626
627
 
627
 
        with person_logged_in(ds_diff.owner):
628
 
            ds_diff.requestPackageDiffs(ds_diff.owner)
 
628
        person = self.factory.makePerson()
 
629
        with person_logged_in(person):
 
630
            ds_diff.requestPackageDiffs(person)
629
631
 
630
632
        self.assertEqual(
631
633
            '1.2', ds_diff.package_diff.to_source.version)
654
656
                'parent': parent_changelog,
655
657
            })
656
658
 
657
 
        with person_logged_in(ds_diff.owner):
658
 
            ds_diff.requestPackageDiffs(ds_diff.owner)
 
659
        person = self.factory.makePerson()
 
660
        with person_logged_in(person):
 
661
            ds_diff.requestPackageDiffs(person)
659
662
        self.assertIs(None, ds_diff.package_diff)
660
663
        self.assertIsNot(None, ds_diff.parent_package_diff)
661
664
 
674
677
                'derived': changelog_lfa,
675
678
                'parent': changelog_lfa,
676
679
            })
677
 
        with person_logged_in(ds_diff.owner):
 
680
        person = self.factory.makePerson()
 
681
        with person_logged_in(person):
678
682
            self.assertRaisesWithContent(
679
683
                DistroSeriesDifferenceError,
680
684
                "Can not generate package diffs for a resolved difference.",
681
 
                ds_diff.requestPackageDiffs, ds_diff.owner)
 
685
                ds_diff.requestPackageDiffs, person)
682
686
 
683
687
    def test_package_diff_urls_none(self):
684
688
        # URLs to the package diffs are only present when the diffs
719
723
        naked_ds_diff.source_version = '1.4'
720
724
        naked_ds_diff.parent_source_version = '1.5'
721
725
 
722
 
        self.assertEqual(ds_diff.source_package_release.version, '1.4')
723
 
        self.assertEqual(ds_diff.parent_source_package_release.version, '1.5')
 
726
        self.assertEqual('1.4', ds_diff.source_package_release.version)
 
727
        self.assertEqual(
 
728
            '1.5', ds_diff.parent_source_package_release.version)
 
729
 
 
730
    def createPublication(self, spn, versions, distroseries,
 
731
                          status=PackagePublishingStatus.PUBLISHED):
 
732
        changelog_lfa = self.factory.makeChangelog(spn.name, versions)
 
733
        transaction.commit() # Yay, librarian.
 
734
        spr = self.factory.makeSourcePackageRelease(
 
735
            sourcepackagename=spn, version=versions[0],
 
736
            changelog=changelog_lfa)
 
737
        return self.factory.makeSourcePackagePublishingHistory(
 
738
            sourcepackagerelease=spr, distroseries=distroseries,
 
739
            status=status, pocket=PackagePublishingPocket.RELEASE)
 
740
 
 
741
    def test_existing_packagediff_is_linked_when_dsd_created(self):
 
742
        # When a relevant packagediff already exists, it is linked to the
 
743
        # DSD when it is created.
 
744
        derived_series = self.factory.makeDistroSeries(
 
745
            parent_series=self.factory.makeDistroSeries())
 
746
        spn = self.factory.getOrMakeSourcePackageName(
 
747
            name=self.factory.getUniqueString())
 
748
        parent_spph = self.createPublication(
 
749
            spn, ['1.2-1', '1.0-1'], derived_series.parent_series)
 
750
        spph = self.createPublication(
 
751
            spn, ['1.1-1', '1.0-1'], derived_series)
 
752
        base_spph = self.createPublication(
 
753
            spn, ['1.0-1'], derived_series,
 
754
            status=PackagePublishingStatus.SUPERSEDED)
 
755
        pd = self.factory.makePackageDiff(
 
756
            from_source=base_spph.sourcepackagerelease,
 
757
            to_source=spph.sourcepackagerelease)
 
758
        # factory.makeDistroSeriesDifference() will always create
 
759
        # publications to be helpful. We don't need the help in this case.
 
760
        dsd = getUtility(IDistroSeriesDifferenceSource).new(
 
761
            derived_series, spn)
 
762
        self.assertEqual(pd, dsd.package_diff)
 
763
 
 
764
    def _initDiffWithMultiplePendingPublications(self, versions, parent):
 
765
        ds_diff = self.factory.makeDistroSeriesDifference(versions=versions)
 
766
        if parent:
 
767
            series = ds_diff.derived_series.parent_series
 
768
            version = versions.get('parent')
 
769
        else:
 
770
            series = ds_diff.derived_series
 
771
            version = versions.get('derived')
 
772
        pub1 = self.factory.makeSourcePackagePublishingHistory(
 
773
            sourcepackagename=ds_diff.source_package_name,
 
774
            distroseries=series,
 
775
            status=PackagePublishingStatus.PENDING,
 
776
            version=version)
 
777
        pub2 = self.factory.makeSourcePackagePublishingHistory(
 
778
            sourcepackagename=ds_diff.source_package_name,
 
779
            distroseries=series,
 
780
            status=PackagePublishingStatus.PENDING,
 
781
            version=version)
 
782
        return ds_diff, pub1, pub2
 
783
 
 
784
    def test_multiple_pending_publications_derived(self):
 
785
        # If multiple (PENDING) publications are present in the derived
 
786
        # series, the most recent is returned.
 
787
        ds_diff, _, pub = self._initDiffWithMultiplePendingPublications(
 
788
            versions={'derived': '1.0'},
 
789
            parent=False)
 
790
        self.assertEqual(
 
791
            pub,
 
792
            ds_diff.source_package_release.publishings[0])
 
793
 
 
794
    def test_multiple_pending_publications_parent(self):
 
795
        # If multiple (PENDING) publications are present in the parent
 
796
        # series, the most recent is returned.
 
797
        ds_diff, _, pub = self._initDiffWithMultiplePendingPublications(
 
798
            versions={'parent': '1.0'},
 
799
            parent=True)
 
800
        self.assertEqual(
 
801
            pub,
 
802
            ds_diff.parent_source_package_release.publishings[0])
724
803
 
725
804
 
726
805
class DistroSeriesDifferenceLibrarianTestCase(TestCaseWithFactory):
859
938
            ds_diff.derived_series, 'fooname')
860
939
 
861
940
        self.assertEqual(ds_diff, result)
 
941
 
 
942
 
 
943
class TestMostRecentComments(TestCaseWithFactory):
 
944
 
 
945
    layer = DatabaseFunctionalLayer
 
946
 
 
947
    def test_most_recent_comments(self):
 
948
        derived_series = self.factory.makeDistroSeries(
 
949
            parent_series=self.factory.makeDistroSeries())
 
950
        dsds = set(
 
951
            self.factory.makeDistroSeriesDifference(
 
952
                derived_series=derived_series) for index in xrange(5))
 
953
        expected_comments = set()
 
954
        for dsd in dsds:
 
955
            # Add a couple of comments.
 
956
            self.factory.makeDistroSeriesDifferenceComment(dsd)
 
957
            expected_comments.add(
 
958
                self.factory.makeDistroSeriesDifferenceComment(dsd))
 
959
        self.assertContentEqual(
 
960
            expected_comments, most_recent_comments(dsds))
 
961
 
 
962
 
 
963
class TestMostRecentPublications(TestCaseWithFactory):
 
964
 
 
965
    layer = DatabaseFunctionalLayer
 
966
 
 
967
    def create_difference(self, derived_series):
 
968
        # Create a new DistroSeriesDifference
 
969
        version = self.factory.getUniqueInteger()
 
970
        versions = {
 
971
            'base': u'1.%d' % version,
 
972
            'derived': u'1.%dderived1' % version,
 
973
            'parent': u'1.%d-1' % version,
 
974
            }
 
975
        dsd = self.factory.makeDistroSeriesDifference(
 
976
            derived_series=derived_series,
 
977
            versions=versions)
 
978
        # Push a base_version in... not sure how better to do it.
 
979
        removeSecurityProxy(dsd).base_version = versions["base"]
 
980
        return dsd
 
981
 
 
982
    def test_simple(self):
 
983
        derived_series = self.factory.makeDistroSeries(
 
984
            parent_series=self.factory.makeDistroSeries())
 
985
        dsds = [
 
986
            self.create_difference(derived_series),
 
987
            self.create_difference(derived_series),
 
988
            ]
 
989
        # Derived publication.
 
990
        source_pubs_by_spn_id_expected = set(
 
991
            (dsd.source_package_name.id, dsd.source_pub)
 
992
            for dsd in dsds)
 
993
        source_pubs_by_spn_id_found = most_recent_publications(
 
994
            dsds, in_parent=False, statuses=(
 
995
                PackagePublishingStatus.PUBLISHED,
 
996
                PackagePublishingStatus.PENDING))
 
997
        self.assertContentEqual(
 
998
            source_pubs_by_spn_id_expected,
 
999
            source_pubs_by_spn_id_found)
 
1000
        # Parent publication
 
1001
        parent_source_pubs_by_spn_id_expected = set(
 
1002
            (dsd.source_package_name.id, dsd.parent_source_pub)
 
1003
            for dsd in dsds)
 
1004
        parent_source_pubs_by_spn_id_found = most_recent_publications(
 
1005
            dsds, in_parent=True, statuses=(
 
1006
                PackagePublishingStatus.PUBLISHED,
 
1007
                PackagePublishingStatus.PENDING))
 
1008
        self.assertContentEqual(
 
1009
            parent_source_pubs_by_spn_id_expected,
 
1010
            parent_source_pubs_by_spn_id_found)
 
1011
 
 
1012
    def test_statuses(self):
 
1013
        derived_series = self.factory.makeDistroSeries(
 
1014
            parent_series=self.factory.makeDistroSeries())
 
1015
        dsd = self.create_difference(derived_series)
 
1016
        # Change the derived source publication to DELETED.
 
1017
        removeSecurityProxy(dsd.source_pub).status = (
 
1018
            PackagePublishingStatus.DELETED)
 
1019
        # Searching for DELETED will find the source publication.
 
1020
        self.assertContentEqual(
 
1021
            [(dsd.source_package_name.id, dsd.source_pub)],
 
1022
            most_recent_publications(
 
1023
                [dsd], in_parent=False, statuses=(
 
1024
                    PackagePublishingStatus.DELETED,)))
 
1025
        # Searched for DELETED will *not* find the parent publication.
 
1026
        self.assertContentEqual(
 
1027
            [], most_recent_publications(
 
1028
                [dsd], in_parent=True, statuses=(
 
1029
                    PackagePublishingStatus.DELETED,)))
 
1030
 
 
1031
    def test_match_version(self):
 
1032
        # When match_version is True, the version of the publications (well,
 
1033
        # the release) must exactly match those recorded on the
 
1034
        # DistroSeriesDifference.
 
1035
        derived_series = self.factory.makeDistroSeries(
 
1036
            parent_series=self.factory.makeDistroSeries())
 
1037
        dsd = self.create_difference(derived_series)
 
1038
        # Modify the release version.
 
1039
        removeSecurityProxy(
 
1040
            dsd.source_package_release.sourcepackagerelease).version += u"2"
 
1041
        # Searching with match_version=False finds the publication.
 
1042
        self.assertContentEqual(
 
1043
            [(dsd.source_package_name.id, dsd.source_pub)],
 
1044
            most_recent_publications(
 
1045
                [dsd], in_parent=False, match_version=False,
 
1046
                statuses=(PackagePublishingStatus.PUBLISHED,)))
 
1047
        # Searching with match_version=True does not find the publication.
 
1048
        self.assertContentEqual(
 
1049
            [], most_recent_publications(
 
1050
                [dsd], in_parent=False, match_version=True,
 
1051
                statuses=(PackagePublishingStatus.PUBLISHED,)))