jsonapi-resources 0.7.1.beta1 → 0.7.1.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +248 -74
  3. data/lib/jsonapi-resources.rb +5 -3
  4. data/lib/jsonapi/acts_as_resource_controller.rb +77 -14
  5. data/lib/jsonapi/configuration.rb +77 -16
  6. data/lib/jsonapi/error.rb +12 -0
  7. data/lib/jsonapi/error_codes.rb +2 -0
  8. data/lib/jsonapi/exceptions.rb +29 -9
  9. data/lib/jsonapi/formatter.rb +29 -4
  10. data/lib/jsonapi/link_builder.rb +18 -18
  11. data/lib/jsonapi/mime_types.rb +25 -6
  12. data/lib/jsonapi/naive_cache.rb +30 -0
  13. data/lib/jsonapi/operation.rb +10 -342
  14. data/lib/jsonapi/operation_dispatcher.rb +87 -0
  15. data/lib/jsonapi/operation_result.rb +2 -1
  16. data/lib/jsonapi/paginator.rb +6 -2
  17. data/lib/jsonapi/processor.rb +283 -0
  18. data/lib/jsonapi/relationship.rb +6 -4
  19. data/lib/jsonapi/{request.rb → request_parser.rb} +46 -35
  20. data/lib/jsonapi/resource.rb +88 -13
  21. data/lib/jsonapi/resource_controller.rb +2 -14
  22. data/lib/jsonapi/resource_controller_metal.rb +17 -0
  23. data/lib/jsonapi/resource_serializer.rb +62 -47
  24. data/lib/jsonapi/resources/version.rb +1 -1
  25. data/lib/jsonapi/response_document.rb +13 -2
  26. data/lib/jsonapi/routing_ext.rb +49 -11
  27. metadata +37 -129
  28. data/.gitignore +0 -22
  29. data/.travis.yml +0 -9
  30. data/Gemfile +0 -23
  31. data/Rakefile +0 -20
  32. data/jsonapi-resources.gemspec +0 -29
  33. data/lib/jsonapi/active_record_operations_processor.rb +0 -35
  34. data/lib/jsonapi/operations_processor.rb +0 -120
  35. data/locales/en.yml +0 -80
  36. data/test/config/database.yml +0 -5
  37. data/test/controllers/controller_test.rb +0 -3312
  38. data/test/fixtures/active_record.rb +0 -1486
  39. data/test/fixtures/author_details.yml +0 -9
  40. data/test/fixtures/book_authors.yml +0 -3
  41. data/test/fixtures/book_comments.yml +0 -12
  42. data/test/fixtures/books.yml +0 -7
  43. data/test/fixtures/categories.yml +0 -35
  44. data/test/fixtures/comments.yml +0 -21
  45. data/test/fixtures/comments_tags.yml +0 -20
  46. data/test/fixtures/companies.yml +0 -4
  47. data/test/fixtures/craters.yml +0 -9
  48. data/test/fixtures/customers.yml +0 -11
  49. data/test/fixtures/documents.yml +0 -3
  50. data/test/fixtures/expense_entries.yml +0 -13
  51. data/test/fixtures/facts.yml +0 -11
  52. data/test/fixtures/hair_cuts.yml +0 -3
  53. data/test/fixtures/iso_currencies.yml +0 -17
  54. data/test/fixtures/line_items.yml +0 -37
  55. data/test/fixtures/makes.yml +0 -2
  56. data/test/fixtures/moons.yml +0 -6
  57. data/test/fixtures/numeros_telefone.yml +0 -3
  58. data/test/fixtures/order_flags.yml +0 -7
  59. data/test/fixtures/people.yml +0 -31
  60. data/test/fixtures/pictures.yml +0 -15
  61. data/test/fixtures/planet_types.yml +0 -19
  62. data/test/fixtures/planets.yml +0 -47
  63. data/test/fixtures/posts.yml +0 -102
  64. data/test/fixtures/posts_tags.yml +0 -59
  65. data/test/fixtures/preferences.yml +0 -14
  66. data/test/fixtures/products.yml +0 -3
  67. data/test/fixtures/purchase_orders.yml +0 -23
  68. data/test/fixtures/sections.yml +0 -8
  69. data/test/fixtures/tags.yml +0 -39
  70. data/test/fixtures/vehicles.yml +0 -17
  71. data/test/fixtures/web_pages.yml +0 -3
  72. data/test/helpers/assertions.rb +0 -13
  73. data/test/helpers/functional_helpers.rb +0 -59
  74. data/test/helpers/value_matchers.rb +0 -60
  75. data/test/helpers/value_matchers_test.rb +0 -40
  76. data/test/integration/requests/namespaced_model_test.rb +0 -13
  77. data/test/integration/requests/request_test.rb +0 -932
  78. data/test/integration/routes/routes_test.rb +0 -218
  79. data/test/integration/sti_fields_test.rb +0 -18
  80. data/test/lib/generators/jsonapi/controller_generator_test.rb +0 -25
  81. data/test/lib/generators/jsonapi/resource_generator_test.rb +0 -30
  82. data/test/test_helper.rb +0 -342
  83. data/test/unit/formatters/dasherized_key_formatter_test.rb +0 -8
  84. data/test/unit/jsonapi_request/jsonapi_request_test.rb +0 -199
  85. data/test/unit/operation/operations_processor_test.rb +0 -528
  86. data/test/unit/pagination/offset_paginator_test.rb +0 -245
  87. data/test/unit/pagination/paged_paginator_test.rb +0 -242
  88. data/test/unit/resource/resource_test.rb +0 -560
  89. data/test/unit/serializer/include_directives_test.rb +0 -113
  90. data/test/unit/serializer/link_builder_test.rb +0 -244
  91. data/test/unit/serializer/polymorphic_serializer_test.rb +0 -383
  92. data/test/unit/serializer/response_document_test.rb +0 -61
  93. data/test/unit/serializer/serializer_test.rb +0 -1939
