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

Auto-generate manager implementation for CustomSoftDelete #612

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)