Skip to content

Commit

Permalink
Merge pull request #612 from ProtixIT/generate-custom-soft-delete-man…
Browse files Browse the repository at this point in the history
…ager

Auto-generate manager implementation for `CustomSoftDelete`
  • Loading branch information
foarsitter committed Apr 17, 2024
2 parents 0693983 + 512d0f1 commit ba9ac5d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
13 changes: 0 additions & 13 deletions tests/managers.py

This file was deleted.

16 changes: 13 additions & 3 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

from model_utils import Choices
from model_utils.fields import MonitorField, SplitField, StatusField, UUIDField
from model_utils.managers import InheritanceManager, JoinManager, QueryManager
from model_utils.managers import (
InheritanceManager,
JoinManager,
QueryManager,
SoftDeletableManager,
SoftDeletableQuerySet,
)
from model_utils.models import (
SoftDeletableModel,
StatusModel,
Expand All @@ -19,7 +25,6 @@
)
from model_utils.tracker import FieldTracker, ModelTracker
from tests.fields import MutableField
from tests.managers import CustomSoftDeleteManager


class InheritanceManagerTestRelated(models.Model):
Expand Down Expand Up @@ -347,10 +352,15 @@ class SoftDeletable(SoftDeletableModel):
all_objects: ClassVar[Manager[SoftDeletable]] = models.Manager()


class CustomSoftDeleteQuerySet(SoftDeletableQuerySet):
def only_read(self):
return self.filter(is_read=True)


class CustomSoftDelete(SoftDeletableModel):
is_read = models.BooleanField(default=False)

objects: ClassVar[CustomSoftDeleteManager[SoftDeletableModel]] = CustomSoftDeleteManager()
available_objects = SoftDeletableManager.from_queryset(CustomSoftDeleteQuerySet)() # type: ignore[misc]


class StringyDescriptor:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_managers/test_softdelete_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
class CustomSoftDeleteManagerTests(TestCase):

def test_custom_manager_empty(self):
qs = CustomSoftDelete.objects.only_read()
qs = CustomSoftDelete.available_objects.only_read()
self.assertEqual(qs.count(), 0)

def test_custom_qs_empty(self):
qs = CustomSoftDelete.objects.all().only_read()
qs = CustomSoftDelete.available_objects.all().only_read()
self.assertEqual(qs.count(), 0)

def test_is_read(self):
for is_read in [True, False, True, False]:
CustomSoftDelete.objects.create(is_read=is_read)
qs = CustomSoftDelete.objects.only_read()
CustomSoftDelete.available_objects.create(is_read=is_read)
qs = CustomSoftDelete.available_objects.only_read()
self.assertEqual(qs.count(), 2)

def test_is_read_removed(self):
for is_read, is_removed in [(True, True), (True, False), (False, False), (False, True)]:
CustomSoftDelete.objects.create(is_read=is_read, is_removed=is_removed)
qs = CustomSoftDelete.objects.only_read()
CustomSoftDelete.available_objects.create(is_read=is_read, is_removed=is_removed)
qs = CustomSoftDelete.available_objects.only_read()
self.assertEqual(qs.count(), 1)

0 comments on commit ba9ac5d

Please sign in to comment.