Пример #1
0
    def test_translate_dennis(self):
        obj = SuperModel(locale='fr', desc=u'This is a test string')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'dennis', 'br', 'desc', 'en', 'trans_desc')
        assert obj.trans_desc == u'\xabTHIS IS A TEST STRING\xbb'
Пример #2
0
    def test_translate_fake(self):
        obj = SuperModel(locale='br', desc=u'This is a test string')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'fake', 'br', 'desc', 'en', 'trans_desc')
        assert obj.trans_desc == u'THIS IS A TEST STRING'
Пример #3
0
    def test_translate_gengo_human_english_copy_over(self):
        obj = SuperModel(locale='es', desc=u'This is English.')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
        # If the guesser guesses English, then we just copy it over.
        assert obj.trans_desc == u'This is English.'
Пример #4
0
    def test_translate_gengo_human_unsupported_pair(self):
        obj = SuperModel(locale='el', desc=u'This is really greek.')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'el', 'desc', 'en', 'trans_desc')
        # el -> en is not a supported pair, so it shouldn't get translated.
        assert obj.trans_desc == u''
Пример #5
0
    def test_gengo_push_translations(self):
        """Tests GengoOrders get created"""
        ght = GengoHumanTranslator()

        # Create a few jobs covering multiple languages
        descs = [
            ('es', u'Facebook no se puede enlazar con peru'),
            ('es', u'No es compatible con whatsap'),

            ('de', u'Absturze und langsam unter Android'),
        ]
        for lang, desc in descs:
            obj = SuperModel(locale=lang, desc=desc)
            obj.save()

            job = GengoJob(
                content_object=obj,
                tier='standard',
                src_field='desc',
                dst_field='trans_desc',
                src_lang=lang,
                dst_lang='en'
            )
            job.save()

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            mocker = GengoMock.return_value
            mocker.getAccountBalance.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'credits': '400.00',
                    u'currency': u'USD'
                }
            }
            # FIXME: This returns the same thing both times, but to
            # make the test "more kosher" we'd have this return two
            # different order_id values.
            mocker.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'order_id': u'1337',
                    u'job_count': 2,
                    u'credits_used': u'0.35',
                    u'currency': u'USD'
                }
            }

            ght.push_translations()

            assert GengoOrder.objects.count() == 2

            order_by_id = dict(
                [(order.id, order) for order in GengoOrder.objects.all()]
            )

            jobs = GengoJob.objects.all()
            for job in jobs:
                assert job.order_id in order_by_id
Пример #6
0
    def test_translate_gengo_human(self):
        # Note: This just sets up the GengoJob--it doesn't create any
        # Gengo human translation jobs.
        obj = SuperModel(
            locale='es', desc=u'Facebook no se puede enlazar con peru')
        obj.save()

        assert obj.trans_desc == u''
        translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
        # Nothing should be translated
        assert obj.trans_desc == u''

        assert len(GengoJob.objects.all()) == 1
Пример #7
0
    def test_translate_gengo_machine_english_copy_over(self):
        """If the guesser guesses english, we copy it over"""
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'This is English.')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'This is English.')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Пример #8
0
    def test_translate_gengo_machine_unknown_language(self):
        """Translation should handle unknown languages without erroring"""
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Пример #9
0
    def test_one(self):
        """Test the basic case"""
        model_path = SuperModel.__module__ + '.' + SuperModel.__name__

        obj = SuperModel(locale='br', desc=u'This is a test string')
        obj.save()

        # Verify no translation, yet
        assert obj.trans_desc == u''
        translate_tasks_by_id_list.delay(model_path, [obj.id])

        # Fetch the object from the db to verify it's been translated.
        obj = SuperModel.objects.get(id=obj.id)

        assert obj.trans_desc == u'THIS IS A TEST STRING'
Пример #10
0
    def test_no_translate_if_disabled(self):
        """No GengoAPI calls if gengosystem switch is disabled"""
        with patch('fjord.translations.models.waffle') as waffle_mock:
            waffle_mock.switch_is_active.return_value = False

            with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
                obj = SuperModel(locale='es', desc=u'Muy lento')
                obj.save()

                eq_(obj.trans_desc, u'')
                translate(obj, 'gengo_machine', 'es', 'desc', 'en',
                          'trans_desc')
                eq_(obj.trans_desc, u'')

                # We should not have used the API at all.
                eq_(GengoMock.called, False)
