Пример #1
0
    def test_get_languages(self):
        gengo_utils.GENGO_LANGUAGE_CACHE = None

        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.getServiceLanguages.return_value = {
                u'opstat': u'ok',
                u'response': [
                    {u'unit_type': u'word', u'localized_name': u'Espa\xf1ol',
                     u'lc': u'es', u'language': u'Spanish (Spain)'},
                ]
            }

            # Make sure the cache is empty
            eq_(gengo_utils.GENGO_LANGUAGE_CACHE, None)

            # Test that we generate a list based on what we think the
            # response is.
            gengo_api = gengo_utils.FjordGengo()
            eq_(gengo_api.get_languages(), (u'es',))

            # Test that the new list is cached.
            eq_(gengo_utils.GENGO_LANGUAGE_CACHE, (u'es',))
Пример #2
0
    def test_no_approved_jobs(self):
        gtoj_resp = {
            u'opstat': u'ok',
            u'response': {
                u'order': {
                    u'jobs_pending': [u'746197'],
                    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'jobs_reviewable': [],
                    u'jobs_available': [],
                    u'total_jobs': u'1'
                }
            }
        }

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

            gengo_api = gengo_utils.FjordGengo()
            jobs = gengo_api.completed_jobs_for_order('263413')
            assert jobs == []
Пример #3
0
    def test_machine_translate(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'
                        }
                    }
                }
            }

            gengo_api = gengo_utils.FjordGengo()
            text = u'Muy lento'
            eq_(gengo_api.machine_translate(1010, 'es', 'en', text),
                u'Very slow')
Пример #4
0
    def test_get_languages(self):
        response = {
            u'opstat':
            u'ok',
            u'response': [{
                u'unit_type': u'word',
                u'localized_name': u'Espa\xf1ol',
                u'lc': u'es',
                u'language': u'Spanish (Spain)'
            }]
        }

        gengo_utils.GENGO_LANGUAGE_CACHE = None

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            instance = GengoMock.return_value
            instance.getServiceLanguages.return_value = response

            # Make sure the cache is empty
            assert gengo_utils.GENGO_LANGUAGE_CACHE is None

            # Test that we generate a list based on what we think the
            # response is.
            gengo_api = gengo_utils.FjordGengo()
            assert gengo_api.get_languages() == (u'es', )

            # Test that the new list is cached.
            assert gengo_utils.GENGO_LANGUAGE_CACHE == (response, (u'es', ))
Пример #5
0
 def test_guess_language_throws_error(self):
     gengo_api = gengo_utils.FjordGengo()
     self.assertRaises(
         gengo_utils.GengoUnknownLanguage,
         gengo_api.guess_language,
         u'Muy lento',
     )
Пример #6
0
    def test_machine_translate_unsupported_lc_src(self):
        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            gengo_api = gengo_utils.FjordGengo()
            self.assertRaises(gengo_utils.GengoUnsupportedLanguage,
                              gengo_api.machine_translate, 1010, 'zh-tw', 'en',
                              'whatevs')

            eq_(GengoMock.return_value.postTranslationJobs.mock_calls, [])
Пример #7
0
    def test_get_language_pairs(self):
        resp = {
            u'opstat':
            u'ok',
            u'response': [{
                u'tier': u'standard',
                u'lc_tgt': u'es-la',
                u'lc_src': u'en',
                u'unit_price': u'0.05',
                u'currency': u'USD'
            }, {
                u'tier': u'ultra',
                u'lc_tgt': u'ar',
                u'lc_src': u'en',
                u'unit_price': u'0.15',
                u'currency': u'USD'
            }, {
                u'tier': u'pro',
                u'lc_tgt': u'ar',
                u'lc_src': u'en',
                u'unit_price': u'0.10',
                u'currency': u'USD'
            }, {
                u'tier': u'ultra',
                u'lc_tgt': u'es',
                u'lc_src': u'en',
                u'unit_price': u'0.15',
                u'currency': u'USD'
            }, {
                u'tier': u'standard',
                u'lc_tgt': u'pl',
                u'lc_src': u'de',
                u'unit_price': u'0.05',
                u'currency': u'USD'
            }]
        }

        gengo_utils.GENGO_LANGUAGE_PAIRS_CACHE = None

        with patch('fjord.translations.gengo_utils.Gengo') as GengoMock:
            instance = GengoMock.return_value
            instance.getServiceLanguagePairs.return_value = resp

            # Make sure the cache is empty
            assert gengo_utils.GENGO_LANGUAGE_PAIRS_CACHE is None

            # Test that we generate a list based on what we think the
            # response is.
            gengo_api = gengo_utils.FjordGengo()
            assert (gengo_api.get_language_pairs() == [(u'en', u'es-la'),
                                                       (u'de', u'pl')])

            # Test that the new list is cached.
            assert (gengo_utils.GENGO_LANGUAGE_PAIRS_CACHE == [(u'en',
                                                                u'es-la'),
                                                               (u'de', u'pl')])
