~launchpad-pqm/launchpad/devel

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
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

__metaclass__ = type

from zope.interface.verify import verifyObject

from lp.testing.layers import ZopelessDatabaseLayer
from lp.testing import TestCaseWithFactory
from lp.translations.interfaces.hastranslationtemplates import (
    IHasTranslationTemplates,
    )
from lp.translations.interfaces.translationfileformat import (
    TranslationFileFormat,
    )


class HasTranslationTemplatesTestMixin:
    """Test behaviour of objects with translation templates."""

    layer = ZopelessDatabaseLayer

    def setUp(self):
        # Create a product with two series and a shared POTemplate
        # in different series ('devel' and 'stable').
        super(HasTranslationTemplatesTestMixin, self).setUp()

    def createTranslationTemplate(self, name=None, priority=0):
        """Attaches a template to appropriate container."""
        raise NotImplementedError(
            'This must be provided by an executable test.')

    def createTranslationFile(self, name=None, priority=0):
        """Attaches a pofile to appropriate container."""
        raise NotImplementedError(
            'This must be provided by an executable test.')

    def createPackaging(self):
        """Creates a packaging link for the container."""
        raise NotImplementedError(
            'This must be provided by an executable test.')

    def createSharingTranslationTemplate(self):
        """Attaches a template to the sharing partner of the container."""
        raise NotImplementedError(
            'This must be provided by an executable test.')

    def test_implements_interface(self):
        # Make sure container implements IHasTranslationTemplates.
        verifyObject(IHasTranslationTemplates, self.container)

    def test_getCurrentTranslationTemplates(self):
        # With no current templates, we get an empty result set.
        results = self.container.getCurrentTranslationTemplates()
        current_templates = list(results)
        self.assertEquals([], current_templates)
        self.assertFalse(bool(results.any()))

        # With one of the templates marked as current, it is returned.
        template1 = self.createTranslationTemplate("one", priority=1)
        current_templates = list(
            self.container.getCurrentTranslationTemplates())
        self.assertEquals([template1], current_templates)

        # With two current templates, they are sorted by priority,
        # with higher numbers representing higher priority.
        template2 = self.createTranslationTemplate("two", priority=2)
        current_templates = list(
            self.container.getCurrentTranslationTemplates())
        self.assertEquals([template2, template1], current_templates)

        # Adding an obsolete template changes nothing.
        template3 = self.createTranslationTemplate("obsolete")
        template3.iscurrent = False
        current_templates = list(
            self.container.getCurrentTranslationTemplates())
        self.assertEquals([template2, template1], current_templates)

    def test_getCurrentTranslationTemplates_ids(self):
        # Returning just IDs works fine as well.
        template1 = self.createTranslationTemplate("one", priority=1)
        template2 = self.createTranslationTemplate("two", priority=2)
        current_templates_ids = list(
            self.container.getCurrentTranslationTemplates(just_ids=True))
        self.assertEquals(
            [template2.id, template1.id],
            current_templates_ids)

    def test_getCurrentTranslationFiles_empty(self):
        # With no current templates, we get an empty result set.
        current_translations = list(
            self.container.getCurrentTranslationFiles())
        self.assertEquals([], current_translations)

        # Even with one of the templates marked as current, nothing is
        # returned before POFile is added.
        template1 = self.createTranslationTemplate("one")
        current_translations = list(
            self.container.getCurrentTranslationFiles())
        self.assertEquals([], current_translations)

        # If template is not current, nothing is returned even if
        # there are POFiles attached to it.
        template1.iscurrent = False
        self.factory.makePOFile('sr', potemplate=template1)
        current_translations = list(
            self.container.getCurrentTranslationFiles())
        self.assertEquals([], current_translations)

    def test_getCurrentTranslationFiles_current(self):
        # If POFiles are attached to a current template, they are returned.
        template1 = self.createTranslationTemplate("one")
        pofile_sr = self.factory.makePOFile('sr', potemplate=template1)
        pofile_es = self.factory.makePOFile('es', potemplate=template1)
        # They are returned unordered, so we'll use a set over them
        # to make tests stable.
        current_translations = set(
            self.container.getCurrentTranslationFiles())
        self.assertEquals(
            set([pofile_sr, pofile_es]),
            current_translations)

        # All files, no matter what template they are in, are returned.
        template2 = self.createTranslationTemplate("two")
        pofile2_sr = self.factory.makePOFile('sr', potemplate=template2)
        current_translations = set(
            self.container.getCurrentTranslationFiles())
        self.assertEquals(
            set([pofile_sr, pofile_es, pofile2_sr]),
            current_translations)

        # If template is marked as obsolete, attached POFiles are
        # not returned anymore.
        template2.iscurrent = False
        current_translations = set(
            self.container.getCurrentTranslationFiles())
        self.assertEquals(
            set([pofile_sr, pofile_es]),
            current_translations)

    def test_getCurrentTranslationFiles_ids(self):
        # We can also fetch only IDs.
        template1 = self.createTranslationTemplate("one")
        pofile_sr = self.factory.makePOFile('sr', potemplate=template1)
        pofile_es = self.factory.makePOFile('es', potemplate=template1)
        current_translations_ids = set(
            self.container.getCurrentTranslationFiles(just_ids=True))
        self.assertEquals(
            set([pofile_sr.id, pofile_es.id]),
            current_translations_ids)

    def test_has_current_translation_templates__no_template(self):
        # A series without templates has no current templates.
        self.assertFalse(self.container.has_current_translation_templates)

    def test_has_current_translation_templates__current_template(self):
        # A series with a current template has current templates.
        self.createTranslationTemplate()
        self.assertTrue(self.container.has_current_translation_templates)

    def test_has_current_translation_templates__noncurrent_template(self):
        # A series with only non-current templates has no current
        # templates.
        template = self.createTranslationTemplate()
        template.iscurrent = False
        self.assertFalse(self.container.has_current_translation_templates)

    def test_has_current_translation_templates__two_templates(self):
        # A series with current and non-current templates has current
        # templates.
        template = self.createTranslationTemplate()
        template.iscurrent = False
        self.createTranslationTemplate()
        self.assertTrue(self.container.has_current_translation_templates)

    def test_has_obsolete_translation_templates__no_templates(self):
        # A series without templates has no obsolete templates.
        self.assertFalse(self.container.has_obsolete_translation_templates)

    def test_has_obsolete_translation_templates__current_template(self):
        # A series with a current template has no obsolete templates either.
        self.createTranslationTemplate()
        self.assertFalse(self.container.has_obsolete_translation_templates)

    def test_has_obsolete_translation_templates__noncurrent_template(self):
        # A series with only non-current templates has obsolete templates.
        template = self.createTranslationTemplate()
        template.iscurrent = False
        self.assertTrue(self.container.has_obsolete_translation_templates)

    def test_has_obsolete_translation_templates__two_templates(self):
        # A series with current and non-current templates has obsolete
        # templates.
        template = self.createTranslationTemplate()
        template.iscurrent = False
        self.createTranslationTemplate()
        self.assertTrue(self.container.has_obsolete_translation_templates)

    def test_has_sharing_translation_templates__no_link(self):
        # Without a packaging link, no sharing templates are found.
        self.assertFalse(self.container.has_sharing_translation_templates)

    def test_has_sharing_translation_templates__no_templates(self):
        # Without templates on the other side, no sharing templates are found.
        self.createPackaging()
        self.assertFalse(self.container.has_sharing_translation_templates)

    def test_has_sharing_translation_templates__templates(self):
        # Without templates on the other side, no sharing templates are found.
        self.createPackaging()
        self.createSharingTranslationTemplate()
        self.assertTrue(self.container.has_sharing_translation_templates)

    def test_has_translation_files(self):
        # has_translations_files should only return true if the object has
        # pofiles.
        self.assertFalse(self.container.has_translation_files)
        self.createTranslationFile("one")
        self.assertTrue(self.container.has_translation_files)

    def test_getTranslationTemplateByName(self):
        template_name = self.factory.getUniqueString()
        # A series without templates does not find the template.
        self.assertEqual(
            None, self.container.getTranslationTemplateByName(template_name))

        # A template with a different name is not found.
        self.createTranslationTemplate(self.factory.getUniqueString())
        self.assertEqual(
            None, self.container.getTranslationTemplateByName(template_name))

        # Only the template with the correct name is returned.
        template = self.createTranslationTemplate(template_name)
        self.assertEqual(
            template,
            self.container.getTranslationTemplateByName(template_name))

    def test_getTranslationTemplateFormats(self):
        # Check that translation_template_formats works properly.

        # With no templates, empty list is returned.
        all_formats = self.container.getTranslationTemplateFormats()
        self.assertEquals([], all_formats)

        # With one template, that template's format is returned.
        template1 = self.createTranslationTemplate("one")
        template1.source_file_format = TranslationFileFormat.PO
        all_formats = self.container.getTranslationTemplateFormats()
        self.assertEquals(
            [TranslationFileFormat.PO],
            all_formats)

        # With multiple templates of the same format, that
        # format is still returned only once.
        template2 = self.createTranslationTemplate("two")
        template2.source_file_format = TranslationFileFormat.PO
        all_formats = self.container.getTranslationTemplateFormats()
        self.assertEquals(
            [TranslationFileFormat.PO],
            all_formats)

        # With another template of a different format,
        # we get that format in a returned list.
        template3 = self.createTranslationTemplate("three")
        template3.source_file_format = TranslationFileFormat.XPI
        all_formats = self.container.getTranslationTemplateFormats()

        # Items are sorted by the format values, PO==1 < XPI==3.
        self.assertEquals(
            [TranslationFileFormat.PO, TranslationFileFormat.XPI],
            all_formats)


