Tests to demonstrate the delete all bugs
@@ -886,6 +886,48 @@ def test_bulk_delete():
assert TFoo.objects.count() == 0
+@pytest.mark.django_db
+def test_bulk_delete_all_uses_original_rows():
+ TFoo.objects.create(a=1, b='a')
+ TFoo.objects.create(a=2, b='b')
+ TFoo.objects.create(a=3, b='a')
+ t = Table(
+ rows=TFoo.objects.all().filter(b='a'),
+ page_size=1,
+ bulk__actions__delete__include=True,
+ ).bind(request=req('post', _all_pks_='1', **{'-delete': ''}))
+ assert 'Are you sure you want to delete these' in t.render_to_response().content.decode()
+ t = Table(
+ rows=TFoo.objects.all().filter(b='a'),
+ bulk__actions__delete__include=True,
+ ).bind(request=req('post', _all_pks_='1', **{'-delete': '', 'confirmed': 'confirmed'}))
+ response = t.render_to_response()
+ assert response.status_code == 302, response.content.decode()
+ # Deleting all should not have touched objects in TFoo that were not in rows.
+ assert list(TFoo.objects.all().order_by('a').values_list('a', flat=True)) == ['2']
+
+
+@pytest.mark.django_db
+def test_bulk_delete_all_respects_query():
+ TFoo.objects.create(a=1, b='a')
+ TFoo.objects.create(a=2, b='b')
+ TFoo.objects.create(a=3, b='a')
+ t = Table(
+ auto__model=TFoo,
+ page_size=1,
+ bulk__actions__delete__include=True,
+ ).bind(request=req('post', _all_pks_='1', **{'-delete': '', 'query': 'b=a'}))
+ assert 'Are you sure you want to delete these' in t.render_to_response().content.decode()
+ t = Table(
+ rows=TFoo.objects.all().filter(b='a'),
+ bulk__actions__delete__include=True,
+ ).bind(request=req('post', _all_pks_='1', **{'-delete': '', 'query': 'b=a', 'confirmed': 'confirmed'}))
+ response = t.render_to_response()
+ assert response.status_code == 302, response.content.decode()
+ # Deleting all should not have touched objects in TFoo that were filtered out.
+ assert list(TFoo.objects.all().order_by('a').values_list('a', flat=True)) == ['2']
+
+
@pytest.mark.django_db
def test_invalid_syntax_query():
class TestTable(Table):
