def test_translation_priority(self): """Verify that the priority option in GPClient.translation works""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages=['fr'] # prioritize local t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'greet in French (local)', value, 'incorrect value; should have returned local translation') # prioritize gp t = client.translation(bundleId=common.bundleId1, languages=languages, priority='gp', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'Bonjour', value, 'incorrect value; should have returned gp translation')
def common_test_caching(self, cacheTimeout=None): """Shared code between the various caching tests """ acc = common.get_gpserviceaccount() if cacheTimeout is None: client = GPClient(acc) else: client = GPClient(acc, cacheTimeout=cacheTimeout) languages=['fr_CA'] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext # key and translated value used for testing below # makes it easier to change key = 'greet' tValue = 'Salut' # translated value # check that the translated value is correct value = _(key) common.my_assert_equal(self, tValue, value, 'incorrect translated value') return (t, _, key, tValue)
def demo(): ## 1 - create GPServiceAccount acc = None try: acc = GPServiceAccount() except AssertionError: logging.error('Unable to create GPServiceAccount') return ## 2 - create Globalization Pipeline client client = GPClient(acc) # bundle containing the source strings bundleId='demo' languages = client.get_avaliable_languages(bundleId=bundleId) messages = [] for language in languages: ## 3 - create translation instance # Note: because we are displaying the translations for all avalible # languages, a new translation instance is create each time; # normally, only one instance for the language of choice is required t = client.translation(bundleId=bundleId, languages=[language]) _ = t.gettext # get the translated value from Globalization Pipeline service welcomeValue = _('welcome') messages.append({'language': language, 'welcome': welcomeValue}) return render_template('index.html', messages=messages)
def __get_reader_credentials(): acc = GPServiceAccount(url=url, instanceId=instanceId, userId=adminUserId, password=adminPassword) client = GPClient(acc) return client.createReaderUser(['*'])
def common_test_caching(self, cacheTimeout=None): """Shared code between the various caching tests """ acc = common.get_gpserviceaccount() if cacheTimeout is None: client = GPClient(acc) else: client = GPClient(acc, cacheTimeout=cacheTimeout) languages=['fr_CA'] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext # key and translated value used for testing below # makes it easier to change key = 'greet' tValue = 'Bonjour' # translated value # check that the translated value is correct value = _(key) common.my_assert_equal(self, tValue, value, 'incorrect translated value') return (t, _, key, tValue)
def test_get_gaas_hmac_headers(self): """Test if the GaaS HMAC header generation is correct """ method = 'POST' url = 'https://example.com/gaas' date = 'Mon, 30 Jun 2014 00:00:00 GMT' body = '{"param":"value"}' userId = 'MyUser' secret = 'MySecret' expectedHeaders = { 'GP-Date': 'Mon, 30 Jun 2014 00:00:00 GMT', 'Authorization': 'GP-HMAC MyUser:ONBJapYEveDZfsPFdqZHQ64GDgc=' } acc = common.get_gpserviceaccount() client = GPClient(acc) headers = client._GPClient__get_gaas_hmac_headers(method=method, url=url, date=date, body=body, secret=secret, userId=userId) common.my_assert_equal(self, expectedHeaders, headers, 'incorrect GaaS HMAC headers')
def test_gp_fallback(self): """Test the fallback feature, i.e. when a translated value is not found for a language, the fallback language should be used - if there is no fallback language then the source value should be returned. If the key is not found, the key should be returned. """ acc = common.get_gpserviceaccount() client = GPClient(acc) # should fallbcak to 'fr', 'ur' is not supported languages = ['ur', 'fr'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('exit') common.my_assert_equal( self, u'Au revoir', value, 'incorrect translated value - should have used fr fallback') # should return key back, key doesn't exist languages = ['es-mx'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext key = 'badKey' value = _(key) common.my_assert_equal( self, key, value, 'incorrect translated value - key doesn\'t exist')
def test_gp_fallback(self): """Test the fallback feature, i.e. when a translated value is not found for a language, the fallback language should be used - if there is no fallback language then the source value should be returned. If the key is not found, the key should be returned. """ acc = common.get_gpserviceaccount() client = GPClient(acc) # should fallbcak to 'es-mx', 'ur' is not supported languages=['ur', 'es-mx', 'fr'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('exit') common.my_assert_equal(self, u'Adiós', value, 'incorrect translated value - should have used es-mx fallback') # should return key back, key doesn't exist languages=['es-mx'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext key = 'badKey' value = _(key) common.my_assert_equal(self, key, value, 'incorrect translated value - key doesn\'t exist')
def setUpClass(self): """Setting up the globalization pipeline for testing""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc) try: client.delete_bundle(common.bundleId1) data = {} data['sourceLanguage'] = "en" #data['targetLanguages'] = ["fr","es-mx"] data['notes'] = ["string"] data['metadata'] = {} data['partner'] = '' data['segmentSeparatorPattern'] = 'string' data['noTranslationPattern'] = 'string' client.create_bundle(common.bundleId1, data=data) bundle1_entries = {} bundle1_entries['greet'] = "Hello" bundle1_entries['weather'] = "It is snowing" client.upload_resource_entries(common.bundleId1, "en", data=bundle1_entries) bundle2_entries = {} bundle2_entries['greet'] = "Salut" bundle2_entries['weather'] = "Il neige" client.upload_resource_entries(common.bundleId1, "fr", data=bundle2_entries) except: pass
def test_admin_basic_auth(self): """Verify basic auth fails with admin account""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc, auth=GPClient.BASIC_AUTH) ids = client.get_bundles() self.assertEqual(0, len(ids), "Admin account can not use basic authentication")
def test_create_bundle(self): """Test to create a new bundle""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc) tresp = client.create_bundle("test-bundle") common.my_assert_equal(self, "SUCCESS", tresp["status"], 'bundle could not be created')
def test_delete_bundle_success(self): """Test to delete a specific bundle which exists""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc) tresp = client.delete_bundle("test-bundle") common.my_assert_equal(self, "SUCCESS", tresp["status"], 'bundle could not be deleted')
def test_reader__get_bundles(self): """Verify bundles can not be obtained with reader acc""" acc = common.get_gpserviceaccount() client = GPClient(acc) expectedBundles = None actualBundles = client._GPClient__get_bundles_data() common.my_assert_equal(self, expectedBundles, actualBundles, 'reader acc can not get bundles list')
def test_delete_bundle_fail(self): """Test to delete a specific bundle which doesn't exist""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc) tresp = client.delete_bundle("test-bundle-notexists") common.my_assert_equal( self, "ERROR", tresp["status"], 'a bundle which does not exist can not be deleted')
def test_reader_get_bundles(self): """Verify bundles can not be obtained with reader acc""" acc = common.get_gpserviceaccount() client = GPClient(acc) expectedBundles = [] actualBundles = client.get_bundles() common.my_assert_equal(self, expectedBundles, actualBundles, 'reader acc can not get bundles list')
def test_upload_resource_entries(self): """Test to upload resource entries""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc) data = {} data["welcome"] = "Hello" tresp = client.upload_resource_entries(common.bundleId1, "en", data=data) common.my_assert_equal( self, "SUCCESS", tresp["status"], 'bundle resource entries could not be uploaded')
def test_update_resource_entries(self): """Test to update resource entries""" acc = common.get_admin_gpserviceaccount(True) client = GPClient(acc) data = {} data["welcome"] = "Welcome" tresp = client.update_resource_entries(common.bundleId1, "en", data=data) common.my_assert_equal( self, "SUCCESS", tresp["status"], 'bundle resource entries for the language could not be updated')
def test_update_resource_entry(self): """Test to update a resource entry""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc) data = {} data['value'] = "weather in spanish" tresp = client.update_resource_entry(common.bundleId1, "es-mx", "weather", data=data) common.my_assert_equal( self, "SUCCESS", tresp["status"], 'bundle resource entry for the language could not be updated')
def test_basic_auth_translation(self): """Test if translation works with basic auth""" acc = common.get_gpserviceaccount() client = GPClient(acc, auth=GPClient.BASIC_AUTH) languages = ['fr'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('show') common.my_assert_equal(self, u'Le Fil', value, 'incorrect translated value')
def test_english_values(self): """Verify English values are returned when asked for""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages = ['en'] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'Hello', value, 'incorrect value')
def test_example_2(self): """Test example 2 used in the docs""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages=['es-mx'] # languages=[locale.getdefaultlocale()[0]] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('exit') # 'exit' key will be localized/translated to Spanish common.my_assert_equal(self, u'Adiós', value, 'incorrect translated value')
def test_basic_auth_translation(self): """Test if translation works with basic auth""" acc = common.get_gpserviceaccount() client = GPClient(acc, auth=GPClient.BASIC_AUTH) languages=['es-mx'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('show') common.my_assert_equal(self, u'El físico', value, 'incorrect translated value')
def test_example_2(self): """Test example 2 used in the docs""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages = ['fr'] # languages=[locale.getdefaultlocale()[0]] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('exit') # 'exit' key will be localized/translated to French common.my_assert_equal(self, u'Au revoir', value, 'incorrect translated value')
def test_english_values(self): """Verify English values are returned when asked for""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages=['en'] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'Hello', value, 'incorrect value')
def test_admin_basic_auth(self): """Verify basic auth fails with admin account""" acc = common.get_admin_gpserviceaccount() client = GPClient(acc, auth=GPClient.BASIC_AUTH) languages=['es-mx'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('show') common.my_assert_equal(self, 'show', value, 'admin acc can not use basic auth')
def test_local_fallback(self): """Verify local translations are used with expected""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages=['fo', 'fr'] t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal(self, 'greet in French (local)', value, 'incorrect value; should have returned local translation')
def test_example_1(self): """Test example 1 used in the docs""" common.set_vcap_env_vars() acc = GPServiceAccount() client = GPClient(acc) languages=['fr_CA'] # languages=[locale.getdefaultlocale()[0]] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext value = _('greet') # 'greet' key will be localized/translated to French common.my_assert_equal(self, 'Bonjour', value, 'incorrect translated value')
def test_example_1(self): """Test example 1 used in the docs""" #common.set_vcap_env_vars() acc = common.get_gpserviceaccount() client = GPClient(acc) languages = ['fr'] # languages=[locale.getdefaultlocale()[0]] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext value = _( 'greet') # 'greet' key will be localized/translated to French common.my_assert_equal(self, 'Salut', value, 'incorrect translated value')
def root(): logging.info('Processing request') # 1 - create GPServiceAccount # if the app is running on Bluemix, the credentials will be obtained # from the VCAP environment variable acc = None try: acc = GPServiceAccount() except AssertionError: logging.error('Unable to create GPServiceAccount', exc_info=True) return # 2 - create Globalization Pipeline client # the client is responsible for communication with the service client = GPClient(acc) logging.info('GP Client setup complete') bundleId = 'demo' # for the demo, get all avalible languages in the bundle # normally, this should equal to the language of the locale languages = client.get_avaliable_languages(bundleId=bundleId) messages = [] for language in languages: # 3 - create translation instance # Note: because we are displaying the translations for all avalible # languages, a new translation instance is create each time; # normally, only one instance for the language of choice is required t = client.translation(bundleId=bundleId, languages=[language]) # 4 - create a shortcut for the function _ = t.gettext # 5 - get the translated value from Globalization Pipeline service # by calling the shortcut translatedValue = _('welcome') messages.append({'language': language, 'welcome': translatedValue}) return render_template('index.html', messages=messages)
def test_local_translations(self): """Verify local translations are used with expected""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages = ['fr'] t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal( self, 'greet in French (local)', value, 'incorrect value; should have returned local translation')
def test_get_gaas_hmac_headers(self): """Test if the GaaS HMAC header generation is correct """ method = 'POST' url = 'https://example.com/gaas' date = 'Mon, 30 Jun 2014 00:00:00 GMT' body = '{"param":"value"}' userId = 'MyUser' secret = 'MySecret' expectedHeaders = {'Date': 'Mon, 30 Jun 2014 00:00:00 GMT', 'Authorization': 'GaaS-HMAC MyUser:ONBJapYEveDZfsPFdqZHQ64GDgc='} acc = common.get_gpserviceaccount() client = GPClient(acc) headers = client._GPClient__get_gaas_hmac_headers( method=method, url=url, date=date, body=body, secret=secret, userId=userId) common.my_assert_equal(self, expectedHeaders, headers, 'incorrect GaaS HMAC headers')
def root(): # 1 - create GPServiceAccount # if the app is running on Bluemix, the credentials will be obtained # from the VCAP environment variable acc = None try: acc = GPServiceAccount() except AssertionError: logging.error('Unable to create GPServiceAccount') return # 2 - create Globalization Pipeline client # the client is responsible for communication with the service client = GPClient(acc) bundleId='demo' # for the demo, get all avalible languages in the bundle # normally, this should equal to the language of the locale languages = client.get_avaliable_languages(bundleId=bundleId) messages = [] for language in languages: # 3 - create translation instance # Note: because we are displaying the translations for all avalible # languages, a new translation instance is create each time; # normally, only one instance for the language of choice is required t = client.translation(bundleId=bundleId, languages=[language]) # 4 - create a shortcut for the function _ = t.gettext # 5 - get the translated value from Globalization Pipeline service # by calling the shortcut translatedValue = _('welcome') messages.append({'language': language, 'welcome': translatedValue}) return render_template('index.html', messages=messages)
def test_get_language_match(self): """Test the matching of langauge codes to supported langauges""" # supported languages in GP supportedLangs = [ 'en', 'de', 'es', 'fr', 'it', 'ja', 'ko', 'pt-BR', 'zh-Hans', 'zh-Hant' ] acc = common.get_gpserviceaccount() client = GPClient(acc) get_language_match = client._GPClient__get_language_match expectedMatches = { 'en': 'en', 'en_US': 'en', 'en-US': 'en', 'de': 'de', 'de_at': 'de', 'de-at': 'de', 'es': 'es', 'es_mx': 'es', 'es-mx': 'es', 'fr': 'fr', 'fr_FR': 'fr', 'fr-Fr': 'fr', 'fr_CA': 'fr', 'it': 'it', 'it_ch': 'it', 'it-ch': 'it', 'it-IT': 'it', 'ja': 'ja', 'ja_JA': 'ja', 'ja-JA': 'ja', 'ko': 'ko', 'ko_KO': 'ko', 'ko-KO': 'ko', 'pt-BR': 'pt-BR', 'pt': None, 'zh': 'zh-Hans', 'zh-tw': 'zh-Hant', 'zh-cn': 'zh-Hans', 'zh-hk': 'zh-Hant', 'zh-sg': 'zh-Hans', } for langCode in expectedMatches: match = get_language_match(langCode, supportedLangs) expectedMatch = expectedMatches[langCode] common.my_assert_equal( self, expectedMatch, match, 'incorrect langauge match (Input= %s)' % (langCode, ))
def test_translation_priority(self): """Verify that the priority option in GPClient.translation works""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages = ['fr'] # prioritize local t = client.translation(bundleId=common.bundleId1, languages=languages, priority='local', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal( self, 'greet in French (local)', value, 'incorrect value; should have returned local translation') # prioritize gp t = client.translation(bundleId=common.bundleId1, languages=languages, priority='gp', domain='messages', localedir='test/data/translations', class_=None, codeset=None) _ = t.gettext value = _('greet') common.my_assert_equal( self, 'Salut', value, 'incorrect value; should have returned gp translation')