Skip to content

Commit

Permalink
Add test for resources.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Eg0ra committed Oct 10, 2024
1 parent 044648a commit cd3ce50
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 0 additions & 2 deletions import_export_extensions/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ def update_task_state(
return

async_result = result.AsyncResult(current_task.request.get("id"))
if not async_result.result:
return

self._update_current_task_state(
state=state,
Expand Down
45 changes: 45 additions & 0 deletions test_project/tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from rest_framework.exceptions import ValidationError

import pytest

from import_export_extensions import results
from test_project.fake_app.factories import ArtistFactory
from test_project.fake_app.models import Artist

from ..fake_app.resources import SimpleArtistResource


def test_resource_get_queryset(existing_artist: Artist):
"""Check that `get_queryset` contains existing artist."""
assert existing_artist in SimpleArtistResource().get_queryset()


def test_resource_with_filter_kwargs(existing_artist: Artist):
"""Check that `get_queryset` with filter kwargs exclude existing artist."""
expected_artist_name = "Expected Artist"
expected_artist = ArtistFactory(name=expected_artist_name)
resource_queryset = SimpleArtistResource(
filter_kwargs={"name": expected_artist_name},
).get_queryset()

assert existing_artist not in resource_queryset
assert expected_artist in resource_queryset


def test_resource_with_invalid_filter_kwargs():
"""Check that `get_queryset` raise error if filter kwargs is invalid."""
with pytest.raises(
ValidationError,
match=(
r"{'id':.*ErrorDetail.*string='Enter a number.', code='invalid'.*"
),
):
SimpleArtistResource(
filter_kwargs={"id": "invalid_id"},
).get_queryset()


def test_resource_get_error_class():
"""Check that returns correct error class."""
error_class = SimpleArtistResource().get_error_result_class()
assert error_class is results.Error

0 comments on commit cd3ce50

Please sign in to comment.