diff --git a/tests/profiles/test_mss.py b/tests/profiles/test_mss.py index 2a618c9..1ac2921 100644 --- a/tests/profiles/test_mss.py +++ b/tests/profiles/test_mss.py @@ -1,11 +1,10 @@ #!/usr/bin/env python3 # encoding: utf-8 -from unittest.mock import MagicMock, AsyncMock +from unittest.mock import AsyncMock, patch import json import pytest -from contextlib import asynccontextmanager from iiif.profiles import MSSProfile from iiif.profiles.mss import MSSImageInfo @@ -14,6 +13,7 @@ def test_mss_choose_file_no_derivatives(): doc = { + 'id': 23, 'file': 'original.tif', 'width': 1000, 'height': 2000, @@ -25,6 +25,7 @@ def test_mss_choose_file_no_derivatives(): def test_mss_choose_file_with_derivatives(): doc = { + 'id': 23, 'file': 'original.tif', 'width': 1000, 'height': 2000, @@ -57,33 +58,10 @@ def create_es_mss_doc(doc): return json.dumps(es_doc) -@asynccontextmanager -async def mock_mss_profile(config, assoc_media_count, mss_doc, aps_is_ok): - profile = MSSProfile('test', config, 'http://creativecommons.org/licenses/by/4.0/', [''], '', 1, - 1, ['collections']) - - count_doc = {'count': assoc_media_count} - es_post_mock_response = AsyncMock(text=AsyncMock(return_value=json.dumps(count_doc))) - es_post_mock = AsyncMock(return_value=es_post_mock_response) - - es_get_mock_response = AsyncMock(text=AsyncMock(return_value=create_es_mss_doc(mss_doc))) - es_get_mock = AsyncMock(return_value=es_get_mock_response) - - mss_get_mock = AsyncMock(return_value=MagicMock(ok=aps_is_ok)) - - original_sessions = (profile.es_session, profile.mss_session, profile.dm_session) - profile.es_session = MagicMock( - get=MagicMock(return_value=MagicMock(__aenter__=es_get_mock)), - post=MagicMock(return_value=MagicMock(__aenter__=es_post_mock)) - ) - profile.mss_session = MagicMock( - get=MagicMock(return_value=MagicMock(__aenter__=mss_get_mock)), - ) - - yield profile - - profile.es_session, profile.mss_session, profile.dm_session = original_sessions - await profile.close() +@pytest.fixture +def mss_profile(config): + return MSSProfile('test', config, 'http://creativecommons.org/licenses/by/4.0/', [''], '', 1, + 1, ['collections']) # TODO: write more and better mss profile tests @@ -91,59 +69,38 @@ async def mock_mss_profile(config, assoc_media_count, mss_doc, aps_is_ok): class TestMSSProfileGetInfo: @pytest.mark.asyncio - async def test_allowed(self, config): + async def test_allowed(self, mss_profile): mss_doc = { 'id': 1234, 'file': 'beans.tiff', 'width': 4000, 'height': 1600, } - async with mock_mss_profile(config, 1, mss_doc, True) as profile: - info = await profile.get_info('1234') + mock_get_mss_doc = AsyncMock(return_value=mss_doc) + with patch.object(mss_profile, 'get_mss_doc', mock_get_mss_doc): + info = await mss_profile.get_info('testname') assert info is not None - assert info.name == '1234' + assert info.name == 'testname' assert info.size == (4000, 1600) assert info.original == 'beans.tiff' @pytest.mark.asyncio - async def test_missing_collections_doc(self, config): - mss_doc = { - 'id': 1234, - 'file': 'beans.tiff', - 'width': 4000, - 'height': 1600, - } - async with mock_mss_profile(config, 0, mss_doc, True) as profile: - info = await profile.get_info('1234') - assert info is None - - @pytest.mark.asyncio - async def test_aps_denies(self, config): - mss_doc = { - 'id': 1234, - 'file': 'beans.tiff', - 'width': 4000, - 'height': 1600, - } - async with mock_mss_profile(config, 1, mss_doc, False) as profile: - info = await profile.get_info('1234') - assert info is None - - @pytest.mark.asyncio - async def test_missing_mss_doc(self, config): - async with mock_mss_profile(config, 1, None, True) as profile: - info = await profile.get_info('1234') + async def test_missing_collections_doc(self, mss_profile): + mock_get_mss_doc = AsyncMock(return_value=None) + with patch.object(mss_profile, 'get_mss_doc', mock_get_mss_doc): + info = await mss_profile.get_info('1234') assert info is None @pytest.mark.asyncio - async def test_missing_size(self, config): + async def test_missing_size(self, config, mss_profile): mss_doc = { 'id': 1234, 'file': 'beans.tiff', } - async with mock_mss_profile(config, 1, mss_doc, True) as profile: - source_path = create_image(config, 140, 504, 'mss', '1234') - profile.fetch_source = AsyncMock(return_value=source_path) - info = await profile.get_info('1234') + mock_get_mss_doc = AsyncMock(return_value=mss_doc) + with patch.object(mss_profile, 'get_mss_doc', mock_get_mss_doc): + source_path = create_image(config, 140, 504, 'mss', 'test') + mss_profile.fetch_source = AsyncMock(return_value=source_path) + info = await mss_profile.get_info('test') assert info is not None assert info.size == (140, 504)