-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathresource_test.rb
More file actions
676 lines (556 loc) · 20.5 KB
/
resource_test.rb
File metadata and controls
676 lines (556 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
require File.expand_path('../../../test_helper', __FILE__)
class ArticleResource < JSONAPI::Resource
model_name 'Post'
def self.records(options)
options[:context].posts
end
end
class PostWithBadAfterSave < ActiveRecord::Base
self.table_name = 'posts'
after_save :do_some_after_save_stuff
def do_some_after_save_stuff
errors[:base] << 'Boom! Error added in after_save callback.'
raise ActiveRecord::RecordInvalid.new(self)
end
end
class PostWithCustomValidationContext < ActiveRecord::Base
self.table_name = 'posts'
validate :api_specific_check, on: :json_api_create
def api_specific_check
errors[:base] << 'Record is invalid'
end
end
class ArticleWithBadAfterSaveResource < JSONAPI::Resource
model_name 'PostWithBadAfterSave'
attribute :title
end
class ArticleWithCustomValidationContextResource < JSONAPI::Resource
model_name 'PostWithCustomValidationContext'
attribute :title
def _save
super(:json_api_create)
end
end
class NoMatchResource < JSONAPI::Resource
end
class NoMatchAbstractResource < JSONAPI::Resource
abstract
end
class FelineResource < JSONAPI::Resource
model_name 'Cat'
attribute :name
attribute :breed
attribute :kind, :delegate => :breed
has_one :mother, class_name: 'Cat'
has_one :father, class_name: 'Cat'
end
class TestSingletonResource < JSONAPI::Resource
end
module MyModule
class MyNamespacedResource < JSONAPI::Resource
model_name "Person"
has_many :related
has_one :default_profile, class_name: "Nested::Profile"
end
class RelatedResource < JSONAPI::Resource
model_name "Comment"
end
module Nested
class ProfileResource < JSONAPI::Resource
model_name "Nested::Profile"
end
end
end
module MyAPI
class MyNamespacedResource < MyModule::MyNamespacedResource
end
class RelatedResource < MyModule::RelatedResource
end
end
class PostWithReadonlyAttributesResource < JSONAPI::Resource
model_name 'Post'
attribute :title, readonly: true
has_one :author, readonly: true
end
class ResourceTest < ActiveSupport::TestCase
def setup
@post = posts(:post_1)
end
def test_model_name
assert_equal("Post", PostResource._model_name)
end
def test_model_name_of_subclassed_non_abstract_resource
assert_equal("Firm", FirmResource._model_name)
end
def test_model
assert_equal(PostResource._model_class, Post)
end
def test_module_path
assert_equal(MyModule::MyNamespacedResource.module_path, 'my_module/')
end
def test_resource_for_root_resource
assert_raises NameError do
JSONAPI::Resource.resource_klass_for('related')
end
end
def test_resource_for_resource_does_not_exist_at_root
assert_raises NameError do
ArticleResource.resource_klass_for('related')
end
end
def test_cache_ids_are_consistent_from_run_to_run
assert_equal '2020-01-15 14:15:12 UTC', @post.updated_at.to_s
post_resource = PostResource.new(@post, {})
assert_equal [@post.id, 1579097712.0], post_resource.cache_id
end
def test_cache_ids_change_when_cache_field_is_changed
assert_equal '2020-01-15 14:15:12 UTC', @post.updated_at.to_s
post_resource = PostResource.new(@post, {})
assert_equal [@post.id, 1579097712.0], post_resource.cache_id
assert @post.touch
refute_equal [@post.id, 1579097712.0], post_resource.cache_id
end
def test_date_based_cache_ids_change_with_milliseconds_if_hash_cache_field_is_time_stamp
assert_equal '2020-01-15 14:15:12 UTC', @post.updated_at.to_s
post_resource = PostResource.new(@post, {})
assert_equal [@post.id, 1579097712.0], post_resource.cache_id
@post.updated_at = @post.updated_at + 0.222
refute_equal [@post.id, 1579097712.0], post_resource.cache_id
end
def test_resource_for_with_underscored_namespaced_paths
assert_equal(JSONAPI::Resource.resource_klass_for('my_module/related'), MyModule::RelatedResource)
assert_equal(PostResource.resource_klass_for('my_module/related'), MyModule::RelatedResource)
assert_equal(MyModule::MyNamespacedResource.resource_klass_for('my_module/related'), MyModule::RelatedResource)
end
def test_resource_for_with_camelized_namespaced_paths
assert_equal(JSONAPI::Resource.resource_klass_for('MyModule::Related'), MyModule::RelatedResource)
assert_equal(PostResource.resource_klass_for('MyModule::Related'), MyModule::RelatedResource)
assert_equal(MyModule::MyNamespacedResource.resource_klass_for('MyModule::Related'), MyModule::RelatedResource)
end
def test_resource_for_namespaced_resource
assert_equal(MyModule::MyNamespacedResource.resource_klass_for('related'), MyModule::RelatedResource)
end
def test_resource_for_nested_namespaced_resource
assert_equal(JSONAPI::Resource.resource_klass_for('my_module/nested/profile'), MyModule::Nested::ProfileResource)
assert_equal(MyModule::MyNamespacedResource.resource_klass_for('my_module/nested/profile'), MyModule::Nested::ProfileResource)
assert_equal(MyModule::MyNamespacedResource.resource_klass_for('nested/profile'), MyModule::Nested::ProfileResource)
end
def test_relationship_parent_point_to_correct_resource
assert_equal MyModule::MyNamespacedResource, MyModule::MyNamespacedResource._relationships[:related].parent_resource
end
def test_relationship_parent_option_point_to_correct_resource
assert_equal MyModule::MyNamespacedResource, MyModule::MyNamespacedResource._relationships[:related].options[:parent_resource]
end
def test_derived_resources_relationships_parent_point_to_correct_resource
assert_equal MyAPI::MyNamespacedResource, MyAPI::MyNamespacedResource._relationships[:related].parent_resource
end
def test_derived_resources_relationships_parent_options_point_to_correct_resource
assert_equal MyAPI::MyNamespacedResource, MyAPI::MyNamespacedResource._relationships[:related].options[:parent_resource]
end
def test_base_resource_abstract
assert BaseResource._abstract
end
def test_derived_not_abstract
assert PersonResource < BaseResource
refute PersonResource._abstract
end
def test_inherited_calls_superclass
assert_equal(BaseResource.subclasses, [PersonResource, SpecialBaseResource])
end
def test_nil_model_class
# ToDo:Figure out why this test does not work on Rails 4.0
# :nocov:
if (Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR >= 1) || (Rails::VERSION::MAJOR >= 5)
assert_output nil, "[MODEL NOT FOUND] Model could not be found for NoMatchResource. If this is a base Resource declare it as abstract.\n" do
assert_nil NoMatchResource._model_class
end
end
# :nocov:
end
def test_nil_abstract_model_class
assert_silent do
assert_nil NoMatchAbstractResource._model_class
end
end
def test_model_alternate
assert_equal(ArticleResource._model_class, Post)
end
def test_class_attributes
attrs = FelineResource._attributes
assert_kind_of(Hash, attrs)
assert_equal(attrs.keys.size, 4)
end
def test_class_relationships
relationships = FelineResource._relationships
assert_kind_of(Hash, relationships)
assert_equal(relationships.size, 2)
end
def test_duplicate_relationship_name
assert_output nil, "[DUPLICATE RELATIONSHIP] `mother` has already been defined in FelineResource.\n" do
FelineResource.instance_eval do
has_one :mother, class_name: 'Cat'
end
end
end
def test_duplicate_attribute_name
assert_output nil, "[DUPLICATE ATTRIBUTE] `name` has already been defined in FelineResource.\n" do
FelineResource.instance_eval do
attribute :name
end
end
end
def test_find_with_customized_base_records
author = Person.find(1001)
posts = ArticleResource.find({}, context: author).map(&:_model)
assert(posts.include?(Post.find(1)))
refute(posts.include?(Post.find(3)))
end
def test_find_by_key_with_customized_base_records
author = Person.find(1001)
post = ArticleResource.find_by_key(1, context: author)._model
assert_equal(post, Post.find(1))
assert_raises JSONAPI::Exceptions::RecordNotFound do
ArticleResource.find_by_key(3, context: author)._model
end
end
def test_updatable_fields_does_not_include_id
assert(!FelineResource.updatable_fields.include?(:id))
end
def test_filter_on_to_many_relationship_id
posts = PostResource.find(:comments => 3)
assert_equal([2], posts.map(&:id))
end
def test_filter_on_aliased_to_many_relationship_id
# Comment 2 is approved
books = Api::V2::BookResource.find(:aliased_comments => 2)
assert_equal([0], books.map(&:id))
# However, comment 3 is non-approved, so it won't be accessible through this relationship
books = Api::V2::BookResource.find(:aliased_comments => 3)
assert_equal([], books.map(&:id))
end
def test_filter_on_has_one_relationship_id
prefs = PreferencesResource.find(:author => 1001)
assert_equal([1], prefs.map(&:id))
end
def test_to_many_relationship_filters
post_resource = PostResource.new(Post.find(1), nil)
comments = PostResource.find_included_fragments([post_resource.identity], :comments, {})
assert_equal(2, comments.size)
filtered_comments = PostResource.find_included_fragments([post_resource.identity], :comments, { filters: { body: 'i liked it' } })
assert_equal(1, filtered_comments.size)
end
def test_to_many_relationship_sorts
post_resource = PostResource.new(Post.find(1), nil)
comment_ids = post_resource.class.find_included_fragments([post_resource.identity], :comments, {}).keys.collect {|c| c.id }
assert_equal [1,2], comment_ids
# define apply_filters method on post resource to sort descending
PostResource.instance_eval do
def apply_sort(records, _order_options, options)
# :nocov:
order_by_query = "#{options[:related_alias]}.id desc"
records.order(order_by_query)
# :nocov:
end
end
sorted_comment_ids = post_resource.class.find_included_fragments(
[post_resource.identity],
:comments,
{ sort_criteria: [{ field: 'id', direction: :desc }] }).keys.collect {|c| c.id}
assert_equal [2,1], sorted_comment_ids
ensure
PostResource.instance_eval do
def apply_sort(records, order_options, context = {})
if order_options.any?
order_options.each_pair do |field, direction|
records = apply_single_sort(records, field, direction, context)
end
end
records
end
end
end
# ToDo: Implement relationship pagination
#
# def test_to_many_relationship_pagination
# post_resource = PostResource.new(Post.find(1), nil)
# comments = post_resource.comments
# assert_equal 2, comments.size
#
# # define apply_filters method on post resource to not respect filters
# PostResource.instance_eval do
# def apply_pagination(records, criteria, order_options)
# # :nocov:
# records
# # :nocov:
# end
# end
#
# paginator_class = Class.new(JSONAPI::Paginator) do
# def initialize(params)
# # param parsing and validation here
# @page = params.to_i
# end
#
# def apply(relation, order_options)
# relation.offset(@page).limit(1)
# end
# end
#
# paged_comments = post_resource.comments(paginator: paginator_class.new(1))
# assert_equal 1, paged_comments.size
#
# ensure
# # reset method to original implementation
# PostResource.instance_eval do
# def apply_pagination(records, criteria, order_options)
# # :nocov:
# records = paginator.apply(records, order_options) if paginator
# records
# # :nocov:
# end
# end
# end
def test_key_type_integer
FelineResource.instance_eval do
key_type :integer
end
assert FelineResource.verify_key('45')
assert FelineResource.verify_key(45)
assert_raises JSONAPI::Exceptions::InvalidFieldValue do
FelineResource.verify_key('45,345')
end
ensure
FelineResource.instance_eval do
key_type nil
end
end
def test_key_type_string
FelineResource.instance_eval do
key_type :string
end
assert FelineResource.verify_key('45')
assert FelineResource.verify_key(45)
assert_raises JSONAPI::Exceptions::InvalidFieldValue do
FelineResource.verify_key('45,345')
end
ensure
FelineResource.instance_eval do
key_type nil
end
end
def test_key_type_uuid
FelineResource.instance_eval do
key_type :uuid
end
assert FelineResource.verify_key('f1a4d5f2-e77a-4d0a-acbb-ee0b98b3f6b5')
assert_raises JSONAPI::Exceptions::InvalidFieldValue do
FelineResource.verify_key('f1a-e77a-4d0a-acbb-ee0b98b3f6b5')
end
ensure
FelineResource.instance_eval do
key_type nil
end
end
def test_key_type_proc
FelineResource.instance_eval do
key_type -> (key, context) {
return key if key.nil?
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}$/)
key
else
raise JSONAPI::Exceptions::InvalidFieldValue.new(:id, key)
end
}
end
assert FelineResource.verify_key('f1a4d5f2-e77a-4d0a-acbb-ee0b98b3f6b5')
assert_raises JSONAPI::Exceptions::InvalidFieldValue do
FelineResource.verify_key('f1a-e77a-4d0a-acbb-ee0b98b3f6b5')
end
ensure
FelineResource.instance_eval do
key_type nil
end
end
def test_id_attr_deprecation
ActiveSupport::Deprecation.silenced = false
_out, err = capture_io do
eval <<-CODE
class ProblemResource < JSONAPI::Resource
attribute :id
end
CODE
end
assert_match /DEPRECATION WARNING: Id without format is no longer supported. Please remove ids from attributes, or specify a format./, err
ensure
ActiveSupport::Deprecation.silenced = true
end
def test_id_attr_with_format
_out, err = capture_io do
eval <<-CODE
class NotProblemResource < JSONAPI::Resource
attribute :id, format: :string
end
CODE
end
assert_equal "", err
end
def test_links_resource_warning
_out, err = capture_io do
eval "class LinksResource < JSONAPI::Resource; end"
end
assert_match /LinksResource` is a reserved resource name/, err
end
def test_reserved_key_warnings
_out, err = capture_io do
eval <<-CODE
class BadlyNamedAttributesResource < JSONAPI::Resource
attributes :type
end
CODE
end
assert_match /`type` is a reserved key in ./, err
end
def test_reserved_relationship_warnings
%w(id type).each do |key|
_out, err = capture_io do
eval <<-CODE
class BadlyNamedAttributesResource < JSONAPI::Resource
has_one :#{key}
end
CODE
end
assert_match /`#{key}` is a reserved relationship name in ./, err
end
%w(types ids).each do |key|
_out, err = capture_io do
eval <<-CODE
class BadlyNamedAttributesResource < JSONAPI::Resource
has_many :#{key}
end
CODE
end
assert_match /`#{key}` is a reserved relationship name in ./, err
end
end
def test_abstract_warning
_out, err = capture_io do
eval <<-CODE
class NoModelResource < JSONAPI::Resource
end
NoModelResource._model_class
CODE
end
assert_match "[MODEL NOT FOUND] Model could not be found for ResourceTest::NoModelResource. If this is a base Resource declare it as abstract.\n", err
end
def test_no_warning_when_abstract
_out, err = capture_io do
eval <<-CODE
class NoModelAbstractResource < JSONAPI::Resource
abstract
end
NoModelAbstractResource._model_class
CODE
end
assert_match "", err
end
def test_correct_error_surfaced_if_validation_errors_in_after_save_callback
post = PostWithBadAfterSave.find(1)
post_resource = ArticleWithBadAfterSaveResource.new(post, nil)
err = assert_raises JSONAPI::Exceptions::ValidationErrors do
post_resource.replace_fields({:attributes => {:title => 'Some title'}})
end
assert_equal(err.error_messages[:base], ['Boom! Error added in after_save callback.'])
end
def test_resource_for_model_use_hint
special_person = Person.create!(name: 'Special', date_joined: Date.today, special: true)
special_resource = SpecialPersonResource.new(special_person, nil)
resource_model = SpecialPersonResource.records({}).first # simulate a find
assert_equal(SpecialPersonResource, SpecialPersonResource.resource_klass_for_model(resource_model))
end
def test_resource_performs_validations_in_custom_context
post = PostWithCustomValidationContext.find(1)
post_resource = ArticleWithCustomValidationContextResource.new(post, nil)
err = assert_raises JSONAPI::Exceptions::ValidationErrors do
post_resource._save
end
assert_equal(err.error_messages[:base], ['Record is invalid'])
end
def test_resources_for_transforms_records_into_resources
resources = PostResource.resources_for([Post.first], {})
assert_equal(PostResource, resources.first.class)
end
def test_readonly_attribute
refute_includes(PostWithReadonlyAttributesResource.creatable_fields, :title)
refute_includes(PostWithReadonlyAttributesResource.updatable_fields, :title)
refute_includes(PostWithReadonlyAttributesResource.creatable_fields, :author)
refute_includes(PostWithReadonlyAttributesResource.updatable_fields, :author)
end
def test_sortable_field?
assert(PostResource.sortable_field?(:title))
assert(PostResource.sortable_field?(:body))
refute(PostResource.sortable_field?(:color))
end
def test_exclude_links_on_resource
Api::V5::PostResource.exclude_links :none
assert_equal [], Api::V5::PostResource._exclude_links
refute Api::V5::PostResource.exclude_link?(:self)
refute Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links :default
assert_equal [:self], Api::V5::PostResource._exclude_links
assert Api::V5::PostResource.exclude_link?(:self)
assert Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links "none"
assert_equal [], Api::V5::PostResource._exclude_links
refute Api::V5::PostResource.exclude_link?(:self)
refute Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links "default"
assert_equal [:self], Api::V5::PostResource._exclude_links
assert Api::V5::PostResource.exclude_link?(:self)
assert Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links :none
assert_equal [], Api::V5::PostResource._exclude_links
refute Api::V5::PostResource.exclude_link?(:self)
refute Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links [:self]
assert_equal [:self], Api::V5::PostResource._exclude_links
assert Api::V5::PostResource.exclude_link?(:self)
assert Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links :none
assert_equal [], Api::V5::PostResource._exclude_links
refute Api::V5::PostResource.exclude_link?(:self)
refute Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links ["self"]
assert_equal [:self], Api::V5::PostResource._exclude_links
assert Api::V5::PostResource.exclude_link?(:self)
assert Api::V5::PostResource.exclude_link?("self")
Api::V5::PostResource.exclude_links []
assert_equal [], Api::V5::PostResource._exclude_links
refute Api::V5::PostResource.exclude_link?(:self)
refute Api::V5::PostResource.exclude_link?("self")
assert_raises do
Api::V5::PostResource.exclude_links :self
end
ensure
Api::V5::PostResource.exclude_links :none
end
def test_singleton_options
TestSingletonResource.singleton true
assert TestSingletonResource.singleton?
assert TestSingletonResource._singleton_options.blank?
TestSingletonResource.singleton false
refute TestSingletonResource.singleton?
assert TestSingletonResource._singleton_options.blank?
TestSingletonResource.singleton true, a: :b
assert TestSingletonResource.singleton?
refute TestSingletonResource._singleton_options.blank?
assert_equal :b, TestSingletonResource._singleton_options[:a]
TestSingletonResource.singleton false, c: :d
refute TestSingletonResource.singleton?
refute TestSingletonResource._singleton_options.blank?
assert_equal :d, TestSingletonResource._singleton_options[:c]
TestSingletonResource.singleton e: :f
assert TestSingletonResource.singleton?
refute TestSingletonResource._singleton_options.blank?
assert_equal :f, TestSingletonResource._singleton_options[:e]
end
end