Skip to content

Commit

Permalink
Test that the list view makes 1 or 2 DB calls based on addons
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Dec 19, 2024
1 parent e67edbc commit 51e1c3d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions api/test/unit/views/test_audio_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import MagicMock, patch

import pytest
import pytest_django.asserts

from test.factory.models import AudioFactory


@pytest.mark.parametrize("peaks, query_count", [(True, 2), (False, 1)])
@pytest.mark.django_db
def test_peaks_param_determines_addons(api_client, peaks, query_count):
num_results = 20

# Since controller returns a list of ``Hit``s, not model instances, we must
# set the ``meta`` param on each of them to match the shape of ``Hit``.
results = AudioFactory.create_batch(size=num_results)
for result in results:
result.meta = None

controller_ret = (
results,
1, # num_pages
num_results,
{}, # search_context
)
with (
patch(
"api.views.media_views.search_controller",
query_media=MagicMock(return_value=controller_ret),
),
patch(
"api.serializers.media_serializers.search_controller",
get_sources=MagicMock(return_value={}),
),
pytest_django.asserts.assertNumQueries(query_count),
):
res = api_client.get(f"/v1/audio/?peaks={peaks}")

assert res.status_code == 200

0 comments on commit 51e1c3d

Please sign in to comment.