File: test_softdeletable_model.py

package info (click to toggle)
django-model-utils 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 552 kB
  • sloc: python: 3,438; makefile: 181
file content (53 lines) | stat: -rw-r--r-- 1,935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from django.db.utils import ConnectionDoesNotExist
from django.test import TestCase

from tests.models import SoftDeletable


class SoftDeletableModelTests(TestCase):
    def test_can_only_see_not_removed_entries(self):
        SoftDeletable.available_objects.create(name='a', is_removed=True)
        SoftDeletable.available_objects.create(name='b', is_removed=False)

        queryset = SoftDeletable.available_objects.all()

        self.assertEqual(queryset.count(), 1)
        self.assertEqual(queryset[0].name, 'b')

    def test_instance_cannot_be_fully_deleted(self):
        instance = SoftDeletable.available_objects.create(name='a')

        instance.delete()

        self.assertEqual(SoftDeletable.available_objects.count(), 0)
        self.assertEqual(SoftDeletable.all_objects.count(), 1)

    def test_instance_cannot_be_fully_deleted_via_queryset(self):
        SoftDeletable.available_objects.create(name='a')

        SoftDeletable.available_objects.all().delete()

        self.assertEqual(SoftDeletable.available_objects.count(), 0)
        self.assertEqual(SoftDeletable.all_objects.count(), 1)

    def test_delete_instance_no_connection(self):
        obj = SoftDeletable.available_objects.create(name='a')

        self.assertRaises(ConnectionDoesNotExist, obj.delete, using='other')

    def test_instance_purge(self):
        instance = SoftDeletable.available_objects.create(name='a')

        instance.delete(soft=False)

        self.assertEqual(SoftDeletable.available_objects.count(), 0)
        self.assertEqual(SoftDeletable.all_objects.count(), 0)

    def test_instance_purge_no_connection(self):
        instance = SoftDeletable.available_objects.create(name='a')

        self.assertRaises(ConnectionDoesNotExist, instance.delete,
                          using='other', soft=False)

    def test_deprecation_warning(self):
        self.assertWarns(DeprecationWarning, SoftDeletable.objects.all)