def test_get_from_cache_not_translated(self, redis_mock, monkeypatch):
     redis_mock.hmget = MagicMock(return_value=[None])
     monkeypatch.setattr(redis.Redis, "hmget", redis_mock.hmget)
     cache = redis_cache.RedisCache()
     cached_translations, not_translated_texts = cache.get_from_cache(
         ["text"], "", "ru")
     redis_mock.hmget.assert_called_with('auto:ru', ['text'])
     assert cached_translations == {}
     assert not_translated_texts == ['text']
    def test_save_to_cache_with_source(self, redis_mock, redis_mock_hmset):
        cache = redis_cache.RedisCache()

        translation = {"translatedText": "text Translated", "input": "text"}

        cache.save_to_cache([translation], "en", "ru")

        redis_mock.hmset.assert_called_with('en:ru',
                                            {'text': 'text Translated'})
 def test_get_from_cache_translated_with_source(self, redis_mock,
                                                monkeypatch):
     redis_mock.hmget = MagicMock(return_value=['text Translated'])
     monkeypatch.setattr(redis.Redis, "hmget", redis_mock.hmget)
     cache = redis_cache.RedisCache()
     cached_translations, not_translated_texts = cache.get_from_cache(
         ["text"], "en", "ru")
     redis_mock.hmget.assert_called_with('en:ru', ['text'])
     assert cached_translations["text"][0] == "text Translated"
     assert cached_translations["text"][1] == "en"
     assert not_translated_texts == []
 def test_get_from_cache_mixed(self, redis_mock, monkeypatch):
     redis_mock.hmget = MagicMock(return_value=[
         None,
         '{"translatedText": "text Translated", "detectedSourceLanguage": "en"}'
     ])
     monkeypatch.setattr(redis.Redis, "hmget", redis_mock.hmget)
     cache = redis_cache.RedisCache()
     cached_translations, not_translated_texts = cache.get_from_cache(
         ["text", "text translated"], "", "ru")
     redis_mock.hmget.assert_called_with('auto:ru',
                                         ['text', 'text translated'])
     assert cached_translations["text translated"][0] == "text Translated"
     assert cached_translations["text translated"][1] == "en"
     assert not_translated_texts == ['text']
예제 #5
0
def initialize_cache():
    """Initialize cache based on environment variable."""
    local_cache = 'RUN_LOCALLY'
    redis_cache = 'REDISHOST'

    if os.environ.get(local_cache) is not None:
        import fake_cache
        cache = fake_cache.FakeCache()
    elif os.environ.get(redis_cache) is not None:
        import redis_cache
        cache = redis_cache.RedisCache()
    else:
        import datastore_cache
        cache = datastore_cache.DatastoreCache()

    return cache
    def test_save_to_cache_without_source(self, redis_mock, redis_mock_hmset):
        cache = redis_cache.RedisCache()

        translation = {
            "translatedText": "text Translated",
            "detectedSourceLanguage": "en",
            "input": "text"
        }

        cache.save_to_cache([translation], "", "ru")

        redis_mock.hmset.assert_called_with(
            'auto:ru', {
                'text':
                '{"translatedText": "text Translated", "detectedSourceLanguage": "en"}'
            })