jsonapi-resources 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,429 +1,552 @@
1
1
  require File.expand_path('../../../test_helper', __FILE__)
2
2
  require File.expand_path('../../../fixtures/active_record', __FILE__)
3
- require 'jsonapi/resource_serializer'
4
-
3
+ require 'jsonapi-resources'
4
+ require 'json'
5
5
 
6
6
  class SerializerTest < MiniTest::Unit::TestCase
7
7
  def setup
8
8
  @post = Post.find(1)
9
9
  @fred = Person.find_by(name: 'Fred Reader')
10
+
11
+ @expense_entry = ExpenseEntry.find(1)
10
12
  end
11
13
 
12
14
  def test_serializer
13
- assert_hash_equals({
14
- posts: [{
15
- id: 1,
16
- title: 'New post',
17
- body: 'A body!!!',
18
- subject: 'New post',
19
- links: {
20
- section: nil,
21
- author: 1,
22
- tags: [1,2,3],
23
- comments: [1,2]
24
- }
25
- }]
26
- }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), nil, nil))
15
+
16
+ assert_hash_equals(
17
+ {
18
+ posts: {
19
+ id: 1,
20
+ title: 'New post',
21
+ body: 'A body!!!',
22
+ subject: 'New post',
23
+ links: {
24
+ section: nil,
25
+ author: 1,
26
+ tags: [1, 2, 3],
27
+ comments: [1, 2]
28
+ }
29
+ }
30
+ },
31
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
32
+ PostResource.new(@post)))
27
33
  end
28
34
 
29
35
  def test_serializer_limited_fieldset
30
- assert_hash_equals({
31
- posts: [{
36
+
37
+ assert_hash_equals(
38
+ {
39
+ posts: {
40
+ id: 1,
41
+ title: 'New post',
42
+ links: {
43
+ author: 1
44
+ }
45
+ }
46
+ },
47
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
48
+ PostResource.new(@post),
49
+ fields: {posts: [:id, :title, :author]}))
50
+ end
51
+
52
+ def test_serializer_include
53
+
54
+ assert_hash_equals(
55
+ {
56
+ posts: {
57
+ id: 1,
58
+ title: 'New post',
59
+ body: 'A body!!!',
60
+ subject: 'New post',
61
+ links: {
62
+ author: 1,
63
+ tags: [1, 2, 3],
64
+ comments: [1, 2],
65
+ section: nil
66
+ }
67
+ },
68
+ linked: {
69
+ people: [{
32
70
  id: 1,
33
- title: 'New post',
71
+ name: 'Joe Author',
72
+ email: 'joe@xyz.fake',
73
+ dateJoined: '2013-08-07 16:25:00 -0400',
34
74
  links: {
35
- author: 1
75
+ comments: [1],
76
+ posts: [1, 2, 11]
36
77
  }
37
78
  }]
38
- }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), nil,
39
- {posts: [:id, :title, :author]}))
79
+ }
80
+ },
81
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
82
+ PostResource.new(@post), include: [:author]))
40
83
  end
41
84
 
42
- def test_serializer_include
43
- assert_hash_equals({
44
- posts: [{
45
- id: 1,
46
- title: 'New post',
47
- body: 'A body!!!',
48
- subject: 'New post',
49
- links: {
50
- author: 1,
51
- tags: [1,2,3],
52
- comments: [1,2],
53
- section: nil
54
- }
55
- }],
56
- linked: {
57
- people: [{
58
- id: 1,
59
- name: 'Joe Author',
60
- email: 'joe@xyz.fake',
61
- date_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'),
62
- links: {
63
- comments: [1],
64
- posts: [1,2,11]
65
- }
66
- }]
67
- }
68
- }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), [:author], nil))
85
+ def test_serializer_key_format
86
+
87
+ assert_hash_equals(
88
+ {
89
+ posts: {
90
+ id: 1,
91
+ title: 'New post',
92
+ body: 'A body!!!',
93
+ subject: 'New post',
94
+ links: {
95
+ author: 1,
96
+ tags: [1, 2, 3],
97
+ comments: [1, 2],
98
+ section: nil
99
+ }
100
+ },
101
+ linked: {
102
+ people: [{
103
+ id: 1,
104
+ name: 'Joe Author',
105
+ email: 'joe@xyz.fake',
106
+ date_joined: '2013-08-07 16:25:00 -0400',
107
+ links: {
108
+ comments: [1],
109
+ posts: [1, 2, 11]
110
+ }
111
+ }]
112
+ }
113
+ },
114
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
115
+ PostResource.new(@post),
116
+ include: [:author],
117
+ key_formatter: UnderscoredKeyFormatter))
69
118
  end
