Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove test dependencies observed when running tests with --reverse #1703

Merged
merged 9 commits into from
Dec 7, 2023
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Please refer to :doc:`release notes<release_notes>`.
- Import form defaults to read-only field if only one format defined (#1690)
- Refactored :module:`~import_export.resources` into separate modules for ``declarative`` and ``options`` (#1695)
- fix multiple inheritance not setting options (#1696)
- Refactored tests to remove dependencies between tests (#1703)

4.0.0-beta.1 (2023-11-16)
--------------------------
Expand Down
19 changes: 12 additions & 7 deletions tests/core/tests/test_resources/test_resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import sys
from collections import OrderedDict
from copy import deepcopy
from datetime import date
from decimal import Decimal, InvalidOperation
from unittest import mock, skipUnless
Expand Down Expand Up @@ -1092,8 +1091,15 @@ def test_empty_get_queryset(self):

@ignore_widget_deprecation_warning
def test_import_data_skip_unchanged(self):
def attempted_save(instance, new, using_transactions, real_dry_run):
self.fail("Resource attempted to save instead of skipping")
class MyBookResource(resources.ModelResource):
save_count = 0

def save_instance(self, instance, is_create, row, **kwargs):
self.save_count += 1

class Meta:
skip_unchanged = True
model = Book

# Make sure we test with ManyToMany related objects
cat1 = Category.objects.create(name="Cat 1")
Expand All @@ -1104,16 +1110,15 @@ def attempted_save(instance, new, using_transactions, real_dry_run):

# Create a new resource that attempts to reimport the data currently
# in the database while skipping unchanged rows (i.e. all of them)
resource = deepcopy(self.resource)
resource._meta.skip_unchanged = True
# Fail the test if the resource attempts to save the row
resource.save_instance = attempted_save
resource = MyBookResource()
result = resource.import_data(dataset, raise_errors=True)
self.assertFalse(result.has_errors())
self.assertEqual(len(result.rows), len(dataset))
self.assertTrue(result.rows[0].diff)
self.assertEqual(result.rows[0].import_type, results.RowResult.IMPORT_TYPE_SKIP)
self.assertEqual(result.rows[0].object_id, self.book.pk)
if resource.save_count > 0:
self.fail("Resource attempted to save instead of skipping")

# Test that we can suppress reporting of skipped rows
resource._meta.report_skipped = False
Expand Down
6 changes: 1 addition & 5 deletions tests/core/tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ def test_add_instance_info_no_instance_pk(self):
self.assertEqual("", row_result.object_repr)

def test_add_instance_info(self):
class BookWithObjectRepr(Book):
def __str__(self):
return self.name

row_result = RowResult()
row_result.add_instance_info(BookWithObjectRepr(pk=1, name="some book"))
row_result.add_instance_info(Book(pk=1, name="some book"))
self.assertEqual(1, row_result.object_id)
self.assertEqual("some book", row_result.object_repr)

Expand Down