@@ -24,8 +24,7 @@ def test_sort(self):
2424 msg = response .content .decode ("utf-8" ))
2525 dja_response = response .json ()
2626 headlines = [c ['attributes' ]['headline' ] for c in dja_response ['data' ]]
27- sorted_headlines = [c ['attributes' ]['headline' ] for c in dja_response ['data' ]]
28- sorted_headlines .sort ()
27+ sorted_headlines = sorted (headlines )
2928 self .assertEqual (headlines , sorted_headlines )
3029
3130 def test_sort_reverse (self ):
@@ -37,8 +36,19 @@ def test_sort_reverse(self):
3736 msg = response .content .decode ("utf-8" ))
3837 dja_response = response .json ()
3938 headlines = [c ['attributes' ]['headline' ] for c in dja_response ['data' ]]
40- sorted_headlines = [c ['attributes' ]['headline' ] for c in dja_response ['data' ]]
41- sorted_headlines .sort ()
39+ sorted_headlines = sorted (headlines )
40+ self .assertNotEqual (headlines , sorted_headlines )
41+
42+ def test_sort_double_negative (self ):
43+ """
44+ what if they provide multiple `-`'s? It's OK.
45+ """
46+ response = self .client .get (self .url , data = {'sort' : '--headline' })
47+ self .assertEqual (response .status_code , 200 ,
48+ msg = response .content .decode ("utf-8" ))
49+ dja_response = response .json ()
50+ headlines = [c ['attributes' ]['headline' ] for c in dja_response ['data' ]]
51+ sorted_headlines = sorted (headlines )
4252 self .assertNotEqual (headlines , sorted_headlines )
4353
4454 def test_sort_invalid (self ):
@@ -52,3 +62,44 @@ def test_sort_invalid(self):
5262 dja_response = response .json ()
5363 self .assertEqual (dja_response ['errors' ][0 ]['detail' ],
5464 "invalid sort parameters: nonesuch,-not_a_field" )
65+
66+ def test_sort_camelcase (self ):
67+ """
68+ test sort of camelcase field name
69+ """
70+ response = self .client .get (self .url , data = {'sort' : 'bodyText' })
71+ self .assertEqual (response .status_code , 200 ,
72+ msg = response .content .decode ("utf-8" ))
73+ dja_response = response .json ()
74+ blog_ids = [(c ['attributes' ]['bodyText' ] or '' ) for c in dja_response ['data' ]]
75+ sorted_blog_ids = sorted (blog_ids )
76+ self .assertEqual (blog_ids , sorted_blog_ids )
77+
78+ def test_sort_underscore (self ):
79+ """
80+ test sort of underscore field name
81+ Do we allow this notation in a search even if camelcase is in effect?
82+ "Be conservative in what you send, be liberal in what you accept"
83+ -- https://en.wikipedia.org/wiki/Robustness_principle
84+ """
85+ response = self .client .get (self .url , data = {'sort' : 'body_text' })
86+ self .assertEqual (response .status_code , 200 ,
87+ msg = response .content .decode ("utf-8" ))
88+ dja_response = response .json ()
89+ blog_ids = [(c ['attributes' ]['bodyText' ] or '' ) for c in dja_response ['data' ]]
90+ sorted_blog_ids = sorted (blog_ids )
91+ self .assertEqual (blog_ids , sorted_blog_ids )
92+
93+ def test_sort_related (self ):
94+ """
95+ test sort via related field using jsonapi path `.` and django orm `__` notation.
96+ ORM relations must be predefined in the View's .ordering_fields attr
97+ """
98+ for datum in ('blog__id' , 'blog.id' ):
99+ response = self .client .get (self .url , data = {'sort' : datum })
100+ self .assertEqual (response .status_code , 200 ,
101+ msg = response .content .decode ("utf-8" ))
102+ dja_response = response .json ()
103+ blog_ids = [c ['relationships' ]['blog' ]['data' ]['id' ] for c in dja_response ['data' ]]
104+ sorted_blog_ids = sorted (blog_ids )
105+ self .assertEqual (blog_ids , sorted_blog_ids )
0 commit comments