70
119
 
71
120
  def test_serializer_include_sub_objects
121
+
72
122
  assert_hash_equals(
73
- {
74
- posts: [{
75
- id: 1,
76
- title: 'New post',
77
- body: 'A body!!!',
78
- subject: 'New post',
79
- links: {
80
- author: 1,
81
- tags: [1,2,3],
82
- comments: [1,2],
83
- section: nil
84
- }
85
- }],
86
- linked: {
87
- tags: [
88
- {
89
- id: 1,
90
- name: 'short',
91
- links: {
92
- posts: :not_nil
93
- }
94
- },
95
- {
96
- id: 2,
97
- name: 'whiny',
98
- links: {
99
- posts: :not_nil
100
- }
101
- },
102
- {
103
- id: 4,
104
- name: 'happy',
105
- links: {
106
- posts: :not_nil
107
- }
108
- }
109
- ],
110
- comments: [
111
- {
112
- id: 1,
113
- body: 'what a dumb post',
114
- links: {
115
- author: 1,
116
- post: 1,
117
- tags: [2, 1]
118
- }
119
- },
120
- {
121
- id: 2,
122
- body: 'i liked it',
123
- links: {
124
- author: 2,
125
- post: 1,
126
- tags: [4, 1]
127
- }
128
- }
129
- ]
123
+ {
124
+ posts: {
125
+ id: 1,
126
+ title: 'New post',
127
+ body: 'A body!!!',
128
+ subject: 'New post',
129
+ links: {
130
+ author: 1,
131
+ tags: [1, 2, 3],
132
+ comments: [1, 2],
133
+ section: nil
130
134
  }
131
- }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), [:comments,'comments.tags'], nil))
135
+ },
136
+ linked: {
137
+ tags: [
138
+ {
139
+ id: 1,
140
+ name: 'short',
141
+ links: {
142
+ posts: :not_nil
143
+ }
144
+ },
145
+ {
146
+ id: 2,
147
+ name: 'whiny',
148
+ links: {
149
+ posts: :not_nil
150
+ }
151
+ },
152
+ {
153
+ id: 4,
154
+ name: 'happy',
155
+ links: {
156
+ posts: :not_nil
157
+ }
158
+ }
159
+ ],
160
+ comments: [
161
+ {
162
+ id: 1,
163
+ body: 'what a dumb post',
164
+ links: {
165
+ author: 1,
166
+ post: 1,
167
+ tags: [2, 1]
168
+ }
169
+ },
170
+ {
171
+ id: 2,
172
+ body: 'i liked it',
173
+ links: {
174
+ author: 2,
175
+ post: 1,
176
+ tags: [4, 1]
177
+ }
178
+ }
179
+ ]
180
+ }
181
+ },
182
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
183
+ PostResource.new(@post), include: [:comments, 'comments.tags']))
132
184
  end
133
185
 
134
186
  def test_serializer_include_has_many_sub_objects_only
187
+
135
188
  assert_hash_equals(
136
- {
137
- posts: [{
138
- id: 1,
139
- title: 'New post',
140
- body: 'A body!!!',
141
- subject: 'New post',
142
- links: {
143
- author: 1,
144
- tags: [1,2,3],
145
- comments: [1,2],
146
- section: nil
147
- }
148
- }],
149
- linked: {
150
- tags: [
151
- {
152
- id: 1,
153
- name: 'short',
154
- links: {
155
- posts: :not_nil
156
- }
157
- },
158
- {
159
- id: 2,
160
- name: 'whiny',
161
- links: {
162
- posts: :not_nil
163
- }
164
- },
165
- {
166
- id: 4,
167
- name: 'happy',
168
- links: {
169
- posts: :not_nil
170
- }
171
- }
172
- ]
189
+ {
190
+ posts: {
191
+ id: 1,
192
+ title: 'New post',
193
+ body: 'A body!!!',
194
+ subject: 'New post',
195
+ links: {
196
+ author: 1,
197
+ tags: [1, 2, 3],
198
+ comments: [1, 2],
199
+ section: nil
173
200
  }
174
- }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), ['comments.tags'], nil))
201
+ },
202
+ linked: {
203
+ tags: [
204
+ {
205
+ id: 1,
206
+ name: 'short',
207
+ links: {
208
+ posts: :not_nil
209
+ }
210
+ },
211
+ {
212
+ id: 2,
213
+ name: 'whiny',
214
+ links: {
215
+ posts: :not_nil
216
+ }
217
+ },
218
+ {
219
+ id: 4,
220
+ name: 'happy',
221
+ links: {
222
+ posts: :not_nil
223
+ }
224
+ }
225
+ ]
226
+ }
227
+ },
228
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
229
+ PostResource.new(@post), include: ['comments.tags']))
175
230
  end
176
231
 
177
232
  def test_serializer_include_has_one_sub_objects_only
233
+
178
234
  assert_hash_equals(
179
- {
180
- posts: [{
181
- id: 1,
182
- title: 'New post',
183
- body: 'A body!!!',
184
- subject: 'New post',
185
- links: {
186
- author: 1,
187
- tags: [1,2,3],
188
- comments: [1,2],
189
- section: nil
190
- }
191
- }],
192
- linked: {
193
- comments: [
194
- {
195
- id: 1,
196
- body: 'what a dumb post',
197
- links: {
198
- author: 1,
199
- post: 1,
200
- tags: [2, 1]
201
- }
202
- }
203
- ]
235
+ {
236
+ posts: {
237
+ id: 1,
238
+ title: 'New post',
239
+ body: 'A body!!!',
240
+ subject: 'New post',
241
+ links: {
242
+ author: 1,
243
+ tags: [1, 2, 3],
244
+ comments: [1, 2],
245
+ section: nil
204
246
  }
205
- }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), ['author.comments'], nil))
247
+ },
248
+ linked: {
249
+ comments: [
250
+ {
251
+ id: 1,
252
+ body: 'what a dumb post',
253
+ links: {
254
+ author: 1,
255
+ post: 1,
256
+ tags: [2, 1]
257
+ }
258
+ }
259
+ ]
260
+ }
261
+ },
262
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
263
+ PostResource.new(@post), include: ['author.comments']))
206
264
  end
207
265
 
208
266
  def test_serializer_different_foreign_key
267
+
209
268
  assert_hash_equals(
210
- {
211
- people: [{
212
- id: 2,
213
- name: 'Fred Reader',
214
- email: 'fred@xyz.fake',
215
- date_joined: DateTime.parse('2013-10-31 20:25:00 UTC +00:00'),
216
- links: {
217
- posts: [],
218
- comments: [2,3]
219
- }
220
- }],
221
- linked: {
222
- comments: [{
223
- id: 2,
224
- body: 'i liked it',
225
- links: {
226
- author: 2,
227
- post: 1,
228
- tags: [4, 1]
229
- }
230
- },
231
- {
232
- id: 3,
233
- body: 'Thanks man. Great post. But what is JR?',
234
- links: {
235
- author: 2,
236
- post: 2,
237
- tags: [5]
238
- }
239
- }
240
- ]
269
+ {
270
+ people: {
271
+ id: 2,
272
+ name: 'Fred Reader',
273
+ email: 'fred@xyz.fake',
274
+ dateJoined: '2013-10-31 16:25:00 -0400',
275
+ links: {
276
+ posts: [],
277
+ comments: [2, 3]
241
278
  }
242
- }, JSONAPI::ResourceSerializer.new.serialize(PersonResource.new(@fred), ['comments'], nil))
279
+ },
280
+ linked: {
281
+ comments: [{
282
+ id: 2,
283
+ body: 'i liked it',
284
+ links: {
285
+ author: 2,
286
+ post: 1,
287
+ tags: [4, 1]
288
+ }
289
+ },
290
+ {
291
+ id: 3,
292
+ body: 'Thanks man. Great post. But what is JR?',
293
+ links: {
294
+ author: 2,
295
+ post: 2,
296
+ tags: [5]
297
+ }
298
+ }
299
+ ]
300
+ }
301
+ },
302
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
303
+ PersonResource.new(@fred), include: ['comments']))
243
304
  end
244
305
 
245
306
  def test_serializer_array_of_resources
246
307
 
247
308
  posts = []
248
- Post.find(1,2).each do |post|
309
+ Post.find(1, 2).each do |post|
249
310
  posts.push PostResource.new(post)
250
311
  end
251
312
 
