-
Notifications
You must be signed in to change notification settings - Fork 216
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
Pull all addons in one DB request #5289
Changes from all commits
2880954
4ce3907
35b39e5
e67edbc
51e1c3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,28 +41,3 @@ def test_audio_waveform_caches(generate_peaks_mock, audio_fixture): | |
audio_fixture.delete() | ||
|
||
assert AudioAddOn.objects.count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch("api.models.audio.AudioAddOn.objects.get") | ||
def test_audio_waveform_sent_when_present(get_mock, audio_fixture): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be new tests for the modified functionality of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, I'll add some tests for that asap. |
||
# When ``AudioAddOn.waveform_peaks`` exists, waveform is filled | ||
peaks = [0, 0.25, 0.5, 0.25, 0.1] | ||
get_mock.return_value = mock.Mock(waveform_peaks=peaks) | ||
assert audio_fixture.get_waveform() == peaks | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch("api.models.audio.AudioAddOn.objects.get") | ||
def test_audio_waveform_blank_when_absent(get_mock, audio_fixture): | ||
# When ``AudioAddOn`` does not exist, waveform is blank | ||
get_mock.side_effect = AudioAddOn.DoesNotExist() | ||
assert audio_fixture.get_waveform() == [] | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch("api.models.audio.AudioAddOn.objects.get") | ||
def test_audio_waveform_blank_when_none(get_mock, audio_fixture): | ||
# When ``AudioAddOn.waveform_peaks`` is None, waveform is blank | ||
get_mock.return_value = mock.Mock(waveform_peaks=None) | ||
assert audio_fixture.get_waveform() == [] |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are addons described anywhere in the codebase docs? Do we only have waveforms as addons now, or is there something else, too?
I would like for this comment to include something like "addons, like waveforms for audio". A description of why something is an addon and not in the main model (if it doesn't exist anywhere now) , would be very useful. Not necessarily in this PR, though :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of the addon model is specific to the audio media type and that too only for the waveforms. The
AudioAddOn
model does not have any docstrings, so the conversation around the PR that added this model WordPress/openverse-api#510 is our only reference.The PR describes that the reason the waveforms are in an
AudioAddOn
model is because the data in any columns that are not in the upstream DB would be gone after a data refresh.