Skip to content

Commit

Permalink
Commit unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
954-Ivory committed Nov 22, 2022
1 parent 61becb6 commit 0d46e58
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from django.contrib.auth.models import User
from django.db import models
from django.db.models import QuerySet
from django.db.models.manager import BaseManager
from django.utils.translation import gettext_lazy as _


Expand Down Expand Up @@ -124,3 +126,27 @@ class OneToOnePKSource(RESTFrameworkModel):
target = models.OneToOneField(
OneToOneTarget, primary_key=True,
related_name='required_source', on_delete=models.CASCADE)


class CustomManagerModel(RESTFrameworkModel):
class CustomManager:
def __new__(cls, *args, **kwargs):
cls = BaseManager.from_queryset(
QuerySet
)
return cls

objects = CustomManager()()
# `CustomManager()` will return a `BaseManager` class.
# We need to instantiation it, so we write `CustomManager()()` here.

text = models.CharField(
max_length=100,
verbose_name=_("Text comes here"),
help_text=_("Text description.")
)

o2o_target = models.ForeignKey(OneToOneTarget,
help_text='OneToOneTarget',
verbose_name='OneToOneTarget',
on_delete=models.CASCADE)
45 changes: 45 additions & 0 deletions tests/test_serializer_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

from rest_framework import serializers
from rest_framework.exceptions import ErrorDetail
from tests.models import (
CustomManagerModel,
NullableOneToOneSource,
OneToOneTarget
)


class BasicObject:
Expand Down Expand Up @@ -683,3 +688,43 @@ def test_min_max_length_six_items(self):
assert min_serializer.validated_data == input_data

assert not max_min_serializer.is_valid()


@pytest.mark.django_db()
class TestToRepresentationManagerCheck:
"""
https://github.com/encode/django-rest-framework/issues/8726
"""

def setup(self):
class CustomManagerModelSerializer(serializers.ModelSerializer):
class Meta:
model = CustomManagerModel
fields = '__all__'

class OneToOneTargetSerializer(serializers.ModelSerializer):
my_model = CustomManagerModelSerializer(many=True, source="custommanagermodel_set")

class Meta:
model = OneToOneTarget
fields = '__all__'
depth = 3

class NullableOneToOneSourceSerializer(serializers.ModelSerializer):
target = OneToOneTargetSerializer()

class Meta:
model = NullableOneToOneSource
fields = '__all__'

self.serializer = NullableOneToOneSourceSerializer

def test(self):
o2o_target = OneToOneTarget.objects.create(name='OneToOneTarget')
NullableOneToOneSource.objects.create(
name='NullableOneToOneSource',
target=o2o_target
)
queryset = NullableOneToOneSource.objects.all()
serializer = self.serializer(queryset, many=True)
assert serializer.data

0 comments on commit 0d46e58

Please sign in to comment.