jsonapi-resources 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/README.md +186 -45
- data/lib/jsonapi/association.rb +6 -20
- data/lib/jsonapi/configuration.rb +27 -0
- data/lib/jsonapi/error_codes.rb +2 -0
- data/lib/jsonapi/exceptions.rb +41 -0
- data/lib/jsonapi/formatter.rb +94 -0
- data/lib/jsonapi/operation.rb +46 -9
- data/lib/jsonapi/request.rb +178 -48
- data/lib/jsonapi/resource.rb +51 -84
- data/lib/jsonapi/resource_controller.rb +53 -25
- data/lib/jsonapi/resource_for.rb +4 -6
- data/lib/jsonapi/resource_serializer.rb +59 -31
- data/lib/jsonapi/resources/version.rb +1 -1
- data/lib/jsonapi/routing_ext.rb +9 -1
- data/lib/jsonapi-resources.rb +2 -0
- data/test/controllers/controller_test.rb +706 -335
- data/test/fixtures/active_record.rb +60 -24
- data/test/integration/requests/request_test.rb +10 -15
- data/test/integration/routes/routes_test.rb +68 -20
- data/test/test_helper.rb +45 -3
- data/test/unit/operation/operations_processor_test.rb +49 -7
- data/test/unit/resource/resource_test.rb +2 -2
- data/test/unit/serializer/serializer_test.rb +483 -360
- metadata +5 -2
@@ -5,21 +5,25 @@ class PostsControllerTest < ActionController::TestCase
|
|
5
5
|
def test_index
|
6
6
|
get :index
|
7
7
|
assert_response :success
|
8
|
+
assert json_response['posts'].is_a?(Array)
|
8
9
|
end
|
9
10
|
|
10
11
|
def test_index_filter_by_id
|
11
12
|
get :index, {id: '1'}
|
12
13
|
assert_response :success
|
14
|
+
assert json_response['posts'].is_a?(Array)
|
13
15
|
end
|
14
16
|
|
15
17
|
def test_index_filter_by_title
|
16
18
|
get :index, {title: 'New post'}
|
17
19
|
assert_response :success
|
20
|
+
assert json_response['posts'].is_a?(Array)
|
18
21
|
end
|
19
22
|
|
20
23
|
def test_index_filter_by_ids
|
21
24
|
get :index, {ids: '1,2'}
|
22
25
|
assert_response :success
|
26
|
+
assert json_response['posts'].is_a?(Array)
|
23
27
|
assert_equal 2, json_response['posts'].size
|
24
28
|
end
|
25
29
|
|
@@ -162,22 +166,22 @@ class PostsControllerTest < ActionController::TestCase
|
|
162
166
|
def test_show_single
|
163
167
|
get :show, {id: '1'}
|
164
168
|
assert_response :success
|
165
|
-
|
166
|
-
assert_equal 'New post', json_response['posts'][
|
167
|
-
assert_equal 'A body!!!', json_response['posts'][
|
168
|
-
assert_equal [1,2,3], json_response['posts'][
|
169
|
-
assert_equal [1,2], json_response['posts'][
|
169
|
+
assert json_response['posts'].is_a?(Hash)
|
170
|
+
assert_equal 'New post', json_response['posts']['title']
|
171
|
+
assert_equal 'A body!!!', json_response['posts']['body']
|
172
|
+
assert_equal [1, 2, 3], json_response['posts']['links']['tags']
|
173
|
+
assert_equal [1, 2], json_response['posts']['links']['comments']
|
170
174
|
assert_nil json_response['linked']
|
171
175
|
end
|
172
176
|
|
173
177
|
def test_show_single_with_includes
|
174
178
|
get :show, {id: '1', include: 'comments'}
|
175
179
|
assert_response :success
|
176
|
-
|
177
|
-
assert_equal 'New post', json_response['posts'][
|
178
|
-
assert_equal 'A body!!!', json_response['posts'][
|
179
|
-
assert_equal [1,2,3], json_response['posts'][
|
180
|
-
assert_equal [1,2], json_response['posts'][
|
180
|
+
assert json_response['posts'].is_a?(Hash)
|
181
|
+
assert_equal 'New post', json_response['posts']['title']
|
182
|
+
assert_equal 'A body!!!', json_response['posts']['body']
|
183
|
+
assert_equal [1, 2, 3], json_response['posts']['links']['tags']
|
184
|
+
assert_equal [1, 2], json_response['posts']['links']['comments']
|
181
185
|
assert_equal 2, json_response['linked']['comments'].size
|
182
186
|
assert_nil json_response['linked']['tags']
|
183
187
|
end
|
@@ -185,10 +189,10 @@ class PostsControllerTest < ActionController::TestCase
|
|
185
189
|
def test_show_single_with_fields
|
186
190
|
get :show, {id: '1', fields: 'author'}
|
187
191
|
assert_response :success
|
188
|
-
|
189
|
-
assert_nil json_response['posts'][
|
190
|
-
assert_nil json_response['posts'][
|
191
|
-
assert_equal 1, json_response['posts'][
|
192
|
+
assert json_response['posts'].is_a?(Hash)
|
193
|
+
assert_nil json_response['posts']['title']
|
194
|
+
assert_nil json_response['posts']['body']
|
195
|
+
assert_equal 1, json_response['posts']['links']['author']
|
192
196
|
end
|
193
197
|
|
194
198
|
def test_show_single_invalid_id_format
|
@@ -216,31 +220,35 @@ class PostsControllerTest < ActionController::TestCase
|
|
216
220
|
end
|
217
221
|
|
218
222
|
def test_create_simple
|
219
|
-
post :create,
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
223
|
+
post :create,
|
224
|
+
{
|
225
|
+
posts: {
|
226
|
+
title: 'JR is Great',
|
227
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
228
|
+
links: {
|
229
|
+
author: 3
|
230
|
+
}
|
231
|
+
}
|
232
|
+
}
|
227
233
|
|
228
234
|
assert_response :created
|
229
|
-
|
230
|
-
assert_equal 3, json_response['posts'][
|
231
|
-
assert_equal 'JR is Great', json_response['posts'][
|
232
|
-
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts'][
|
235
|
+
assert json_response['posts'].is_a?(Hash)
|
236
|
+
assert_equal 3, json_response['posts']['links']['author']
|
237
|
+
assert_equal 'JR is Great', json_response['posts']['title']
|
238
|
+
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts']['body']
|
233
239
|
end
|
234
240
|
|
235
241
|
def test_create_link_to_missing_object
|
236
|
-
post :create,
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
242
|
+
post :create,
|
243
|
+
{
|
244
|
+
posts: {
|
245
|
+
title: 'JR is Great',
|
246
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
247
|
+
links: {
|
248
|
+
author: 304567
|
249
|
+
}
|
250
|
+
}
|
251
|
+
}
|
244
252
|
|
245
253
|
assert_response :bad_request
|
246
254
|
# Todo: check if this validation is working
|
@@ -248,112 +256,150 @@ class PostsControllerTest < ActionController::TestCase
|
|
248
256
|
end
|
249
257
|
|
250
258
|
def test_create_extra_param
|
251
|
-
post :create,
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
259
|
+
post :create,
|
260
|
+
{
|
261
|
+
posts: {
|
262
|
+
asdfg: 'aaaa',
|
263
|
+
title: 'JR is Great',
|
264
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
265
|
+
links: {
|
266
|
+
author: 3
|
267
|
+
}
|
268
|
+
}
|
269
|
+
}
|
260
270
|
|
261
271
|
assert_response :bad_request
|
262
272
|
assert_match /asdfg is not allowed/, response.body
|
263
273
|
end
|
264
274
|
|
265
275
|
def test_create_multiple
|
266
|
-
post :create,
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
276
|
+
post :create,
|
277
|
+
{
|
278
|
+
posts: [
|
279
|
+
{
|
280
|
+
title: 'JR is Great',
|
281
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
282
|
+
links: {
|
283
|
+
author: 3
|
284
|
+
}
|
285
|
+
},
|
286
|
+
{
|
287
|
+
title: 'Ember is Great',
|
288
|
+
body: 'Ember is the greatest thing since unsliced bread.',
|
289
|
+
links: {
|
290
|
+
author: 3
|
291
|
+
}
|
292
|
+
}
|
293
|
+
]
|
294
|
+
}
|
283
295
|
|
284
296
|
assert_response :created
|
297
|
+
assert json_response['posts'].is_a?(Array)
|
285
298
|
assert_equal json_response['posts'].size, 2
|
286
299
|
assert_equal json_response['posts'][0]['links']['author'], 3
|
287
300
|
assert_match /JR is Great/, response.body
|
288
301
|
assert_match /Ember is Great/, response.body
|
289
302
|
end
|
290
303
|
|
304
|
+
def test_create_multiple_wrong_case
|
305
|
+
post :create,
|
306
|
+
{
|
307
|
+
posts: [
|
308
|
+
{
|
309
|
+
Title: 'JR is Great',
|
310
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
311
|
+
links: {
|
312
|
+
author: 3
|
313
|
+
}
|
314
|
+
},
|
315
|
+
{
|
316
|
+
title: 'Ember is Great',
|
317
|
+
BODY: 'Ember is the greatest thing since unsliced bread.',
|
318
|
+
links: {
|
319
|
+
author: 3
|
320
|
+
}
|
321
|
+
}
|
322
|
+
]
|
323
|
+
}
|
324
|
+
|
325
|
+
assert_response :bad_request
|
326
|
+
assert_match /Title/, json_response['errors'][0]['detail']
|
327
|
+
end
|
328
|
+
|
291
329
|
def test_create_simple_missing_posts
|
292
|
-
post :create,
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
330
|
+
post :create,
|
331
|
+
{
|
332
|
+
posts_spelled_wrong: {
|
333
|
+
title: 'JR is Great',
|
334
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
335
|
+
links: {
|
336
|
+
author: 3
|
337
|
+
}
|
338
|
+
}
|
339
|
+
}
|
300
340
|
|
301
341
|
assert_response :bad_request
|
302
342
|
assert_match /The required parameter, posts, is missing./, json_response['errors'][0]['detail']
|
303
343
|
end
|
304
344
|
|
305
345
|
def test_create_simple_unpermitted_attributes
|
306
|
-
post :create,
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
346
|
+
post :create,
|
347
|
+
{
|
348
|
+
posts: {
|
349
|
+
subject: 'JR is Great',
|
350
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
351
|
+
links: {
|
352
|
+
author: 3
|
353
|
+
}
|
354
|
+
}
|
355
|
+
}
|
314
356
|
|
315
357
|
assert_response :bad_request
|
316
358
|
assert_match /subject/, json_response['errors'][0]['detail']
|
317
359
|
end
|
318
360
|
|
319
361
|
def test_create_with_links
|
320
|
-
post :create,
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
362
|
+
post :create,
|
363
|
+
{
|
364
|
+
posts: {
|
365
|
+
title: 'JR is Great',
|
366
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
367
|
+
links: {
|
368
|
+
author: 3,
|
369
|
+
tags: [3, 4]
|
370
|
+
}
|
371
|
+
}
|
372
|
+
}
|
329
373
|
|
330
374
|
assert_response :created
|
331
|
-
|
332
|
-
assert_equal 3, json_response['posts'][
|
333
|
-
assert_equal 'JR is Great', json_response['posts'][
|
334
|
-
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts'][
|
335
|
-
assert_equal [3,4], json_response['posts'][
|
375
|
+
assert json_response['posts'].is_a?(Hash)
|
376
|
+
assert_equal 3, json_response['posts']['links']['author']
|
377
|
+
assert_equal 'JR is Great', json_response['posts']['title']
|
378
|
+
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts']['body']
|
379
|
+
assert_equal [3, 4], json_response['posts']['links']['tags']
|
336
380
|
end
|
337
381
|
|
338
382
|
def test_create_with_links_include_and_fields
|
339
|
-
post :create,
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
383
|
+
post :create,
|
384
|
+
{
|
385
|
+
posts: {
|
386
|
+
title: 'JR is Great!',
|
387
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread!',
|
388
|
+
links: {
|
389
|
+
author: 3,
|
390
|
+
tags: [3, 4]
|
391
|
+
}
|
392
|
+
},
|
393
|
+
include: 'author,author.posts',
|
394
|
+
fields: 'id,title,author'
|
395
|
+
}
|
350
396
|
|
351
397
|
assert_response :created
|
352
|
-
|
353
|
-
assert_equal 3, json_response['posts'][
|
354
|
-
assert_equal 'JR is Great!', json_response['posts'][
|
355
|
-
assert_equal nil, json_response['posts'][
|
356
|
-
assert_equal nil, json_response['posts'][
|
398
|
+
assert json_response['posts'].is_a?(Hash)
|
399
|
+
assert_equal 3, json_response['posts']['links']['author']
|
400
|
+
assert_equal 'JR is Great!', json_response['posts']['title']
|
401
|
+
assert_equal nil, json_response['posts']['body']
|
402
|
+
assert_equal nil, json_response['posts']['links']['tags']
|
357
403
|
assert_not_nil json_response['linked']['posts']
|
358
404
|
assert_not_nil json_response['linked']['people']
|
359
405
|
assert_nil json_response['linked']['tags']
|
@@ -362,111 +408,201 @@ class PostsControllerTest < ActionController::TestCase
|
|
362
408
|
def test_update_with_links
|
363
409
|
javascript = Section.find_by(name: 'javascript')
|
364
410
|
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
411
|
+
put :update,
|
412
|
+
{
|
413
|
+
id: 3,
|
414
|
+
posts: {
|
415
|
+
title: 'A great new Post',
|
416
|
+
links: {
|
417
|
+
section: javascript.id,
|
418
|
+
tags: [3, 4]
|
419
|
+
}
|
420
|
+
}
|
370
421
|
}
|
371
|
-
}
|
372
|
-
}
|
373
422
|
|
374
423
|
assert_response :success
|
375
|
-
|
376
|
-
assert_equal 3, json_response['posts'][
|
377
|
-
assert_equal
|
378
|
-
assert_equal 'A great new Post', json_response['posts'][
|
379
|
-
assert_equal 'AAAA', json_response['posts'][
|
380
|
-
assert matches_array?([3,4], json_response['posts'][
|
424
|
+
assert json_response['posts'].is_a?(Hash)
|
425
|
+
assert_equal 3, json_response['posts']['links']['author']
|
426
|
+
assert_equal javascript.id, json_response['posts']['links']['section']
|
427
|
+
assert_equal 'A great new Post', json_response['posts']['title']
|
428
|
+
assert_equal 'AAAA', json_response['posts']['body']
|
429
|
+
assert matches_array?([3, 4], json_response['posts']['links']['tags'])
|
381
430
|
end
|
382
431
|
|
383
432
|
def test_update_remove_links
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
433
|
+
put :update,
|
434
|
+
{
|
435
|
+
id: 3,
|
436
|
+
posts: {
|
437
|
+
title: 'A great new Post',
|
438
|
+
links: {
|
439
|
+
section: nil,
|
440
|
+
tags: []
|
441
|
+
}
|
442
|
+
}
|
389
443
|
}
|
390
|
-
}
|
391
|
-
}
|
392
444
|
|
393
445
|
assert_response :success
|
394
|
-
|
395
|
-
assert_equal 3, json_response['posts'][
|
396
|
-
assert_equal
|
397
|
-
assert_equal 'A great new Post', json_response['posts'][
|
398
|
-
assert_equal 'AAAA', json_response['posts'][
|
399
|
-
assert matches_array?([], json_response['posts'][
|
446
|
+
assert json_response['posts'].is_a?(Hash)
|
447
|
+
assert_equal 3, json_response['posts']['links']['author']
|
448
|
+
assert_equal nil, json_response['posts']['links']['section']
|
449
|
+
assert_equal 'A great new Post', json_response['posts']['title']
|
450
|
+
assert_equal 'AAAA', json_response['posts']['body']
|
451
|
+
assert matches_array?([], json_response['posts']['links']['tags'])
|
400
452
|
end
|
401
453
|
|
402
454
|
def test_update_relationship_has_one
|
403
455
|
ruby = Section.find_by(name: 'ruby')
|
456
|
+
post_object = Post.find(3)
|
457
|
+
assert_not_equal ruby.id, post_object.section_id
|
404
458
|
|
405
|
-
|
406
|
-
section: ruby.id }
|
459
|
+
put :update_association, {post_id: 3, association: 'section', sections: ruby.id}
|
407
460
|
|
408
|
-
assert_response :
|
409
|
-
|
410
|
-
assert_equal
|
411
|
-
assert_equal ruby.id, json_response['posts'][0]['links']['section']
|
412
|
-
assert_equal 'AAAA', json_response['posts'][0]['body']
|
461
|
+
assert_response :no_content
|
462
|
+
post_object = Post.find(3)
|
463
|
+
assert_equal ruby.id, post_object.section_id
|
413
464
|
end
|
414
465
|
|
415
|
-
def
|
416
|
-
|
417
|
-
|
466
|
+
def test_update_relationship_has_one_singular_param
|
467
|
+
ruby = Section.find_by(name: 'ruby')
|
468
|
+
post_object = Post.find(3)
|
418
469
|
|
419
|
-
|
420
|
-
post :create_association, {post_id: 3, association: 'tags',
|
421
|
-
tags: [2] }
|
470
|
+
put :update_association, {post_id: 3, association: 'section', section: ruby.id}
|
422
471
|
|
423
|
-
assert_response :
|
424
|
-
|
425
|
-
tags: [5] }
|
472
|
+
assert_response :bad_request
|
473
|
+
end
|
426
474
|
|
427
|
-
|
428
|
-
|
429
|
-
|
475
|
+
def test_update_relationship_has_one_singular_param_relation_nil
|
476
|
+
ruby = Section.find_by(name: 'ruby')
|
477
|
+
post_object = Post.find(3)
|
478
|
+
post_object.section_id = nil
|
479
|
+
post_object.save!
|
430
480
|
|
431
|
-
|
481
|
+
put :update_association, {post_id: 3, association: 'section', sections: ruby.id}
|
432
482
|
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
assert_equal 'AAAA', json_response['posts'][0]['body']
|
483
|
+
assert_response :no_content
|
484
|
+
post_object = Post.find(3)
|
485
|
+
assert_equal ruby.id, post_object.section_id
|
437
486
|
end
|
438
487
|
|
439
|
-
def
|
440
|
-
|
441
|
-
|
488
|
+
def test_create_relationship_has_one_singular_param_relation_nil
|
489
|
+
ruby = Section.find_by(name: 'ruby')
|
490
|
+
post_object = Post.find(3)
|
491
|
+
post_object.section_id = nil
|
492
|
+
post_object.save!
|
442
493
|
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
assert_equal
|
494
|
+
post :create_association, {post_id: 3, association: 'section', sections: ruby.id}
|
495
|
+
|
496
|
+
assert_response :no_content
|
497
|
+
post_object = Post.find(3)
|
498
|
+
assert_equal ruby.id, post_object.section_id
|
448
499
|
end
|
449
500
|
|
450
|
-
def
|
501
|
+
def test_create_relationship_has_one_singular_param_relation_not_nil
|
451
502
|
ruby = Section.find_by(name: 'ruby')
|
503
|
+
js = Section.find_by(name: 'javascript')
|
504
|
+
post_object = Post.find(3)
|
505
|
+
post_object.section_id = js.id
|
506
|
+
post_object.save!
|
452
507
|
|
453
|
-
post :create_association, {post_id: 3, association: 'section',
|
454
|
-
author: 1 }
|
508
|
+
post :create_association, {post_id: 3, association: 'section', sections: ruby.id}
|
455
509
|
|
456
510
|
assert_response :bad_request
|
457
|
-
assert_match /The
|
511
|
+
assert_match /The relation already exists./, response.body
|
512
|
+
post_object = Post.find(3)
|
513
|
+
assert_equal js.id, post_object.section_id
|
514
|
+
end
|
515
|
+
|
516
|
+
def test_update_relationship_has_many_join_table_single
|
517
|
+
put :update_association, {post_id: 3, association: 'tags', tags: []}
|
518
|
+
assert_response :no_content
|
519
|
+
|
520
|
+
post_object = Post.find(3)
|
521
|
+
assert_equal 0, post_object.tags.length
|
522
|
+
|
523
|
+
put :update_association, {post_id: 3, association: 'tags', tags: [2]}
|
524
|
+
|
525
|
+
assert_response :no_content
|
526
|
+
post_object = Post.find(3)
|
527
|
+
assert_equal 1, post_object.tags.length
|
528
|
+
|
529
|
+
put :update_association, {post_id: 3, association: 'tags', tags: 5}
|
530
|
+
|
531
|
+
assert_response :no_content
|
532
|
+
post_object = Post.find(3)
|
533
|
+
tags = post_object.tags.collect { |tag| tag.id }
|
534
|
+
assert_equal 1, tags.length
|
535
|
+
assert matches_array? [5], tags
|
536
|
+
end
|
537
|
+
|
538
|
+
def test_update_relationship_has_many_join_table
|
539
|
+
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
|
540
|
+
|
541
|
+
assert_response :no_content
|
542
|
+
post_object = Post.find(3)
|
543
|
+
assert_equal 2, post_object.tags.collect { |tag| tag.id }.length
|
544
|
+
assert matches_array? [2, 3], post_object.tags.collect { |tag| tag.id }
|
545
|
+
end
|
546
|
+
|
547
|
+
def test_create_relationship_has_many_join_table
|
548
|
+
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
|
549
|
+
|
550
|
+
assert_response :no_content
|
551
|
+
post_object = Post.find(3)
|
552
|
+
assert_equal 2, post_object.tags.collect { |tag| tag.id }.length
|
553
|
+
assert matches_array? [2, 3], post_object.tags.collect { |tag| tag.id }
|
554
|
+
|
555
|
+
post :create_association, {post_id: 3, association: 'tags', tags: [5]}
|
556
|
+
|
557
|
+
assert_response :no_content
|
558
|
+
post_object = Post.find(3)
|
559
|
+
assert_equal 3, post_object.tags.collect { |tag| tag.id }.length
|
560
|
+
assert matches_array? [2, 3, 5], post_object.tags.collect { |tag| tag.id }
|
561
|
+
end
|
562
|
+
|
563
|
+
def test_create_relationship_has_many_missing_tags
|
564
|
+
post :create_association, {post_id: 3, association: 'tags'}
|
565
|
+
|
566
|
+
assert_response :bad_request
|
567
|
+
assert_match /The required parameter, tags, is missing./, response.body
|
568
|
+
end
|
569
|
+
|
570
|
+
def test_create_relationship_has_many_join_table_record_exists
|
571
|
+
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
|
572
|
+
|
573
|
+
assert_response :no_content
|
574
|
+
post_object = Post.find(3)
|
575
|
+
assert_equal 2, post_object.tags.collect { |tag| tag.id }.length
|
576
|
+
assert matches_array? [2, 3], post_object.tags.collect { |tag| tag.id }
|
577
|
+
|
578
|
+
post :create_association, {post_id: 3, association: 'tags', tags: [5, 2]}
|
579
|
+
|
580
|
+
assert_response :bad_request
|
581
|
+
assert_match /The relation to 2 already exists./, response.body
|
582
|
+
end
|
583
|
+
|
584
|
+
def test_update_relationship_has_one_mismatch_params
|
585
|
+
post :create_association, {post_id: 3, association: 'section', authors: 1}
|
586
|
+
|
587
|
+
assert_response :bad_request
|
588
|
+
assert_match /The required parameter, sections, is missing./, response.body
|
589
|
+
end
|
590
|
+
|
591
|
+
def test_update_relationship_has_many_missing_tags
|
592
|
+
put :update_association, {post_id: 3, association: 'tags'}
|
593
|
+
|
594
|
+
assert_response :bad_request
|
595
|
+
assert_match /The required parameter, tags, is missing./, response.body
|
458
596
|
end
|
459
597
|
|
460
598
|
def test_delete_relationship_has_one
|
461
599
|
ruby = Section.find_by(name: 'ruby')
|
462
600
|
|
463
|
-
post :create_association, {post_id: 9, association: 'section',
|
464
|
-
section: ruby.id }
|
601
|
+
post :create_association, {post_id: 9, association: 'section', sections: ruby.id}
|
465
602
|
|
466
|
-
assert_response :
|
467
|
-
assert_equal ruby.id, json_response['posts'][0]['links']['section']
|
603
|
+
assert_response :no_content
|
468
604
|
|
469
|
-
|
605
|
+
delete :destroy_association, {post_id: 9, association: 'section'}
|
470
606
|
|
471
607
|
assert_response :no_content
|
472
608
|
post = Post.find(9)
|
@@ -474,31 +610,46 @@ class PostsControllerTest < ActionController::TestCase
|
|
474
610
|
end
|
475
611
|
|
476
612
|
def test_delete_relationship_has_many
|
477
|
-
|
478
|
-
|
479
|
-
assert_response :success
|
613
|
+
put :update_association, {post_id: 9, association: 'tags', tags: [2, 3]}
|
614
|
+
assert_response :no_content
|
480
615
|
p = Post.find(9)
|
481
|
-
assert_equal [2,3], p.tag_ids
|
616
|
+
assert_equal [2, 3], p.tag_ids
|
482
617
|
|
483
|
-
|
618
|
+
delete :destroy_association, {post_id: 9, association: 'tags', keys: '3'}
|
484
619
|
|
620
|
+
p.reload
|
485
621
|
assert_response :no_content
|
486
622
|
assert_equal [2], p.tag_ids
|
487
623
|
end
|
488
624
|
|
625
|
+
def test_delete_relationship_has_many_does_not_exist
|
626
|
+
put :update_association, {post_id: 9, association: 'tags', tags: [2, 3]}
|
627
|
+
assert_response :no_content
|
628
|
+
p = Post.find(9)
|
629
|
+
assert_equal [2, 3], p.tag_ids
|
630
|
+
|
631
|
+
delete :destroy_association, {post_id: 9, association: 'tags', keys: '4'}
|
632
|
+
|
633
|
+
p.reload
|
634
|
+
assert_response :not_found
|
635
|
+
assert_equal [2, 3], p.tag_ids
|
636
|
+
end
|
489
637
|
|
490
638
|
def test_update_mismatched_keys
|
491
639
|
javascript = Section.find_by(name: 'javascript')
|
492
640
|
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
641
|
+
put :update,
|
642
|
+
{
|
643
|
+
id: 3,
|
644
|
+
posts: {
|
645
|
+
id: 2,
|
646
|
+
title: 'A great new Post',
|
647
|
+
links: {
|
648
|
+
section: javascript.id,
|
649
|
+
tags: [3, 4]
|
650
|
+
}
|
651
|
+
}
|
499
652
|
}
|
500
|
-
}
|
501
|
-
}
|
502
653
|
|
503
654
|
assert_response :bad_request
|
504
655
|
assert_match /The URL does not support the key 2/, response.body
|
@@ -507,15 +658,18 @@ class PostsControllerTest < ActionController::TestCase
|
|
507
658
|
def test_update_extra_param
|
508
659
|
javascript = Section.find_by(name: 'javascript')
|
509
660
|
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
661
|
+
put :update,
|
662
|
+
{
|
663
|
+
id: 3,
|
664
|
+
posts: {
|
665
|
+
asdfg: 'aaaa',
|
666
|
+
title: 'A great new Post',
|
667
|
+
links: {
|
668
|
+
section: javascript.id,
|
669
|
+
tags: [3, 4]
|
670
|
+
}
|
671
|
+
}
|
516
672
|
}
|
517
|
-
}
|
518
|
-
}
|
519
673
|
|
520
674
|
assert_response :bad_request
|
521
675
|
assert_match /asdfg is not allowed/, response.body
|
@@ -524,15 +678,18 @@ class PostsControllerTest < ActionController::TestCase
|
|
524
678
|
def test_update_extra_param_in_links
|
525
679
|
javascript = Section.find_by(name: 'javascript')
|
526
680
|
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
681
|
+
put :update,
|
682
|
+
{
|
683
|
+
id: 3,
|
684
|
+
posts: {
|
685
|
+
title: 'A great new Post',
|
686
|
+
links: {
|
687
|
+
asdfg: 'aaaa',
|
688
|
+
section: javascript.id,
|
689
|
+
tags: [3, 4]
|
690
|
+
}
|
691
|
+
}
|
533
692
|
}
|
534
|
-
}
|
535
|
-
}
|
536
693
|
|
537
694
|
assert_response :bad_request
|
538
695
|
assert_match /asdfg is not allowed/, response.body
|
@@ -541,14 +698,17 @@ class PostsControllerTest < ActionController::TestCase
|
|
541
698
|
def test_update_missing_param
|
542
699
|
javascript = Section.find_by(name: 'javascript')
|
543
700
|
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
701
|
+
put :update,
|
702
|
+
{
|
703
|
+
id: 3,
|
704
|
+
posts_spelled_wrong: {
|
705
|
+
title: 'A great new Post',
|
706
|
+
links: {
|
707
|
+
section: javascript.id,
|
708
|
+
tags: [3, 4]
|
709
|
+
}
|
710
|
+
}
|
549
711
|
}
|
550
|
-
}
|
551
|
-
}
|
552
712
|
|
553
713
|
assert_response :bad_request
|
554
714
|
assert_match /The required parameter, posts, is missing./, response.body
|
@@ -557,24 +717,27 @@ class PostsControllerTest < ActionController::TestCase
|
|
557
717
|
def test_update_multiple
|
558
718
|
javascript = Section.find_by(name: 'javascript')
|
559
719
|
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
720
|
+
put :update,
|
721
|
+
{
|
722
|
+
id: [3, 9],
|
723
|
+
posts: [
|
724
|
+
{
|
725
|
+
id: 3,
|
726
|
+
title: 'A great new Post QWERTY',
|
727
|
+
links: {
|
728
|
+
section: javascript.id,
|
729
|
+
tags: [3, 4]
|
730
|
+
}
|
731
|
+
},
|
732
|
+
{
|
733
|
+
id: 9,
|
734
|
+
title: 'A great new Post ASDFG',
|
735
|
+
links: {
|
736
|
+
section: javascript.id,
|
737
|
+
tags: [3, 4]
|
738
|
+
}
|
739
|
+
}
|
740
|
+
]}
|
578
741
|
|
579
742
|
assert_response :success
|
580
743
|
assert_equal json_response['posts'].size, 2
|
@@ -582,34 +745,37 @@ class PostsControllerTest < ActionController::TestCase
|
|
582
745
|
assert_equal json_response['posts'][0]['links']['section'], javascript.id
|
583
746
|
assert_equal json_response['posts'][0]['title'], 'A great new Post QWERTY'
|
584
747
|
assert_equal json_response['posts'][0]['body'], 'AAAA'
|
585
|
-
assert_equal json_response['posts'][0]['links']['tags'], [3,4]
|
748
|
+
assert_equal json_response['posts'][0]['links']['tags'], [3, 4]
|
586
749
|
|
587
750
|
assert_equal json_response['posts'][1]['links']['author'], 3
|
588
751
|
assert_equal json_response['posts'][1]['links']['section'], javascript.id
|
589
752
|
assert_equal json_response['posts'][1]['title'], 'A great new Post ASDFG'
|
590
753
|
assert_equal json_response['posts'][1]['body'], 'AAAA'
|
591
|
-
assert_equal json_response['posts'][1]['links']['tags'], [3,4]
|
754
|
+
assert_equal json_response['posts'][1]['links']['tags'], [3, 4]
|
592
755
|
end
|
593
756
|
|
594
757
|
def test_update_multiple_missing_keys
|
595
758
|
javascript = Section.find_by(name: 'javascript')
|
596
759
|
|
597
|
-
|
760
|
+
put :update,
|
598
761
|
{
|
599
|
-
|
600
|
-
|
762
|
+
id: [3, 9],
|
763
|
+
posts: [
|
764
|
+
{
|
765
|
+
title: 'A great new Post ASDFG',
|
766
|
+
links: {
|
601
767
|
section: javascript.id,
|
602
|
-
tags: [3,4]
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
768
|
+
tags: [3, 4]
|
769
|
+
}
|
770
|
+
},
|
771
|
+
{
|
772
|
+
title: 'A great new Post QWERTY',
|
773
|
+
links: {
|
608
774
|
section: javascript.id,
|
609
|
-
tags: [3,4]
|
775
|
+
tags: [3, 4]
|
776
|
+
}
|
610
777
|
}
|
611
|
-
|
612
|
-
]}
|
778
|
+
]}
|
613
779
|
|
614
780
|
assert_response :bad_request
|
615
781
|
assert_match /A key is required/, response.body
|
@@ -618,24 +784,27 @@ class PostsControllerTest < ActionController::TestCase
|
|
618
784
|
def test_update_mismatch_keys
|
619
785
|
javascript = Section.find_by(name: 'javascript')
|
620
786
|
|
621
|
-
|
787
|
+
put :update,
|
622
788
|
{
|
623
|
-
|
624
|
-
|
625
|
-
|
789
|
+
id: [3, 9],
|
790
|
+
posts: [
|
791
|
+
{
|
792
|
+
id: 3,
|
793
|
+
title: 'A great new Post ASDFG',
|
794
|
+
links: {
|
626
795
|
section: javascript.id,
|
627
|
-
tags: [3,4]
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
796
|
+
tags: [3, 4]
|
797
|
+
}
|
798
|
+
},
|
799
|
+
{
|
800
|
+
id: 8,
|
801
|
+
title: 'A great new Post QWERTY',
|
802
|
+
links: {
|
634
803
|
section: javascript.id,
|
635
|
-
tags: [3,4]
|
804
|
+
tags: [3, 4]
|
805
|
+
}
|
636
806
|
}
|
637
|
-
|
638
|
-
]}
|
807
|
+
]}
|
639
808
|
|
640
809
|
assert_response :bad_request
|
641
810
|
assert_match /The URL does not support the key 8/, response.body
|
@@ -644,38 +813,44 @@ class PostsControllerTest < ActionController::TestCase
|
|
644
813
|
def test_update_multiple_count_mismatch
|
645
814
|
javascript = Section.find_by(name: 'javascript')
|
646
815
|
|
647
|
-
|
816
|
+
put :update,
|
648
817
|
{
|
649
|
-
|
650
|
-
|
651
|
-
|
818
|
+
id: [3, 9, 2],
|
819
|
+
posts: [
|
820
|
+
{
|
821
|
+
id: 3,
|
822
|
+
title: 'A great new Post QWERTY',
|
823
|
+
links: {
|
652
824
|
section: javascript.id,
|
653
|
-
tags: [3,4]
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
825
|
+
tags: [3, 4]
|
826
|
+
}
|
827
|
+
},
|
828
|
+
{
|
829
|
+
id: 9,
|
830
|
+
title: 'A great new Post ASDFG',
|
831
|
+
links: {
|
660
832
|
section: javascript.id,
|
661
|
-
tags: [3,4]
|
833
|
+
tags: [3, 4]
|
834
|
+
}
|
662
835
|
}
|
663
|
-
|
664
|
-
]}
|
836
|
+
]}
|
665
837
|
|
666
838
|
assert_response :bad_request
|
667
839
|
assert_match /Count to key mismatch/, response.body
|
668
840
|
end
|
669
841
|
|
670
842
|
def test_update_unpermitted_attributes
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
843
|
+
put :update,
|
844
|
+
{
|
845
|
+
id: 3,
|
846
|
+
posts: {
|
847
|
+
subject: 'A great new Post',
|
848
|
+
links: {
|
849
|
+
author: 1,
|
850
|
+
tags: [3, 4]
|
851
|
+
}
|
852
|
+
}
|
676
853
|
}
|
677
|
-
}
|
678
|
-
}
|
679
854
|
|
680
855
|
assert_response :bad_request
|
681
856
|
assert_match /author is not allowed./, response.body
|
@@ -683,42 +858,45 @@ class PostsControllerTest < ActionController::TestCase
|
|
683
858
|
end
|
684
859
|
|
685
860
|
def test_update_bad_attributes
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
861
|
+
put :update,
|
862
|
+
{
|
863
|
+
id: 3,
|
864
|
+
posts: {
|
865
|
+
subject: 'A great new Post',
|
866
|
+
linked_objects: {
|
867
|
+
author: 1,
|
868
|
+
tags: [3, 4]
|
869
|
+
}
|
870
|
+
}
|
691
871
|
}
|
692
|
-
}
|
693
|
-
}
|
694
872
|
|
695
873
|
assert_response :bad_request
|
696
874
|
end
|
697
875
|
|
698
876
|
def test_delete_single
|
699
877
|
initial_count = Post.count
|
700
|
-
|
878
|
+
delete :destroy, {id: '4'}
|
701
879
|
assert_response :no_content
|
702
880
|
assert_equal initial_count - 1, Post.count
|
703
881
|
end
|
704
882
|
|
705
883
|
def test_delete_multiple
|
706
884
|
initial_count = Post.count
|
707
|
-
|
885
|
+
delete :destroy, {id: '5,6'}
|
708
886
|
assert_response :no_content
|
709
887
|
assert_equal initial_count - 2, Post.count
|
710
888
|
end
|
711
889
|
|
712
890
|
def test_delete_multiple_one_does_not_exist
|
713
891
|
initial_count = Post.count
|
714
|
-
|
892
|
+
delete :destroy, {id: '5,6,99999'}
|
715
893
|
assert_response :not_found
|
716
894
|
assert_equal initial_count, Post.count
|
717
895
|
end
|
718
896
|
|
719
897
|
def test_delete_extra_param
|
720
898
|
initial_count = Post.count
|
721
|
-
|
899
|
+
delete :destroy, {id: '4', asdfg: 'aaaa'}
|
722
900
|
assert_response :bad_request
|
723
901
|
assert_equal initial_count, Post.count
|
724
902
|
end
|
@@ -732,7 +910,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
732
910
|
def test_show_has_many_relationship
|
733
911
|
get :show_association, {post_id: '1', association: 'tags'}
|
734
912
|
assert_response :success
|
735
|
-
assert_equal [1,2,3], json_response['tags']
|
913
|
+
assert_equal [1, 2, 3], json_response['tags']
|
736
914
|
end
|
737
915
|
end
|
738
916
|
|
@@ -747,57 +925,237 @@ class TagsControllerTest < ActionController::TestCase
|
|
747
925
|
def test_tags_show_multiple
|
748
926
|
get :show, {id: '6,7,8,9'}
|
749
927
|
assert_response :success
|
928
|
+
assert json_response['tags'].is_a?(Array)
|
750
929
|
assert_equal 4, json_response['tags'].size
|
751
930
|
end
|
752
931
|
|
753
932
|
def test_tags_show_multiple_with_include
|
754
933
|
get :show, {id: '6,7,8,9', include: 'posts,posts.tags,posts.author.posts'}
|
755
934
|
assert_response :success
|
935
|
+
assert json_response['tags'].is_a?(Array)
|
756
936
|
assert_equal 4, json_response['tags'].size
|
757
937
|
assert_equal 2, json_response['linked']['posts'].size
|
758
938
|
end
|
759
939
|
end
|
760
940
|
|
761
941
|
class ExpenseEntriesControllerTest < ActionController::TestCase
|
942
|
+
def after_teardown
|
943
|
+
JSONAPI.configuration.json_key_format = :camelized_key
|
944
|
+
end
|
945
|
+
|
762
946
|
def test_expense_entries_index
|
763
947
|
get :index
|
764
948
|
assert_response :success
|
765
|
-
|
949
|
+
assert json_response['expenseEntries'].is_a?(Array)
|
950
|
+
assert_equal 2, json_response['expenseEntries'].size
|
951
|
+
end
|
952
|
+
|
953
|
+
def test_expense_entries_show
|
954
|
+
get :show, {id: 1}
|
955
|
+
assert_response :success
|
956
|
+
assert json_response['expenseEntries'].is_a?(Hash)
|
957
|
+
end
|
958
|
+
|
959
|
+
def test_expense_entries_show_include
|
960
|
+
get :show, {id: 1, include: 'isoCurrency,employee'}
|
961
|
+
assert_response :success
|
962
|
+
assert json_response['expenseEntries'].is_a?(Hash)
|
963
|
+
assert_equal 1, json_response['linked']['isoCurrencies'].size
|
964
|
+
assert_equal 1, json_response['linked']['people'].size
|
965
|
+
end
|
966
|
+
|
967
|
+
def test_expense_entries_show_bad_include_missing_association
|
968
|
+
get :show, {id: 1, include: 'isoCurrencies,employees'}
|
969
|
+
assert_response :bad_request
|
970
|
+
assert_match /isoCurrencies is not a valid association of expenseEntries/, json_response['errors'][0]['detail']
|
971
|
+
assert_match /employees is not a valid association of expenseEntries/, json_response['errors'][1]['detail']
|
972
|
+
end
|
973
|
+
|
974
|
+
def test_expense_entries_show_bad_include_missing_sub_association
|
975
|
+
get :show, {id: 1, include: 'isoCurrency,employee.post'}
|
976
|
+
assert_response :bad_request
|
977
|
+
assert_match /post is not a valid association of people/, json_response['errors'][0]['detail']
|
978
|
+
end
|
979
|
+
|
980
|
+
def test_expense_entries_show_fields
|
981
|
+
get :show, {id: 1, include: 'isoCurrency,employee', 'fields' => 'transactionDate'}
|
982
|
+
assert_response :success
|
983
|
+
assert json_response['expenseEntries'].is_a?(Hash)
|
984
|
+
assert json_response['expenseEntries'].has_key?('transactionDate')
|
985
|
+
refute json_response['expenseEntries'].has_key?('id')
|
986
|
+
refute json_response['expenseEntries'].has_key?('links')
|
987
|
+
assert_equal 1, json_response['linked']['isoCurrencies'].size
|
988
|
+
assert_equal 1, json_response['linked']['people'].size
|
989
|
+
end
|
990
|
+
|
991
|
+
def test_expense_entries_show_fields_type
|
992
|
+
get :show, {id: 1, include: 'isoCurrency,employee', 'fields' => {'expenseEntries' => 'transactionDate'}}
|
993
|
+
assert_response :success
|
994
|
+
assert json_response['expenseEntries'].is_a?(Hash)
|
995
|
+
assert json_response['expenseEntries'].has_key?('transactionDate')
|
996
|
+
refute json_response['expenseEntries'].has_key?('id')
|
997
|
+
refute json_response['expenseEntries'].has_key?('links')
|
998
|
+
assert_equal 1, json_response['linked']['isoCurrencies'].size
|
999
|
+
assert_equal 1, json_response['linked']['people'].size
|
1000
|
+
end
|
1001
|
+
|
1002
|
+
def test_expense_entries_show_fields_type_many
|
1003
|
+
get :show, {id: 1, include: 'isoCurrency,employee', 'fields' => {'expenseEntries' => 'transactionDate',
|
1004
|
+
'isoCurrencies' => 'code,name'}}
|
1005
|
+
assert_response :success
|
1006
|
+
assert json_response['expenseEntries'].is_a?(Hash)
|
1007
|
+
assert json_response['expenseEntries'].has_key?('transactionDate')
|
1008
|
+
refute json_response['expenseEntries'].has_key?('id')
|
1009
|
+
refute json_response['expenseEntries'].has_key?('links')
|
1010
|
+
assert_equal 1, json_response['linked']['isoCurrencies'].size
|
1011
|
+
assert_equal 1, json_response['linked']['people'].size
|
1012
|
+
assert json_response['linked']['isoCurrencies'][0].has_key?('code')
|
1013
|
+
assert json_response['linked']['isoCurrencies'][0].has_key?('name')
|
1014
|
+
refute json_response['linked']['isoCurrencies'][0].has_key?('countryName')
|
1015
|
+
end
|
1016
|
+
|
1017
|
+
def test_create_expense_entries_underscored
|
1018
|
+
JSONAPI.configuration.json_key_format = :underscored_key
|
1019
|
+
|
1020
|
+
post :create,
|
1021
|
+
{
|
1022
|
+
expense_entries: {
|
1023
|
+
transaction_date: '2014/04/15',
|
1024
|
+
cost: 50.58,
|
1025
|
+
links: {
|
1026
|
+
employee: 3,
|
1027
|
+
iso_currency: 'USD'
|
1028
|
+
}
|
1029
|
+
},
|
1030
|
+
include: 'iso_currency',
|
1031
|
+
fields: 'id,transaction_date,iso_currency,cost,employee'
|
1032
|
+
}
|
1033
|
+
|
1034
|
+
assert_response :created
|
1035
|
+
assert json_response['expense_entries'].is_a?(Hash)
|
1036
|
+
assert_equal 3, json_response['expense_entries']['links']['employee']
|
1037
|
+
assert_equal 'USD', json_response['expense_entries']['links']['iso_currency']
|
1038
|
+
assert_equal '50.58', json_response['expense_entries']['cost']
|
1039
|
+
|
1040
|
+
delete :destroy, {id: json_response['expense_entries']['id']}
|
1041
|
+
assert_response :no_content
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
def test_create_expense_entries_camelized_key
|
1045
|
+
JSONAPI.configuration.json_key_format = :camelized_key
|
1046
|
+
|
1047
|
+
post :create,
|
1048
|
+
{
|
1049
|
+
expenseEntries: {
|
1050
|
+
transactionDate: '2014/04/15',
|
1051
|
+
cost: 50.58,
|
1052
|
+
links: {
|
1053
|
+
employee: 3,
|
1054
|
+
isoCurrency: 'USD'
|
1055
|
+
}
|
1056
|
+
},
|
1057
|
+
include: 'isoCurrency',
|
1058
|
+
fields: 'id,transactionDate,isoCurrency,cost,employee'
|
1059
|
+
}
|
1060
|
+
|
1061
|
+
assert_response :created
|
1062
|
+
assert json_response['expenseEntries'].is_a?(Hash)
|
1063
|
+
assert_equal 3, json_response['expenseEntries']['links']['employee']
|
1064
|
+
assert_equal 'USD', json_response['expenseEntries']['links']['isoCurrency']
|
1065
|
+
assert_equal '50.58', json_response['expenseEntries']['cost']
|
1066
|
+
|
1067
|
+
delete :destroy, {id: json_response['expenseEntries']['id']}
|
1068
|
+
assert_response :no_content
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
def test_create_expense_entries_dasherized_key
|
1072
|
+
JSONAPI.configuration.json_key_format = :dasherized_key
|
1073
|
+
|
1074
|
+
post :create,
|
1075
|
+
{
|
1076
|
+
'expense-entries' => {
|
1077
|
+
'transaction-date' => '2014/04/15',
|
1078
|
+
cost: 50.58,
|
1079
|
+
links: {
|
1080
|
+
employee: 3,
|
1081
|
+
'iso-currency' => 'USD'
|
1082
|
+
}
|
1083
|
+
},
|
1084
|
+
include: 'iso-currency',
|
1085
|
+
fields: 'id,transaction-date,iso-currency,cost,employee'
|
1086
|
+
}
|
1087
|
+
|
1088
|
+
assert_response :created
|
1089
|
+
assert json_response['expense-entries'].is_a?(Hash)
|
1090
|
+
assert_equal 3, json_response['expense-entries']['links']['employee']
|
1091
|
+
assert_equal 'USD', json_response['expense-entries']['links']['iso-currency']
|
1092
|
+
assert_equal '50.58', json_response['expense-entries']['cost']
|
1093
|
+
|
1094
|
+
delete :destroy, {id: json_response['expense-entries']['id']}
|
1095
|
+
assert_response :no_content
|
766
1096
|
end
|
767
1097
|
end
|
768
1098
|
|
769
|
-
class
|
1099
|
+
class IsoCurrenciesControllerTest < ActionController::TestCase
|
1100
|
+
def after_teardown
|
1101
|
+
JSONAPI.configuration.json_key_format = :camelized_key
|
1102
|
+
end
|
1103
|
+
|
770
1104
|
def test_currencies_index
|
771
1105
|
get :index
|
772
1106
|
assert_response :success
|
773
|
-
assert_equal 2, json_response['
|
1107
|
+
assert_equal 2, json_response['isoCurrencies'].size
|
1108
|
+
end
|
1109
|
+
|
1110
|
+
def test_currencies_json_key_underscored
|
1111
|
+
JSONAPI.configuration.json_key_format = :underscored_key
|
1112
|
+
get :index
|
1113
|
+
assert_response :success
|
1114
|
+
assert_equal 2, json_response['iso_currencies'].size
|
1115
|
+
end
|
1116
|
+
|
1117
|
+
def test_currencies_json_key_dasherized
|
1118
|
+
JSONAPI.configuration.json_key_format = :dasherized_key
|
1119
|
+
get :index
|
1120
|
+
assert_response :success
|
1121
|
+
assert_equal 2, json_response['iso-currencies'].size
|
1122
|
+
end
|
1123
|
+
|
1124
|
+
def test_currencies_custom_json_key
|
1125
|
+
JSONAPI.configuration.json_key_format = :upper_camelized_key
|
1126
|
+
get :index
|
1127
|
+
assert_response :success
|
1128
|
+
assert_equal 2, json_response['IsoCurrencies'].size
|
774
1129
|
end
|
775
1130
|
|
776
1131
|
def test_currencies_show
|
777
|
-
get :show, {code: 'USD'
|
1132
|
+
get :show, {code: 'USD'}
|
778
1133
|
assert_response :success
|
779
|
-
|
780
|
-
assert_equal 2, json_response['linked']['expense_entries'].size
|
1134
|
+
assert json_response['isoCurrencies'].is_a?(Hash)
|
781
1135
|
end
|
782
1136
|
end
|
783
1137
|
|
784
1138
|
class PeopleControllerTest < ActionController::TestCase
|
785
1139
|
def test_create_validations
|
786
|
-
post :create,
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
1140
|
+
post :create,
|
1141
|
+
{
|
1142
|
+
people: {
|
1143
|
+
name: 'Steve Jobs',
|
1144
|
+
email: 'sj@email.zzz',
|
1145
|
+
dateJoined: DateTime.parse('2014-1-30 4:20:00 UTC +00:00')
|
1146
|
+
}
|
1147
|
+
}
|
792
1148
|
|
793
1149
|
assert_response :success
|
794
1150
|
end
|
795
1151
|
|
796
1152
|
def test_create_validations_missing_attribute
|
797
|
-
post :create,
|
798
|
-
|
799
|
-
|
800
|
-
|
1153
|
+
post :create,
|
1154
|
+
{
|
1155
|
+
people: {
|
1156
|
+
email: 'sj@email.zzz'
|
1157
|
+
}
|
1158
|
+
}
|
801
1159
|
|
802
1160
|
assert_response :bad_request
|
803
1161
|
assert_equal 2, json_response['errors'].size
|
@@ -808,10 +1166,13 @@ class PeopleControllerTest < ActionController::TestCase
|
|
808
1166
|
end
|
809
1167
|
|
810
1168
|
def test_update_validations_missing_attribute
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
1169
|
+
put :update,
|
1170
|
+
{
|
1171
|
+
id: 3,
|
1172
|
+
people: {
|
1173
|
+
name: ''
|
1174
|
+
}
|
1175
|
+
}
|
815
1176
|
|
816
1177
|
assert_response :bad_request
|
817
1178
|
assert_equal 1, json_response['errors'].size
|
@@ -821,7 +1182,7 @@ class PeopleControllerTest < ActionController::TestCase
|
|
821
1182
|
|
822
1183
|
def test_delete_locked
|
823
1184
|
initial_count = Person.count
|
824
|
-
|
1185
|
+
delete :destroy, {id: '3'}
|
825
1186
|
assert_response :locked
|
826
1187
|
assert_equal initial_count, Person.count
|
827
1188
|
end
|
@@ -875,66 +1236,76 @@ class AuthorsControllerTest < ActionController::TestCase
|
|
875
1236
|
end
|
876
1237
|
|
877
1238
|
class BreedsControllerTest < ActionController::TestCase
|
1239
|
+
# Note: Breed names go through the TitleValueFormatter
|
1240
|
+
|
878
1241
|
def test_poro_index
|
879
1242
|
get :index
|
880
1243
|
assert_response :success
|
881
1244
|
assert_equal 0, json_response['breeds'][0]['id']
|
882
|
-
assert_equal '
|
1245
|
+
assert_equal 'Persian', json_response['breeds'][0]['name']
|
883
1246
|
end
|
884
1247
|
|
885
1248
|
def test_poro_show
|
886
1249
|
get :show, {id: '0'}
|
887
1250
|
assert_response :success
|
888
|
-
|
889
|
-
assert_equal 0, json_response['breeds'][
|
890
|
-
assert_equal '
|
1251
|
+
assert json_response['breeds'].is_a?(Hash)
|
1252
|
+
assert_equal 0, json_response['breeds']['id']
|
1253
|
+
assert_equal 'Persian', json_response['breeds']['name']
|
891
1254
|
end
|
892
1255
|
|
893
1256
|
def test_poro_show_multiple
|
894
1257
|
get :show, {id: '0,2'}
|
895
1258
|
assert_response :success
|
1259
|
+
assert json_response['breeds'].is_a?(Array)
|
896
1260
|
assert_equal 2, json_response['breeds'].size
|
897
1261
|
assert_equal 0, json_response['breeds'][0]['id']
|
898
|
-
assert_equal '
|
1262
|
+
assert_equal 'Persian', json_response['breeds'][0]['name']
|
899
1263
|
assert_equal 2, json_response['breeds'][1]['id']
|
900
|
-
assert_equal '
|
1264
|
+
assert_equal 'Sphinx', json_response['breeds'][1]['name']
|
901
1265
|
end
|
902
1266
|
|
903
1267
|
def test_poro_create_simple
|
904
|
-
post :create,
|
905
|
-
|
906
|
-
|
907
|
-
|
1268
|
+
post :create,
|
1269
|
+
{
|
1270
|
+
breeds: {
|
1271
|
+
name: 'tabby'
|
1272
|
+
}
|
1273
|
+
}
|
908
1274
|
|
909
1275
|
assert_response :created
|
910
|
-
|
911
|
-
assert_equal '
|
1276
|
+
assert json_response['breeds'].is_a?(Hash)
|
1277
|
+
assert_equal 'Tabby', json_response['breeds']['name']
|
912
1278
|
end
|
913
1279
|
|
914
1280
|
def test_poro_create_update
|
915
|
-
post :create,
|
916
|
-
|
917
|
-
|
918
|
-
|
1281
|
+
post :create,
|
1282
|
+
{
|
1283
|
+
breeds: {
|
1284
|
+
name: 'CALIC'
|
1285
|
+
}
|
1286
|
+
}
|
919
1287
|
|
920
1288
|
assert_response :created
|
921
|
-
|
922
|
-
assert_equal '
|
1289
|
+
assert json_response['breeds'].is_a?(Hash)
|
1290
|
+
assert_equal 'Calic', json_response['breeds']['name']
|
923
1291
|
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
1292
|
+
put :update,
|
1293
|
+
{
|
1294
|
+
id: json_response['breeds']['id'],
|
1295
|
+
breeds: {
|
1296
|
+
name: 'calico'
|
1297
|
+
}
|
1298
|
+
}
|
928
1299
|
assert_response :success
|
929
|
-
|
930
|
-
assert_equal '
|
1300
|
+
assert json_response['breeds'].is_a?(Hash)
|
1301
|
+
assert_equal 'Calico', json_response['breeds']['name']
|
931
1302
|
end
|
932
1303
|
|
933
1304
|
def test_poro_delete
|
934
1305
|
initial_count = $breed_data.breeds.keys.count
|
935
|
-
|
1306
|
+
delete :destroy, {id: '3'}
|
936
1307
|
assert_response :no_content
|
937
1308
|
assert_equal initial_count - 1, $breed_data.breeds.keys.count
|
938
1309
|
end
|
939
1310
|
|
940
|
-
end
|
1311
|
+
end
|