~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/scripts/tests/test_garbo.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-02 10:15:39 UTC
  • mfrom: (13581.1.2 bug-814576-gmb)
  • Revision ID: launchpad@pqm.canonical.com-20110802101539-o6suv9c6uqyi8z4r
[r=danilo][bug=814576] Provide an UnusedPOTMsgSetPruner to clean-up
 unused POTMsgSets inside the garbo-daily cron job. Fix by Graham,
 improved by Stuart, landed on devel by Danilo.  Drums played by a Cow.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
from pytz import UTC
18
18
from storm.expr import (
 
19
    In,
19
20
    Min,
 
21
    Not,
20
22
    SQL,
21
23
    )
22
24
from storm.locals import (
95
97
    TestCase,
96
98
    TestCaseWithFactory,
97
99
    )
 
100
from lp.translations.model.potemplate import POTemplate
 
101
from lp.translations.model.potmsgset import POTMsgSet
 
102
from lp.translations.model.translationtemplateitem import (
 
103
    TranslationTemplateItem,
 
104
    )
98
105
 
99
106
 
100
107
class TestGarboScript(TestCase):
897
904
        self.assertNotEqual(0, unmigrated())
898
905
        self.runHourly()
899
906
        self.assertEqual(0, unmigrated())
 
907
 
 
908
    def test_UnusedPOTMsgSetPruner_removes_obsolete_message_sets(self):
 
909
        # UnusedPOTMsgSetPruner removes any POTMsgSet that are
 
910
        # participating in a POTemplate only as obsolete messages.
 
911
        LaunchpadZopelessLayer.switchDbUser('testadmin')
 
912
        pofile = self.factory.makePOFile()
 
913
        translation_message = self.factory.makeCurrentTranslationMessage(
 
914
            pofile=pofile)
 
915
        translation_message.potmsgset.setSequence(
 
916
            pofile.potemplate, 0)
 
917
        transaction.commit()
 
918
        store = IMasterStore(POTMsgSet)
 
919
        obsolete_msgsets = store.find(
 
920
            POTMsgSet,
 
921
            TranslationTemplateItem.potmsgset == POTMsgSet.id,
 
922
            TranslationTemplateItem.sequence == 0)
 
923
        self.assertNotEqual(0, obsolete_msgsets.count())
 
924
        self.runDaily()
 
925
        self.assertEqual(0, obsolete_msgsets.count())
 
926
 
 
927
    def test_UnusedPOTMsgSetPruner_removes_unreferenced_message_sets(self):
 
928
        # If a POTMsgSet is not referenced by any templates the
 
929
        # UnusedPOTMsgSetPruner will remove it.
 
930
        LaunchpadZopelessLayer.switchDbUser('testadmin')
 
931
        potmsgset = self.factory.makePOTMsgSet()
 
932
        # Cheekily drop any references to the POTMsgSet we just created.
 
933
        store = IMasterStore(POTMsgSet)
 
934
        store.execute(
 
935
            "DELETE FROM TranslationTemplateItem WHERE potmsgset = %s"
 
936
            % potmsgset.id)
 
937
        transaction.commit()
 
938
        unreferenced_msgsets = store.find(
 
939
            POTMsgSet,
 
940
            Not(In(
 
941
                POTMsgSet.id,
 
942
                SQL("SELECT potmsgset FROM TranslationTemplateItem"))))
 
943
        self.assertNotEqual(0, unreferenced_msgsets.count())
 
944
        self.runDaily()
 
945
        self.assertEqual(0, unreferenced_msgsets.count())