-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from saritasa-nest/feature/improve-coverage
Improve coverage for utils.py and result.py
- Loading branch information
Showing
7 changed files
with
130 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |