From f1b08650237de7a0771f46ed4410b42daac0ac9d Mon Sep 17 00:00:00 2001 From: Sam Arbid Date: Thu, 8 Feb 2024 15:29:59 +0100 Subject: [PATCH] mock fetch test for fetch_voc function --- tests/test_fetch_voc.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/test_fetch_voc.py b/tests/test_fetch_voc.py index 4fad06c..364b54c 100644 --- a/tests/test_fetch_voc.py +++ b/tests/test_fetch_voc.py @@ -5,12 +5,34 @@ # invenio-subjects-CESSDA is free software, you can redistribute it and/or # modify it under the terms of the MIT License; see LICENSE file details. +from unittest.mock import MagicMock, patch + +import pytest + from invenio_subjects_cessda.fetch_voc import fetch_voc from tests.data import urls_to_fetch +# Sample data to mock response from the API +mock_response_data = [ + {"name": "sample_voc1", "data": {"key": "value"}}, + {"name": "sample_voc2", "data": {"key": "value"}}, +] + + +@pytest.mark.asyncio +async def test_fetch_voc(): + """Test fetch_voc function.""" + + async def mock_json(): + return {"key": "value"} + + async def mock_get(): + mock_resp = MagicMock() + mock_resp.json = mock_json + return mock_resp -# @pytest.mark.asyncio -def test_fetch_voc(): - """Test fetch_voc.""" - fetch_res = fetch_voc(urls_to_fetch) - assert len(fetch_res) == len(urls_to_fetch) + with patch("aiohttp.ClientSession.get", new=mock_get): + fetch_res = await fetch_voc(urls_to_fetch) + assert len(fetch_res) == len(urls_to_fetch) + for item in fetch_res: + assert item in mock_response_data