Skip to content

Commit

Permalink
Add test for pickle error case
Browse files Browse the repository at this point in the history
  • Loading branch information
yalef committed May 17, 2024
1 parent c6d9949 commit 09488c8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
18 changes: 18 additions & 0 deletions tests/fake_app/migrations/0002_alter_artist_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-05-17 05:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("fake_app", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="artist",
name="name",
field=models.CharField(max_length=100, unique=True),
),
]
2 changes: 1 addition & 1 deletion tests/fake_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __str__(self) -> str:

class Artist(models.Model):
"""Model representing artist."""
name = models.CharField(max_length=100)
name = models.CharField(max_length=100, unique=True)
bands = models.ManyToManyField("Band", through="Membership")

instrument = models.ForeignKey(
Expand Down
2 changes: 1 addition & 1 deletion tests/fake_app/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SimpleArtistResource(CeleryModelResource):

class Meta:
model = Artist
clean_model_instances = True
fields = [
"id",
"name",
Expand All @@ -35,4 +36,3 @@ class ArtistResourceWithM2M(CeleryModelResource):
class Meta:
model = Artist
fields = ["id", "name", "bands", "instrument"]
clean_model_instances = True
15 changes: 14 additions & 1 deletion tests/test_models/test_import/test_import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from import_export_extensions.models import ImportJob

from ...fake_app.factories import ArtistImportJobFactory
from ...fake_app.factories import ArtistFactory, ArtistImportJobFactory
from ...fake_app.models import Artist


Expand Down Expand Up @@ -143,3 +143,16 @@ def test_force_import_create_correct_rows(
import_job.refresh_from_db()
assert import_job.import_status == import_job.ImportStatus.IMPORTED
assert Artist.objects.filter(name=new_artist.name).exists()


def test_import_data_with_validation_error(existing_artist: Artist):
"""Test import handles `ValidationError` instances correctly.
If validation error occur, then job should end with `INPUT_ERROR` status.
"""
wrong_artist = ArtistFactory.build(name=existing_artist.name)
job = ArtistImportJobFactory(artists=[wrong_artist])
job.parse_data()
job.refresh_from_db()
assert job.import_status == ImportJob.ImportStatus.INPUT_ERROR

0 comments on commit 09488c8

Please sign in to comment.