1
1
import uuid
2
2
3
3
import pytest
4
+ from _pytest .monkeypatch import MonkeyPatch
4
5
from django .conf .urls import url
5
6
from django .core .exceptions import ImproperlyConfigured
6
7
from django .test import override_settings
7
8
from django .utils .datastructures import MultiValueDict
8
9
9
- from rest_framework import serializers
10
+ from rest_framework import relations , serializers
10
11
from rest_framework .fields import empty
11
12
from rest_framework .test import APISimpleTestCase
12
13
@@ -25,6 +26,61 @@ def test_string_related_representation(self):
25
26
assert representation == '<MockObject name=foo, pk=1>'
26
27
27
28
29
+ class MockApiSettings (object ):
30
+ def __init__ (self , cutoff , cutoff_text ):
31
+ self .HTML_SELECT_CUTOFF = cutoff
32
+ self .HTML_SELECT_CUTOFF_TEXT = cutoff_text
33
+
34
+
35
+ class TestRelatedFieldHTMLCutoff (APISimpleTestCase ):
36
+ def setUp (self ):
37
+ self .queryset = MockQueryset ([
38
+ MockObject (pk = i , name = str (i )) for i in range (0 , 1100 )
39
+ ])
40
+ self .monkeypatch = MonkeyPatch ()
41
+
42
+ def test_no_settings (self ):
43
+ # The default is 1,000, so sans settings it should be 1,000 plus one.
44
+ for many in (False , True ):
45
+ field = serializers .PrimaryKeyRelatedField (queryset = self .queryset ,
46
+ many = many )
47
+ options = list (field .iter_options ())
48
+ assert len (options ) == 1001
49
+ assert options [- 1 ].display_text == "More than 1000 items..."
50
+
51
+ def test_settings_cutoff (self ):
52
+ self .monkeypatch .setattr (relations , "api_settings" ,
53
+ MockApiSettings (2 , "Cut Off" ))
54
+ for many in (False , True ):
55
+ field = serializers .PrimaryKeyRelatedField (queryset = self .queryset ,
56
+ many = many )
57
+ options = list (field .iter_options ())
58
+ assert len (options ) == 3 # 2 real items plus the 'Cut Off' item.
59
+ assert options [- 1 ].display_text == "Cut Off"
60
+
61
+ def test_settings_cutoff_none (self ):
62
+ # Setting it to None should mean no limit; the default limit is 1,000.
63
+ self .monkeypatch .setattr (relations , "api_settings" ,
64
+ MockApiSettings (None , "Cut Off" ))
65
+ for many in (False , True ):
66
+ field = serializers .PrimaryKeyRelatedField (queryset = self .queryset ,
67
+ many = many )
68
+ options = list (field .iter_options ())
69
+ assert len (options ) == 1100
70
+
71
+ def test_settings_kwargs_cutoff (self ):
72
+ # The explicit argument should override the settings.
73
+ self .monkeypatch .setattr (relations , "api_settings" ,
74
+ MockApiSettings (2 , "Cut Off" ))
75
+ for many in (False , True ):
76
+ field = serializers .PrimaryKeyRelatedField (queryset = self .queryset ,
77
+ many = many ,
78
+ html_cutoff = 100 )
79
+ options = list (field .iter_options ())
80
+ assert len (options ) == 101
81
+ assert options [- 1 ].display_text == "Cut Off"
82
+
83
+
28
84
class TestPrimaryKeyRelatedField (APISimpleTestCase ):
29
85
def setUp (self ):
30
86
self .queryset = MockQueryset ([
0 commit comments