Skip to content

Commit

Permalink
Merge pull request #50 from saritasa-nest/feature/improve-coverage
Browse files Browse the repository at this point in the history
Improve coverage for utils.py and result.py
  • Loading branch information
Eg0ra authored Oct 9, 2024
2 parents fa6335e + 8f37e23 commit 4ed1cf6
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 21 deletions.
2 changes: 1 addition & 1 deletion import_export_extensions/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __repr__(self) -> str:
def __reduce__(self):
"""Simplify Exception object for pickling.
`error` object may contain not pickable objects (for example, django's
`error` object may contain not picklable objects (for example, django's
lazy text), so here it replaced with simple string.
"""
Expand Down
4 changes: 2 additions & 2 deletions import_export_extensions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_mime_type_by_file_url(file_url: str) -> str:
for binary files
"""
file_extension = f".{get_file_extension(file_url)}"
file_extension = get_file_extension(file_url)
if file_extension in settings.MIME_TYPES_MAP:
return settings.MIME_TYPES_MAP[file_extension]

Expand Down Expand Up @@ -68,7 +68,7 @@ def get_file_extension(url: str, lower: bool = True) -> str:
String: extension of the file (lower or not)
Example:
'dir/subdir/file.ext' -> 'ext'
'dir/subdir/file.ext' -> '.ext'
"""
get_index = url.find("?")
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,11 @@ source = [
"import_export_extensions",
]
omit = [
"import_export_extensions/migrations",
"*/__init__.py",
"**/invocations/**",
"**/migrations/**",
"**/__init__.py",
"test_project/**",
"tasks.py",
]

[tool.coverage.report]
Expand Down
2 changes: 1 addition & 1 deletion test_project/fake_app/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def data_file(self):
class ArtistExportJobFactory(factory.django.DjangoModelFactory):
"""Factory for creating ExportJob for Artist."""

resource_path = "test_project.fake_app." "resources.SimpleArtistResource"
resource_path = "test_project.fake_app.resources.SimpleArtistResource"
resource_kwargs: dict[str, str] = {}
file_format_path = "import_export.formats.base_formats.CSV"

Expand Down
15 changes: 0 additions & 15 deletions test_project/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,6 @@ def force_import_artist_job(new_artist: Artist) -> Artist:
)


@pytest.fixture
def invalid_uploaded_file(new_artist: Artist) -> SimpleUploadedFile:
"""Generate invalid `Artist` imort file."""
import_job = ArtistImportJobFactory.build(
artists=[new_artist],
force_import=True,
is_valid_file=False,
)
return SimpleUploadedFile(
"test_file.csv",
content=import_job.data_file.file.read().encode(),
content_type="text/plain",
)


@pytest.fixture
def superuser():
"""Return superuser instance."""
Expand Down
42 changes: 42 additions & 0 deletions test_project/tests/test_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pickle

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

import pytest

from import_export_extensions import results


@pytest.fixture
def row_result_with_skipped_errors() -> results.RowResult:
"""Create RowResult with skipped errors."""
row_result = results.RowResult()
row_result.non_field_skipped_errors = [results.Error("Error")]
row_result.field_skipped_errors = {
"title": [ValidationError("Error")],
}
return row_result


def test_reduce_error():
"""Test simplify exception object for pickling."""
assert pickle.dumps(results.Error(_))


def test_result_skipped_properties(
row_result_with_skipped_errors: results.RowResult,
):
"""Check that result properties calculate value correct."""
result = results.Result()
result.rows = [row_result_with_skipped_errors]
assert result.has_skipped_rows
assert len(result.skipped_rows) == 1


def test_row_result_properties(
row_result_with_skipped_errors: results.RowResult,
):
"""Check that row result properties calculate value correct."""
assert row_result_with_skipped_errors.has_skipped_errors
assert row_result_with_skipped_errors.skipped_errors_count == 2
79 changes: 79 additions & 0 deletions test_project/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import django.test
from django.db.models import Q

import pytest

from import_export_extensions import utils

AWS_STORAGE_BUCKET_NAME = "test-bucket-name"


@pytest.mark.parametrize(
argnames=["file_url", "expected_mime_type"],
argvalues=[
pytest.param(
"dir/subdir/file.invalid_extension",
"application/octet-stream",
id="File extension not in setting.MIME_TYPES_MAP",
),
pytest.param(
"dir/subdir/file.json",
"application/json",
id="File extension in settings.MIME_TYPES_MAP",
),
pytest.param(
"http://testdownload.org/file.csv?width=10",
"text/csv",
id="File url with GET params",
),
],
)
def test_get_mime_type_by_file_url(
file_url: str,
expected_mime_type: str,
):
"""Check that correct mime type is returned."""
assert utils.get_mime_type_by_file_url(file_url) == expected_mime_type


def test_clear_q_filter():
"""Check that filter is cleaned correctly."""
value = "Hello world"
attribute_name = "title"
expected_q_filter = Q(title__iregex=r"^Hello\s+world$")

assert utils.get_clear_q_filter(value, attribute_name) == expected_q_filter


@django.test.override_settings(
AWS_STORAGE_BUCKET_NAME=AWS_STORAGE_BUCKET_NAME,
)
@pytest.mark.parametrize(
argnames=["file_url", "expected_file_url"],
argvalues=[
pytest.param(
"http://localhost:8000/media/dir/file.csv",
"dir/file.csv",
id="File from media",
),
pytest.param(
f"http://s3.region.com/{AWS_STORAGE_BUCKET_NAME}/dir/file.csv",
"dir/file.csv",
id="File from s3 bucket",
),
pytest.param(
f"http://{AWS_STORAGE_BUCKET_NAME}.s3.region.com/dir/file.csv",
"dir/file.csv",
id=(
"File from s3 bucket if using virtual addressing style:"
"https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html"
),
),
],
)
def test_url_to_internal_value(
file_url: str,
expected_file_url: str,
):
"""Check that file url is converted correctly."""
assert utils.url_to_internal_value(file_url) == expected_file_url

0 comments on commit 4ed1cf6

Please sign in to comment.