Skip to content

Commit

Permalink
Add unit test for collection params
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Sep 4, 2023
1 parent 70cd828 commit 348a941
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions api/test/unit/views/test_media_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from unittest.mock import MagicMock, patch
from uuid import uuid4

from rest_framework.response import Response

import pytest
import pytest_django.asserts

Expand Down Expand Up @@ -42,6 +44,40 @@ def test_retrieve_query_count(api_client, media_type_config):
assert res.status_code == 200


@pytest.mark.django_db
@pytest.mark.parametrize(
"path, expected_collection_params",
[
pytest.param("tag/cat/", {"tag": "cat"}, id="tag"),
pytest.param("source/met/", {"source": "met"}, id="source"),
pytest.param(
"source/flickr/creator/cat/",
{"source": "flickr", "creator": "cat"},
id="source_creator",
),
],
)
def test_collection_parameters(
path, expected_collection_params, api_client, media_type_config
):
mock_get_media_results = MagicMock(return_value=Response())

with patch(
"api.views.media_views.MediaViewSet.get_media_results",
new_callable=lambda: mock_get_media_results,
) as mock_get_media_results:
api_client.get(f"/v1/{media_type_config.url_prefix}/{path}")

# Make sure the mock was called
assert mock_get_media_results.called

# Make sure the mock was called with the correct parameters
request_kind, actual_collection_params = mock_get_media_results.call_args[0][1:]

assert actual_collection_params == expected_collection_params
assert request_kind == "collection"


@pytest.mark.parametrize(
"filter_content", (True, False), ids=lambda x: "filtered" if x else "not_filtered"
)
Expand Down

0 comments on commit 348a941

Please sign in to comment.