Пример #11
0
    def test_gengo_machine_translation(self):
        # Note: This doesn't work in the sandbox, so we skip it if
        # we're in sandbox mode. That is some happy horseshit, but so
        # it goes.

        # Note: This test might be brittle since it's calling out to
        # Gengo to do a machine translation and it's entirely possible
        # that they might return a different translation some day.
        obj = SuperModel(
            locale='es',
            desc=u'Facebook no se puede enlazar con peru'
        )
        obj.save()

        eq_(obj.trans_desc, u'')
        translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
        eq_(obj.trans_desc, u'Facebook can bind with peru')
Пример #12
0
    def test_many(self):
        model_path = SuperModel.__module__ + '.' + SuperModel.__name__

        objs = []
        for i in range(50):
            obj = SuperModel(locale='br', desc=u'string %d' % i)
            obj.save()
            objs.append(obj)

        translate_tasks_by_id_list.delay(
            model_path, [obj_.id for obj_ in objs])

        for obj in objs:
            obj = SuperModel.objects.get(id=obj.id)
            # Note: The fake translation just uppercases things. We're
            # abusing inner knowledge of that here.
            assert obj.trans_desc == obj.desc.upper()
Пример #13
0
    def test_no_translate_if_disabled(self):
        """No GengoAPI calls if gengosystem switch is disabled"""
        with patch('fjord.translations.models.waffle') as waffle_mock:
            waffle_mock.switch_is_active.return_value = False

            with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
                # Note: This just sets up the GengoJob--it doesn't
                # create any Gengo human translation jobs.
                obj = SuperModel(
                    locale='es', desc=u'Facebook no se puede enlazar con peru')
                obj.save()

                assert obj.trans_desc == u''
                translate(obj, 'gengo_human', 'es', 'desc', 'en', 'trans_desc')
                assert obj.trans_desc == u''

                # Verify no jobs were created
                assert len(GengoJob.objects.all()) == 0

                # Verify we didn't call the API at all.
                assert GengoMock.called == False
Пример #14
0
    def test_translate_gengo_machine_unsupported_language(self):
        """Translation should handle unsupported languages without erroring"""
        gengo_utils.GENGO_LANGUAGE_CACHE = (
            {u'opstat': u'ok',
             u'response': [
                 {u'unit_type': u'word', u'localized_name': u'Deutsch',
                  u'lc': u'de', u'language': u'German'}
             ]},
            (u'de',)
        )

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_mock_instance = GengoMock.return_value

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'')

            # Make sure we don't call postTranslationJobs().
            eq_(gengo_mock_instance.postTranslationJobs.call_count, 0)
Пример #15
0
    def test_translate_gengo_machine(self):
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            # Note: We're mocking with "Muy lento" because it's
            # short, but the Gengo language guesser actually can't
            # figure out what language that is.
            instance = GengoMock.return_value
            instance.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'jobs': {
                        u'job_1': {
                            u'status': u'approved',
                            u'job_id': u'NULL',
                            u'credits': 0,
                            u'unit_count': 7,
                            u'body_src': u'Muy lento',
                            u'mt': 1,
                            u'eta': -1,
                            u'custom_data': u'10101',
                            u'tier': u'machine',
                            u'lc_tgt': u'en',
                            u'lc_src': u'es',
                            u'body_tgt': u'Very slow',
                            u'slug': u'Input machine translation',
                            u'ctime': u'2014-05-21 15:09:50.361847'
                        }
                    }
                }
            }

            obj = SuperModel(locale='es', desc=u'Muy lento')
            obj.save()

            eq_(obj.trans_desc, u'')
            translate(obj, 'gengo_machine', 'es', 'desc', 'en', 'trans_desc')
            eq_(obj.trans_desc, u'Very slow')
