Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Dec 12, 2022
1 parent 610ec6b commit 0105e8c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
5 changes: 1 addition & 4 deletions api/catalog/api/examples/audio_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@
"results": [
base_audio
| {
"fields_matched": ["title"],
"audio_set": None,
"attribution": None,
"filesize": None,
"fields_matched": [],
},
],
},
Expand Down
21 changes: 10 additions & 11 deletions api/catalog/api/examples/image_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"provider": "stocksnap",
"source": "stocksnap",
"category": "photograph",
"filesize": None,
"filetype": None,
"filesize": 896128,
"filetype": "jpg",
"tags": [
{"name": "tree"},
{"name": "bark"},
Expand All @@ -43,11 +43,15 @@
{"name": "closeup"},
{"name": "root"},
],
"attribution": None,
"attribution": (
'"Tree Bark Photo" by Tim Sullivan is marked with '
"CC0 1.0. To view the terms, visit "
"https://creativecommons.org/publicdomain/zero/1.0/."
),
"fields_matched": [],
"mature": False,
"height": None,
"width": None,
"height": 4016,
"width": 6016,
"thumbnail": f"{origin}/v1/images/{identifier}/thumb/",
"detail_url": f"{origin}/v1/images/{identifier}/",
"related_url": f"{origin}/v1/images/{identifier}/related/",
Expand All @@ -70,12 +74,7 @@
"results": [
base_image
| {
"fields_matched": ["title"],
"height": None,
"width": None,
"attribution": None,
"filesize": None,
"filetype": None,
"fields_matched": [],
}
],
},
Expand Down
3 changes: 2 additions & 1 deletion api/test/media_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ def report(media_type, fixture):
test_id = fixture["results"][0]["id"]
response = requests.post(
f"{API_URL}/v1/{media_type}/{test_id}/report/",
{
json={
"reason": "mature",
"description": "This item contains sensitive content",
},
verify=False,
)
print(response.json())
assert response.status_code == 201
data = json.loads(response.text)
assert data["identifier"] == test_id
Expand Down
30 changes: 13 additions & 17 deletions api/test/unit/models/media_report_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Literal, Union
from unittest.mock import MagicMock, patch

from django.core.exceptions import ValidationError
from django.core.exceptions import ObjectDoesNotExist

import pytest

Expand Down Expand Up @@ -53,9 +53,9 @@ def _get_media_obj(type: MediaType):
)
@reason_params
def test_cannot_report_invalid_identifier(media_type, report_class, reason):
with pytest.raises(ValidationError):
with pytest.raises(ObjectDoesNotExist):
report_class.objects.create(
identifier=uuid.uuid4(),
media_obj_id=uuid.uuid4(),
reason=reason,
)

Expand All @@ -72,14 +72,11 @@ def test_pending_reports_have_no_subreport_models(
media_type: MediaType, report_class, mature_class, deleted_class, reason, media_obj
):
media = media_obj(media_type)
report = report_class.objects.create(
identifier=media.identifier,
reason=reason,
)
report = report_class.objects.create(media_obj=media, reason=reason)

assert report.status == PENDING
assert not mature_class.objects.filter(identifier=media.identifier).exists()
assert not deleted_class.objects.filter(identifier=media.identifier).exists()
assert not mature_class.objects.filter(media_obj=media).exists()
assert not deleted_class.objects.filter(media_obj=media).exists()


@pytest.mark.parametrize(
Expand All @@ -93,10 +90,10 @@ def test_mature_filtering_creates_mature_image_instance(
mock_es = MagicMock()
with patch("django.conf.settings.ES", mock_es):
report_class.objects.create(
identifier=media.identifier, reason=MATURE, status=MATURE_FILTERED
media_obj=media, reason=MATURE, status=MATURE_FILTERED
)

assert mature_class.objects.filter(identifier=media.identifier).exists()
assert mature_class.objects.filter(media_obj=media).exists()
assert mock_es.update.called_with(
index=media_type, id=media.id, body={"doc": {"mature": True}}
)
Expand All @@ -115,13 +112,14 @@ def test_deleting_mature_image_instance_resets_mature_flag(
with patch("django.conf.settings.ES", mock_es):
# Mark as mature.
report_class.objects.create(
identifier=media.identifier, reason=MATURE, status=MATURE_FILTERED
media_obj=media, reason=MATURE, status=MATURE_FILTERED
)
# Delete ``MatureImage`` instance.
mature_class.objects.get(identifier=media.identifier).delete()
mature_class.objects.get(media_obj=media).delete()

assert mock_es.update.call_count == 2
assert mock_es.update.called_with(id=media.id, body={"doc": {"mature": False}})
media.refresh_from_db()
assert not media.mature


Expand All @@ -142,10 +140,8 @@ def test_deindexing_creates_deleted_image_instance(

mock_es = MagicMock()
with patch("django.conf.settings.ES", mock_es):
report_class.objects.create(
identifier=identifier, reason=DMCA, status=DEINDEXED
)
report_class.objects.create(media_obj=media, reason=DMCA, status=DEINDEXED)

assert deleted_class.objects.filter(identifier=identifier).exists()
assert deleted_class.objects.filter(media_obj=media).exists()
assert not media_class.objects.filter(identifier=identifier).exists()
assert mock_es.delete.called_with(id=image_id)

0 comments on commit 0105e8c

Please sign in to comment.