~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-06-10 19:18:44 UTC
  • mfrom: (13175.2.3 bug-735991)
  • Revision ID: launchpad@pqm.canonical.com-20110610191844-wo6ryjj31ooqr3aa
[r=bac][bug=735991] more efficient implementation of
        Person.getBugSubscriberPackages();
        call canonical_url(self.context) exactly once when ~person/+packagebugs
        is rendered.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from lazr.lifecycle.snapshot import Snapshot
9
9
import pytz
10
10
from storm.store import Store
11
 
from testtools.matchers import LessThan
 
11
from testtools.matchers import (
 
12
    Equals,
 
13
    LessThan,
 
14
    )
12
15
import transaction
13
16
from zope.component import getUtility
14
17
from zope.interface import providedBy
374
377
        with person_logged_in(person):
375
378
            self.assertRaises(Unauthorized, getattr, other, 'canWrite')
376
379
 
 
380
    def makeSubscribedDistroSourcePackages(self):
 
381
        # Create a person, a distribution and four
 
382
        # DistributionSourcePacakage. Subscribe the person to two
 
383
        # DSPs, and subscribe another person to another DSP.
 
384
        user = self.factory.makePerson()
 
385
        distribution = self.factory.makeDistribution()
 
386
        dsp1 = self.factory.makeDistributionSourcePackage(
 
387
            sourcepackagename='sp-b', distribution=distribution)
 
388
        distribution = self.factory.makeDistribution()
 
389
        dsp2 = self.factory.makeDistributionSourcePackage(
 
390
            sourcepackagename='sp-a', distribution=distribution)
 
391
        dsp3 = self.factory.makeDistributionSourcePackage(
 
392
            sourcepackagename='sp-c', distribution=distribution)
 
393
        with person_logged_in(user):
 
394
            dsp1.addSubscription(user, subscribed_by=user)
 
395
            dsp2.addSubscription(user, subscribed_by=user)
 
396
        dsp4 = self.factory.makeDistributionSourcePackage(
 
397
            sourcepackagename='sp-d', distribution=distribution)
 
398
        other_user = self.factory.makePerson()
 
399
        with person_logged_in(other_user):
 
400
            dsp4.addSubscription(other_user, subscribed_by=other_user)
 
401
        return user, dsp1, dsp2
 
402
 
 
403
    def test_getBugSubscriberPackages(self):
 
404
        # getBugSubscriberPackages() returns the DistributionSourcePackages
 
405
        # to which a user is subscribed.
 
406
        user, dsp1, dsp2 = self.makeSubscribedDistroSourcePackages()
 
407
 
 
408
        # We cannot directly compare the objects returned by
 
409
        # getBugSubscriberPackages() with the expected DSPs:
 
410
        # These are different objects and the class does not have
 
411
        # an __eq__ operator. So we compare the attributes distribution
 
412
        # and sourcepackagename.
 
413
 
 
414
        def get_distribution(dsp):
 
415
            return dsp.distribution
 
416
 
 
417
        def get_spn(dsp):
 
418
            return dsp.sourcepackagename
 
419
 
 
420
        result = user.getBugSubscriberPackages()
 
421
        self.assertEqual(
 
422
            [get_distribution(dsp) for dsp in (dsp2, dsp1)],
 
423
            [get_distribution(dsp) for dsp in result])
 
424
        self.assertEqual(
 
425
            [get_spn(dsp) for dsp in (dsp2, dsp1)],
 
426
            [get_spn(dsp) for dsp in result])
 
427
 
 
428
    def test_getBugSubscriberPackages__one_query(self):
 
429
        # getBugSubscriberPackages() retrieves all objects
 
430
        # needed to build the DistributionSourcePackages in
 
431
        # one SQL query.
 
432
        user, dsp1, dsp2 = self.makeSubscribedDistroSourcePackages()
 
433
        Store.of(user).invalidate()
 
434
        with StormStatementRecorder() as recorder:
 
435
            list(user.getBugSubscriberPackages())
 
436
        self.assertThat(recorder, HasQueryCount(Equals(1)))
 
437
 
377
438
 
378
439
class TestPersonStates(TestCaseWithFactory):
379
440