Пример #16
0
    def test_pull_translations(self):
        ght = GengoHumanTranslator()

        obj = SuperModel(locale='es', desc=u'No es compatible con whatsap')
        obj.save()

        gj = GengoJob(
            content_object=obj,
            tier='standard',
            src_field='desc',
            dst_field='trans_desc',
            src_lang='es',
            dst_lang='en'
        )
        gj.save()

        order = GengoOrder(order_id=u'263413')
        order.save()

        gj.assign_to_order(order)

        gtoj_resp = {
            u'opstat': u'ok',
            u'response': {
                u'order': {
                    u'jobs_pending': [],
                    u'jobs_revising': [],
                    u'as_group': 0,
                    u'order_id': u'263413',
                    u'jobs_queued': u'0',
                    u'total_credits': u'0.35',
                    u'currency': u'USD',
                    u'total_units': u'7',
                    u'jobs_approved': [u'746197'],
                    u'jobs_reviewable': [],
                    u'jobs_available': [],
                    u'total_jobs': u'1'
                }
            }
        }

        gtjb_resp = {
            u'opstat': u'ok',
            u'response': {
                u'jobs': [
                    {
                        u'status': u'approved',
                        u'job_id': u'746197',
                        u'currency': u'USD',
                        u'order_id': u'263413',
                        u'body_tgt': u'No es compatible con whatsap',
                        u'body_src': u'Not compatible with whatsap',
                        u'credits': u'0.35',
                        u'eta': -1,
                        u'custom_data': u'localhost||GengoJob||{0}'.format(
                            gj.id),
                        u'tier': u'standard',
                        u'lc_tgt': u'en',
                        u'lc_src': u'es',
                        u'auto_approve': u'1',
                        u'unit_count': u'7',
                        u'slug': u'Mozilla Input feedback response',
                        u'ctime': 1403296006
                    }
                ]
            }
        }

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            instance = GengoMock.return_value
            instance.getTranslationOrderJobs.return_value = gtoj_resp
            instance.getTranslationJobBatch.return_value = gtjb_resp

            ght.pull_translations()

            jobs = GengoJob.objects.all()
            assert len(jobs) == 1
            assert jobs[0].status == 'complete'

            orders = GengoOrder.objects.all()
            assert len(orders) == 1
            assert orders[0].status == 'complete'
Пример #17
0
    def test_gengo_push_translations_not_enough_balance(self):
        """Tests enough balance for one order, but not both"""
        ght = GengoHumanTranslator()

        # Create a few jobs covering multiple languages
        descs = [
            ('es', u'Facebook no se puede enlazar con peru'),
            ('de', u'Absturze und langsam unter Android'),
        ]
        for lang, desc in descs:
            obj = SuperModel(locale=lang, desc=desc)
            obj.save()

            job = GengoJob(
                content_object=obj,
                tier='standard',
                src_field='desc',
                dst_field='trans_desc',
                src_lang=lang,
                dst_lang='en'
            )
            job.save()

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            # FIXME: This returns the same thing both times, but to
            # make the test "more kosher" we'd have this return two
            # different order_id values.
            mocker = GengoMock.return_value
            mocker.getAccountBalance.return_value = {
                u'opstat': u'ok',
                u'response': {
                    # Enough for one order, but dips below threshold
                    # for the second one.
                    u'credits': '20.30',
                    u'currency': u'USD'
                }
            }
            mocker.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'order_id': u'1337',
                    u'job_count': 2,
                    u'credits_used': u'0.35',
                    u'currency': u'USD'
                }
            }

            ght.push_translations()

            assert GengoOrder.objects.count() == 1
            # The "it's too low" email only.
            assert len(mail.outbox) == 1

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            # FIXME: This returns the same thing both times, but to
            # make the test "more kosher" we'd have this return two
            # different order_id values.
            mocker = GengoMock.return_value
            mocker.getAccountBalance.return_value = {
                u'opstat': u'ok',
                u'response': {
                    # This is the balance after one order.
                    u'credits': '19.95',
                    u'currency': u'USD'
                }
            }
            mocker.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'order_id': u'1337',
                    u'job_count': 2,
                    u'credits_used': u'0.35',
                    u'currency': u'USD'
                }
            }

            # The next time push_translations runs, it shouldn't
            # create any new jobs, but should send an email.
            ght.push_translations()

            assert GengoOrder.objects.count() == 1
            # This generates one more email.
            assert len(mail.outbox) == 2