252
313
  assert_hash_equals(
253
- {
254
- posts: [{
255
- id: 1,
256
- title: 'New post',
257
- body: 'A body!!!',
258
- subject: 'New post',
259
- links: {
260
- author: 1,
261
- tags: [1,2,3],
262
- comments: [1,2],
263
- section: nil
264
- }
265
- },
266
- {
267
- id: 2,
268
- title: 'JR Solves your serialization woes!',
269
- body: 'Use JR',
270
- subject: 'JR Solves your serialization woes!',
271
- links: {
272
- author: 1,
273
- tags: [5],
274
- comments: [3],
275
- section: 3
276
- }
277
- }],
278
- linked: {
279
- tags: [
280
- {
314
+ {
315
+ posts: [{
281
316
  id: 1,
282
- name: 'short',
317
+ title: 'New post',
318
+ body: 'A body!!!',
319
+ subject: 'New post',
283
320
  links: {
284
- posts: :not_nil
321
+ author: 1,
322
+ tags: [1, 2, 3],
323
+ comments: [1, 2],
324
+ section: nil
285
325
  }
286
326
  },
287
327
  {
288
328
  id: 2,
289
- name: 'whiny',
329
+ title: 'JR Solves your serialization woes!',
330
+ body: 'Use JR',
331
+ subject: 'JR Solves your serialization woes!',
290
332
  links: {
291
- posts: :not_nil
333
+ author: 1,
334
+ tags: [5],
335
+ comments: [3],
336
+ section: 3
292
337
  }
293
- },
294
- {
295
- id: 4,
296
- name: 'happy',
338
+ }],
339
+ linked: {
340
+ tags: [
341
+ {
342
+ id: 1,
343
+ name: 'short',
344
+ links: {
345
+ posts: :not_nil
346
+ }
347
+ },
348
+ {
349
+ id: 2,
350
+ name: 'whiny',
351
+ links: {
352
+ posts: :not_nil
353
+ }
354
+ },
355
+ {
356
+ id: 4,
357
+ name: 'happy',
358
+ links: {
359
+ posts: :not_nil
360
+ }
361
+ },
362
+ {
363
+ id: 5,
364
+ name: 'JR',
365
+ links: {
366
+ posts: [2, 11]
367
+ }
368
+ }
369
+ ],
370
+ comments: [
371
+ {
372
+ id: 1,
373
+ body: 'what a dumb post',
374
+ links: {
375
+ author: 1,
376
+ post: 1,
377
+ tags: [2, 1]
378
+ }
379
+ },
380
+ {
381
+ id: 2,
382
+ body: 'i liked it',
383
+ links: {
384
+ author: 2,
385
+ post: 1,
386
+ tags: [4, 1]
387
+ }
388
+ },
389
+ {
390
+ id: 3,
391
+ body: 'Thanks man. Great post. But what is JR?',
392
+ links: {
393
+ author: 2,
394
+ post: 2,
395
+ tags: [5]
396
+ }
397
+ }
398
+ ]
399
+ }
400
+ },
401
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
402
+ posts, include: ['comments', 'comments.tags']))
403
+ end
404
+
405
+ def test_serializer_array_of_resources_limited_fields
406
+
407
+ posts = []
408
+ Post.find(1, 2).each do |post|
409
+ posts.push PostResource.new(post)
410
+ end
411
+
412
+ assert_hash_equals(
413
+ {
414
+ posts: [{
415
+ id: 1,
416
+ title: 'New post',
297
417
  links: {
298
- posts: :not_nil
418
+ author: 1
299
419
  }
300
420
  },
301
421
  {
302
- id: 5,
303
- name: 'JR',
422
+ id: 2,
423
+ title: 'JR Solves your serialization woes!',
304
424
  links: {
305
- posts: [2,11]
425
+ author: 1
306
426
  }
307
- }
308
- ],
309
- comments: [
310
- {
311
- id: 1,
312
- body: 'what a dumb post',
313
- links: {
314
- author: 1,
315
- post: 1,
316
- tags: [2, 1]
317
- }
318
- },
319
- {
320
- id: 2,
321
- body: 'i liked it',
322
- links: {
323
- author: 2,
324
- post: 1,
325
- tags: [4, 1]
326
- }
327
- },
328
- {
329
- id: 3,
330
- body: 'Thanks man. Great post. But what is JR?',
331
- links: {
332
- author: 2,
333
- post: 2,
334
- tags: [5]
335
- }
336
- }
337
- ]
338
- }
339
- }, JSONAPI::ResourceSerializer.new.serialize(posts, ['comments','comments.tags'], nil))
427
+ }],
428
+ linked: {
429
+ tags: [
430
+ {
431
+ name: 'short'
432
+ },
433
+ {
434
+ name: 'whiny'
435
+ },
436
+ {
437
+ name: 'happy'
438
+ },
439
+ {
440
+ name: 'JR'
441
+ }
442
+ ],
443
+ comments: [
444
+ {
445
+ id: 1,
446
+ body: 'what a dumb post',
447
+ links: {
448
+ post: 1
449
+ }
450
+ },
451
+ {
452
+ id: 2,
453
+ body: 'i liked it',
454
+ links: {
455
+ post: 1
456
+ }
457
+ },
458
+ {
459
+ id: 3,
460
+ body: 'Thanks man. Great post. But what is JR?',
461
+ links: {
462
+ post: 2
463
+ }
464
+ }
465
+ ],
466
+ posts: [
467
+ {
468
+ id: 11,
469
+ title: 'JR How To',
470
+ links: {
471
+ author: 1
472
+ }
473
+ }
474
+ ],
475
+ people: [
476
+ {
477
+ id: 1,
478
+ email: 'joe@xyz.fake',
479
+ links: {
480
+ comments: [1]
481
+ }
482
+ }]
483
+ }
484
+ },
485
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
486
+ posts,
487
+ include: ['comments', 'author', 'comments.tags', 'author.posts'],
488
+ fields: {
489
+ people: [:id, :email, :comments],
490
+ posts: [:id, :title, :author],
491
+ tags: [:name],
492
+ comments: [:id, :body, :post]
493
+ }))
340
494
  end
