mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 11:00:13 +03:00
Move test_relations_with_limited_querysets file within test_relations_pk
This commit is contained in:
parent
ca9dcdffa2
commit
a66e8ac579
|
@ -5,10 +5,10 @@ from django.utils import six
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from tests.models import (
|
from tests.models import (
|
||||||
ForeignKeySource, ForeignKeyTarget, ManyToManySource, ManyToManyTarget,
|
ForeignKeySource, ForeignKeySourceWithLimitedChoices, ForeignKeyTarget,
|
||||||
NullableForeignKeySource, NullableOneToOneSource,
|
ManyToManySource, ManyToManyTarget, NullableForeignKeySource,
|
||||||
NullableUUIDForeignKeySource, OneToOnePKSource, OneToOneTarget,
|
NullableOneToOneSource, NullableUUIDForeignKeySource, OneToOnePKSource,
|
||||||
UUIDForeignKeyTarget
|
OneToOneTarget, UUIDForeignKeyTarget
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,6 +38,12 @@ class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
fields = ('id', 'name', 'target')
|
fields = ('id', 'name', 'target')
|
||||||
|
|
||||||
|
|
||||||
|
class ForeignKeySourceWithLimitedChoicesSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ForeignKeySourceWithLimitedChoices
|
||||||
|
fields = ("id", "target")
|
||||||
|
|
||||||
|
|
||||||
# Nullable ForeignKey
|
# Nullable ForeignKey
|
||||||
class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
|
class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -360,6 +366,18 @@ class PKForeignKeyTests(TestCase):
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
assert 'target' not in serializer.validated_data
|
assert 'target' not in serializer.validated_data
|
||||||
|
|
||||||
|
def test_queryset_size_without_limited_choices(self):
|
||||||
|
limited_target = ForeignKeyTarget(name="limited-target")
|
||||||
|
limited_target.save()
|
||||||
|
queryset = ForeignKeySourceSerializer().fields["target"].get_queryset()
|
||||||
|
assert len(queryset) == 3
|
||||||
|
|
||||||
|
def test_queryset_size_with_limited_choices(self):
|
||||||
|
limited_target = ForeignKeyTarget(name="limited-target")
|
||||||
|
limited_target.save()
|
||||||
|
queryset = ForeignKeySourceWithLimitedChoicesSerializer().fields["target"].get_queryset()
|
||||||
|
assert len(queryset) == 1
|
||||||
|
|
||||||
|
|
||||||
class PKNullableForeignKeyTests(TestCase):
|
class PKNullableForeignKeyTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
from rest_framework import serializers
|
|
||||||
|
|
||||||
from .models import (
|
|
||||||
ForeignKeySource,
|
|
||||||
ForeignKeyTarget,
|
|
||||||
ForeignKeySourceWithLimitedChoices,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ForeignKeySourceWithLimitedChoicesSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = ForeignKeySourceWithLimitedChoices
|
|
||||||
fields = ("id", "target")
|
|
||||||
|
|
||||||
|
|
||||||
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = ForeignKeySource
|
|
||||||
fields = ("id", "target")
|
|
||||||
|
|
||||||
|
|
||||||
class LimitedChoicesInQuerySetTests(TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
for idx in range(1, 4):
|
|
||||||
limited_target = ForeignKeyTarget(name="limited-target-%d" % idx)
|
|
||||||
limited_target.save()
|
|
||||||
target = ForeignKeyTarget(name="target-%d" % idx)
|
|
||||||
target.save()
|
|
||||||
|
|
||||||
def test_queryset_size_without_limited_choices(self):
|
|
||||||
queryset = ForeignKeySourceSerializer().fields["target"].get_queryset()
|
|
||||||
assert len(queryset) == 6
|
|
||||||
|
|
||||||
def test_queryset_size_with_limited_choices(self):
|
|
||||||
queryset = ForeignKeySourceWithLimitedChoicesSerializer().fields["target"].get_queryset()
|
|
||||||
assert len(queryset) == 3
|
|
Loading…
Reference in New Issue
Block a user