~launchpad-pqm/launchpad/devel

14193.3.1 by Benji York
add pofile staticstics update job and related config
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Tests for merging translations."""
5
6
__metaclass__ = type
7
8
14612.2.1 by William Grant
format-imports on lib/. So many imports.
9
from lp.app.enums import ServiceUsage
14605.1.1 by Curtis Hovey
Moved canonical.config to lp.services.
10
from lp.services.config import config
14193.3.1 by Benji York
add pofile staticstics update job and related config
11
from lp.services.job.interfaces.job import (
12
    IJobSource,
13
    IRunnableJob,
14
    )
14612.2.1 by William Grant
format-imports on lib/. So many imports.
15
from lp.services.webapp.testing import verifyObject
14193.3.3 by Benji York
fix some lint
16
from lp.testing import TestCaseWithFactory
14448.4.3 by Benji York
use a context manager for db user switching
17
from lp.testing.dbuser import dbuser
14612.2.1 by William Grant
format-imports on lib/. So many imports.
18
from lp.testing.layers import LaunchpadZopelessLayer
14193.3.1 by Benji York
add pofile staticstics update job and related config
19
from lp.translations.interfaces.pofilestatsjob import IPOFileStatsJobSource
20
from lp.translations.interfaces.side import TranslationSide
21
from lp.translations.model import pofilestatsjob
22
from lp.translations.model.pofilestatsjob import POFileStatsJob
23
24
25
class TestPOFileStatsJob(TestCaseWithFactory):
26
27
    layer = LaunchpadZopelessLayer
28
29
    def test_job_interface(self):
30
        # Instances of POFileStatsJob are runnable jobs.
31
        verifyObject(IRunnableJob, POFileStatsJob(0))
32
33
    def test_source_interface(self):
34
        # The POFileStatsJob class is a source of POFileStatsJobs.
35
        verifyObject(IPOFileStatsJobSource, POFileStatsJob)
36
        verifyObject(IJobSource, POFileStatsJob)
37
38
    def test_run(self):
39
        # Running a job causes the POFile statistics to be updated.
40
        singular = self.factory.getUniqueString()
41
        pofile = self.factory.makePOFile(side=TranslationSide.UPSTREAM)
42
        # Create a message so we have something to have statistics about.
43
        self.factory.makePOTMsgSet(pofile.potemplate, singular)
44
        # The statistics start at 0.
45
        self.assertEqual(pofile.potemplate.messageCount(), 0)
46
        job = pofilestatsjob.schedule(pofile.id)
47
        # Just scheduling the job doesn't update the statistics.
48
        self.assertEqual(pofile.potemplate.messageCount(), 0)
14448.4.3 by Benji York
use a context manager for db user switching
49
        with dbuser(config.pofile_stats.dbuser):
50
            job.run()
14448.4.1 by Benji York
add permissions needed for script and add test that fails without
51
        # Now that the job ran, the statistics have been updated.
52
        self.assertEqual(pofile.potemplate.messageCount(), 1)
53
14515.4.18 by Benji York
add missing permission (and tweak a test name)
54
    def test_run_with_product(self):
14448.4.1 by Benji York
add permissions needed for script and add test that fails without
55
        product = self.factory.makeProduct(
56
            translations_usage=ServiceUsage.LAUNCHPAD)
57
        productseries = self.factory.makeProductSeries(product=product)
58
        potemplate = self.factory.makePOTemplate(productseries=productseries)
59
        pofile = self.factory.makePOFile('en', potemplate)
60
        # Create a message so we have something to have statistics about.
61
        singular = self.factory.getUniqueString()
62
        self.factory.makePOTMsgSet(pofile.potemplate, singular)
63
        # The statistics are still at 0, even though there is a message.
64
        self.assertEqual(potemplate.messageCount(), 0)
65
        job = pofilestatsjob.schedule(pofile.id)
66
        # Just scheduling the job doesn't update the statistics.
67
        self.assertEqual(pofile.potemplate.messageCount(), 0)
14448.4.3 by Benji York
use a context manager for db user switching
68
        with dbuser(config.pofile_stats.dbuser):
69
            job.run()
14448.4.1 by Benji York
add permissions needed for script and add test that fails without
70
        # Now that the job ran, the statistics have been updated.
71
        self.assertEqual(pofile.potemplate.messageCount(), 1)
72
14193.3.1 by Benji York
add pofile staticstics update job and related config
73
    def test_iterReady(self):
74
        # The POFileStatsJob class provides a way to iterate over the jobs
75
        # that are ready to run.  Initially, there aren't any.
76
        self.assertEqual(len(list(POFileStatsJob.iterReady())), 0)
77
        # We need a POFile to update.
78
        pofile = self.factory.makePOFile(side=TranslationSide.UPSTREAM)
79
        # If we schedule a job, then we'll get it back.
80
        job = pofilestatsjob.schedule(pofile.id)
81
        self.assertIs(list(POFileStatsJob.iterReady())[0], job)
14193.3.4 by Benji York
- add test
82
14193.3.6 by Benji York
switch to a max of two translation jobs
83
    def test_second_job_is_scheduled(self):
84
        # If there is already one POFileStatsJob scheduled for a particular
85
        # POFile, then a second one is scheduled.
14193.3.4 by Benji York
- add test
86
        self.assertEqual(len(list(POFileStatsJob.iterReady())), 0)
87
        # We need a POFile to update.
88
        pofile = self.factory.makePOFile(side=TranslationSide.UPSTREAM)
89
        # If we schedule a job, then there will be one scheduled.
90
        pofilestatsjob.schedule(pofile.id)
91
        self.assertIs(len(list(POFileStatsJob.iterReady())), 1)
14193.3.6 by Benji York
switch to a max of two translation jobs
92
        # If we attempt to schedule another job for the same POFile, a new job
93
        # is added.
94
        pofilestatsjob.schedule(pofile.id)
95
        self.assertIs(len(list(POFileStatsJob.iterReady())), 2)
14515.4.2 by Benji York
checkpoint
96
14515.4.13 by Benji York
- fix a bug found in staging
97
    def assertJobUpdatesStats(self, pofile1, pofile2):
98
        # Create a single POTMsgSet and add it to only one of the POTemplates.
99
        self.factory.makeSuggestion(pofile1)
100
        self.factory.makeSuggestion(pofile2)
101
        # The statistics start at 0.
102
        self.assertEqual(pofile1.getStatistics(), (0, 0, 0, 0))
103
        self.assertEqual(pofile2.getStatistics(), (0, 0, 0, 0))
104
        job = pofilestatsjob.schedule(pofile1.id)
105
        # Just scheduling the job doesn't update the statistics.
106
        self.assertEqual(pofile1.getStatistics(), (0, 0, 0, 0))
107
        self.assertEqual(pofile2.getStatistics(), (0, 0, 0, 0))
108
        with dbuser(config.pofile_stats.dbuser):
109
            job.run()
110
        # Now that the job ran, the statistics for the POFile have been
111
        # updated.
112
        self.assertEqual(pofile1.getStatistics(), (0, 0, 0, 1))
113
        # The statistics for the other POFile is also updated as a result of
114
        # running the job for the other POFile because they share
115
        # translations.
116
        self.assertEqual(pofile2.getStatistics(), (0, 0, 0, 1))
117
118
    def test_run_with_project_shared_template(self):
14515.4.6 by Benji York
checkpoint
119
        # Create a product with two series and sharing POTemplates
120
        # in different series ('devel' and 'stable').
121
        product = self.factory.makeProduct(
122
            translations_usage=ServiceUsage.LAUNCHPAD)
123
        devel = self.factory.makeProductSeries(
124
            name='devel', product=product)
125
        stable = self.factory.makeProductSeries(
126
            name='stable', product=product)
127
128
        # POTemplate is a 'sharing' one if it has the same name ('messages').
14515.4.9 by Benji York
a little cleanup
129
        template1 = self.factory.makePOTemplate(devel, name='messages')
130
        template2 = self.factory.makePOTemplate(stable, name='messages')
14515.4.6 by Benji York
checkpoint
131
14515.4.5 by Benji York
checkpoint
132
        self.factory.makeLanguage('en-tt')
14515.4.6 by Benji York
checkpoint
133
        pofile1 = self.factory.makePOFile('en-tt', template1)
134
        pofile2 = self.factory.makePOFile('en-tt', template2)
135
14515.4.13 by Benji York
- fix a bug found in staging
136
        self.assertJobUpdatesStats(pofile1, pofile2)
137
138
    def test_run_with_product_and_distro_translation_sharing(self):
139
        language = self.factory.makeLanguage('en-tt')
140
        distroseries = self.factory.makeUbuntuDistroSeries()
141
        distroseries.distribution.translation_focus = distroseries
142
        sourcepackagename = self.factory.makeSourcePackageName()
143
        sourcepackage = self.factory.makeSourcePackage(
144
            distroseries=distroseries,
145
            sourcepackagename=sourcepackagename)
146
        productseries = self.factory.makeProductSeries()
147
        sourcepackage.setPackaging(
148
            productseries, self.factory.makePerson())
149
150
        # Create template ready for sharing on the Ubuntu side.
151
        template1 = self.factory.makePOTemplate(
152
            distroseries=distroseries,
153
            sourcepackagename=sourcepackagename,
154
            name='messages')
155
        pofile1 = self.factory.makePOFile(
156
            language=language, potemplate=template1)
157
158
        # Create template ready for sharing on the upstream side.
159
        template2 = self.factory.makePOTemplate(
160
            productseries=productseries, name='messages')
161
        pofile2 = template2.getPOFileByLang(language.code)
162
163
        self.assertJobUpdatesStats(pofile1, pofile2)
164
165
    def test_run_with_distro_translation_sharing(self):
166
        language = self.factory.makeLanguage('en-tt')
167
        distroseries1 = self.factory.makeUbuntuDistroSeries()
168
        distroseries1.distribution.translation_focus = distroseries1
169
        sourcepackagename = self.factory.makeSourcePackageName()
14515.4.14 by Benji York
fix lint
170
        self.factory.makeSourcePackage(
14515.4.13 by Benji York
- fix a bug found in staging
171
            distroseries=distroseries1,
172
            sourcepackagename=sourcepackagename)
173
        distroseries2 = self.factory.makeUbuntuDistroSeries()
174
        distroseries2.distribution.translation_focus = distroseries2
14515.4.14 by Benji York
fix lint
175
        self.factory.makeSourcePackage(
14515.4.13 by Benji York
- fix a bug found in staging
176
            distroseries=distroseries2,
177
            sourcepackagename=sourcepackagename)
178
179
        template1 = self.factory.makePOTemplate(
180
            distroseries=distroseries1,
181
            sourcepackagename=sourcepackagename,
182
            name='messages')
183
        pofile1 = self.factory.makePOFile(
184
            language=language, potemplate=template1)
185
186
        template2 = self.factory.makePOTemplate(
187
            distroseries=distroseries2,
188
            sourcepackagename=sourcepackagename,
189
            name='messages')
190
        pofile2 = template2.getPOFileByLang(language.code)
191
192
        self.assertJobUpdatesStats(pofile1, pofile2)