341
495
 
342
- def test_serializer_array_of_resources_limited_fields
496
+ def test_serializer_camelized_with_value_formatters
497
+ assert_hash_equals(
498
+ {
499
+ expenseEntries: {
500
+ id: 1,
501
+ transactionDate: '04/15/2014',
502
+ cost: '12.05',
503
+ links: {
504
+ isoCurrency: 'USD',
505
+ employee: 3
506
+ }
507
+ },
508
+ linked: {
509
+ isoCurrencies: [{
510
+ code: 'USD',
511
+ countryName: 'United States',
512
+ name: 'United States Dollar',
513
+ minorUnit: 'cent'
514
+ }],
515
+ people: [{
516
+ id: 3,
517
+ name: 'Lazy Author',
518
+ email: 'lazy@xyz.fake',
519
+ dateJoined: '2013-10-31 17:25:00 -0400',
520
+ }]
521
+ }
522
+ },
523
+ JSONAPI::ResourceSerializer.new.serialize_to_hash(
524
+ ExpenseEntryResource.new(@expense_entry),
525
+ include: ['iso_currency', 'employee'],
526
+ fields: {people: [:id, :name, :email, :date_joined]}
527
+ )
528
+ )
529
+ end
343
530
 
344
- posts = []
345
- Post.find(1,2).each do |post|
346
- posts.push PostResource.new(post)
347
- end
531
+ def test_serializer_empty_links_null_and_array
532
+ planet_hash = JSONAPI::ResourceSerializer.new.serialize_to_hash(PlanetResource.new(Planet.find(8)))
348
533
 
349
534
  assert_hash_equals(
350
- {
351
- posts: [{
352
- id: 1,
353
- title: 'New post',
354
- links: {
355
- author: 1
356
- }
357
- },
358
- {
359
- id: 2,
360
- title: 'JR Solves your serialization woes!',
361
- links: {
362
- author: 1
363
- }
364
- }],
365
- linked: {
366
- tags: [
367
- {
368
- name: 'short'
369
- },
370
- {
371
- name: 'whiny'
372
- },
373
- {
374
- name: 'happy'
375
- },
376
- {
377
- name: 'JR'
378
- }
379
- ],
380
- comments: [
381
- {
382
- id: 1,
383
- body: 'what a dumb post',
384
- links: {
385
- post: 1
386
- }
387
- },
388
- {
389
- id: 2,
390
- body: 'i liked it',
391
- links: {
392
- post: 1
393
- }
394
- },
395
- {
396
- id: 3,
397
- body: 'Thanks man. Great post. But what is JR?',
398
- links: {
399
- post: 2
400
- }
401
- }
402
- ],
403
- posts: [
404
- {
405
- id: 11,
406
- title: 'JR How To',
407
- links: {
408
- author: 1
409
- }
410
- }
411
- ],
412
- people: [
413
- {
414
- id: 1,
415
- email: 'joe@xyz.fake',
416
- links: {
417
- comments: [1]
418
- }
419
- }]
420
- }
421
- }, JSONAPI::ResourceSerializer.new.serialize(posts, ['comments','author','comments.tags','author.posts'],
422
- {
423
- people: [:id, :email, :comments],
424
- posts: [:id, :title, :author],
425
- tags: [:name],
426
- comments: [:id, :body, :post]
427
- }))
535
+ {
536
+ planets: {
537
+ id: 8,
538
+ name: 'Beta W',
539
+ description: 'Newly discovered Planet W',
540
+ links: {
541
+ planetType: nil,
542
+ tags: [],
543
+ moons: []
544
+ }
545
+ }
546
+ }, planet_hash)
547
+
548
+ json = planet_hash.to_json
549
+ assert_match /\"planetType\":null/, json
550
+ assert_match /\"moons\":\[\]/, json
428
551
  end
429
552
  end