@@ -1,61 +0,0 @@
1
- require File.expand_path('../../../test_helper', __FILE__)
2
- require 'jsonapi-resources'
3
- require 'json'
4
-
5
- class ResponseDocumentTest < ActionDispatch::IntegrationTest
6
- def setup
7
- JSONAPI.configuration.json_key_format = :dasherized_key
8
- JSONAPI.configuration.route_format = :dasherized_route
9
- end
10
-
11
- def create_response_document(operation_results, resource_klass)
12
- JSONAPI::ResponseDocument.new(
13
- operation_results,
14
- {
15
- primary_resource_klass: resource_klass
16
- }
17
- )
18
- end
19
-
20
- def test_response_document
21
- operations = [
22
- JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'Earth 2.0'}}),
23
- JSONAPI::CreateResourceOperation.new(PlanetResource, data: {attributes: {'name' => 'Vulcan'}})
24
- ]
25
-
26
- request = JSONAPI::Request.new
27
- request.operations = operations
28
-
29
- op = BasicOperationsProcessor.new()
30
- operation_results = op.process(request)
31
-
32
- response_doc = create_response_document(operation_results, PlanetResource)
33
-
34
- assert_equal :created, response_doc.status
35
- contents = response_doc.contents
36
- assert contents.is_a?(Hash)
37
- assert contents[:data].is_a?(Array)
38
- assert_equal 2, contents[:data].size
39
- end
40
-
41
- def test_response_document_multiple_find
42
- operations = [
43
- JSONAPI::FindOperation.new(PostResource, filters: {id: '1'}),
44
- JSONAPI::FindOperation.new(PostResource, filters: {id: '2'})
45
- ]
46
-
47
- request = JSONAPI::Request.new
48
- request.operations = operations
49
-
50
- op = ActiveRecordOperationsProcessor.new()
51
- operation_results = op.process(request)
52
-
53
- response_doc = create_response_document(operation_results, PostResource)
54
-
55
- assert_equal :ok, response_doc.status
56
- contents = response_doc.contents
57
- assert contents.is_a?(Hash)
58
- assert contents[:data].is_a?(Array)
59
- assert_equal 2, contents[:data].size
60
- end
61
- end
@@ -1,1939 +0,0 @@
1
- require File.expand_path('../../../test_helper', __FILE__)
2
- require 'jsonapi-resources'
3
- require 'json'
4
-
5
- class SerializerTest < ActionDispatch::IntegrationTest
6
- def setup
7
- @post = Post.find(1)
8
- @fred = Person.find_by(name: 'Fred Reader')
9
-
10
- @expense_entry = ExpenseEntry.find(1)
11
-
12
- JSONAPI.configuration.json_key_format = :camelized_key
13
- JSONAPI.configuration.route_format = :camelized_route
14
- JSONAPI.configuration.always_include_to_one_linkage_data = false
15
- end
16
-
17
- def after_teardown
18
- JSONAPI.configuration.always_include_to_one_linkage_data = false
19
- JSONAPI.configuration.json_key_format = :underscored_key
20
- end
21
-
22
- def test_serializer
23
-
24
- serialized = JSONAPI::ResourceSerializer.new(
25
- PostResource,
26
- base_url: 'http://example.com').serialize_to_hash(PostResource.new(@post, nil)
27
- )
28
-
29
- assert_hash_equals(
30
- {
31
- data: {
32
- type: 'posts',
33
- id: '1',
34
- links: {
35
- self: 'http://example.com/posts/1',
36
- },
37
- attributes: {
38
- title: 'New post',
39
- body: 'A body!!!',
40
- subject: 'New post'
41
- },
42
- relationships: {
43
- section: {
44
- links: {
45
- self: 'http://example.com/posts/1/relationships/section',
46
- related: 'http://example.com/posts/1/section'
47
- }
48
- },
49
- author: {
50
- links: {
51
- self: 'http://example.com/posts/1/relationships/author',
52
- related: 'http://example.com/posts/1/author'
53
- }
54
- },
55
- tags: {
56
- links: {
57
- self: 'http://example.com/posts/1/relationships/tags',
58
- related: 'http://example.com/posts/1/tags'
59
- }
60
- },
61
- comments: {
62
- links: {
63
- self: 'http://example.com/posts/1/relationships/comments',
64
- related: 'http://example.com/posts/1/comments'
65
- }
66
- }
67
- }
68
- }
69
- },
70
- serialized
71
- )
72
- end
73
-
74
- def test_serializer_nil_handling
75
- assert_hash_equals(
76
- {
77
- data: nil
78
- },
79
- JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(nil)
80
- )
81
- end
82
-
83
- def test_serializer_namespaced_resource
84
- assert_hash_equals(
85
- {
86
- data: {
87
- type: 'posts',
88
- id: '1',
89
- links: {
90
- self: 'http://example.com/api/v1/posts/1'
91
- },
92
- attributes: {
93
- title: 'New post',
94
- body: 'A body!!!',
95
- subject: 'New post'
96
- },
97
- relationships: {
98
- section: {
99
- links:{
100
- self: 'http://example.com/api/v1/posts/1/relationships/section',
101
- related: 'http://example.com/api/v1/posts/1/section'
102
- }
103
- },
104
- writer: {
105
- links:{
106
- self: 'http://example.com/api/v1/posts/1/relationships/writer',
107
- related: 'http://example.com/api/v1/posts/1/writer'
108
- }
109
- },
110
- comments: {
111
- links:{
112
- self: 'http://example.com/api/v1/posts/1/relationships/comments',
113
- related: 'http://example.com/api/v1/posts/1/comments'
114
- }
115
- }
116
- }
117
- }
118
- },
119
- JSONAPI::ResourceSerializer.new(Api::V1::PostResource,
120
- base_url: 'http://example.com').serialize_to_hash(
121
- Api::V1::PostResource.new(@post, nil))
122
- )
123
- end
124
-
125
- def test_serializer_limited_fieldset
126
-
127
- assert_hash_equals(
128
- {
129
- data: {
130
- type: 'posts',
131
- id: '1',
132
- links: {
133
- self: '/posts/1'
134
- },
135
- attributes: {
136
- title: 'New post'
137
- },
138
- relationships: {
139
- author: {
140
- links: {
141
- self: '/posts/1/relationships/author',
142
- related: '/posts/1/author'
143
- }
144
- }
145
- }
146
- }
147
- },
148
- JSONAPI::ResourceSerializer.new(PostResource,
149
- fields: {posts: [:id, :title, :author]}).serialize_to_hash(PostResource.new(@post, nil))
150
- )
151
- end
152
-
153
- def test_serializer_include
154
- serialized = JSONAPI::ResourceSerializer.new(
155
- PostResource,
156
- include: ['author']
157
- ).serialize_to_hash(PostResource.new(@post, nil))
158
-
159
- assert_hash_equals(
160
- {
161
- data: {
162
- type: 'posts',
163
- id: '1',
164
- links: {
165
- self: '/posts/1'
166
- },
167
- attributes: {
168
- title: 'New post',
169
- body: 'A body!!!',
170
- subject: 'New post'
171
- },
172
- relationships: {
173
- section: {
174
- links: {
175
- self: '/posts/1/relationships/section',
176
- related: '/posts/1/section'
177
- }
178
- },
179
- author: {
180
- links: {
181
- self: '/posts/1/relationships/author',
182
- related: '/posts/1/author'
183
- },
184
- data: {
185
- type: 'people',
186
- id: '1'
187
- }
188
- },
189
- tags: {
190
- links: {
191
- self: '/posts/1/relationships/tags',
192
- related: '/posts/1/tags'
193
- }
194
- },
195
- comments: {
196
- links: {
197
- self: '/posts/1/relationships/comments',
198
- related: '/posts/1/comments'
199
- }
200
- }
201
- }
202
- },
203
- included: [
204
- {
205
- type: 'people',
206
- id: '1',
207
- attributes: {
208
- name: 'Joe Author',
209
- email: 'joe@xyz.fake',
210
- dateJoined: '2013-08-07 16:25:00 -0400'
211
- },
212
- links: {
213
- self: '/people/1'
214
- },
215
- relationships: {
216
- comments: {
217
- links: {
218
- self: '/people/1/relationships/comments',
219
- related: '/people/1/comments'
220
- }
221
- },
222
- posts: {
223
- links: {
224
- self: '/people/1/relationships/posts',
225
- related: '/people/1/posts'
226
- }
227
- },
228
- preferences: {
229
- links: {
230
- self: '/people/1/relationships/preferences',
231
- related: '/people/1/preferences'
232
- }
233
- },
234
- hairCut: {
235
- links: {
236
- self: "/people/1/relationships/hairCut",
237
- related: "/people/1/hairCut"
238
- }
239
- },
240
- vehicles: {
241
- links: {
242
- self: "/people/1/relationships/vehicles",
243
- related: "/people/1/vehicles"
244
- }
245
- }
246
- }
247
- }
248
- ]
249
- },
250
- serialized
251
- )
252
- end
253
-
254
- def test_serializer_key_format
255
- serialized = JSONAPI::ResourceSerializer.new(
256
- PostResource,
257
- include: ['author'],
258
- key_formatter: UnderscoredKeyFormatter
259
- ).serialize_to_hash(PostResource.new(@post, nil))
260
-
261
- assert_hash_equals(
262
- {
263
- data: {
264
- type: 'posts',
265
- id: '1',
266
- attributes: {
267
- title: 'New post',
268
- body: 'A body!!!',
269
- subject: 'New post'
270
- },
271
- links: {
272
- self: '/posts/1'
273
- },
274
- relationships: {
275
- section: {
276
- links: {
277
- self: '/posts/1/relationships/section',
278
- related: '/posts/1/section'
279
- }
280
- },
281
- author: {
282
- links: {
283
- self: '/posts/1/relationships/author',
284
- related: '/posts/1/author'
285
- },
286
- data: {
287
- type: 'people',
288
- id: '1'
289
- }
290
- },
291
- tags: {
292
- links: {
293
- self: '/posts/1/relationships/tags',
294
- related: '/posts/1/tags'
295
- }
296
- },
297
- comments: {
298
- links: {
299
- self: '/posts/1/relationships/comments',
300
- related: '/posts/1/comments'
301
- }
302
- }
303
- }
304
- },
305
- included: [
306
- {
307
- type: 'people',
308
- id: '1',
309
- attributes: {
310
- name: 'Joe Author',
311
- email: 'joe@xyz.fake',
312
- date_joined: '2013-08-07 16:25:00 -0400'
313
- },
314
- links: {
315
- self: '/people/1'
316
- },
317
- relationships: {
318
- comments: {
319
- links: {
320
- self: '/people/1/relationships/comments',
321
- related: '/people/1/comments'
322
- }
323
- },
324
- posts: {
325
- links: {
326
- self: '/people/1/relationships/posts',
327
- related: '/people/1/posts'
328
- }
329
- },
330
- preferences: {
331
- links: {
332
- self: '/people/1/relationships/preferences',
333
- related: '/people/1/preferences'
334
- }
335
- },
336
- hair_cut: {
337
- links: {
338
- self: '/people/1/relationships/hairCut',
339
- related: '/people/1/hairCut'
340
- }
341
- },
342
- vehicles: {
343
- links: {
344
- self: "/people/1/relationships/vehicles",
345
- related: "/people/1/vehicles"
346
- }
347
- }
348
- }
349
- }
350
- ]
351
- },
352
- serialized
353
- )
354
- end
355
-
356
- def test_serializer_include_sub_objects
357
-
358
- assert_hash_equals(
359
- {
360
- data: {
361
- type: 'posts',
362
- id: '1',
363
- attributes: {
364
- title: 'New post',
365
- body: 'A body!!!',
366
- subject: 'New post'
367
- },
368
- links: {
369
- self: '/posts/1'
370
- },
371
- relationships: {
372
- section: {
373
- links: {
374
- self: '/posts/1/relationships/section',
375
- related: '/posts/1/section'
376
- }
377
- },
378
- author: {
379
- links: {
380
- self: '/posts/1/relationships/author',
381
- related: '/posts/1/author'
382
- }
383
- },
384
- tags: {
385
- links: {
386
- self: '/posts/1/relationships/tags',
387
- related: '/posts/1/tags'
388
- }
389
- },
390
- comments: {
391
- links: {
392
- self: '/posts/1/relationships/comments',
393
- related: '/posts/1/comments'
394
- },
395
- data: [
396
- {type: 'comments', id: '1'},
397
- {type: 'comments', id: '2'}
398
- ]
399
- }
400
- }
401
- },
402
- included: [
403
- {
404
- type: 'tags',
405
- id: '1',
406
- attributes: {
407
- name: 'short'
408
- },
409
- links: {
410
- self: '/tags/1'
411
- },
412
- relationships: {
413
- posts: {
414
- links: {
415
- self: '/tags/1/relationships/posts',
416
- related: '/tags/1/posts'
417
- }
418
- }
419
- }
420
- },
421
- {
422
- type: 'tags',
423
- id: '2',
424
- attributes: {
425
- name: 'whiny'
426
- },
427
- links: {
428
- self: '/tags/2'
429
- },
430
- relationships: {
431
- posts: {
432
- links: {
433
- self: '/tags/2/relationships/posts',
434
- related: '/tags/2/posts'
435
- }
436
- }
437
- }
438
- },
439
- {
440
- type: 'tags',
441
- id: '4',
442
- attributes: {
443
- name: 'happy'
444
- },
445
- links: {
446
- self: '/tags/4'
447
- },
448
- relationships: {
449
- posts: {
450
- links: {
451
- self: '/tags/4/relationships/posts',
452
- related: '/tags/4/posts'
453
- },
454
- }
455
- }
456
- },
457
- {
458
- type: 'comments',
459
- id: '1',
460
- attributes: {
461
- body: 'what a dumb post'
462
- },
463
- links: {
464
- self: '/comments/1'
465
- },
466
- relationships: {
467
- author: {
468
- links: {
469
- self: '/comments/1/relationships/author',
470
- related: '/comments/1/author'
471
- }
472
- },
473
- post: {
474
- links: {
475
- self: '/comments/1/relationships/post',
476
- related: '/comments/1/post'
477
- }
478
- },
479
- tags: {
480
- links: {
481
- self: '/comments/1/relationships/tags',
482
- related: '/comments/1/tags'
483
- },
484
- data: [
485
- {type: 'tags', id: '1'},
486
- {type: 'tags', id: '2'}
487
- ]
488
- }
489
- }
490
- },
491
- {
492
- type: 'comments',
493
- id: '2',
494
- attributes: {
495
- body: 'i liked it'
496
- },
497
- links: {
498
- self: '/comments/2'
499
- },
500
- relationships: {
501
- author: {
502
- links: {
503
- self: '/comments/2/relationships/author',
504
- related: '/comments/2/author'
505
- }
506
- },
507
- post: {
508
- links: {
509
- self: '/comments/2/relationships/post',
510
- related: '/comments/2/post'
511
- }
512
- },
513
- tags: {
514
- links: {
515
- self: '/comments/2/relationships/tags',
516
- related: '/comments/2/tags'
517
- },
518
- data: [
519
- {type: 'tags', id: '1'},
520
- {type: 'tags', id: '4'}
521
- ]
522
- }
523
- }
524
- }
525
- ]
526
- },
527
- JSONAPI::ResourceSerializer.new(PostResource,
528
- include: ['comments', 'comments.tags']).serialize_to_hash(PostResource.new(@post, nil))
529
- )
530
- end
531
-
532
- def test_serializer_different_foreign_key
533
- serialized = JSONAPI::ResourceSerializer.new(
534
- PersonResource,
535
- include: ['comments']
536
- ).serialize_to_hash(PersonResource.new(@fred, nil))
537
-
538
- assert_hash_equals(
539
- {
540
- data: {
541
- type: 'people',
542
- id: '2',
543
- attributes: {
544
- name: 'Fred Reader',
545
- email: 'fred@xyz.fake',
546
- dateJoined: '2013-10-31 16:25:00 -0400'
547
- },
548
- links: {
549
- self: '/people/2'
550
- },
551
- relationships: {
552
- posts: {
553
- links: {
554
- self: '/people/2/relationships/posts',
555
- related: '/people/2/posts'
556
- }
557
- },
558
- comments: {
559
- links: {
560
- self: '/people/2/relationships/comments',
561
- related: '/people/2/comments'
562
- },
563
- data: [
564
- {type: 'comments', id: '2'},
565
- {type: 'comments', id: '3'}
566
- ]
567
- },
568
- preferences: {
569
- links: {
570
- self: "/people/2/relationships/preferences",
571
- related: "/people/2/preferences"
572
- }
573
- },
574
- hairCut: {
575
- links: {
576
- self: "/people/2/relationships/hairCut",
577
- related: "/people/2/hairCut"
578
- }
579
- },
580
- vehicles: {
581
- links: {
582
- self: "/people/2/relationships/vehicles",
583
- related: "/people/2/vehicles"
584
- }
585
- },
586
- }
587
- },
588
- included: [
589
- {
590
- type: 'comments',
591
- id: '2',
592
- attributes: {
593
- body: 'i liked it'
594
- },
595
- links: {
596
- self: '/comments/2'
597
- },
598
- relationships: {
599
- author: {
600
- links: {
601
- self: '/comments/2/relationships/author',
602
- related: '/comments/2/author'
603
- }
604
- },
605
- post: {
606
- links: {
607
- self: '/comments/2/relationships/post',
608
- related: '/comments/2/post'
609
- }
610
- },
611
- tags: {
612
- links: {
613
- self: '/comments/2/relationships/tags',
614
- related: '/comments/2/tags'
615
- }
616
- }
617
- }
618
- },
619
- {
620
- type: 'comments',
621
- id: '3',
622
- attributes: {
623
- body: 'Thanks man. Great post. But what is JR?'
624
- },
625
- links: {
626
- self: '/comments/3'
627
- },
628
- relationships: {
629
- author: {
630
- links: {
631
- self: '/comments/3/relationships/author',
632
- related: '/comments/3/author'
633
- }
634
- },
635
- post: {
636
- links: {
637
- self: '/comments/3/relationships/post',
638
- related: '/comments/3/post'
639
- }
640
- },
641
- tags: {
642
- links: {
643
- self: '/comments/3/relationships/tags',
644
- related: '/comments/3/tags'
645
- }
646
- }
647
- }
648
- }
649
- ]
650
- },
651
- serialized
652
- )
653
- end
654
-
655
- def test_serializer_array_of_resources_always_include_to_one_linkage_data
656
-
657
- posts = []
658
- Post.find(1, 2).each do |post|
659
- posts.push PostResource.new(post, nil)
660
- end
661
-
662
- JSONAPI.configuration.always_include_to_one_linkage_data = true
663
-
664
- assert_hash_equals(
665
- {
666
- data: [
667
- {
668
- type: 'posts',
669
- id: '1',
670
- attributes: {
671
- title: 'New post',
672
- body: 'A body!!!',
673
- subject: 'New post'
674
- },
675
- links: {
676
- self: '/posts/1'
677
- },
678
- relationships: {
679
- section: {
680
- links: {
681
- self: '/posts/1/relationships/section',
682
- related: '/posts/1/section'
683
- },
684
- data: nil
685
- },
686
- author: {
687
- links: {
688
- self: '/posts/1/relationships/author',
689
- related: '/posts/1/author'
690
- },
691
- data: {
692
- type: 'people',
693
- id: '1'
694
- }
695
- },
696
- tags: {
697
- links: {
698
- self: '/posts/1/relationships/tags',
699
- related: '/posts/1/tags'
700
- }
701
- },
702
- comments: {
703
- links: {
704
- self: '/posts/1/relationships/comments',
705
- related: '/posts/1/comments'
706
- },
707
- data: [
708
- {type: 'comments', id: '1'},
709
- {type: 'comments', id: '2'}
710
- ]
711
- }
712
- }
713
- },
714
- {
715
- type: 'posts',
716
- id: '2',
717
- attributes: {
718
- title: 'JR Solves your serialization woes!',
719
- body: 'Use JR',
720
- subject: 'JR Solves your serialization woes!'
721
- },
722
- links: {
723
- self: '/posts/2'
724
- },
725
- relationships: {
726
- section: {
727
- links: {
728
- self: '/posts/2/relationships/section',
729
- related: '/posts/2/section'
730
- },
731
- data: {
732
- type: 'sections',
733
- id: '2'
734
- }
735
- },
736
- author: {
737
- links: {
738
- self: '/posts/2/relationships/author',
739
- related: '/posts/2/author'
740
- },
741
- data: {
742
- type: 'people',
743
- id: '1'
744
- }
745
- },
746
- tags: {
747
- links: {
748
- self: '/posts/2/relationships/tags',
749
- related: '/posts/2/tags'
750
- }
751
- },
752
- comments: {
753
- links: {
754
- self: '/posts/2/relationships/comments',
755
- related: '/posts/2/comments'
756
- },
757
- data: [
758
- {type: 'comments', id: '3'}
759
- ]
760
- }
761
- }
762
- }
763
- ],
764
- included: [
765
- {
766
- type: 'tags',
767
- id: '1',
768
- attributes: {
769
- name: 'short'
770
- },
771
- links: {
772
- self: '/tags/1'
773
- },
774
- relationships: {
775
- posts: {
776
- links: {
777
- self: '/tags/1/relationships/posts',
778
- related: '/tags/1/posts'
779
- }
780
- }
781
- }
782
- },
783
- {
784
- type: 'tags',
785
- id: '2',
786
- attributes: {
787
- name: 'whiny'
788
- },
789
- links: {
790
- self: '/tags/2'
791
- },
792
- relationships: {
793
- posts: {
794
- links: {
795
- self: '/tags/2/relationships/posts',
796
- related: '/tags/2/posts'
797
- }
798
- }
799
- }
800
- },
801
- {
802
- type: 'tags',
803
- id: '4',
804
- attributes: {
805
- name: 'happy'
806
- },
807
- links: {
808
- self: '/tags/4'
809
- },
810
- relationships: {
811
- posts: {
812
- links: {
813
- self: '/tags/4/relationships/posts',
814
- related: '/tags/4/posts'
815
- }
816
- }
817
- }
818
- },
819
- {
820
- type: 'tags',
821
- id: '5',
822
- attributes: {
823
- name: 'JR'
824
- },
825
- links: {
826
- self: '/tags/5'
827
- },
828
- relationships: {
829
- posts: {
830
- links: {
831
- self: '/tags/5/relationships/posts',
832
- related: '/tags/5/posts'
833
- }
834
- }
835
- }
836
- },
837
- {
838
- type: 'comments',
839
- id: '1',
840
- attributes: {
841
- body: 'what a dumb post'
842
- },
843
- links: {
844
- self: '/comments/1'
845
- },
846
- relationships: {
847
- author: {
848
- links: {
849
- self: '/comments/1/relationships/author',
850
- related: '/comments/1/author'
851
- },
852
- data: {
853
- type: 'people',
854
- id: '1'
855
- }
856
- },
857
- post: {
858
- links: {
859
- self: '/comments/1/relationships/post',
860
- related: '/comments/1/post'
861
- },
862
- data: {
863
- type: 'posts',
864
- id: '1'
865
- }
866
- },
867
- tags: {
868
- links: {
869
- self: '/comments/1/relationships/tags',
870
- related: '/comments/1/tags'
871
- },
872
- data: [
873
- {type: 'tags', id: '1'},
874
- {type: 'tags', id: '2'}
875
- ]
876
- }
877
- }
878
- },
879
- {
880
- type: 'comments',
881
- id: '2',
882
- attributes: {
883
- body: 'i liked it'
884
- },
885
- links: {
886
- self: '/comments/2'
887
- },
888
- relationships: {
889
- author: {
890
- links: {
891
- self: '/comments/2/relationships/author',
892
- related: '/comments/2/author'
893
- },
894
- data: {
895
- type: 'people',
896
- id: '2'
897
- }
898
- },
899
- post: {
900
- links: {
901
- self: '/comments/2/relationships/post',
902
- related: '/comments/2/post'
903
- },
904
- data: {
905
- type: 'posts',
906
- id: '1'
907
- }
908
- },
909
- tags: {
910
- links: {
911
- self: '/comments/2/relationships/tags',
912
- related: '/comments/2/tags'
913
- },
914
- data: [
915
- {type: 'tags', id: '4'},
916
- {type: 'tags', id: '1'}
917
- ]
918
- }
919
- }
920
- },
921
- {
922
- type: 'comments',
923
- id: '3',
924
- attributes: {
925
- body: 'Thanks man. Great post. But what is JR?'
926
- },
927
- links: {
928
- self: '/comments/3'
929
- },
930
- relationships: {
931
- author: {
932
- links: {
933
- self: '/comments/3/relationships/author',
934
- related: '/comments/3/author'
935
- },
936
- data: {
937
- type: 'people',
938
- id: '2'
939
- }
940
- },
941
- post: {
942
- links: {
943
- self: '/comments/3/relationships/post',
944
- related: '/comments/3/post'
945
- },
946
- data: {
947
- type: 'posts',
948
- id: '2'
949
- }
950
- },
951
- tags: {
952
- links: {
953
- self: '/comments/3/relationships/tags',
954
- related: '/comments/3/tags'
955
- },
956
- data: [
957
- {type: 'tags', id: '5'}
958
- ]
959
- }
960
- }
961
- }
962
- ]
963
- },
964
- JSONAPI::ResourceSerializer.new(PostResource,
965
- include: ['comments', 'comments.tags']).serialize_to_hash(posts)
966
- )
967
- ensure
968
- JSONAPI.configuration.always_include_to_one_linkage_data = false
969
- end
970
-
971
- def test_serializer_array_of_resources
972
-
973
- posts = []
974
- Post.find(1, 2).each do |post|
975
- posts.push PostResource.new(post, nil)
976
- end
977
-
978
- assert_hash_equals(
979
- {
980
- data: [
981
- {
982
- type: 'posts',
983
- id: '1',
984
- attributes: {
985
- title: 'New post',
986
- body: 'A body!!!',
987
- subject: 'New post'
988
- },
989
- links: {
990
- self: '/posts/1'
991
- },
992
- relationships: {
993
- section: {
994
- links: {
995
- self: '/posts/1/relationships/section',
996
- related: '/posts/1/section'
997
- }
998
- },
999
- author: {
1000
- links: {
1001
- self: '/posts/1/relationships/author',
1002
- related: '/posts/1/author'
1003
- }
1004
- },
1005
- tags: {
1006
- links: {
1007
- self: '/posts/1/relationships/tags',
1008
- related: '/posts/1/tags'
1009
- }
1010
- },
1011
- comments: {
1012
- links: {
1013
- self: '/posts/1/relationships/comments',
1014
- related: '/posts/1/comments'
1015
- },
1016
- data: [
1017
- {type: 'comments', id: '1'},
1018
- {type: 'comments', id: '2'}
1019
- ]
1020
- }
1021
- }
1022
- },
1023
- {
1024
- type: 'posts',
1025
- id: '2',
1026
- attributes: {
1027
- title: 'JR Solves your serialization woes!',
1028
- body: 'Use JR',
1029
- subject: 'JR Solves your serialization woes!'
1030
- },
1031
- links: {
1032
- self: '/posts/2'
1033
- },
1034
- relationships: {
1035
- section: {
1036
- links: {
1037
- self: '/posts/2/relationships/section',
1038
- related: '/posts/2/section'
1039
- }
1040
- },
1041
- author: {
1042
- links: {
1043
- self: '/posts/2/relationships/author',
1044
- related: '/posts/2/author'
1045
- }
1046
- },
1047
- tags: {
1048
- links: {
1049
- self: '/posts/2/relationships/tags',
1050
- related: '/posts/2/tags'
1051
- }
1052
- },
1053
- comments: {
1054
- links: {
1055
- self: '/posts/2/relationships/comments',
1056
- related: '/posts/2/comments'
1057
- },
1058
- data: [
1059
- {type: 'comments', id: '3'}
1060
- ]
1061
- }
1062
- }
1063
- }
1064
- ],
1065
- included: [
1066
- {
1067
- type: 'tags',
1068
- id: '1',
1069
- attributes: {
1070
- name: 'short'
1071
- },
1072
- links: {
1073
- self: '/tags/1'
1074
- },
1075
- relationships: {
1076
- posts: {
1077
- links: {
1078
- self: '/tags/1/relationships/posts',
1079
- related: '/tags/1/posts'
1080
- }
1081
- }
1082
- }
1083
- },
1084
- {
1085
- type: 'tags',
1086
- id: '2',
1087
- attributes: {
1088
- name: 'whiny'
1089
- },
1090
- links: {
1091
- self: '/tags/2'
1092
- },
1093
- relationships: {
1094
- posts: {
1095
- links: {
1096
- self: '/tags/2/relationships/posts',
1097
- related: '/tags/2/posts'
1098
- }
1099
- }
1100
- }
1101
- },
1102
- {
1103
- type: 'tags',
1104
- id: '4',
1105
- attributes: {
1106
- name: 'happy'
1107
- },
1108
- links: {
1109
- self: '/tags/4'
1110
- },
1111
- relationships: {
1112
- posts: {
1113
- links: {
1114
- self: '/tags/4/relationships/posts',
1115
- related: '/tags/4/posts'
1116
- }
1117
- }
1118
- }
1119
- },
1120
- {
1121
- type: 'tags',
1122
- id: '5',
1123
- attributes: {
1124
- name: 'JR'
1125
- },
1126
- links: {
1127
- self: '/tags/5'
1128
- },
1129
- relationships: {
1130
- posts: {
1131
- links: {
1132
- self: '/tags/5/relationships/posts',
1133
- related: '/tags/5/posts'
1134
- }
1135
- }
1136
- }
1137
- },
1138
- {
1139
- type: 'comments',
1140
- id: '1',
1141
- attributes: {
1142
- body: 'what a dumb post'
1143
- },
1144
- links: {
1145
- self: '/comments/1'
1146
- },
1147
- relationships: {
1148
- author: {
1149
- links: {
1150
- self: '/comments/1/relationships/author',
1151
- related: '/comments/1/author'
1152
- }
1153
- },
1154
- post: {
1155
- links: {
1156
- self: '/comments/1/relationships/post',
1157
- related: '/comments/1/post'
1158
- }
1159
- },
1160
- tags: {
1161
- links: {
1162
- self: '/comments/1/relationships/tags',
1163
- related: '/comments/1/tags'
1164
- },
1165
- data: [
1166
- {type: 'tags', id: '1'},
1167
- {type: 'tags', id: '2'}
1168
- ]
1169
- }
1170
- }
1171
- },
1172
- {
1173
- type: 'comments',
1174
- id: '2',
1175
- attributes: {
1176
- body: 'i liked it'
1177
- },
1178
- links: {
1179
- self: '/comments/2'
1180
- },
1181
- relationships: {
1182
- author: {
1183
- links: {
1184
- self: '/comments/2/relationships/author',
1185
- related: '/comments/2/author'
1186
- }
1187
- },
1188
- post: {
1189
- links: {
1190
- self: '/comments/2/relationships/post',
1191
- related: '/comments/2/post'
1192
- }
1193
- },
1194
- tags: {
1195
- links: {
1196
- self: '/comments/2/relationships/tags',
1197
- related: '/comments/2/tags'
1198
- },
1199
- data: [
1200
- {type: 'tags', id: '4'},
1201
- {type: 'tags', id: '1'}
1202
- ]
1203
- }
1204
- }
1205
- },
1206
- {
1207
- type: 'comments',
1208
- id: '3',
1209
- attributes: {
1210
- body: 'Thanks man. Great post. But what is JR?'
1211
- },
1212
- links: {
1213
- self: '/comments/3'
1214
- },
1215
- relationships: {
1216
- author: {
1217
- links: {
1218
- self: '/comments/3/relationships/author',
1219
- related: '/comments/3/author'
1220
- }
1221
- },
1222
- post: {
1223
- links: {
1224
- self: '/comments/3/relationships/post',
1225
- related: '/comments/3/post'
1226
- }
1227
- },
1228
- tags: {
1229
- links: {
1230
- self: '/comments/3/relationships/tags',
1231
- related: '/comments/3/tags'
1232
- },
1233
- data: [
1234
- {type: 'tags', id: '5'}
1235
- ]
1236
- }
1237
- }
1238
- }
1239
- ]
1240
- },
1241
- JSONAPI::ResourceSerializer.new(PostResource,
1242
- include: ['comments', 'comments.tags']).serialize_to_hash(posts)
1243
- )
1244
- end
1245
-
1246
- def test_serializer_array_of_resources_limited_fields
1247
-
1248
- posts = []
1249
- Post.find(1, 2).each do |post|
1250
- posts.push PostResource.new(post, nil)
1251
- end
1252
-
1253
- assert_hash_equals(
1254
- {
1255
- data: [
1256
- {
1257
- type: 'posts',
1258
- id: '1',
1259
- attributes: {
1260
- title: 'New post'
1261
- },
1262
- links: {
1263
- self: '/posts/1'
1264
- }
1265
- },
1266
- {
1267
- type: 'posts',
1268
- id: '2',
1269
- attributes: {
1270
- title: 'JR Solves your serialization woes!'
1271
- },
1272
- links: {
1273
- self: '/posts/2'
1274
- }
1275
- }
1276
- ],
1277
- included: [
1278
- {
1279
- type: 'posts',
1280
- id: '11',
1281
- attributes: {
1282
- title: 'JR How To'
1283
- },
1284
- links: {
1285
- self: '/posts/11'
1286
- }
1287
- },
1288
- {
1289
- type: 'people',
1290
- id: '1',
1291
- attributes: {
1292
- email: 'joe@xyz.fake'
1293
- },
1294
- links: {
1295
- self: '/people/1'
1296
- },
1297
- relationships: {
1298
- comments: {
1299
- links: {
1300
- self: '/people/1/relationships/comments',
1301
- related: '/people/1/comments'
1302
- }
1303
- }
1304
- }
1305
- },
1306
- {
1307
- id: '1',
1308
- type: 'tags',
1309
- attributes: {
1310
- name: 'short'
1311
- },
1312
- links: {
1313
- self: '/tags/1'
1314
- }
1315
- },
1316
- {
1317
- id: '2',
1318
- type: 'tags',
1319
- attributes: {
1320
- name: 'whiny'
1321
- },
1322
- links: {
1323
- self: '/tags/2'
1324
- }
1325
- },
1326
- {
1327
- id: '4',
1328
- type: 'tags',
1329
- attributes: {
1330
- name: 'happy'
1331
- },
1332
- links: {
1333
- self: '/tags/4'
1334
- }
1335
- },
1336
- {
1337
- id: '5',
1338
- type: 'tags',
1339
- attributes: {
1340
- name: 'JR'
1341
- },
1342
- links: {
1343
- self: '/tags/5'
1344
- }
1345
- },
1346
- {
1347
- type: 'comments',
1348
- id: '1',
1349
- attributes: {
1350
- body: 'what a dumb post'
1351
- },
1352
- links: {
1353
- self: '/comments/1'
1354
- },
1355
- relationships: {
1356
- post: {
1357
- links: {
1358
- self: '/comments/1/relationships/post',
1359
- related: '/comments/1/post'
1360
- }
1361
- }
1362
- }
1363
- },
1364
- {
1365
- type: 'comments',
1366
- id: '2',
1367
- attributes: {
1368
- body: 'i liked it'
1369
- },
1370
- links: {
1371
- self: '/comments/2'
1372
- },
1373
- relationships: {
1374
- post: {
1375
- links: {
1376
- self: '/comments/2/relationships/post',
1377
- related: '/comments/2/post'
1378
- }
1379
- }
1380
- }
1381
- },
1382
- {
1383
- type: 'comments',
1384
- id: '3',
1385
- attributes: {
1386
- body: 'Thanks man. Great post. But what is JR?'
1387
- },
1388
- links: {
1389
- self: '/comments/3'
1390
- },
1391
- relationships: {
1392
- post: {
1393
- links: {
1394
- self: '/comments/3/relationships/post',
1395
- related: '/comments/3/post'
1396
- }
1397
- }
1398
- }
1399
- }
1400
- ]
1401
- },
1402
- JSONAPI::ResourceSerializer.new(PostResource,
1403
- include: ['comments', 'author', 'comments.tags', 'author.posts'],
1404
- fields: {
1405
- people: [:id, :email, :comments],
1406
- posts: [:id, :title],
1407
- tags: [:name],
1408
- comments: [:id, :body, :post]
1409
- }).serialize_to_hash(posts)
1410
- )
1411
- end
1412
-
1413
- def test_serializer_camelized_with_value_formatters
1414
- assert_hash_equals(
1415
- {
1416
- data: {
1417
- type: 'expenseEntries',
1418
- id: '1',
1419
- attributes: {
1420
- transactionDate: '04/15/2014',
1421
- cost: 12.05
1422
- },
1423
- links: {
1424
- self: '/expenseEntries/1'
1425
- },
1426
- relationships: {
1427
- isoCurrency: {
1428
- links: {
1429
- self: '/expenseEntries/1/relationships/isoCurrency',
1430
- related: '/expenseEntries/1/isoCurrency'
1431
- },
1432
- data: {
1433
- type: 'isoCurrencies',
1434
- id: 'USD'
1435
- }
1436
- },
1437
- employee: {
1438
- links: {
1439
- self: '/expenseEntries/1/relationships/employee',
1440
- related: '/expenseEntries/1/employee'
1441
- },
1442
- data: {
1443
- type: 'people',
1444
- id: '3'
1445
- }
1446
- }
1447
- }
1448
- },
1449
- included: [
1450
- {
1451
- type: 'isoCurrencies',
1452
- id: 'USD',
1453
- attributes: {
1454
- countryName: 'United States',
1455
- name: 'United States Dollar',
1456
- minorUnit: 'cent'
1457
- },
1458
- links: {
1459
- self: '/isoCurrencies/USD'
1460
- }
1461
- },
1462
- {
1463
- type: 'people',
1464
- id: '3',
1465
- attributes: {
1466
- email: 'lazy@xyz.fake',
1467
- name: 'Lazy Author',
1468
- dateJoined: '2013-10-31 17:25:00 -0400'
1469
- },
1470
- links: {
1471
- self: '/people/3',
1472
- }
1473
- }
1474
- ]
1475
- },
1476
- JSONAPI::ResourceSerializer.new(ExpenseEntryResource,
1477
- include: ['iso_currency', 'employee'],
1478
- fields: {people: [:id, :name, :email, :date_joined]}).serialize_to_hash(
1479
- ExpenseEntryResource.new(@expense_entry, nil))
1480
- )
1481
- end
1482
-
1483
- def test_serializer_empty_links_null_and_array
1484
- planet_hash = JSONAPI::ResourceSerializer.new(PlanetResource).serialize_to_hash(
1485
- PlanetResource.new(Planet.find(8), nil))
1486
-
1487
- assert_hash_equals(
1488
- {
1489
- data: {
1490
- type: 'planets',
1491
- id: '8',
1492
- attributes: {
1493
- name: 'Beta W',
1494
- description: 'Newly discovered Planet W'
1495
- },
1496
- links: {
1497
- self: '/planets/8'
1498
- },
1499
- relationships: {
1500
- planetType: {
1501
- links: {
1502
- self: '/planets/8/relationships/planetType',
1503
- related: '/planets/8/planetType'
1504
- }
1505
- },
1506
- tags: {
1507
- links: {
1508
- self: '/planets/8/relationships/tags',
1509
- related: '/planets/8/tags'
1510
- }
1511
- },
1512
- moons: {
1513
- links: {
1514
- self: '/planets/8/relationships/moons',
1515
- related: '/planets/8/moons'
1516
- }
1517
- }
1518
- }
1519
- }
1520
- }, planet_hash)
1521
- end
1522
-
1523
- def test_serializer_include_with_empty_links_null_and_array
1524
- planets = []
1525
- Planet.find(7, 8).each do |planet|
1526
- planets.push PlanetResource.new(planet, nil)
1527
- end
1528
-
1529
- planet_hash = JSONAPI::ResourceSerializer.new(PlanetResource,
1530
- include: ['planet_type'],
1531
- fields: { planet_types: [:id, :name] }).serialize_to_hash(planets)
1532
-
1533
- assert_hash_equals(
1534
- {
1535
- data: [{
1536
- type: 'planets',
1537
- id: '7',
1538
- attributes: {
1539
- name: 'Beta X',
1540
- description: 'Newly discovered Planet Z'
1541
- },
1542
- links: {
1543
- self: '/planets/7'
1544
- },
1545
- relationships: {
1546
- planetType: {
1547
- links: {
1548
- self: '/planets/7/relationships/planetType',
1549
- related: '/planets/7/planetType'
1550
- },
1551
- data: {
1552
- type: 'planetTypes',
1553
- id: '5'
1554
- }
1555
- },
1556
- tags: {
1557
- links: {
1558
- self: '/planets/7/relationships/tags',
1559
- related: '/planets/7/tags'
1560
- }
1561
- },
1562
- moons: {
1563
- links: {
1564
- self: '/planets/7/relationships/moons',
1565
- related: '/planets/7/moons'
1566
- }
1567
- }
1568
- }
1569
- },
1570
- {
1571
- type: 'planets',
1572
- id: '8',
1573
- attributes: {
1574
- name: 'Beta W',
1575
- description: 'Newly discovered Planet W'
1576
- },
1577
- links: {
1578
- self: '/planets/8'
1579
- },
1580
- relationships: {
1581
- planetType: {
1582
- links: {
1583
- self: '/planets/8/relationships/planetType',
1584
- related: '/planets/8/planetType'
1585
- },
1586
- data: nil
1587
- },
1588
- tags: {
1589
- links: {
1590
- self: '/planets/8/relationships/tags',
1591
- related: '/planets/8/tags'
1592
- }
1593
- },
1594
- moons: {
1595
- links: {
1596
- self: '/planets/8/relationships/moons',
1597
- related: '/planets/8/moons'
1598
- }
1599
- }
1600
- }
1601
- }
1602
- ],
1603
- included: [
1604
- {
1605
- type: 'planetTypes',
1606
- id: '5',
1607
- attributes: {
1608
- name: 'unknown'
1609
- },
1610
- links: {
1611
- self: '/planetTypes/5'
1612
- }
1613
- }
1614
- ]
1615
- }, planet_hash)
1616
- end
1617
-
1618
- def test_serializer_booleans
1619
- original_config = JSONAPI.configuration.dup
1620
- JSONAPI.configuration.json_key_format = :underscored_key
1621
-
1622
- preferences = PreferencesResource.new(Preferences.find(1), nil)
1623
-
1624
- assert_hash_equals(
1625
- {
1626
- data: {
1627
- type: 'preferences',
1628
- id: '1',
1629
- attributes: {
1630
- advanced_mode: false
1631
- },
1632
- links: {
1633
- self: '/preferences/1'
1634
- },
1635
- relationships: {
1636
- author: {
1637
- links: {
1638
- self: '/preferences/1/relationships/author',
1639
- related: '/preferences/1/author'
1640
- }
1641
- }
1642
- }
1643
- }
1644
- },
1645
- JSONAPI::ResourceSerializer.new(PreferencesResource).serialize_to_hash(preferences)
1646
- )
1647
- ensure
1648
- JSONAPI.configuration = original_config
1649
- end
1650
-
1651
- def test_serializer_data_types
1652
- original_config = JSONAPI.configuration.dup
1653
- JSONAPI.configuration.json_key_format = :underscored_key
1654
-
1655
- facts = FactResource.new(Fact.find(1), nil)
1656
-
1657
- assert_hash_equals(
1658
- {
1659
- data: {
1660
- type: 'facts',
1661
- id: '1',
1662
- attributes: {
1663
- spouse_name: 'Jane Author',
1664
- bio: 'First man to run across Antartica.',
1665
- quality_rating: 23.89/45.6,
1666
- salary: BigDecimal('47000.56', 30),
1667
- date_time_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'),
1668
- birthday: Date.parse('1965-06-30'),
1669
- bedtime: Time.parse('2000-01-01 20:00:00 UTC +00:00'), #DB seems to set the date to 2001-01-01 for time types
1670
- photo: "abc",
1671
- cool: false
1672
- },
1673
- links: {
1674
- self: '/facts/1'
1675
- }
1676
- }
1677
- },
1678
- JSONAPI::ResourceSerializer.new(FactResource).serialize_to_hash(facts)
1679
- )
1680
- ensure
1681
- JSONAPI.configuration = original_config
1682
- end
1683
-
1684
- def test_serializer_to_one
1685
- serialized = JSONAPI::ResourceSerializer.new(
1686
- Api::V5::AuthorResource,
1687
- include: ['author_detail']
1688
- ).serialize_to_hash(Api::V5::AuthorResource.new(Person.find(1), nil))
1689
-
1690
- assert_hash_equals(
1691
- {
1692
- data: {
1693
- type: 'authors',
1694
- id: '1',
1695
- attributes: {
1696
- name: 'Joe Author',
1697
- },
1698
- links: {
1699
- self: '/api/v5/authors/1'
1700
- },
1701
- relationships: {
1702
- posts: {
1703
- links: {
1704
- self: '/api/v5/authors/1/relationships/posts',
1705
- related: '/api/v5/authors/1/posts'
1706
- }
1707
- },
1708
- authorDetail: {
1709
- links: {
1710
- self: '/api/v5/authors/1/relationships/authorDetail',
1711
- related: '/api/v5/authors/1/authorDetail'
1712
- },
1713
- data: {type: 'authorDetails', id: '1'}
1714
- }
1715
- }
1716
- },
1717
- included: [
1718
- {
1719
- type: 'authorDetails',
1720
- id: '1',
1721
- attributes: {
1722
- authorStuff: 'blah blah'
1723
- },
1724
- links: {
1725
- self: '/api/v5/authorDetails/1'
1726
- }
1727
- }
1728
- ]
1729
- },
1730
- serialized
1731
- )
1732
- end
1733
-
1734
- def test_serializer_resource_meta_fixed_value
1735
- Api::V5::AuthorResource.class_eval do
1736
- def meta(options)
1737
- {
1738
- fixed: 'Hardcoded value',
1739
- computed: "#{self.class._type.to_s}: #{options[:serializer].link_builder.self_link(self)}"
1740
- }
1741
- end
1742
- end
1743
-
1744
- serialized = JSONAPI::ResourceSerializer.new(
1745
- Api::V5::AuthorResource,
1746
- include: ['author_detail']
1747
- ).serialize_to_hash(Api::V5::AuthorResource.new(Person.find(1), nil))
1748
-
1749
- assert_hash_equals(
1750
- {
1751
- data: {
1752
- type: 'authors',
1753
- id: '1',
1754
- attributes: {
1755
- name: 'Joe Author',
1756
- },
1757
- links: {
1758
- self: '/api/v5/authors/1'
1759
- },
1760
- relationships: {
1761
- posts: {
1762
- links: {
1763
- self: '/api/v5/authors/1/relationships/posts',
1764
- related: '/api/v5/authors/1/posts'
1765
- }
1766
- },
1767
- authorDetail: {
1768
- links: {
1769
- self: '/api/v5/authors/1/relationships/authorDetail',
1770
- related: '/api/v5/authors/1/authorDetail'
1771
- },
1772
- data: {type: 'authorDetails', id: '1'}
1773
- }
1774
- },
1775
- meta: {
1776
- fixed: 'Hardcoded value',
1777
- computed: 'authors: /api/v5/authors/1'
1778
- }
1779
- },
1780
- included: [
1781
- {
1782
- type: 'authorDetails',
1783
- id: '1',
1784
- attributes: {
1785
- authorStuff: 'blah blah'
1786
- },
1787
- links: {
1788
- self: '/api/v5/authorDetails/1'
1789
- }
1790
- }
1791
- ]
1792
- },
1793
- serialized
1794
- )
1795
- ensure
1796
- Api::V5::AuthorResource.class_eval do
1797
- def meta(options)
1798
- # :nocov:
1799
- { }
1800
- # :nocov:
1801
- end
1802
- end
1803
- end
1804
-
1805
- def test_serialize_model_attr
1806
- @make = Make.first
1807
- serialized = JSONAPI::ResourceSerializer.new(
1808
- MakeResource,
1809
- ).serialize_to_hash(MakeResource.new(@make, nil))
1810
-
1811
- assert_hash_equals(
1812
- {
1813
- "model" => "A model attribute"
1814
- },
1815
- serialized[:data]["attributes"]
1816
- )
1817
- end
1818
-
1819
- def test_confusingly_named_attrs
1820
- @wp = WebPage.first
1821
- serialized = JSONAPI::ResourceSerializer.new(
1822
- WebPageResource,
1823
- ).serialize_to_hash(WebPageResource.new(@wp, nil))
1824
-
1825
- assert_hash_equals(
1826
- {
1827
- :data=>{
1828
- "id"=>"#{@wp.id}",
1829
- "type"=>"webPages",
1830
- "links"=>{
1831
- :self=>"/webPages/#{@wp.id}"
1832
- },
1833
- "attributes"=>{
1834
- "href"=>"http://example.com",
1835
- "link"=>"http://link.example.com"
1836
- }
1837
- }
1838
- },
1839
- serialized
1840
- )
1841
- end
1842
-
1843
- def test_questionable_has_one
1844
- # has_one
1845
- out, err = capture_io do
1846
- eval <<-CODE
1847
- class ::Questionable < ActiveRecord::Base
1848
- has_one :link
1849
- has_one :href
1850
- end
1851
- class ::QuestionableResource < JSONAPI::Resource
1852
- model_name '::Questionable'
1853
- has_one :link
1854
- has_one :href
1855
- end
1856
- cn = ::Questionable.new id: 1
1857
- puts JSONAPI::ResourceSerializer.new(
1858
- ::QuestionableResource,
1859
- ).serialize_to_hash(::QuestionableResource.new(cn, nil))
1860
- CODE
1861
- end
1862
- assert err.blank?
1863
- assert_equal(
1864
- {
1865
- :data=>{
1866
- "id"=>"1",
1867
- "type"=>"questionables",
1868
- "links"=>{
1869
- :self=>"/questionables/1"
1870
- },
1871
- "relationships"=>{
1872
- "link"=>{
1873
- :links=>{
1874
- :self=>"/questionables/1/relationships/link",
1875
- :related=>"/questionables/1/link"
1876
- }
1877
- },
1878
- "href"=>{
1879
- :links=>{
1880
- :self=>"/questionables/1/relationships/href",
1881
- :related=>"/questionables/1/href"
1882
- }
1883
- }
1884
- }
1885
- }
1886
- }.to_s,
1887
- out.strip
1888
- )
1889
- end
1890
-
1891
- def test_questionable_has_many
1892
- # has_one
1893
- out, err = capture_io do
1894
- eval <<-CODE
1895
- class ::Questionable2 < ActiveRecord::Base
1896
- self.table_name = 'questionables'
1897
- has_many :links
1898
- has_many :hrefs
1899
- end
1900
- class ::Questionable2Resource < JSONAPI::Resource
1901
- model_name '::Questionable2'
1902
- has_many :links
1903
- has_many :hrefs
1904
- end
1905
- cn = ::Questionable2.new id: 1
1906
- puts JSONAPI::ResourceSerializer.new(
1907
- ::Questionable2Resource,
1908
- ).serialize_to_hash(::Questionable2Resource.new(cn, nil))
1909
- CODE
1910
- end
1911
- assert err.blank?
1912
- assert_equal(
1913
- {
1914
- :data=>{
1915
- "id"=>"1",
1916
- "type"=>"questionable2s",
1917
- "links"=>{
1918
- :self=>"/questionable2s/1"
1919
- },
1920
- "relationships"=>{
1921
- "links"=>{
1922
- :links=>{
1923
- :self=>"/questionable2s/1/relationships/links",
1924
- :related=>"/questionable2s/1/links"
1925
- }
1926
- },
1927
- "hrefs"=>{
1928
- :links=>{
1929
- :self=>"/questionable2s/1/relationships/hrefs",
1930
- :related=>"/questionable2s/1/hrefs"
1931
- }
1932
- }
1933
- }
1934
- }
1935
- }.to_s,
1936
- out.strip
1937
- )
1938
- end
1939
- end