Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock fetch test for fetch_voc function #2

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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