Пример #8
0
    def test_approved_jobs(self):
        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'Facebook can bind with peru',
                    u'body_src': u'Facebook no se puede enlazar con peru',
                    u'credits': u'0.35',
                    u'eta': -1,
                    u'custom_data': u'localhost||GengoJob||7',
                    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

            gengo_api = gengo_utils.FjordGengo()
            jobs = gengo_api.completed_jobs_for_order('263413')
            assert ([item['custom_data']
                     for item in jobs] == [u'localhost||GengoJob||7'])
Пример #9
0
    def test_machine_translation(self):
        with patch('fjord.translations.gengo_utils.requests') as requests_mock:
            post_return = MagicMock()
            post_return.json.return_value = {
                u'text_bytes_found': 40,
                u'opstat': u'ok',
                u'is_reliable': False,
                u'detected_lang_code': u'es',
                u'details': [
                    [u'SPANISH', u'es', 62, 46.728971962616825],
                    [u'ITALIAN', u'it', 38, 9.237875288683602]
                ],
                u'detected_lang_name': u'SPANISH'
            }
            requests_mock.post.return_value = post_return

            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'
                            }
                        }
                    }
                }

                gengo_api = gengo_utils.FjordGengo()
                text = u'Muy lento'
                eq_(gengo_api.get_machine_translation(1010, text),
                    u'Very slow')
Пример #10
0
    def test_guess_language_unintelligible_response(self):
        # If the gengo api returns some crazy non-json response, make sure
        # that guess_language should throw a GengoAPIFailure.
        gengo_api = gengo_utils.FjordGengo()

        with patch('fjord.translations.gengo_utils.requests') as req_patch:
            # Create a mock that we can call .post() on and it returns
            # a response that has a .json() method that throws a
            # ValueError which is what happens when it's not valid
            # JSON.
            post_return = MagicMock()
            post_return.text = 'abcd'
            post_return.status_code = 500
            post_return.json.side_effect = ValueError('bleh')
            req_patch.post.return_value = post_return

            with pytest.raises(gengo_utils.GengoAPIFailure):
                gengo_api.guess_language(u'whatever text')
Пример #11
0
    def test_guess_language_throws_error(self):
        with patch('fjord.translations.gengo_utils.requests') as requests_mock:
            post_return = MagicMock()
            post_return.json.return_value = {
                u'text_bytes_found': 10,
                u'opstat': u'ok',
                u'is_reliable': True,
                u'detected_lang_code': u'un',
                u'details': [],
                u'detected_lang_name': u'Unknown'
            }
            requests_mock.post.return_value = post_return

            gengo_api = gengo_utils.FjordGengo()
            self.assertRaises(
                gengo_utils.GengoUnknownLanguage,
                gengo_api.guess_language,
                u'Muy lento',
            )
Пример #12
0
    def test_guess_language_returns_language(self):
        with patch('fjord.translations.gengo_utils.requests') as requests_mock:
            post_return = MagicMock()
            post_return.json.return_value = {
                u'text_bytes_found': 40,
                u'opstat': u'ok',
                u'is_reliable': False,
                u'detected_lang_code': u'es',
                u'details': [
                    [u'SPANISH', u'es', 62, 46.728971962616825],
                    [u'ITALIAN', u'it', 38, 9.237875288683602]
                ],
                u'detected_lang_name': u'SPANISH'
            }
            requests_mock.post.return_value = post_return

            gengo_api = gengo_utils.FjordGengo()
            text = u'Facebook no se puede enlazar con peru'
            eq_(gengo_api.guess_language(text), u'es')
Пример #13
0
 def test_get_language(self):
     text = u'Facebook no se puede enlazar con peru'
     gengo_api = gengo_utils.FjordGengo()
     assert gengo_api.guess_language(text) == u'es'
Пример #14
0
 def test_get_balance(self):
     gengo_api = gengo_utils.FjordGengo()
     eq_(gengo_api.get_balance(), 20.0)
Пример #15
0
 def test_get_balance(self):
     gengo_api = gengo_utils.FjordGengo()
     assert gengo_api.get_balance() == 20.0
Пример #16
0
 def test_guess_language_returns_language(self):
     gengo_api = gengo_utils.FjordGengo()
     text = u'Facebook no se puede enlazar con peru'
     eq_(gengo_api.guess_language(text), u'es')
Пример #17
0
 def test_guess_language_throws_error(self):
     gengo_api = gengo_utils.FjordGengo()
     with pytest.raises(gengo_utils.GengoUnknownLanguage):
         gengo_api.guess_language(u'Muy lento')