class TestProductSeriesHasTranslationTemplates(
    HasTranslationTemplatesTestMixin, TestCaseWithFactory):
    """Test implementation of IHasTranslationTemplates on ProductSeries."""

    def createTranslationTemplate(self, name=None, priority=0):
        potemplate = self.factory.makePOTemplate(
            name=name, productseries=self.container)
        potemplate.priority = priority
        return potemplate

    def createTranslationFile(self, name, priority=0):
        potemplate = self.createTranslationTemplate(name, priority)
        pofile = self.factory.makePOFile(
            language_code='es',
            potemplate=potemplate)
        return pofile

    def createPackaging(self):
        self.packaging = self.factory.makePackagingLink(
            productseries=self.container, in_ubuntu=True)
        return self.packaging

    def createSharingTranslationTemplate(self):
        return self.factory.makePOTemplate(
            sourcepackage=self.packaging.sourcepackage)

    def setUp(self):
        super(TestProductSeriesHasTranslationTemplates, self).setUp()
        self.container = self.factory.makeProductSeries()


class TestSourcePackageHasTranslationTemplates(
    HasTranslationTemplatesTestMixin, TestCaseWithFactory):
    """Test implementation of IHasTranslationTemplates on ProductSeries."""

    def createTranslationTemplate(self, name=None, priority=0):
        potemplate = self.factory.makePOTemplate(
            name=name, distroseries=self.container.distroseries,
            sourcepackagename=self.container.sourcepackagename)
        potemplate.priority = priority
        return potemplate

    def createTranslationFile(self, name, priority=0):
        potemplate = self.createTranslationTemplate(name, priority)
        pofile = self.factory.makePOFile(
            language_code='es',
            potemplate=potemplate)
        return pofile

    def createPackaging(self):
        self.packaging = self.factory.makePackagingLink(
            sourcepackage=self.container)
        return self.packaging

    def createSharingTranslationTemplate(self):
        return self.factory.makePOTemplate(
            productseries=self.packaging.productseries)

    def setUp(self):
        super(TestSourcePackageHasTranslationTemplates, self).setUp()
        self.container = self.factory.makeSourcePackage()


