@@ -779,6 +779,22 @@ class TestRequests(EnumTypeMixin, TestCase):
779779
780780 maxDiff = None
781781
782+ fields = [
783+ 'small_pos_int' ,
784+ 'small_int' ,
785+ 'pos_int' ,
786+ 'int' ,
787+ 'big_pos_int' ,
788+ 'big_int' ,
789+ 'constant' ,
790+ 'text' ,
791+ 'dj_int_enum' ,
792+ 'dj_text_enum' ,
793+ 'non_strict_int' ,
794+ 'non_strict_text' ,
795+ 'no_coerce' ,
796+ ]
797+
782798 def setUp (self ):
783799 self .values = {val : {} for val in self .compared_attributes }
784800 self .objects = []
@@ -873,6 +889,65 @@ def post_params_symmetric(self):
873889 ** self .post_params ,
874890 }
875891
892+ def test_drf_read (self ):
893+ c = Client ()
894+ response = c .get (reverse (f'{ self .NAMESPACE } :enumtester-list' ))
895+ read_objects = response .json ()
896+ self .assertEqual (len (read_objects ), len (self .objects ))
897+
898+ for idx , obj in enumerate (response .json ()):
899+ # should be same order
900+ self .assertEqual (obj ['id' ], self .objects [idx ].id )
901+ for field in self .fields :
902+ self .assertEqual (obj [field ], getattr (self .objects [idx ], field ))
903+
904+ def test_drf_update (self ):
905+ c = Client ()
906+ params = self .post_params #_symmetric TODO
907+ response = c .put (
908+ reverse (
909+ f'{ self .NAMESPACE } :enumtester-detail' ,
910+ kwargs = {'pk' : self .objects [0 ].id }
911+ ),
912+ params ,
913+ follow = True ,
914+ content_type = 'application/json'
915+ )
916+ self .assertEqual (response .status_code , 200 )
917+ fetched = c .get (
918+ reverse (
919+ f'{ self .NAMESPACE } :enumtester-detail' ,
920+ kwargs = {'pk' : self .objects [0 ].id }
921+ ),
922+ follow = True
923+ ).json ()
924+
925+ obj = self .MODEL_CLASS .objects .get (pk = self .objects [0 ].id )
926+
927+ self .assertEqual (fetched ['id' ], obj .id )
928+ for field in self .fields :
929+ self .assertEqual (fetched [field ], getattr (obj , field ))
930+ self .assertEqual (params [field ], getattr (obj , field ))
931+
932+ def test_drf_post (self ):
933+ c = Client ()
934+ params = self .post_params #_symmetric TODO
935+ response = c .post (
936+ reverse (f'{ self .NAMESPACE } :enumtester-list' ),
937+ params ,
938+ follow = True ,
939+ content_type = 'application/json'
940+ )
941+ self .assertEqual (response .status_code , 201 )
942+ created = response .json ()
943+
944+ obj = self .MODEL_CLASS .objects .last ()
945+
946+ self .assertEqual (created ['id' ], obj .id )
947+ for field in self .fields :
948+ self .assertEqual (created [field ], getattr (obj , field ))
949+ self .assertEqual (params [field ], getattr (obj , field ))
950+
876951 def test_add (self ):
877952 """
878953 Test that add forms work and that EnumField type allows creations
@@ -959,21 +1034,7 @@ def test_add_form(self):
9591034 response = c .get (reverse (f'{ self .NAMESPACE } :{ form_url } ' ))
9601035 soup = Soup (response .content , features = 'html.parser' )
9611036
962- for field in [
963- 'small_pos_int' ,
964- 'small_int' ,
965- 'pos_int' ,
966- 'int' ,
967- 'big_pos_int' ,
968- 'big_int' ,
969- 'constant' ,
970- 'text' ,
971- 'dj_int_enum' ,
972- 'dj_text_enum' ,
973- 'non_strict_int' ,
974- 'non_strict_text' ,
975- 'no_coerce'
976- ]:
1037+ for field in self .fields :
9771038 field = EnumTester ._meta .get_field (field )
9781039 expected = dict (zip ([en for en in field .enum ], field .enum .labels )) # value -> label
9791040 null_opt = False
@@ -1006,21 +1067,7 @@ def verify_form(self, obj, soup, non_strict_options=False):
10061067 in the options lists for non strict fields
10071068 :return:
10081069 """
1009- for field in [
1010- 'small_pos_int' ,
1011- 'small_int' ,
1012- 'pos_int' ,
1013- 'int' ,
1014- 'big_pos_int' ,
1015- 'big_int' ,
1016- 'constant' ,
1017- 'text' ,
1018- 'dj_int_enum' ,
1019- 'dj_text_enum' ,
1020- 'non_strict_int' ,
1021- 'non_strict_text' ,
1022- 'no_coerce' ,
1023- ]:
1070+ for field in self .fields :
10241071 field = self .MODEL_CLASS ._meta .get_field (field )
10251072 expected = dict (zip ([en for en in field .enum ],
10261073 field .enum .labels )) # value -> label
0 commit comments