2016-11-23 23:49:53 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
|
from django.db.utils import ConnectionDoesNotExist
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
2017-02-15 23:00:10 +00:00
|
|
|
from tests.models import SoftDeletable
|
2016-11-23 23:49:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class SoftDeletableModelTests(TestCase):
|
|
|
|
|
def test_can_only_see_not_removed_entries(self):
|
|
|
|
|
SoftDeletable.objects.create(name='a', is_removed=True)
|
|
|
|
|
SoftDeletable.objects.create(name='b', is_removed=False)
|
|
|
|
|
|
|
|
|
|
queryset = SoftDeletable.objects.all()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(queryset.count(), 1)
|
|
|
|
|
self.assertEqual(queryset[0].name, 'b')
|
|
|
|
|
|
|
|
|
|
def test_instance_cannot_be_fully_deleted(self):
|
|
|
|
|
instance = SoftDeletable.objects.create(name='a')
|
|
|
|
|
|
|
|
|
|
instance.delete()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(SoftDeletable.objects.count(), 0)
|
|
|
|
|
self.assertEqual(SoftDeletable.all_objects.count(), 1)
|
|
|
|
|
|
|
|
|
|
def test_instance_cannot_be_fully_deleted_via_queryset(self):
|
|
|
|
|
SoftDeletable.objects.create(name='a')
|
|
|
|
|
|
|
|
|
|
SoftDeletable.objects.all().delete()
|
|
|
|
|
|
|
|
|
|
self.assertEqual(SoftDeletable.objects.count(), 0)
|
|
|
|
|
self.assertEqual(SoftDeletable.all_objects.count(), 1)
|
|
|
|
|
|
|
|
|
|
def test_delete_instance_no_connection(self):
|
|
|
|
|
obj = SoftDeletable.objects.create(name='a')
|
|
|
|
|
|
|
|
|
|
self.assertRaises(ConnectionDoesNotExist, obj.delete, using='other')
|
2016-11-28 23:31:16 +00:00
|
|
|
|
|
|
|
|
def test_instance_purge(self):
|
|
|
|
|
instance = SoftDeletable.objects.create(name='a')
|
|
|
|
|
|
2016-11-28 23:45:48 +00:00
|
|
|
instance.delete(soft=False)
|
2016-11-28 23:31:16 +00:00
|
|
|
|
|
|
|
|
self.assertEqual(SoftDeletable.objects.count(), 0)
|
|
|
|
|
self.assertEqual(SoftDeletable.all_objects.count(), 0)
|
|
|
|
|
|
|
|
|
|
def test_instance_purge_no_connection(self):
|
|
|
|
|
instance = SoftDeletable.objects.create(name='a')
|
|
|
|
|
|
2016-11-28 23:45:48 +00:00
|
|
|
self.assertRaises(ConnectionDoesNotExist, instance.delete,
|
|
|
|
|
using='other', soft=False)
|