Skip to content

Commit

Permalink
add tests and reference the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
manelclos committed May 2, 2021
1 parent 31d57f5 commit 3489f0f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ def skip_row(self, instance, original):
# For fields that are models.fields.related.ManyRelatedManager
# we need to compare the results
if isinstance(field.widget, widgets.ManyToManyWidget):
# compare with the future value to detect changes
instance_value = list(field.clean(row))
else:
instance_value = list(field.get_value(instance).all())
Expand Down
56 changes: 56 additions & 0 deletions tests/core/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,62 @@ def check_value(self, result, export_headers, expected_value):
expected_value)


class ManyToManyWidgetDiffTest(TestCase):
fixtures = ["category", "book"]

def setUp(self):
pass

def test_many_to_many_widget_create(self):
# issue #1270 - ensure ManyToMany fields are correctly checked for
# changes when skip_unchanged=True
book = Book.objects.first()
book.categories.clear()
dataset_headers = ["id", "name", "categories"]
dataset_row = [book.id, book.name, "1"]
dataset = tablib.Dataset(headers=dataset_headers)
dataset.append(dataset_row)

book_resource = BookResource()
book_resource._meta.skip_unchanged = True

result = book_resource.import_data(dataset, dry_run=False)
self.assertEqual(result.rows[0].import_type,
results.RowResult.IMPORT_TYPE_UPDATE)

def test_many_to_many_widget_update(self):
# issue #1270 - ensure ManyToMany fields are correctly checked for
# changes when skip_unchanged=True
book = Book.objects.first()
dataset_headers = ["id", "name", "categories"]
dataset_row = [book.id, book.name, "1"]
dataset = tablib.Dataset(headers=dataset_headers)
dataset.append(dataset_row)

book_resource = BookResource()
book_resource._meta.skip_unchanged = True

result = book_resource.import_data(dataset, dry_run=False)
self.assertEqual(result.rows[0].import_type,
results.RowResult.IMPORT_TYPE_UPDATE)

def test_many_to_many_widget_no_changes(self):
# issue #1270 - ensure ManyToMany fields are correctly checked for
# changes when skip_unchanged=True
book = Book.objects.first()
dataset_headers = ["id", "name", "categories"]
dataset_row = [book.id, book.name, book.categories.all()]
dataset = tablib.Dataset(headers=dataset_headers)
dataset.append(dataset_row)

book_resource = BookResource()
book_resource._meta.skip_unchanged = True

result = book_resource.import_data(dataset, dry_run=False)
self.assertEqual(result.rows[0].import_type,
results.RowResult.IMPORT_TYPE_SKIP)


@mock.patch("import_export.resources.Diff", spec=True)
class SkipDiffTest(TestCase):
"""
Expand Down

0 comments on commit 3489f0f

Please sign in to comment.