class TestDistroSeriesHasTranslationTemplates(
    HasTranslationTemplatesTestMixin, TestCaseWithFactory):
    """Test implementation of IHasTranslationTemplates on ProductSeries."""

    def createTranslationTemplate(self, name=None, priority=0):
        sourcepackage = self.factory.makeSourcePackage(
            distroseries=self.container)
        potemplate = self.factory.makePOTemplate(
            name=name, distroseries=self.container,
            sourcepackagename=sourcepackage.sourcepackagename)
        potemplate.priority = priority
        return potemplate

    def createTranslationFile(self, name, priority=0):
        potemplate = self.createTranslationTemplate(name, priority)
        pofile = self.factory.makePOFile(
            language_code='es',
            potemplate=potemplate)
        return pofile

    def createPackaging(self):
        sourcepackage = self.factory.makeSourcePackage(
            distroseries=self.container)
        self.packaging = self.factory.makePackagingLink(
            sourcepackage=sourcepackage)
        return self.packaging

    def createSharingTranslationTemplate(self):
        return self.factory.makePOTemplate(
            productseries=self.packaging.productseries)

    def setUp(self):
        super(TestDistroSeriesHasTranslationTemplates, self).setUp()
        self.container = self.factory.makeDistroSeries()

    def test_has_sharing_translation_templates__templates(self):
        # This attribute is always False for DistroSeries
        self.createPackaging()
        self.createSharingTranslationTemplate()
        self.assertFalse(self.container.has_sharing_translation_templates)