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

"""Tests for the `TranslationBuildApprover`."""

__metaclass__ = type

import transaction
from zope.component import getUtility

from lp.services.config import config
from lp.testing.layers import LaunchpadZopelessLayer
from lp.testing import TestCaseWithFactory
from lp.translations.enums import RosettaImportStatus
from lp.translations.interfaces.translationimportqueue import (
    ITranslationImportQueue,
    )
from lp.translations.model.approver import TranslationBuildApprover


class TestTranslationBuildApprover(TestCaseWithFactory):

    layer = LaunchpadZopelessLayer

    def setUp(self):
        super(TestTranslationBuildApprover, self).setUp()
        self.queue = getUtility(ITranslationImportQueue)
        self.uploader = self.factory.makePerson()

    def _makeApprovedEntries(self, series, approver, filenames):
        """Create a list of queue entries and approve them."""
        return [
            approver.approve(self.queue.addOrUpdateEntry(
                path, "#Dummy content.", False, self.uploader,
                productseries=series))
            for path in filenames]

    def _becomeBuilddMaster(self):
        """Switch db identity to the script that uses this approver."""
        transaction.commit()
        self.layer.switchDbUser(config.builddmaster.dbuser)

    def test_approve_all_new(self):
        # A happy approval case, all new templates.
        filenames = [
            'po-domain1/domain1.pot',
            'po-domain2/domain2.pot',
            'po-domain3/domain3.pot',
            ]
        series = self.factory.makeProductSeries()

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(
            [RosettaImportStatus.APPROVED] * len(entries),
            [entry.status for entry in entries])
        self.assertEqual(
            ['domain1', 'domain2', 'domain3'],
            [entry.potemplate.name for entry in entries])

    def test_approve_only_pots(self):
        # Only template files will be approved.
        filenames = [
            'po/domain1.po',
            'po/eo.po',
            ]
        series = self.factory.makeProductSeries()

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(
            [RosettaImportStatus.NEEDS_REVIEW] * len(entries),
            [entry.status for entry in entries])

    def test_approve_all_existing(self):
        # A happy approval case, all existing templates.
        filenames = [
            'po-domain1/domain1.pot',
            'po-domain2/domain2.pot',
            'po-domain3/domain3.pot',
            ]
        series = self.factory.makeProductSeries()
        domain1_pot = self.factory.makePOTemplate(
            productseries=series, name='domain1')
        domain2_pot = self.factory.makePOTemplate(
            productseries=series, name='domain2')
        domain3_pot = self.factory.makePOTemplate(
            productseries=series, name='domain3')

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(
            [RosettaImportStatus.APPROVED] * len(entries),
            [entry.status for entry in entries])
        self.assertEqual(
            [domain1_pot, domain2_pot, domain3_pot],
            [entry.potemplate for entry in entries])

    def test_approve_some_existing(self):
        # A happy approval case, some existing templates.
        filenames = [
            'po-domain1/domain1.pot',
            'po-domain2/domain2.pot',
            'po-domain3/domain3.pot',
            'po-domain4/domain4.pot',
            ]
        series = self.factory.makeProductSeries()
        domain1_pot = self.factory.makePOTemplate(
            productseries=series, name='domain1')
        domain2_pot = self.factory.makePOTemplate(
            productseries=series, name='domain2')

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(
            [RosettaImportStatus.APPROVED] * len(entries),
            [entry.status for entry in entries])
        self.assertEqual(domain1_pot, entries[0].potemplate)
        self.assertEqual(domain2_pot, entries[1].potemplate)
        self.assertEqual('domain3', entries[2].potemplate.name)
        self.assertEqual('domain4', entries[3].potemplate.name)

    def test_approve_generic_name_one_new(self):
        # Generic names are OK, if there is only one.
        filenames = [
            'po/messages.pot',
            ]
        product = self.factory.makeProduct(name='fooproduct')
        series = product.getSeries('trunk')

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(RosettaImportStatus.APPROVED, entries[0].status)
        self.assertEqual('fooproduct', entries[0].potemplate.name)

    def test_approve_generic_name_one_existing(self):
        # Generic names are OK, if there is only one.
        filenames = [
            'po/messages.pot',
            ]
        series = self.factory.makeProductSeries()
        pot = self.factory.makePOTemplate(productseries=series)

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(RosettaImportStatus.APPROVED, entries[0].status)
        self.assertEqual(pot, entries[0].potemplate)

    def test_approve_generic_name_multiple_files(self):
        # Generic names in combination with others don't get approved.
        filenames = [
            'po/messages.pot',
            'mydomain/mydomain.pot',
            ]
        series = self.factory.makeProductSeries()

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(
            [RosettaImportStatus.NEEDS_REVIEW, RosettaImportStatus.APPROVED],
            [entry.status for entry in entries])
        self.assertEqual('mydomain', entries[1].potemplate.name)

    def test_approve_generic_name_multiple_templates(self):
        # Generic names don't get approved when more than one template exists.
        filenames = [
            'po/messages.pot',
            ]
        series = self.factory.makeProductSeries()
        self.factory.makePOTemplate(productseries=series)
        self.factory.makePOTemplate(productseries=series)

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(RosettaImportStatus.NEEDS_REVIEW, entries[0].status)

    def test_approve_not_in_list(self):
        # A file that is not the list of filenames is not approved.
        filenames = [
            'po-domain1/domain1.pot',
            'po-domain2/domain2.pot',
            ]
        series = self.factory.makeProductSeries()

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(
            series, approver, filenames + ['po-domain3/domain3.pot'])

        self.assertEqual([
                RosettaImportStatus.APPROVED,
                RosettaImportStatus.APPROVED,
                RosettaImportStatus.NEEDS_REVIEW,
                ],
                [entry.status for entry in entries])

    def test_approve_by_path(self):
        # A file will be targeted to an existing template if the paths match.
        filenames = [
            'po-domain1/domain1.pot',
            'po-domain2/domain2.pot',
            ]
        series = self.factory.makeProductSeries()
        domain1_pot = self.factory.makePOTemplate(
            productseries=series, name='name1', path=filenames[0])
        domain2_pot = self.factory.makePOTemplate(
            productseries=series, name='name2', path=filenames[1])

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(
            [RosettaImportStatus.APPROVED] * len(entries),
            [entry.status for entry in entries])
        self.assertEqual(
            [domain1_pot, domain2_pot],
            [entry.potemplate for entry in entries])

    def test_approve_path_updated(self):
        # The path of an existing template will be updated with the path
        # from the entry..
        filenames = [
            'po-domain1/domain1.pot',
            ]
        series = self.factory.makeProductSeries()
        domain1_pot = self.factory.makePOTemplate(
            productseries=series, name='domain1', path='po/foo.pot')

        self._becomeBuilddMaster()
        approver = TranslationBuildApprover(filenames, productseries=series)
        entries = self._makeApprovedEntries(series, approver, filenames)

        self.assertEqual(RosettaImportStatus.APPROVED, entries[0].status)
        self.assertEqual(filenames[0], domain1_pot.path)