"""Test Film Transparency cache utilities.""" from unittest.mock import Mock import pytest from api.utils import cache @cache.cache def _fixture_function_to_cache(upc): """Something cached.""" class Fresh(): message = 'something_fresh' return Fresh() @pytest.mark.parametrize('cached_value', [ ('something_cached'), (None) ]) def test_cache_decorator(monkeypatch, cached_value): """Testing cache decorator.""" upc = 123 key = 'upc:{}'.format(upc) # cached_value = 'something_cached' # film_transparency.caching.create_cache_key function create_cache_key_mock = Mock(return_value=key) monkeypatch.setattr( cache.caching, 'create_cache_key', create_cache_key_mock) # film_transparency.caching.get_hash function get_hash_mock = Mock(return_value=cached_value) monkeypatch.setattr( cache.caching, 'get_hash', get_hash_mock) # film_transparency.caching.set_hash function set_hash_mock = Mock() monkeypatch.setattr( cache.caching, 'set_hash', set_hash_mock) response = _fixture_function_to_cache(upc) create_cache_key_mock.assert_called_once_with(upc) get_hash_mock.assert_called_once_with(key) if cached_value: set_hash_mock.assert_not_called() assert response.message is cached_value else: set_hash_mock.assert_called_once_with(key, response.message) assert response.message is 'something_fresh'