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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Unit tests for translation import queue auto-approval.
This test overlaps with the one in doc/translationimportqueue.txt.
Documentation-style tests go in there, ones that go systematically
through the possibilities should go here.
"""
from contextlib import contextmanager
from datetime import (
datetime,
timedelta,
)
from pytz import UTC
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy
from lp.app.enums import ServiceUsage
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.registry.interfaces.series import SeriesStatus
from lp.registry.model.distribution import Distribution
from lp.registry.model.sourcepackagename import (
SourcePackageName,
SourcePackageNameSet,
)
from lp.services.database.lpstorm import IMasterStore
from lp.services.webapp.testing import verifyObject
from lp.services.worlddata.model.language import (
Language,
LanguageSet,
)
from lp.testing import TestCaseWithFactory
from lp.testing.fakemethod import FakeMethod
from lp.testing.layers import LaunchpadZopelessLayer
from lp.translations.enums import RosettaImportStatus
from lp.translations.interfaces.customlanguagecode import ICustomLanguageCode
from lp.translations.interfaces.translationimportqueue import (
ITranslationImportQueue,
translation_import_queue_entry_age,
)
from lp.translations.model.customlanguagecode import CustomLanguageCode
from lp.translations.model.pofile import POFile
from lp.translations.model.potemplate import (
POTemplateSet,
POTemplateSubset,
)
from lp.translations.model.translationimportqueue import (
TranslationImportQueue,
TranslationImportQueueEntry,
)
class GardenerDbUserMixin(object):
"""Switch to the translations import queue gardener database role.
Admittedly, this might be a little over-engineered but it looks good. ;)
"""
def becomeTheGardener(self):
"""One-way method to avoid unnecessary switch back."""
self.becomeDbUser('translations_import_queue_gardener')
@contextmanager
def beingTheGardener(self):
"""Context manager to restore the launchpad user."""
self.becomeTheGardener()
yield
self.becomeDbUser('launchpad')
class TestCustomLanguageCode(TestCaseWithFactory):
"""Unit tests for `CustomLanguageCode`."""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestCustomLanguageCode, self).setUp()
self.product_codes = {}
self.package_codes = {}
self.product = self.factory.makeProduct()
# Map "es_ES" to "no language."
self.product_codes['es_ES'] = CustomLanguageCode(
product=self.product, language_code='es_ES')
# Map "pt_PT" to "pt."
self.product_codes['pt_PT'] = CustomLanguageCode(
product=self.product, language_code='pt_PT',
language=Language.byCode('pt'))
self.distro = Distribution.byName('ubuntu')
self.sourcepackagename = SourcePackageName.byName('evolution')
self.package_codes['Brazilian'] = CustomLanguageCode(
distribution=self.distro,
sourcepackagename=self.sourcepackagename,
language_code='Brazilian',
language=Language.byCode('pt_BR'))
def test_ICustomLanguageCode(self):
# Does CustomLanguageCode conform to ICustomLanguageCode?
custom_language_code = CustomLanguageCode(
language_code='sux', product=self.product)
verifyObject(ICustomLanguageCode, custom_language_code)
def test_NoCustomLanguageCode(self):
# Look up custom language code for context that has none.
# The "fresh" items here are ones that have no custom language codes
# associated with them.
fresh_product = self.factory.makeProduct()
self.assertEqual(fresh_product.getCustomLanguageCode('nocode'), None)
self.assertEqual(fresh_product.getCustomLanguageCode('pt_PT'), None)
fresh_distro = Distribution.byName('gentoo')
gentoo_package = fresh_distro.getSourcePackage(self.sourcepackagename)
nocode = gentoo_package.getCustomLanguageCode('nocode')
self.assertEqual(nocode, None)
brazilian = gentoo_package.getCustomLanguageCode('Brazilian')
self.assertEqual(brazilian, None)
cnews = SourcePackageName.byName('cnews')
cnews_package = self.distro.getSourcePackage(cnews)
self.assertEqual(cnews_package.getCustomLanguageCode('nocode'), None)
self.assertEqual(
cnews_package.getCustomLanguageCode('Brazilian'), None)
def test_UnsuccessfulCustomLanguageCodeLookup(self):
# Look up nonexistent custom language code for product.
self.assertEqual(self.product.getCustomLanguageCode('nocode'), None)
package = self.distro.getSourcePackage(self.sourcepackagename)
self.assertEqual(package.getCustomLanguageCode('nocode'), None)
def test_SuccessfulProductCustomLanguageCodeLookup(self):
# Look up custom language code.
es_ES_code = self.product.getCustomLanguageCode('es_ES')
self.assertEqual(es_ES_code, self.product_codes['es_ES'])
self.assertEqual(es_ES_code.product, self.product)
self.assertEqual(es_ES_code.distribution, None)
self.assertEqual(es_ES_code.sourcepackagename, None)
self.assertEqual(es_ES_code.language_code, 'es_ES')
self.assertEqual(es_ES_code.language, None)
def test_SuccessfulPackageCustomLanguageCodeLookup(self):
# Look up custom language code.
package = self.distro.getSourcePackage(self.sourcepackagename)
Brazilian_code = package.getCustomLanguageCode('Brazilian')
self.assertEqual(Brazilian_code, self.package_codes['Brazilian'])
self.assertEqual(Brazilian_code.product, None)
self.assertEqual(Brazilian_code.distribution, self.distro)
self.assertEqual(
Brazilian_code.sourcepackagename, self.sourcepackagename)
self.assertEqual(Brazilian_code.language_code, 'Brazilian')
self.assertEqual(Brazilian_code.language, Language.byCode('pt_BR'))
class TestGuessPOFileCustomLanguageCode(TestCaseWithFactory,
GardenerDbUserMixin):
"""Test interaction with `TranslationImportQueueEntry.getGuessedPOFile`.
Auto-approval of translation files, i.e. figuring out which existing
translation file a new upload might match, is a complex process.
One of the factors that influence it is the existence of custom
language codes that may redirect translations from a wrong language
code to a right one, or to none at all.
"""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestGuessPOFileCustomLanguageCode, self).setUp()
self.product = self.factory.makeProduct()
self.series = self.factory.makeProductSeries(product=self.product)
self.queue = TranslationImportQueue()
self.template = POTemplateSubset(productseries=self.series).new(
'test', 'test', 'test.pot', self.product.owner)
def _makePOFile(self, language_code):
"""Create a translation file."""
file = self.template.newPOFile(language_code)
file.syncUpdate()
return file
def _makeQueueEntry(self, language_code):
"""Create translation import queue entry."""
return self.queue.addOrUpdateEntry(
"%s.po" % language_code, 'contents', True, self.product.owner,
productseries=self.series)
def _setCustomLanguageCode(self, language_code, target_language_code):
"""Create custom language code."""
if target_language_code is None:
language = None
else:
language = Language.byCode(target_language_code)
customcode = CustomLanguageCode(
product=self.product, language_code=language_code,
language=language)
customcode.syncUpdate()
def test_MatchWithoutCustomLanguageCode(self):
# Of course matching will work without custom language codes.
tr_file = self._makePOFile('tr')
entry = self._makeQueueEntry('tr')
self.becomeTheGardener()
self.assertEqual(entry.getGuessedPOFile(), tr_file)
def test_CustomLanguageCodeEnablesMatch(self):
# Custom language codes may enable matches that wouldn't have been
# found otherwise.
fy_file = self._makePOFile('fy')
entry = self._makeQueueEntry('fy_NL')
self.assertEqual(entry.getGuessedPOFile(), None)
self._setCustomLanguageCode('fy_NL', 'fy')
self.becomeTheGardener()
self.assertEqual(entry.getGuessedPOFile(), fy_file)
def test_CustomLanguageCodeParsesBogusLanguage(self):
# A custom language code can tell the importer how to deal with a
# completely nonstandard language code.
entry = self._makeQueueEntry('flemish')
self.assertEqual(entry.getGuessedPOFile(), None)
self._setCustomLanguageCode('flemish', 'nl')
self.becomeTheGardener()
nl_file = entry.getGuessedPOFile()
self.assertEqual(nl_file.language.code, 'nl')
def test_CustomLanguageCodePreventsMatch(self):
# A custom language code that disables a language code may hide an
# existing translation file from the matching process.
sv_file = self._makePOFile('sv')
entry = self._makeQueueEntry('sv')
self.assertEqual(entry.getGuessedPOFile(), sv_file)
self._setCustomLanguageCode('sv', None)
self.becomeTheGardener()
self.assertEqual(entry.getGuessedPOFile(), None)
self.assertEqual(entry.status, RosettaImportStatus.DELETED)
def test_CustomLanguageCodeHidesPOFile(self):
# A custom language code may redirect the search away from an existing
# translation file, even if it points to an existing language.
elx_file = self._makePOFile('elx')
entry = self._makeQueueEntry('elx')
self.assertEqual(entry.getGuessedPOFile(), elx_file)
self._setCustomLanguageCode('elx', 'el')
self.becomeTheGardener()
el_file = entry.getGuessedPOFile()
self.failIfEqual(el_file, elx_file)
self.assertEqual(el_file.language.code, 'el')
def test_CustomLanguageCodeRedirectsMatch(self):
# A custom language code may cause one match to be replaced by another
# one.
nn_file = self._makePOFile('nn')
nb_file = self._makePOFile('nb')
entry = self._makeQueueEntry('nb')
self.assertEqual(entry.getGuessedPOFile(), nb_file)
self._setCustomLanguageCode('nb', 'nn')
self.becomeTheGardener()
self.assertEqual(entry.getGuessedPOFile(), nn_file)
def test_CustomLanguageCodeReplacesMatch(self):
# One custom language code can block uploads for language code pt
# while another redirects the uploads for pt_PT into their place.
pt_file = self._makePOFile('pt')
pt_entry = self._makeQueueEntry('pt')
pt_PT_entry = self._makeQueueEntry('pt_PT')
self._setCustomLanguageCode('pt', None)
self._setCustomLanguageCode('pt_PT', 'pt')
self.becomeTheGardener()
self.assertEqual(pt_entry.getGuessedPOFile(), None)
self.assertEqual(pt_PT_entry.getGuessedPOFile(), pt_file)
def test_CustomLanguageCodesSwitchLanguages(self):
# Two CustomLanguageCodes may switch two languages around.
zh_CN_file = self._makePOFile('zh_CN')
zh_TW_file = self._makePOFile('zh_TW')
zh_CN_entry = self._makeQueueEntry('zh_CN')
zh_TW_entry = self._makeQueueEntry('zh_TW')
self._setCustomLanguageCode('zh_CN', 'zh_TW')
self._setCustomLanguageCode('zh_TW', 'zh_CN')
self.becomeTheGardener()
self.assertEqual(zh_CN_entry.getGuessedPOFile(), zh_TW_file)
self.assertEqual(zh_TW_entry.getGuessedPOFile(), zh_CN_file)
class TestTemplateGuess(TestCaseWithFactory, GardenerDbUserMixin):
"""Test auto-approval's attempts to find the right template."""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestTemplateGuess, self).setUp()
self.templateset = POTemplateSet()
def _setUpProduct(self):
"""Set up a `Product` with release series and two templates."""
self.product = self.factory.makeProduct()
self.productseries = self.factory.makeProductSeries(
product=self.product)
product_subset = POTemplateSubset(productseries=self.productseries)
self.producttemplate1 = product_subset.new(
'test1', 'test1', 'test.pot', self.product.owner)
self.producttemplate2 = product_subset.new(
'test2', 'test2', 'test.pot', self.product.owner)
def _makeTemplateForDistroSeries(self, distroseries, name):
"""Create a template in the given `DistroSeries`."""
distro_subset = POTemplateSubset(
distroseries=distroseries, sourcepackagename=self.packagename)
return distro_subset.new(name, name, 'test.pot', self.distro.owner)
def _setUpDistro(self):
"""Set up a `Distribution` with two templates."""
self.distro = self.factory.makeDistribution()
self.distroseries = self.factory.makeDistroSeries(
distribution=self.distro)
self.packagename = SourcePackageNameSet().new('package')
self.from_packagename = SourcePackageNameSet().new('from')
self.distrotemplate1 = self._makeTemplateForDistroSeries(
self.distroseries, 'test1')
self.distrotemplate2 = self._makeTemplateForDistroSeries(
self.distroseries, 'test2')
def test_ByPathAndOrigin_product_duplicate(self):
# When multiple templates match for a product series,
# getPOTemplateByPathAndOrigin returns none.
self._setUpProduct()
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', productseries=self.productseries)
self.assertEqual(None, guessed_template)
def test_ByPathAndOrigin_package_duplicate(self):
# When multiple templates match on sourcepackagename,
# getPOTemplateByPathAndOrigin returns none.
self._setUpDistro()
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', sourcepackagename=self.packagename)
self.assertEqual(None, guessed_template)
def test_ByPathAndOrigin_from_package_duplicate(self):
# When multiple templates match on from_sourcepackagename,
# getPOTemplateByPathAndOrigin returns none.
self._setUpDistro()
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', sourcepackagename=self.from_packagename)
self.assertEqual(None, guessed_template)
def test_ByPathAndOrigin_similar_between_distroseries(self):
# getPOTemplateByPathAndOrigin disregards templates from other
# distroseries.
self._setUpDistro()
other_series = self.factory.makeDistroSeries(
distribution=self.distro)
self._makeTemplateForDistroSeries(other_series, 'test1')
self.distrotemplate1.iscurrent = False
self.distrotemplate2.iscurrent = True
self.distrotemplate1.from_sourcepackagename = None
self.distrotemplate2.from_sourcepackagename = None
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', distroseries=self.distroseries,
sourcepackagename=self.packagename)
self.assertEqual(self.distrotemplate2, guessed_template)
def test_ByPathAndOrigin_preferred_match(self):
# getPOTemplateByPathAndOrigin prefers from_sourcepackagename
# matches over sourcepackagename matches.
self._setUpDistro()
# Use unique name for this package, since the search does not
# pass a distroseries and so might pick one of the same name up
# from elsewhere.
match_package = SourcePackageNameSet().new(
self.factory.getUniqueString())
self.distrotemplate1.sourcepackagename = match_package
self.distrotemplate2.from_sourcepackagename = match_package
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', sourcepackagename=match_package)
self.assertEqual(self.distrotemplate2, guessed_template)
def test_ByPathAndOriginProductNonCurrentDuplicate(self):
# If two templates for the same product series have the same
# path, but only one is current, that one is returned.
self._setUpProduct()
self.producttemplate1.iscurrent = False
self.producttemplate2.iscurrent = True
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', productseries=self.productseries)
self.assertEqual(guessed_template, self.producttemplate2)
def test_ByPathAndOriginProductNoCurrentTemplate(self):
# Non-current templates in product series are ignored.
self._setUpProduct()
self.producttemplate1.iscurrent = False
self.producttemplate2.iscurrent = False
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', productseries=self.productseries)
self.assertEqual(guessed_template, None)
def test_ByPathAndOriginDistroNonCurrentDuplicate(self):
# If two templates for the same distroseries and source package
# have the same path, but only one is current, the current one
# is returned.
self._setUpDistro()
self.distrotemplate1.iscurrent = False
self.distrotemplate2.iscurrent = True
self.distrotemplate1.from_sourcepackagename = None
self.distrotemplate2.from_sourcepackagename = None
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', distroseries=self.distroseries,
sourcepackagename=self.packagename)
self.assertEqual(guessed_template, self.distrotemplate2)
def test_ByPathAndOriginDistroNoCurrentTemplate(self):
# Non-current templates in distroseries are ignored.
self._setUpDistro()
self.distrotemplate1.iscurrent = False
self.distrotemplate2.iscurrent = False
self.distrotemplate1.from_sourcepackagename = None
self.distrotemplate2.from_sourcepackagename = None
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', distroseries=self.distroseries,
sourcepackagename=self.packagename)
self.assertEqual(guessed_template, None)
def test_ByPathAndOriginDistroFromSourcePackageNonCurrentDuplicate(self):
# If two templates for the same distroseries and original source
# package have the same path, but only one is current, that one is
# returned.
self._setUpDistro()
self.distrotemplate1.iscurrent = False
self.distrotemplate2.iscurrent = True
self.distrotemplate1.from_sourcepackagename = self.from_packagename
self.distrotemplate2.from_sourcepackagename = self.from_packagename
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', distroseries=self.distroseries,
sourcepackagename=self.from_packagename)
self.assertEqual(guessed_template, self.distrotemplate2)
def test_ByPathAndOriginDistroFromSourcePackageNoCurrentTemplate(self):
# Non-current templates in distroseries are ignored by the
# "from_sourcepackagename" match.
self._setUpDistro()
self.distrotemplate1.iscurrent = False
self.distrotemplate2.iscurrent = False
self.distrotemplate1.from_sourcepackagename = self.from_packagename
self.distrotemplate2.from_sourcepackagename = self.from_packagename
self.becomeTheGardener()
guessed_template = self.templateset.getPOTemplateByPathAndOrigin(
'test.pot', distroseries=self.distroseries,
sourcepackagename=self.from_packagename)
self.assertEqual(guessed_template, None)
def test_ByDomain_finds_by_domain(self):
# matchPOTemplateByDomain looks for a template of a given domain
# in the entry's context. It ignores other domains.
series = self.factory.makeProductSeries()
templates = [
self.factory.makePOTemplate(productseries=series)
for counter in xrange(2)]
entry = self.factory.makeTranslationImportQueueEntry(
productseries=series)
self.assertEqual(
templates[0],
removeSecurityProxy(entry).matchPOTemplateByDomain(
templates[0].translation_domain))
def test_byDomain_finds_in_productseries(self):
# matchPOTemplateByDomain for a productseries upload looks only
# in that productseries.
domain = self.factory.getUniqueString()
templates = [
self.factory.makePOTemplate(
translation_domain=domain,
productseries=self.factory.makeProductSeries())
for counter in xrange(2)]
entry = self.factory.makeTranslationImportQueueEntry(
productseries=templates[0].productseries)
self.assertEqual(
templates[0],
removeSecurityProxy(entry).matchPOTemplateByDomain(domain))
def test_byDomain_finds_in_source_package(self):
# matchPOTemplateByDomain for a distro upload, if given a source
# package, looks only in that source package. It doesn't matter
# if the entry itself is for the same source package or not.
domain = self.factory.getUniqueString()
distroseries = self.factory.makeDistroSeries()
templates = [
self.factory.makePOTemplate(
translation_domain=domain, distroseries=distroseries,
sourcepackagename=self.factory.makeSourcePackageName())
for counter in xrange(2)]
entry = self.factory.makeTranslationImportQueueEntry(
distroseries=distroseries,
sourcepackagename=templates[1].sourcepackagename)
self.assertEqual(
templates[0],
removeSecurityProxy(entry).matchPOTemplateByDomain(
domain, templates[0].sourcepackagename))
def test_byDomain_ignores_sourcepackagename_by_default(self):
# If no sourcepackagename is given, matchPOTemplateByDomain
# on a distroseries searches all packages in the series.
distroseries = self.factory.makeDistroSeries()
template = self.factory.makePOTemplate(
distroseries=distroseries,
sourcepackagename=self.factory.makeSourcePackageName())
entry = self.factory.makeTranslationImportQueueEntry(
distroseries=distroseries,
sourcepackagename=self.factory.makeSourcePackageName())
self.assertEqual(
template,
removeSecurityProxy(entry).matchPOTemplateByDomain(
template.translation_domain))
def test_ByDomain_may_return_None(self):
# If no templates match, matchPOTemplateByDomain returns None.
entry = self.factory.makeTranslationImportQueueEntry()
self.assertEqual(
None,
removeSecurityProxy(entry).matchPOTemplateByDomain("domain"))
def test_ByDomain_reports_conflicts(self):
# If multiple templates match, matchPOTemplateByDomain registers
# an error in the entry's error_output, and returns None.
domain = self.factory.getUniqueString()
series = self.factory.makeProductSeries()
templates = [
self.factory.makePOTemplate(
translation_domain=domain, productseries=series)
for counter in xrange(2)]
entry = self.factory.makeTranslationImportQueueEntry(
productseries=series)
with self.beingTheGardener():
result = removeSecurityProxy(entry).matchPOTemplateByDomain(
domain)
self.assertIs(None, result)
self.assertIn(templates[0].displayname, entry.error_output)
def test_ByDomain_ignores_inactive_templates(self):
series = self.factory.makeProductSeries()
template = self.factory.makePOTemplate(
productseries=series, iscurrent=False)
entry = self.factory.makeTranslationImportQueueEntry(
productseries=series)
self.assertIs(
None,
removeSecurityProxy(entry).matchPOTemplateByDomain(
template.translation_domain))
def test_approval_clears_error_output(self):
# If a previous approval attempt set an error notice on the
# entry, successful approval clears it away.
template = self.factory.makePOTemplate(path='messages.pot')
pofile = self.factory.makePOFile(potemplate=template)
entry = self.factory.makeTranslationImportQueueEntry(
productseries=pofile.potemplate.productseries,
potemplate=pofile.potemplate, pofile=pofile)
entry.setErrorOutput("Entry can't be approved for whatever reason.")
TranslationImportQueue()._attemptToApprove(entry)
self.assertIs(None, entry.error_output)
def test_ClashingEntries(self):
# Very rarely two entries may have the same uploader, path, and
# target package/productseries. They would be approved for the
# same template, except there's a uniqueness condition on that
# set of properties.
# To tickle this condition, the user first has to upload a file
# that's not attached to a template; then upload another one
# that is, before the first one goes into auto-approval.
self._setUpProduct()
queue = TranslationImportQueue()
template = self.producttemplate1
template.path = 'program/program.pot'
self.producttemplate2.path = 'errors/errors.pot'
entry1 = queue.addOrUpdateEntry(
'program/nl.po', 'contents', False, template.owner,
productseries=template.productseries)
# The clashing entry goes through approval unsuccessfully, but
# without causing breakage.
queue.addOrUpdateEntry(
'program/nl.po', 'other contents', False, template.owner,
productseries=template.productseries, potemplate=template)
self.becomeTheGardener()
entry1.getGuessedPOFile()
self.assertEqual(entry1.potemplate, None)
def test_getGuessedPOFile_ignores_obsolete_POFiles(self):
pofile = self.factory.makePOFile()
template = pofile.potemplate
template.iscurrent = False
queue = getUtility(ITranslationImportQueue)
entry = queue.addOrUpdateEntry(
pofile.path, 'contents', False, self.factory.makePerson(),
productseries=template.productseries)
self.assertEqual(None, entry.getGuessedPOFile())
def test_getGuessedPOFile_survives_clashing_obsolete_POFile_path(self):
series = self.factory.makeProductSeries()
current_template = self.factory.makePOTemplate(productseries=series)
current_template.iscurrent = True
current_pofile = self.factory.makePOFile(
'nl', potemplate=current_template)
obsolete_template = self.factory.makePOTemplate(productseries=series)
obsolete_template.iscurrent = False
obsolete_pofile = self.factory.makePOFile(
'nl', potemplate=obsolete_template)
obsolete_pofile.path = current_pofile.path
queue = getUtility(ITranslationImportQueue)
entry = queue.addOrUpdateEntry(
current_pofile.path, 'contents', False, self.factory.makePerson(),
productseries=series)
self.assertEqual(current_pofile, entry.getGuessedPOFile())
def test_pathless_template_match(self):
# If an uploaded template has no directory component in its
# path, and no matching template is found in the database, the
# approver also tries if there might be exactly 1 template with
# the same base filename. If so, that's a match.
self._setUpProduct()
template = self.producttemplate1
template.path = 'po/test.pot'
self.producttemplate2.path = 'different.pot'
queue = TranslationImportQueue()
entry = queue.addOrUpdateEntry(
'test.pot', 'contents', False, template.owner,
productseries=template.productseries)
self.assertEqual(template, entry.guessed_potemplate)
def test_pathless_template_no_match(self):
# The search for a matching filename will still ignore
# templates with non-matching paths.
self._setUpProduct()
template = self.producttemplate1
queue = TranslationImportQueue()
entry = queue.addOrUpdateEntry(
'other.pot', 'contents', False, template.owner,
productseries=template.productseries)
self.assertEqual(None, entry.guessed_potemplate)
def test_pathless_template_multiple_matches(self):
# If multiple active templates have matching filenames
# (regardless of whether they're in subdirectories or in the
# project root directory) then there is no unique match.
self._setUpProduct()
template = self.producttemplate1
template.path = 'here/test.pot'
self.producttemplate2.path = 'there/test.pot'
queue = TranslationImportQueue()
entry = queue.addOrUpdateEntry(
'test.pot', 'contents', False, template.owner,
productseries=template.productseries)
self.assertEqual(None, entry.guessed_potemplate)
def test_pathless_template_one_current_match(self):
# Deactivated templates are not considered in the match; if one
# active and one non-active template both match on filename, the
# active one is returned as a unique match.
self._setUpProduct()
template = self.producttemplate1
template.iscurrent = True
template.path = 'here/test.pot'
self.producttemplate2.iscurrent = False
self.producttemplate2.path = 'there/test.pot'
queue = TranslationImportQueue()
entry = queue.addOrUpdateEntry(
'test.pot', 'contents', False, template.owner,
productseries=template.productseries)
self.assertEqual(template, entry.guessed_potemplate)
def test_avoid_clash_with_existing_entry(self):
# When trying to approve a template upload that didn't have its
# potemplate field set during upload or an earlier approval run,
# the approver will fill out the field if it can. But if by
# then there's already another entry from the same person and
# for the same target that does have the field set, then filling
# out the field would make the two entries clash.
queue = TranslationImportQueue()
template = self.factory.makePOTemplate()
old_entry = queue.addOrUpdateEntry(
template.path, '# Content here', False, template.owner,
productseries=template.productseries)
new_entry = queue.addOrUpdateEntry(
template.path, '# Content here', False, template.owner,
productseries=template.productseries, potemplate=template)
# Before approval, the two entries differ in that the new one
# has a potemplate.
self.assertNotEqual(old_entry, new_entry)
self.assertEqual(RosettaImportStatus.NEEDS_REVIEW, old_entry.status)
self.assertIs(None, old_entry.potemplate)
self.assertEqual(template, new_entry.potemplate)
IMasterStore(old_entry).flush()
# The approver deals with the problem by skipping the entry.
queue._attemptToApprove(old_entry)
# So nothing changes.
self.assertIs(None, old_entry.potemplate)
self.assertEqual(template, new_entry.potemplate)
class TestKdePOFileGuess(TestCaseWithFactory, GardenerDbUserMixin):
"""Test auto-approval's `POFile` guessing for KDE uploads.
KDE has an unusual setup that the approver recognizes as a special
case: translation uploads are done in a package that represents a
KDE language pack, following a naming convention that differs
between KDE3 and KDE4. The approver then attaches entries to the
real software packages they belong to, which it finds by looking
for a matching translation domain.
"""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestKdePOFileGuess, self).setUp()
self.queue = TranslationImportQueue()
self.distroseries = self.factory.makeDistroSeries()
# For each of KDE3 and KDE4, set up:
# a translation package following that KDE's naming pattern,
# another package that the translations really belong in,
# a template for that other package, and
# a translation file into a language we'll test in.
self.kde_i18n_ca = SourcePackageNameSet().new('kde-i18n-ca')
kde3_package = SourcePackageNameSet().new('kde3')
ca_template = self.factory.makePOTemplate(
distroseries=self.distroseries,
sourcepackagename=kde3_package, name='kde3',
translation_domain='kde3')
self.pofile_ca = ca_template.newPOFile('ca')
self.kde_l10n_nl = SourcePackageNameSet().new('kde-l10n-nl')
kde4_package = SourcePackageNameSet().new('kde4')
nl_template = self.factory.makePOTemplate(
distroseries=self.distroseries,
sourcepackagename=kde4_package, name='kde4',
translation_domain='kde4')
self.pofile_nl = nl_template.newPOFile('nl')
self.pocontents = """
msgid "foo"
msgstr ""
"""
def test_kde3(self):
# KDE3 translations are in packages named kde-i10n-** (where **
# is the language code).
poname = self.pofile_ca.potemplate.name + '.po'
entry = self.queue.addOrUpdateEntry(
poname, self.pocontents, False, self.distroseries.owner,
sourcepackagename=self.kde_i18n_ca,
distroseries=self.distroseries)
self.becomeTheGardener()
pofile = entry.getGuessedPOFile()
self.assertEqual(pofile, self.pofile_ca)
def test_kde4(self):
# KDE4 translations are in packages named kde-l10n-** (where **
# is the language code).
poname = self.pofile_nl.potemplate.name + '.po'
entry = self.queue.addOrUpdateEntry(
poname, self.pocontents, False, self.distroseries.owner,
sourcepackagename=self.kde_l10n_nl,
distroseries=self.distroseries)
self.becomeTheGardener()
pofile = entry.getGuessedPOFile()
self.assertEqual(pofile, self.pofile_nl)
class TestGetPOFileFromLanguage(TestCaseWithFactory, GardenerDbUserMixin):
"""Test `TranslationImportQueueEntry._get_pofile_from_language`."""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestGetPOFileFromLanguage, self).setUp()
self.queue = TranslationImportQueue()
def test_get_pofile_from_language_feeds_enabled_template(self):
# _get_pofile_from_language will find an enabled template, and
# return either an existing POFile for the given language, or a
# newly created one.
product = self.factory.makeProduct(
translations_usage=ServiceUsage.LAUNCHPAD)
trunk = product.getSeries('trunk')
template = self.factory.makePOTemplate(
productseries=trunk, translation_domain='domain')
template.iscurrent = True
entry = self.queue.addOrUpdateEntry(
'nl.po', '# ...', False, template.owner, productseries=trunk)
self.becomeTheGardener()
pofile = entry._get_pofile_from_language('nl', 'domain')
self.assertNotEqual(None, pofile)
def test_get_pofile_from_language_starves_disabled_template(self):
# _get_pofile_from_language will not consider a disabled
# template as an auto-approval target, and so will not return a
# POFile for it.
product = self.factory.makeProduct(
translations_usage=ServiceUsage.LAUNCHPAD)
trunk = product.getSeries('trunk')
template = self.factory.makePOTemplate(
productseries=trunk, translation_domain='domain')
template.iscurrent = False
entry = self.queue.addOrUpdateEntry(
'nl.po', '# ...', False, template.owner, productseries=trunk)
self.becomeTheGardener()
pofile = entry._get_pofile_from_language('nl', 'domain')
self.assertEqual(None, pofile)
def test_get_pofile_from_language_works_with_translation_credits(self):
# When the template has translation credits, a new dummy translation
# is created in the new POFile. Since this is running with gardener
# privileges, we need to check that this works, too.
product = self.factory.makeProduct(
translations_usage=ServiceUsage.LAUNCHPAD)
trunk = product.getSeries('trunk')
template = self.factory.makePOTemplate(
productseries=trunk, translation_domain='domain')
template.iscurrent = True
self.factory.makePOTMsgSet(template, "translator-credits")
entry = self.queue.addOrUpdateEntry(
'nl.po', '# ...', False, template.owner, productseries=trunk)
self.becomeTheGardener()
pofile = entry._get_pofile_from_language('nl', 'domain')
self.assertNotEqual(None, pofile)
class TestCleanup(TestCaseWithFactory, GardenerDbUserMixin):
"""Test `TranslationImportQueueEntry` garbage collection."""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestCleanup, self).setUp()
self.queue = TranslationImportQueue()
self.store = IMasterStore(TranslationImportQueueEntry)
def _makeProductEntry(self, path='foo.pot', status=None):
"""Simulate upload for a product."""
product = self.factory.makeProduct(
translations_usage=ServiceUsage.LAUNCHPAD)
trunk = product.getSeries('trunk')
entry = self.queue.addOrUpdateEntry(
path, '# contents', False, product.owner, productseries=trunk)
if status is not None:
entry.status = status
return entry
def _makeDistroEntry(self, path='bar.pot', status=None):
"""Simulate upload for a distribution package."""
package = self.factory.makeSourcePackage()
owner = package.distroseries.owner
entry = self.queue.addOrUpdateEntry(
path, '# contents', False, owner,
sourcepackagename=package.sourcepackagename,
distroseries=package.distroseries)
if status is not None:
entry.status = status
return entry
def _ageEntry(self, entry, interval):
"""Make an entry's timestamps older by a given interval."""
entry.dateimported -= interval
entry.date_status_changed -= interval
entry.syncUpdate()
def _exists(self, entry_id):
"""Is the entry with the given id still on the queue?"""
entry = self.store.find(
TranslationImportQueueEntry,
TranslationImportQueueEntry.id == entry_id).any()
return entry is not None
def _setStatus(self, entry, status, when=None):
"""Simulate status on queue entry having been set at a given time."""
entry.setStatus(status,
getUtility(ILaunchpadCelebrities).rosetta_experts)
if when is not None:
entry.date_status_changed = when
entry.syncUpdate()
def test_cleanUpObsoleteEntries_unaffected_statuses(self):
# _cleanUpObsoleteEntries leaves entries in states without
# expiry age (currently only Blocked) alone no matter how old
# they are.
unaffected_statuses = (
set(RosettaImportStatus.items) -
set(translation_import_queue_entry_age.keys()))
self.assertNotEqual(
0, len(unaffected_statuses),
"This test is no longer needed; "
"there are no statuses without expiry ages.")
years_ago = datetime.now(UTC) - timedelta(days=2000)
entry = self._makeProductEntry()
entry.potemplate = self.factory.makePOTemplate(
productseries=entry.productseries)
entry_id = entry.id
for status in unaffected_statuses:
self._setStatus(entry, status, years_ago)
self.queue._cleanUpObsoleteEntries(self.store)
self.assertTrue(self._exists(entry_id))
def test_cleanUpObsoleteEntries_affected_statuses(self):
# _cleanUpObsoleteEntries deletes entries in terminal states
# (Imported, Failed, Deleted) after a few days. The exact
# period depends on the state. Entries in certain other states
# get cleaned up after longer periods.
for status in translation_import_queue_entry_age.keys():
entry = self._makeProductEntry()
entry.potemplate = self.factory.makePOTemplate()
maximum_age = translation_import_queue_entry_age[status]
self._setStatus(entry, status)
# A day before the cleanup age for this status, the
# entry is left intact.
self._ageEntry(entry, maximum_age - timedelta(days=1))
entry_id = entry.id
# No write or delete action expected, so no reason to switch the
# database user. If it writes or deletes, the test has failed
# anyway.
self.queue._cleanUpObsoleteEntries(self.store)
self.assertTrue(self._exists(entry_id))
# Two days later, the entry is past its cleanup age and will
# be removed.
self._ageEntry(entry, timedelta(days=2))
with self.beingTheGardener():
self.queue._cleanUpObsoleteEntries(self.store)
self.assertFalse(
self._exists(entry_id),
"Queue entry in state '%s' was not removed." % status)
def test_cleanUpObsoleteEntries_blocked_ubuntu_po(self):
# _cleanUpObsoleteEntries deletes Ubuntu entries for gettext
# translations that are Blocked if they haven't been touched in
# a year. These entries once made up about half the queue. As
# far as we can tell all these PO files have been auto-blocked
# after their template uploads were blocked, so even if they
# were ever re-uploaded, they'd just get blocked again.
entry = self._makeDistroEntry(
path='fo.po', status=RosettaImportStatus.BLOCKED)
self._ageEntry(entry, timedelta(days=300))
entry_id = entry.id
# It hasn't been a year yet since the last status change; the
# entry stays in place.
with self.beingTheGardener():
self.queue._cleanUpObsoleteEntries(self.store)
self.assertTrue(self._exists(entry_id))
# Months later, a year has passed; the entry gets cleaned up.
self._ageEntry(entry, timedelta(days=100))
with self.beingTheGardener():
self.queue._cleanUpObsoleteEntries(self.store)
self.assertFalse(self._exists(entry_id))
def test_cleanUpObsoleteEntries_ignores_entry_age(self):
# _cleanUpObsoleteEntries looks at date of an entry's last
# status change; the upload date does not matter.
entry = self._makeDistroEntry(
path='fo.po', status=RosettaImportStatus.BLOCKED)
entry.dateimported -= timedelta(days=9000)
entry_id = entry.id
with self.beingTheGardener():
self.queue._cleanUpObsoleteEntries(self.store)
self.assertTrue(self._exists(entry_id))
entry.date_status_changed -= timedelta(days=400)
with self.beingTheGardener():
self.queue._cleanUpObsoleteEntries(self.store)
self.assertFalse(self._exists(entry_id))
def test_cleanUpObsoleteEntries_blocked_product_po(self):
# _cleanUpObsoleteEntries leaves blocked project uploads in
# place.
entry = self._makeProductEntry(
path='fo.po', status=RosettaImportStatus.BLOCKED)
self._ageEntry(entry, timedelta(days=400))
entry_id = entry.id
with self.beingTheGardener():
self.queue._cleanUpObsoleteEntries(self.store)
self.assertTrue(self._exists(entry_id))
def test_cleanUpObsoleteEntries_blocked_ubuntu_pot(self):
# _cleanUpObsoleteEntries leaves blocked Ubuntu templates in
# place.
entry = self._makeDistroEntry(
path='foo.pot', status=RosettaImportStatus.BLOCKED)
self._ageEntry(entry, timedelta(days=400))
entry_id = entry.id
with self.beingTheGardener():
self.queue._cleanUpObsoleteEntries(self.store)
self.assertTrue(self._exists(entry_id))
def test_cleanUpInactiveProductEntries(self):
# After a product is deactivated, _cleanUpInactiveProductEntries
# will clean up any entries it may have on the queue.
entry = self._makeProductEntry()
entry_id = entry.id
self.queue._cleanUpInactiveProductEntries(self.store)
self.assertTrue(self._exists(entry_id))
entry.productseries.product.active = False
entry.productseries.product.syncUpdate()
self.becomeTheGardener()
self.queue._cleanUpInactiveProductEntries(self.store)
self.assertFalse(self._exists(entry_id))
def test_cleanUpObsoleteDistroEntries(self):
# _cleanUpObsoleteDistroEntries cleans up entries for
# distroseries that are in the Obsolete state.
entry = self._makeDistroEntry()
entry_id = entry.id
self.queue._cleanUpObsoleteDistroEntries(self.store)
self.assertTrue(self._exists(entry_id))
entry.distroseries.status = SeriesStatus.OBSOLETE
entry.distroseries.syncUpdate()
self.becomeTheGardener()
self.queue._cleanUpObsoleteDistroEntries(self.store)
self.assertFalse(self._exists(entry_id))
class TestAutoApprovalNewPOFile(TestCaseWithFactory, GardenerDbUserMixin):
"""Test creation of new `POFile`s in approval."""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestAutoApprovalNewPOFile, self).setUp()
self.product = self.factory.makeProduct()
self.queue = TranslationImportQueue()
self.language = LanguageSet().getLanguageByCode('nl')
def _makeTemplate(self, series):
"""Create a template."""
return POTemplateSubset(productseries=series).new(
'test', 'test', 'test.pot', self.product.owner)
def _makeQueueEntry(self, series):
"""Create translation import queue entry."""
return self.queue.addOrUpdateEntry(
"%s.po" % self.language.code, 'contents', True,
self.product.owner, productseries=series)
def test_getGuessedPOFile_creates_POFile(self):
# Auto-approval may involve creating POFiles. The queue
# gardener has permissions to do this. The POFile's owner is
# the rosetta_experts team.
trunk = self.product.getSeries('trunk')
self._makeTemplate(trunk)
entry = self._makeQueueEntry(trunk)
rosetta_experts = getUtility(ILaunchpadCelebrities).rosetta_experts
self.becomeTheGardener()
pofile = entry.getGuessedPOFile()
self.assertIsInstance(pofile, POFile)
self.assertNotEqual(rosetta_experts, pofile.owner)
def test_getGuessedPOFile_creates_POFile_with_credits(self):
# When the approver creates a POFile for a template that
# has a translation credits message, it also includes a
# "translation" for the credits message.
trunk = self.product.getSeries('trunk')
template = self._makeTemplate(trunk)
credits = self.factory.makePOTMsgSet(
template, singular='translation-credits')
entry = self._makeQueueEntry(trunk)
self.becomeTheGardener()
entry.getGuessedPOFile()
credits.getCurrentTranslation(
template, self.language, template.translation_side)
self.assertNotEqual(None, credits)
class TestAutoBlocking(TestCaseWithFactory):
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestAutoBlocking, self).setUp()
self.queue = TranslationImportQueue()
# Our test queue operates on the master store instead of the
# slave store so we don't have to synchronize stores.
master_store = IMasterStore(TranslationImportQueueEntry)
self.queue._getSlaveStore = FakeMethod(result=master_store)
def _copyTargetFromEntry(self, entry):
"""Return a dict representing `entry`'s translation target.
:param entry: An existing `TranslationImportQueueEntry`, or None.
"""
if entry is None:
return {}
else:
return {
'distroseries': entry.distroseries,
'sourcepackagename': entry.sourcepackagename,
'productseries': entry.productseries,
}
def _makeTemplateEntry(self, suffix='.pot', directory=None, status=None,
same_target_as=None):
"""Create an import queue entry for a template.
If `same_target_as` is given, creates an entry for the same
translation target as `same_target_as`. This lets you create an
entry for the same translation target as another one.
"""
if suffix == '.xpi':
basename = 'en-US'
else:
basename = self.factory.getUniqueString()
filename = basename + suffix
if directory is None:
path = filename
else:
path = '/'.join([directory, filename])
target = self._copyTargetFromEntry(same_target_as)
return removeSecurityProxy(
self.factory.makeTranslationImportQueueEntry(
path=path, status=status, **target))
def _makeTranslationEntry(self, path, status=None, same_target_as=None):
"""Create an import queue entry for a translation file.
If `same_target_as` is given, creates an entry for the same
translation target as `same_target_as`. This lets you create an
entry that may have to be blocked depending on same_target_as.
"""
target = self._copyTargetFromEntry(same_target_as)
return removeSecurityProxy(
self.factory.makeTranslationImportQueueEntry(
path=path, status=status, **target))
def test_getBlockableDirectories_checks_templates(self):
old_blocklist = self.queue._getBlockableDirectories()
self._makeTemplateEntry(status=RosettaImportStatus.BLOCKED)
new_blocklist = self.queue._getBlockableDirectories()
self.assertEqual(len(old_blocklist) + 1, len(new_blocklist))
def test_getBlockableDirectories_ignores_translations(self):
old_blocklist = self.queue._getBlockableDirectories()
self._makeTranslationEntry(
'gl.po', status=RosettaImportStatus.BLOCKED)
new_blocklist = self.queue._getBlockableDirectories()
self.assertEqual(len(old_blocklist), len(new_blocklist))
def test_getBlockableDirectories_checks_xpi_templates(self):
old_blocklist = self.queue._getBlockableDirectories()
self._makeTemplateEntry(
suffix='.xpi', status=RosettaImportStatus.BLOCKED)
new_blocklist = self.queue._getBlockableDirectories()
self.assertEqual(len(old_blocklist) + 1, len(new_blocklist))
def test_getBlockableDirectories_ignores_xpi_translations(self):
old_blocklist = self.queue._getBlockableDirectories()
self._makeTranslationEntry(
'lt.xpi', status=RosettaImportStatus.BLOCKED)
new_blocklist = self.queue._getBlockableDirectories()
self.assertEqual(len(old_blocklist), len(new_blocklist))
def test_isBlockable_none(self):
blocklist = self.queue._getBlockableDirectories()
entry = self._makeTranslationEntry('nl.po')
self.assertFalse(self.queue._isBlockable(entry, blocklist))
def test_isBlockable_one_blocked(self):
blocked_template = self._makeTemplateEntry(
status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'de.po', same_target_as=blocked_template)
self.assertTrue(self.queue._isBlockable(translations, blocklist))
def test_isBlockable_multiple_blocked(self):
blocked1 = self._makeTemplateEntry(status=RosettaImportStatus.BLOCKED)
self._makeTemplateEntry(
status=RosettaImportStatus.BLOCKED, same_target_as=blocked1)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'lo.po', same_target_as=blocked1)
self.assertTrue(self.queue._isBlockable(translations, blocklist))
def test_isBlockable_one_unblocked(self):
unblocked = self._makeTemplateEntry()
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'xh.po', same_target_as=unblocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
def test_isBlockable_mixed(self):
# When there are both blocked and unblocked template entries in
# a directory, translation uploads for that directory are not
# blocked.
blocked = self._makeTemplateEntry(status=RosettaImportStatus.BLOCKED)
self._makeTemplateEntry(same_target_as=blocked)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'fr.po', same_target_as=blocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_rootdir_match(self):
# _getBlockableDirectories matches sees a template and
# translations file in the root directory as being in the same
# directory.
blocked = self._makeTemplateEntry(
directory=None, status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'es.po', same_target_as=blocked)
self.assertTrue(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_rootdir_nonmatch(self):
# _getBlockableDirectories matches sees a template in the root
# directory (i.e. without a directory component in its path) as
# being in a different directory from a translations upload in a
# subdirectory.
blocked = self._makeTemplateEntry(
directory=None, status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'po/es.po', same_target_as=blocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_subdir_match(self):
# _getBlockableDirectories matches sees a template and
# translations file in the same directory as being in the same
# directory.
blocked = self._makeTemplateEntry(
directory='po/module', status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'po/module/es.po', same_target_as=blocked)
self.assertTrue(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_subdir_nonmatch(self):
# _getBlockableDirectories matches sees a template in a
# subdirectory as being in a different directory from a
# translations upload in the root directory.
blocked = self._makeTemplateEntry(
directory='po', status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'es.po', same_target_as=blocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_nested_translations(self):
# _getBlockableDirectories sees a translations upload in a
# subdirectory of that on the template upload as being in a
# different directory.
blocked = self._makeTemplateEntry(
directory='po', status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'po/module/es.po', same_target_as=blocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_nested_template(self):
# _getBlockableDirectories sees a translations upload in one
# directory and a template upload in a subdirectory of that
# directory as being in different directories.
blocked = self._makeTemplateEntry(
directory='po/module', status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'po/es.po', same_target_as=blocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_substring_translations(self):
# _getBlockableDirectories sees the difference between a
# template's directory and a translation upload's directory even
# if the latter is a substring of the former.
blocked = self._makeTemplateEntry(
directory='po/moduleX', status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'po/module/es.po', same_target_as=blocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
def test_getBlockableDirectories_path_substring_template(self):
# _getBlockableDirectories sees the difference between a
# template's directory and a translation upload's directory even
# if the former is a substring of the latter.
blocked = self._makeTemplateEntry(
directory='po/module', status=RosettaImportStatus.BLOCKED)
blocklist = self.queue._getBlockableDirectories()
translations = self._makeTranslationEntry(
'po/moduleX/es.po', same_target_as=blocked)
self.assertFalse(self.queue._isBlockable(translations, blocklist))
|