2
2
General serializer field tests.
3
3
"""
4
4
from __future__ import unicode_literals
5
+ from django .utils .datastructures import SortedDict
5
6
import datetime
6
7
from decimal import Decimal
7
-
8
8
from django .db import models
9
9
from django .test import TestCase
10
10
from django .core import validators
11
-
12
11
from rest_framework import serializers
13
12
from rest_framework .serializers import Serializer
14
13
@@ -63,6 +62,20 @@ def test_non_auto_pk_fields_not_read_only(self):
63
62
serializer = CharPrimaryKeyModelSerializer ()
64
63
self .assertEqual (serializer .fields ['id' ].read_only , False )
65
64
65
+ def test_dict_field_ordering (self ):
66
+ """
67
+ Field should preserve dictionary ordering, if it exists.
68
+ See: https://github.com/tomchristie/django-rest-framework/issues/832
69
+ """
70
+ ret = SortedDict ()
71
+ ret ['c' ] = 1
72
+ ret ['b' ] = 1
73
+ ret ['a' ] = 1
74
+ ret ['z' ] = 1
75
+ field = serializers .Field ()
76
+ keys = list (field .to_native (ret ).keys ())
77
+ self .assertEqual (keys , ['c' , 'b' , 'a' , 'z' ])
78
+
66
79
67
80
class DateFieldTest (TestCase ):
68
81
"""
@@ -645,4 +658,4 @@ class DecimalSerializer(Serializer):
645
658
s = DecimalSerializer (data = {'decimal_field' : '12345.6' })
646
659
647
660
self .assertFalse (s .is_valid ())
648
- self .assertEqual (s .errors , {'decimal_field' : ['Ensure that there are no more than 4 digits in total.' ]})
661
+ self .assertEqual (s .errors , {'decimal_field' : ['Ensure that there are no more than 4 digits in total.' ]})
0 commit comments