Skip to content

Commit

Permalink
Merge pull request #2 from Samk13/add-actions
Browse files Browse the repository at this point in the history
mock fetch test for fetch_voc function
  • Loading branch information
Samk13 committed Feb 8, 2024
2 parents 93d41ed + f1b0865 commit 1b30622
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions tests/test_fetch_voc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1b30622

Please sign in to comment.