Skip to content

Commit

Permalink
Merge pull request #52 from saritasa-nest/feature/improve-coverage
Browse files Browse the repository at this point in the history
Add tests for resources.py
  • Loading branch information
Eg0ra authored Oct 11, 2024
2 parents 4ed1cf6 + 489c9c5 commit 3c16af1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ repos:
language: system
pass_filenames: false
types: [file]
stages: [push]
stages: [pre-push]
- id: tests
name: run tests
entry: inv pytest.run --params="--cov=."
language: system
pass_filenames: false
types: [python]
stages: [push]
stages: [pre-push]
- id: mypy
name: mypy
entry: inv mypy.run
language: system
pass_filenames: false
types: [file]
stages: [push]
stages: [pre-push]
- id: package_installation_verify
name: verify package can be installed
entry: pip install --dry-run .
language: system
pass_filenames: false
types: [python]
stages: [push]
stages: [pre-push]
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():
"""Ensure that CeleryResource overrides error class."""
error_class = SimpleArtistResource().get_error_result_class()
assert error_class is results.Error

0 comments on commit 3c16af1

Please sign in to comment.