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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
# Copyright 2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
from __future__ import with_statement
__metaclass__ = type
from zope.component import getUtility
from zope.interface.verify import verifyObject
from zope.security.proxy import removeSecurityProxy
from lp.app.enums import ServiceUsage
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.testing import (
person_logged_in,
TestCaseWithFactory,
)
from lp.testing.layers import ZopelessDatabaseLayer
from lp.translations.interfaces.productserieslanguage import (
IProductSeriesLanguageSet,
)
from lp.translations.interfaces.translatedlanguage import ITranslatedLanguage
class TestTranslatedLanguageMixin(TestCaseWithFactory):
"""Test TranslatedLanguageMixin."""
layer = ZopelessDatabaseLayer
def setUp(self):
# Create a productseries that uses translations.
TestCaseWithFactory.setUp(self)
product = self.factory.makeProduct(
translations_usage = ServiceUsage.LAUNCHPAD)
self.productseries = self.factory.makeProductSeries(
product=product)
self.parent = self.productseries
self.psl_set = getUtility(IProductSeriesLanguageSet)
self.language = self.factory.makeLanguage('sr@test')
def getTranslatedLanguage(self, language):
return self.psl_set.getProductSeriesLanguage(self.productseries,
language)
def addPOTemplate(self, number_of_potmsgsets=0, priority=0):
potemplate = self.factory.makePOTemplate(
productseries=self.productseries)
for sequence in range(number_of_potmsgsets):
self.factory.makePOTMsgSet(potemplate, sequence=sequence+1)
removeSecurityProxy(potemplate).messagecount = number_of_potmsgsets
potemplate.priority = priority
return potemplate
def addPOFile(self, potemplate=None):
"""Add a `POFile` for the given `POTemplate`, in `self.language`.
If no `potemplate` is given, one will be created.
"""
if potemplate is None:
potemplate = self.addPOTemplate()
return self.factory.makePOFile(self.language.code, potemplate)
def assertIsDummy(self, pofile):
"""Assert that `pofile` is actually a `DummyPOFile`."""
# Avoid circular imports.
from lp.translations.model.pofile import DummyPOFile
self.assertIsInstance(pofile, DummyPOFile)
def test_interface(self):
translated_language = self.getTranslatedLanguage(self.language)
self.assertTrue(verifyObject(ITranslatedLanguage,
translated_language))
def test_language(self):
translated_language = self.getTranslatedLanguage(self.language)
self.assertEqual(self.language,
translated_language.language)
def test_parent(self):
translated_language = self.getTranslatedLanguage(self.language)
self.assertEqual(self.parent,
translated_language.parent)
def test_pofiles_notemplates(self):
translated_language = self.getTranslatedLanguage(self.language)
self.assertEqual([], list(translated_language.pofiles))
def test_pofiles_template_no_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate()
dummy_pofile = potemplate.getDummyPOFile(self.language)
pofiles = list(translated_language.pofiles)
self.assertEqual(1, len(pofiles))
# When there are no actual PO files, we get a DummyPOFile object
# instead.
dummy_pofile = pofiles[0]
naked_dummy = removeSecurityProxy(dummy_pofile)
self.assertIsDummy(naked_dummy)
self.assertEqual(self.language, dummy_pofile.language)
self.assertEqual(potemplate, dummy_pofile.potemplate)
# Two queries get executed when listifying
# TranslatedLanguageMixin.pofiles: a len() does a count, and
# then all POTemplates and POFiles are fetched with the other.
self.assertStatementCount(2, list, translated_language.pofiles)
def test_pofiles_template_with_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate()
pofile = self.addPOFile(potemplate)
self.assertEqual([pofile], list(translated_language.pofiles))
# Two queries get executed when listifying
# TranslatedLanguageMixin.pofiles: a len() does a count, and
# then all POTemplates and POFiles are fetched with the other.
self.assertStatementCount(2, list, translated_language.pofiles)
def test_pofiles_two_templates(self):
translated_language = self.getTranslatedLanguage(self.language)
# Two templates with different priorities so they get sorted
# appropriately.
potemplate1 = self.addPOTemplate(priority=2)
pofile1 = self.addPOFile(potemplate1)
potemplate2 = self.addPOTemplate(priority=1)
pofile2 = self.addPOFile(potemplate2)
self.assertEqual([pofile1, pofile2],
list(translated_language.pofiles))
# Two queries get executed when listifying
# TranslatedLanguageMixin.pofiles: a len() does a count, and
# then all POTemplates and POFiles are fetched with the other.
self.assertStatementCount(2, list, translated_language.pofiles)
def test_pofiles_two_templates_one_dummy(self):
translated_language = self.getTranslatedLanguage(self.language)
# Two templates with different priorities so they get sorted
# appropriately.
potemplate1 = self.addPOTemplate(priority=2)
pofile1 = self.addPOFile(potemplate1)
potemplate2 = self.addPOTemplate(priority=1)
pofiles = translated_language.pofiles
self.assertEqual(pofile1, pofiles[0])
dummy_pofile = removeSecurityProxy(pofiles[1])
self.assertIsDummy(dummy_pofile)
# Two queries get executed when listifying
# TranslatedLanguageMixin.pofiles: a len() does a count, and
# then all POTemplates and POFiles are fetched with the other.
self.assertStatementCount(2, list, translated_language.pofiles)
def test_pofiles_slicing(self):
# Slicing still works, and always does the same constant number
# of queries (1).
translated_language = self.getTranslatedLanguage(self.language)
pofile1 = self.addPOFile()
pofile2 = self.addPOFile()
self.addPOTemplate(priority=-1)
# This does assume that a few teams with special privileges are
# already cached. For a normal user without those special
# privileges, no further queries are needed to authorize access.
user = self.factory.makePerson()
celebs = getUtility(ILaunchpadCelebrities)
with person_logged_in(user):
self.assertFalse(user.inTeam(celebs.admin))
self.assertFalse(user.inTeam(celebs.rosetta_experts))
pofiles = translated_language.pofiles[0:2]
self.assertContentEqual([pofile1, pofile2], list(pofiles))
get_slice = lambda of, start, end: list(of[start:end])
self.assertStatementCount(
1, get_slice, translated_language.pofiles, 1, 3)
def test_pofiles_slicing_dummies(self):
# Slicing includes DummyPOFiles.
translated_language = self.getTranslatedLanguage(self.language)
# Three templates with different priorities so they get sorted
# appropriately.
pofile1 = self.addPOFile(self.addPOTemplate(priority=2))
pofile2 = self.addPOFile(self.addPOTemplate(priority=1))
self.addPOTemplate(priority=0)
pofiles = translated_language.pofiles[1:3]
self.assertEqual(pofile2, pofiles[0])
dummy_pofile = removeSecurityProxy(pofiles[1])
self.assertIsDummy(dummy_pofile)
def test_statistics_empty(self):
translated_language = self.getTranslatedLanguage(self.language)
expected = {
'total_count': 0,
'translated_count': 0,
'new_count': 0,
'changed_count': 0,
'unreviewed_count': 0,
'untranslated_count': 0,
}
self.assertEqual(expected,
translated_language.translation_statistics)
def test_setCounts_statistics(self):
translated_language = self.getTranslatedLanguage(self.language)
total = 5
translated = 4
new = 3
changed = 2
unreviewed = 1
untranslated = total - translated
translated_language.setCounts(
total, translated, new, changed, unreviewed)
expected = {
'total_count': total,
'translated_count': translated,
'new_count': new,
'changed_count': changed,
'unreviewed_count': unreviewed,
'untranslated_count': untranslated,
}
self.assertEqual(expected,
translated_language.translation_statistics)
def test_recalculateCounts_empty(self):
translated_language = self.getTranslatedLanguage(self.language)
translated_language.recalculateCounts()
expected = {
'total_count': 0,
'translated_count': 0,
'new_count': 0,
'changed_count': 0,
'unreviewed_count': 0,
'untranslated_count': 0,
}
self.assertEqual(expected,
translated_language.translation_statistics)
def test_recalculateCounts_total_one_pofile(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate(number_of_potmsgsets=5)
pofile = self.addPOFile(potemplate)
translated_language.recalculateCounts()
self.assertEqual(
5, translated_language.translation_statistics['total_count'])
def test_recalculateCounts_total_two_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate1 = self.addPOTemplate(number_of_potmsgsets=5)
pofile1 = self.addPOFile(potemplate1)
potemplate2 = self.addPOTemplate(number_of_potmsgsets=3)
pofile2 = self.addPOFile(potemplate2)
translated_language.recalculateCounts()
self.assertEqual(
5+3, translated_language.translation_statistics['total_count'])
def test_recalculateCounts_translated_one_pofile(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate(number_of_potmsgsets=5)
pofile = self.addPOFile(potemplate)
naked_pofile = removeSecurityProxy(pofile)
# translated count is current + rosetta
naked_pofile.currentcount = 3
naked_pofile.rosettacount = 1
translated_language.recalculateCounts()
self.assertEqual(
4, translated_language.translation_statistics['translated_count'])
def test_recalculateCounts_translated_two_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate1 = self.addPOTemplate(number_of_potmsgsets=5)
pofile1 = self.addPOFile(potemplate1)
naked_pofile1 = removeSecurityProxy(pofile1)
# translated count is current + rosetta
naked_pofile1.currentcount = 3
naked_pofile1.rosettacount = 1
potemplate2 = self.addPOTemplate(number_of_potmsgsets=3)
pofile2 = self.addPOFile(potemplate2)
naked_pofile2 = removeSecurityProxy(pofile2)
# translated count is current + rosetta
naked_pofile2.currentcount = 1
naked_pofile2.rosettacount = 1
translated_language.recalculateCounts()
self.assertEqual(
6, translated_language.translation_statistics['translated_count'])
def test_recalculateCounts_changed_one_pofile(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate(number_of_potmsgsets=5)
pofile = self.addPOFile(potemplate)
naked_pofile = removeSecurityProxy(pofile)
# translated count is current + rosetta
naked_pofile.updatescount = 3
translated_language.recalculateCounts()
self.assertEqual(
3, translated_language.translation_statistics['changed_count'])
def test_recalculateCounts_changed_two_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate1 = self.addPOTemplate(number_of_potmsgsets=5)
pofile1 = self.addPOFile(potemplate1)
naked_pofile1 = removeSecurityProxy(pofile1)
naked_pofile1.updatescount = 3
potemplate2 = self.addPOTemplate(number_of_potmsgsets=3)
pofile2 = self.addPOFile(potemplate2)
naked_pofile2 = removeSecurityProxy(pofile2)
naked_pofile2.updatescount = 1
translated_language.recalculateCounts()
self.assertEqual(
4, translated_language.translation_statistics['changed_count'])
def test_recalculateCounts_new_one_pofile(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate(number_of_potmsgsets=5)
pofile = self.addPOFile(potemplate)
naked_pofile = removeSecurityProxy(pofile)
# new count is rosetta - changed
naked_pofile.rosettacount = 3
naked_pofile.updatescount = 1
translated_language.recalculateCounts()
self.assertEqual(
2, translated_language.translation_statistics['new_count'])
def test_recalculateCounts_new_two_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate1 = self.addPOTemplate(number_of_potmsgsets=5)
pofile1 = self.addPOFile(potemplate1)
naked_pofile1 = removeSecurityProxy(pofile1)
# new count is rosetta - changed
naked_pofile1.rosettacount = 3
naked_pofile1.updatescount = 1
potemplate2 = self.addPOTemplate(number_of_potmsgsets=3)
pofile2 = self.addPOFile(potemplate2)
naked_pofile2 = removeSecurityProxy(pofile2)
# new count is rosetta - changed
naked_pofile2.rosettacount = 2
naked_pofile2.updatescount = 1
translated_language.recalculateCounts()
self.assertEqual(
3, translated_language.translation_statistics['new_count'])
def test_recalculateCounts_unreviewed_one_pofile(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate(number_of_potmsgsets=5)
pofile = self.addPOFile(potemplate)
naked_pofile = removeSecurityProxy(pofile)
# translated count is current + rosetta
naked_pofile.unreviewed_count = 3
translated_language.recalculateCounts()
self.assertEqual(
3, translated_language.translation_statistics['unreviewed_count'])
def test_recalculateCounts_unreviewed_two_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate1 = self.addPOTemplate(number_of_potmsgsets=5)
pofile1 = self.addPOFile(potemplate1)
naked_pofile1 = removeSecurityProxy(pofile1)
naked_pofile1.unreviewed_count = 3
potemplate2 = self.addPOTemplate(number_of_potmsgsets=3)
pofile2 = self.addPOFile(potemplate2)
naked_pofile2 = removeSecurityProxy(pofile2)
naked_pofile2.unreviewed_count = 1
translated_language.recalculateCounts()
self.assertEqual(
4, translated_language.translation_statistics['unreviewed_count'])
def test_recalculateCounts_one_pofile(self):
translated_language = self.getTranslatedLanguage(self.language)
potemplate = self.addPOTemplate(number_of_potmsgsets=5)
pofile = self.addPOFile(potemplate)
naked_pofile = removeSecurityProxy(pofile)
# translated count is current + rosetta
naked_pofile.currentcount = 3
naked_pofile.rosettacount = 1
# Changed count is 'updatescount' on POFile.
# It has to be lower or equal to currentcount.
naked_pofile.updatescount = 1
# new is rosettacount-updatescount.
naked_pofile.newcount = 0
naked_pofile.unreviewed_count = 3
translated_language.recalculateCounts()
expected = {
'total_count': 5,
'translated_count': 4,
'new_count': 0,
'changed_count': 1,
'unreviewed_count': 3,
'untranslated_count': 1,
}
self.assertEqual(expected,
translated_language.translation_statistics)
def test_recalculateCounts_two_pofiles(self):
translated_language = self.getTranslatedLanguage(self.language)
# Set up one template with a single PO file.
potemplate1 = self.addPOTemplate(number_of_potmsgsets=5)
pofile1 = self.addPOFile(potemplate1)
naked_pofile1 = removeSecurityProxy(pofile1)
# translated count is current + rosetta
naked_pofile1.currentcount = 2
naked_pofile1.rosettacount = 2
# Changed count is 'updatescount' on POFile.
# It has to be lower or equal to currentcount.
# new is rosettacount-updatescount.
naked_pofile1.updatescount = 1
naked_pofile1.unreviewed_count = 3
# Set up second template with a single PO file.
potemplate2 = self.addPOTemplate(number_of_potmsgsets=3)
pofile2 = self.addPOFile(potemplate2)
naked_pofile2 = removeSecurityProxy(pofile2)
# translated count is current + rosetta
naked_pofile2.currentcount = 1
naked_pofile2.rosettacount = 2
# Changed count is 'updatescount' on POFile.
# It has to be lower or equal to currentcount.
# new is rosettacount-updatescount.
naked_pofile2.updatescount = 1
naked_pofile2.unreviewed_count = 1
translated_language.recalculateCounts()
expected = {
'total_count': 8,
'translated_count': 7,
'new_count': 2,
'changed_count': 2,
'unreviewed_count': 4,
'untranslated_count': 1,
}
self.assertEqual(expected,
translated_language.translation_statistics)
def test_recalculateCounts_two_templates_one_translation(self):
# Make sure recalculateCounts works even if a POFile is missing
# for one of the templates.
translated_language = self.getTranslatedLanguage(self.language)
# Set up one template with a single PO file.
potemplate1 = self.addPOTemplate(number_of_potmsgsets=5)
pofile1 = self.addPOFile(potemplate1)
naked_pofile1 = removeSecurityProxy(pofile1)
# translated count is current + rosetta
naked_pofile1.currentcount = 2
naked_pofile1.rosettacount = 2
# Changed count is 'updatescount' on POFile.
# It has to be lower or equal to currentcount.
# new is rosettacount-updatescount.
naked_pofile1.updatescount = 1
naked_pofile1.unreviewed_count = 3
# Set up second template with a single PO file.
potemplate2 = self.addPOTemplate(number_of_potmsgsets=3)
translated_language.recalculateCounts()
expected = {
'total_count': 8,
'translated_count': 4,
'new_count': 1,
'changed_count': 1,
'unreviewed_count': 3,
'untranslated_count': 4,
}
self.assertEqual(expected,
translated_language.translation_statistics)
|