Пример #18
0
    def test_gengo_push_translations_not_enough_balance(self):
        """Tests enough balance for one order, but not both"""
        # Add recipients to mailing list
        ml = MailingList.objects.get(name='gengo_balance')
        ml.members = u'*****@*****.**'
        ml.save()

        ght = GengoHumanTranslator()

        # Create a few jobs covering multiple languages
        descs = [
            ('es', u'Facebook no se puede enlazar con peru'),
            ('de', u'Absturze und langsam unter Android'),
        ]
        for lang, desc in descs:
            obj = SuperModel(locale=lang, desc=desc)
            obj.save()

            job = GengoJob(content_object=obj,
                           tier='standard',
                           src_field='desc',
                           dst_field='trans_desc',
                           src_lang=lang,
                           dst_lang='en')
            job.save()

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            # FIXME: This returns the same thing both times, but to
            # make the test "more kosher" we'd have this return two
            # different order_id values.
            mocker = GengoMock.return_value
            mocker.getAccountBalance.return_value = {
                u'opstat': u'ok',
                u'response': {
                    # Enough for one order, but dips below threshold
                    # for the second one.
                    u'credits': '20.30',
                    u'currency': u'USD'
                }
            }
            mocker.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'order_id': u'1337',
                    u'job_count': 2,
                    u'credits_used': u'0.35',
                    u'currency': u'USD'
                }
            }

            ght.push_translations()

            assert GengoOrder.objects.count() == 1
            # The "it's too low" email only.
            assert len(mail.outbox) == 1

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            # FIXME: This returns the same thing both times, but to
            # make the test "more kosher" we'd have this return two
            # different order_id values.
            mocker = GengoMock.return_value
            mocker.getAccountBalance.return_value = {
                u'opstat': u'ok',
                u'response': {
                    # This is the balance after one order.
                    u'credits': '19.95',
                    u'currency': u'USD'
                }
            }
            mocker.postTranslationJobs.return_value = {
                u'opstat': u'ok',
                u'response': {
                    u'order_id': u'1337',
                    u'job_count': 2,
                    u'credits_used': u'0.35',
                    u'currency': u'USD'
                }
            }

            # The next time push_translations runs, it shouldn't
            # create any new jobs, but should send an email.
            ght.push_translations()

            assert GengoOrder.objects.count() == 1
            # This generates one more email.
            assert len(mail.outbox) == 2
Пример #19
0
    def test_pull_translations(self):
        ght = GengoHumanTranslator()

        obj = SuperModel(locale='es', desc=u'No es compatible con whatsap')
        obj.save()

        gj = GengoJob(content_object=obj,
                      tier='standard',
                      src_field='desc',
                      dst_field='trans_desc',
                      src_lang='es',
                      dst_lang='en')
        gj.save()

        order = GengoOrder(order_id=u'263413')
        order.save()

        gj.assign_to_order(order)

        gtoj_resp = {
            u'opstat': u'ok',
            u'response': {
                u'order': {
                    u'jobs_pending': [],
                    u'jobs_revising': [],
                    u'as_group': 0,
                    u'order_id': u'263413',
                    u'jobs_queued': u'0',
                    u'total_credits': u'0.35',
                    u'currency': u'USD',
                    u'total_units': u'7',
                    u'jobs_approved': [u'746197'],
                    u'jobs_reviewable': [],
                    u'jobs_available': [],
                    u'total_jobs': u'1'
                }
            }
        }

        gtjb_resp = {
            u'opstat': u'ok',
            u'response': {
                u'jobs': [{
                    u'status':
                    u'approved',
                    u'job_id':
                    u'746197',
                    u'currency':
                    u'USD',
                    u'order_id':
                    u'263413',
                    u'body_tgt':
                    u'No es compatible con whatsap',
                    u'body_src':
                    u'Not compatible with whatsap',
                    u'credits':
                    u'0.35',
                    u'eta':
                    -1,
                    u'custom_data':
                    u'localhost||GengoJob||{0}'.format(gj.id),
                    u'tier':
                    u'standard',
                    u'lc_tgt':
                    u'en',
                    u'lc_src':
                    u'es',
                    u'auto_approve':
                    u'1',
                    u'unit_count':
                    u'7',
                    u'slug':
                    u'Mozilla Input feedback response',
                    u'ctime':
                    1403296006
                }]
            }
        }

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            instance = GengoMock.return_value
            instance.getTranslationOrderJobs.return_value = gtoj_resp
            instance.getTranslationJobBatch.return_value = gtjb_resp

            ght.pull_translations()

            jobs = GengoJob.objects.all()
            assert len(jobs) == 1
            assert jobs[0].status == 'complete'

            orders = GengoOrder.objects.all()
            assert len(orders) == 1
            assert orders[0].status == 'complete'