|
| 1 | +# Copyright 2016 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import os |
| 17 | + |
| 18 | +import unittest2 |
| 19 | + |
| 20 | +from gcloud import translate |
| 21 | +from gcloud.translate.client import ENGLISH_ISO_639 |
| 22 | + |
| 23 | + |
| 24 | +ENV_VAR = 'GCLOUD_TESTS_API_KEY' |
| 25 | + |
| 26 | + |
| 27 | +class Config(object): |
| 28 | + """Run-time configuration to be modified at set-up. |
| 29 | +
|
| 30 | + This is a mutable stand-in to allow test set-up to modify |
| 31 | + global state. |
| 32 | + """ |
| 33 | + CLIENT = None |
| 34 | + |
| 35 | + |
| 36 | +def setUpModule(): |
| 37 | + key = os.getenv(ENV_VAR) |
| 38 | + Config.CLIENT = translate.Client(key=key) |
| 39 | + |
| 40 | + |
| 41 | +class TestTranslate(unittest2.TestCase): |
| 42 | + |
| 43 | + def test_get_languages(self): |
| 44 | + result = Config.CLIENT.get_languages(ENGLISH_ISO_639) |
| 45 | + # There are **many** more than 10 languages. |
| 46 | + self.assertGreater(len(result), 10) |
| 47 | + |
| 48 | + lang_map = {val['language']: val['name'] for val in result} |
| 49 | + self.assertEqual(lang_map['en'], 'English') |
| 50 | + self.assertEqual(lang_map['ja'], 'Japanese') |
| 51 | + self.assertEqual(lang_map['lv'], 'Latvian') |
| 52 | + self.assertEqual(lang_map['zu'], 'Zulu') |
| 53 | + |
| 54 | + def test_detect_language(self): |
| 55 | + values = ['takoy', u'fa\xe7ade', 's\'il vous plait'] |
| 56 | + detections = Config.CLIENT.detect_language(*values) |
| 57 | + self.assertEqual(len(values), len(detections)) |
| 58 | + self.assertEqual(detections[0]['language'], 'ru') |
| 59 | + self.assertEqual(detections[1]['language'], 'fr') |
| 60 | + self.assertEqual(detections[2]['language'], 'fr') |
| 61 | + |
| 62 | + def test_translate(self): |
| 63 | + values = ['hvala ti', 'dankon', |
| 64 | + 'Me llamo Jeff', 'My name is Jeff'] |
| 65 | + translations = Config.CLIENT.translate(*values, |
| 66 | + target_language='de') |
| 67 | + self.assertEqual(len(values), len(translations)) |
| 68 | + |
| 69 | + self.assertEqual( |
| 70 | + translations[0]['detectedSourceLanguage'], 'hr') |
| 71 | + self.assertEqual( |
| 72 | + translations[0]['translatedText'], 'danke') |
| 73 | + |
| 74 | + self.assertEqual( |
| 75 | + translations[1]['detectedSourceLanguage'], 'eo') |
| 76 | + self.assertEqual( |
| 77 | + translations[1]['translatedText'], 'dank') |
| 78 | + |
| 79 | + self.assertEqual( |
| 80 | + translations[2]['detectedSourceLanguage'], 'es') |
| 81 | + self.assertEqual( |
| 82 | + translations[2]['translatedText'], 'Mein Name ist Jeff') |
| 83 | + |
| 84 | + self.assertEqual( |
| 85 | + translations[3]['detectedSourceLanguage'], 'en') |
| 86 | + self.assertEqual( |
| 87 | + translations[3]['translatedText'], 'Mein Name ist Jeff') |
0 commit comments