@@ -195,6 +195,7 @@ def apply_filters(records, filters, options)
195195 filtered_comments = post_resource . comments ( { filters : { body : 'i liked it' } } )
196196 assert_equal ( 1 , filtered_comments . size )
197197
198+ ensure
198199 # reset method to original implementation
199200 PostResource . instance_eval do
200201 def apply_filters ( records , filters , options )
@@ -222,6 +223,7 @@ def apply_sort(records, criteria)
222223 sorted_comment_ids = post_resource . comments ( sort_criteria : [ { field : 'id' , direction : 'desc' } ] ) . map { |c | c . model . id }
223224 assert_equal [ 2 , 1 ] , sorted_comment_ids
224225
226+ ensure
225227 # reset method to original implementation
226228 PostResource . instance_eval do
227229 def apply_sort ( records , criteria )
@@ -260,6 +262,7 @@ def apply(relation, order_options)
260262 paged_comments = post_resource . comments ( paginator : paginator_class . new ( 1 ) )
261263 assert_equal 1 , paged_comments . size
262264
265+ ensure
263266 # reset method to original implementation
264267 PostResource . instance_eval do
265268 def apply_pagination ( records , criteria , order_options )
@@ -269,4 +272,81 @@ def apply_pagination(records, criteria, order_options)
269272 end
270273 end
271274 end
275+
276+ def test_key_type_integer
277+ CatResource . instance_eval do
278+ key_type :integer
279+ end
280+
281+ assert CatResource . verify_key ( '45' )
282+ assert CatResource . verify_key ( 45 )
283+
284+ assert_raises JSONAPI ::Exceptions ::InvalidFieldValue do
285+ CatResource . verify_key ( '45,345' )
286+ end
287+
288+ ensure
289+ CatResource . instance_eval do
290+ key_type nil
291+ end
292+ end
293+
294+ def test_key_type_string
295+ CatResource . instance_eval do
296+ key_type :string
297+ end
298+
299+ assert CatResource . verify_key ( '45' )
300+ assert CatResource . verify_key ( 45 )
301+
302+ assert_raises JSONAPI ::Exceptions ::InvalidFieldValue do
303+ CatResource . verify_key ( '45,345' )
304+ end
305+
306+ ensure
307+ CatResource . instance_eval do
308+ key_type nil
309+ end
310+ end
311+
312+ def test_key_type_uuid
313+ CatResource . instance_eval do
314+ key_type :uuid
315+ end
316+
317+ assert CatResource . verify_key ( 'f1a4d5f2-e77a-4d0a-acbb-ee0b98b3f6b5' )
318+
319+ assert_raises JSONAPI ::Exceptions ::InvalidFieldValue do
320+ CatResource . verify_key ( 'f1a-e77a-4d0a-acbb-ee0b98b3f6b5' )
321+ end
322+
323+ ensure
324+ CatResource . instance_eval do
325+ key_type nil
326+ end
327+ end
328+
329+ def test_key_type_proc
330+ CatResource . instance_eval do
331+ key_type -> ( key , context ) {
332+ return key if key . nil?
333+ if key . to_s . match ( /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/ )
334+ key
335+ else
336+ raise JSONAPI ::Exceptions ::InvalidFieldValue . new ( :id , key )
337+ end
338+ }
339+ end
340+
341+ assert CatResource . verify_key ( 'f1a4d5f2-e77a-4d0a-acbb-ee0b98b3f6b5' )
342+
343+ assert_raises JSONAPI ::Exceptions ::InvalidFieldValue do
344+ CatResource . verify_key ( 'f1a-e77a-4d0a-acbb-ee0b98b3f6b5' )
345+ end
346+
347+ ensure
348+ CatResource . instance_eval do
349+ key_type nil
350+ end
351+ end
272352end
0 commit comments