1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Tests for merging translations."""
__metaclass__ = type
from lazr.lifecycle.event import ObjectModifiedEvent
from lazr.lifecycle.snapshot import Snapshot
import transaction
from zope.component import getUtility
from zope.event import notify
from canonical.launchpad.webapp.testing import verifyObject
from canonical.testing.layers import LaunchpadZopelessLayer
from lp.registry.interfaces.packaging import IPackagingUtil
from lp.services.job.interfaces.job import (
IRunnableJob,
JobStatus,
)
from lp.testing import (
EventRecorder,
person_logged_in,
TestCaseWithFactory,
)
from lp.translations.interfaces.potemplate import IPOTemplate
from lp.translations.interfaces.side import TranslationSide
from lp.translations.interfaces.translationpackagingjob import (
ITranslationPackagingJobSource,
)
from lp.translations.model.potemplate import POTemplateSubset
from lp.translations.model.translationpackagingjob import (
TranslationMergeJob,
TranslationPackagingJob,
TranslationSplitJob,
TranslationTemplateChangeJob,
)
from lp.translations.model.translationsharingjob import (
TranslationSharingJob,
TranslationSharingJobDerived,
)
from lp.translations.tests.test_translationsplitter import (
make_shared_potmsgset,
)
def make_translation_merge_job(factory, not_ubuntu=False):
singular = factory.getUniqueString()
upstream_pofile = factory.makePOFile(side=TranslationSide.UPSTREAM)
upstream_potmsgset = factory.makePOTMsgSet(
upstream_pofile.potemplate, singular)
upstream = factory.makeCurrentTranslationMessage(
pofile=upstream_pofile, potmsgset=upstream_potmsgset)
if not_ubuntu:
distroseries = factory.makeDistroSeries()
else:
distroseries = factory.makeUbuntuDistroSeries()
package_potemplate = factory.makePOTemplate(
distroseries=distroseries, name=upstream_pofile.potemplate.name)
package_pofile = factory.makePOFile(
potemplate=package_potemplate, language=upstream_pofile.language)
package_potmsgset = factory.makePOTMsgSet(
package_pofile.potemplate, singular)
factory.makeCurrentTranslationMessage(
pofile=package_pofile, potmsgset=package_potmsgset,
translations=upstream.translations)
productseries = upstream_pofile.potemplate.productseries
distroseries = package_pofile.potemplate.distroseries
sourcepackagename = package_pofile.potemplate.sourcepackagename
return TranslationMergeJob.create(
productseries=productseries, distroseries=distroseries,
sourcepackagename=sourcepackagename)
def get_msg_sets(productseries=None, distroseries=None,
sourcepackagename=None):
msg_sets = []
for template in POTemplateSubset(
productseries=productseries, distroseries=distroseries,
sourcepackagename=sourcepackagename):
msg_sets.extend(template.getPOTMsgSets())
return msg_sets
def get_translations(productseries=None, distroseries=None,
sourcepackagename=None):
msg_sets = get_msg_sets(
productseries=productseries, distroseries=distroseries,
sourcepackagename=sourcepackagename)
translations = set()
for msg_set in msg_sets:
translations.update(msg_set.getAllTranslationMessages())
return translations
def count_translations(job):
tm = get_translations(productseries=job.productseries)
tm.update(get_translations(
sourcepackagename=job.sourcepackagename,
distroseries=job.distroseries))
return len(tm)
class JobFinder:
def __init__(self, productseries, sourcepackage, job_class,
potemplate=None):
if potemplate is None:
self.productseries = productseries
self.sourcepackagename = sourcepackage.sourcepackagename
self.distroseries = sourcepackage.distroseries
self.potemplate = None
else:
self.potemplate = potemplate
self.job_type = job_class.class_job_type
def find(self):
if self.potemplate is None:
return list(TranslationSharingJobDerived.iterReady([
TranslationSharingJob.productseries_id == self.productseries.id,
(TranslationSharingJob.sourcepackagename_id ==
self.sourcepackagename.id),
TranslationSharingJob.distroseries_id == self.distroseries.id,
TranslationSharingJob.job_type == self.job_type,
]))
else:
return list(
TranslationSharingJobDerived.iterReady([
TranslationSharingJob.potemplate_id == self.potemplate.id,
TranslationSharingJob.job_type == self.job_type,
]))
class TestTranslationPackagingJob(TestCaseWithFactory):
layer = LaunchpadZopelessLayer
def test_interface(self):
"""Should implement ITranslationPackagingJobSource."""
verifyObject(ITranslationPackagingJobSource, TranslationPackagingJob)
class TestTranslationMergeJob(TestCaseWithFactory):
layer = LaunchpadZopelessLayer
def test_interface(self):
"""TranslationMergeJob must implement IRunnableJob."""
job = make_translation_merge_job(self.factory)
verifyObject(IRunnableJob, job)
def test_run_merges_msgset(self):
"""Run should merge msgsets."""
job = make_translation_merge_job(self.factory)
self.becomeDbUser('rosettaadmin')
product_msg = get_msg_sets(productseries=job.productseries)
package_msg = get_msg_sets(
sourcepackagename=job.sourcepackagename,
distroseries=job.distroseries)
self.assertNotEqual(package_msg, product_msg)
job.run()
product_msg = get_msg_sets(productseries=job.productseries)
package_msg = get_msg_sets(
sourcepackagename=job.sourcepackagename,
distroseries=job.distroseries)
self.assertEqual(package_msg, product_msg)
def test_run_merges_translations(self):
"""Run should merge translations."""
job = make_translation_merge_job(self.factory)
self.becomeDbUser('rosettaadmin')
self.assertEqual(2, count_translations(job))
job.run()
self.assertEqual(1, count_translations(job))
def test_skips_non_ubuntu_distros(self):
"""Run should ignore non-Ubuntu distributions."""
job = make_translation_merge_job(self.factory, not_ubuntu=True)
self.becomeDbUser('rosettaadmin')
self.assertEqual(2, count_translations(job))
job.run()
self.assertEqual(2, count_translations(job))
def test_create_packaging_makes_job(self):
"""Creating a Packaging should make a TranslationMergeJob."""
productseries = self.factory.makeProductSeries()
sourcepackage = self.factory.makeSourcePackage()
finder = JobFinder(productseries, sourcepackage, TranslationMergeJob)
self.assertEqual([], finder.find())
sourcepackage.setPackaging(productseries, productseries.owner)
self.assertNotEqual([], finder.find())
# Ensure no constraints were violated.
transaction.commit()
def test_getNextJobStatus(self):
"""Should find next packaging job."""
#suppress job creation.
with EventRecorder():
packaging = self.factory.makePackagingLink()
self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
TranslationMergeJob.forPackaging(packaging)
self.assertEqual(
JobStatus.WAITING,
TranslationMergeJob.getNextJobStatus(packaging))
def test_getNextJobStatus_wrong_packaging(self):
"""Jobs on wrong packaging should be ignored."""
#suppress job creation.
with EventRecorder():
packaging = self.factory.makePackagingLink()
self.factory.makePackagingLink(
productseries=packaging.productseries)
self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
self.factory.makePackagingLink()
self.factory.makePackagingLink(
distroseries=packaging.distroseries)
self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
TranslationMergeJob.create(
sourcepackagename=packaging.sourcepackagename,
distroseries=packaging.distroseries,
productseries=self.factory.makeProductSeries())
self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
def test_getNextJobStatus_wrong_type(self):
"""Only TranslationMergeJobs should result."""
#suppress job creation.
with EventRecorder():
packaging = self.factory.makePackagingLink()
TranslationSplitJob.forPackaging(packaging)
self.assertIs(
None, TranslationMergeJob.getNextJobStatus(packaging))
def test_getNextJobStatus_status(self):
"""Only RUNNING and WAITING jobs should influence status."""
#suppress job creation.
with EventRecorder():
packaging = self.factory.makePackagingLink()
job = TranslationMergeJob.forPackaging(packaging)
job.start()
self.assertEqual(JobStatus.RUNNING,
TranslationMergeJob.getNextJobStatus(packaging))
job.fail()
self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
job2 = TranslationMergeJob.forPackaging(packaging)
job2.start()
job2.complete()
job3 = TranslationMergeJob.forPackaging(packaging)
job3.suspend()
self.assertIs(None, TranslationMergeJob.getNextJobStatus(packaging))
def test_getNextJobStatus_order(self):
"""Status should order by id."""
with EventRecorder():
packaging = self.factory.makePackagingLink()
job = TranslationMergeJob.forPackaging(packaging)
job.start()
TranslationMergeJob.forPackaging(packaging)
self.assertEqual(JobStatus.RUNNING,
TranslationMergeJob.getNextJobStatus(packaging))
class TestTranslationSplitJob(TestCaseWithFactory):
layer = LaunchpadZopelessLayer
def test_run_splits_translations(self):
upstream_item, ubuntu_item = make_shared_potmsgset(self.factory)
job = TranslationSplitJob.create(
upstream_item.potemplate.productseries,
ubuntu_item.potemplate.distroseries,
ubuntu_item.potemplate.sourcepackagename,
)
self.assertEqual(upstream_item.potmsgset, ubuntu_item.potmsgset)
job.run()
self.assertNotEqual(upstream_item.potmsgset, ubuntu_item.potmsgset)
def test_deletePackaging_makes_job(self):
"""Creating a Packaging should make a TranslationMergeJob."""
packaging = self.factory.makePackagingLink()
finder = JobFinder(
packaging.productseries, packaging.sourcepackage,
TranslationSplitJob)
self.assertEqual([], finder.find())
with person_logged_in(packaging.owner):
getUtility(IPackagingUtil).deletePackaging(
packaging.productseries, packaging.sourcepackagename,
packaging.distroseries)
(job,) = finder.find()
self.assertIsInstance(job, TranslationSplitJob)
class TestTranslationTemplateChangeJob(TestCaseWithFactory):
layer = LaunchpadZopelessLayer
def test_modifyPOTemplate_makes_job(self):
"""Creating a Packaging should make a TranslationMergeJob."""
potemplate = self.factory.makePOTemplate()
finder = JobFinder(
None, None, TranslationTemplateChangeJob, potemplate)
self.assertEqual([], finder.find())
with person_logged_in(potemplate.owner):
snapshot = Snapshot(potemplate, providing=IPOTemplate)
potemplate.name = self.factory.getUniqueString()
notify(ObjectModifiedEvent(potemplate, snapshot, ["name"]))
(job,) = finder.find()
self.assertIsInstance(job, TranslationTemplateChangeJob)
def test_splits_and_merges(self):
"""Changing a template makes the translations split and then
re-merged in the new target sharing set."""
potemplate = self.factory.makePOTemplate(name='template')
other_ps = self.factory.makeProductSeries(
product=potemplate.productseries.product)
old_shared = self.factory.makePOTemplate(name='template',
productseries=other_ps)
new_shared = self.factory.makePOTemplate(name='renamed',
productseries=other_ps)
# Set up shared POTMsgSets and translations.
potmsgset = self.factory.makePOTMsgSet(potemplate, sequence=1)
potmsgset.setSequence(old_shared, 1)
self.factory.makeCurrentTranslationMessage(potmsgset=potmsgset)
# This is the identical English message in the new_shared template.
target_potmsgset = self.factory.makePOTMsgSet(
new_shared, sequence=1, singular=potmsgset.singular_text)
# Rename the template and confirm that messages are now shared
# with new_shared instead of old_shared.
potemplate.name = 'renamed'
job = TranslationTemplateChangeJob.create(potemplate=potemplate)
self.becomeDbUser('rosettaadmin')
job.run()
# New POTMsgSet is now different from the old one (it's been split),
# but matches the target potmsgset (it's been merged into it).
new_potmsgset = potemplate.getPOTMsgSets()[0]
self.assertNotEqual(potmsgset, new_potmsgset)
self.assertEqual(target_potmsgset, new_potmsgset)
# Translations have been merged as well.
self.assertContentEqual(
[tm.translations for tm in potmsgset.getAllTranslationMessages()],
[tm.translations
for tm in new_potmsgset.getAllTranslationMessages()])
|