jsonapi-resources 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+ require File.expand_path('../../../fixtures/active_record', __FILE__)
3
+
4
+ class ArticleResource < JSONAPI::Resource
5
+ model_name 'Post'
6
+ end
7
+
8
+ class CatResource < JSONAPI::Resource
9
+ attribute :id
10
+ attribute :name
11
+ attribute :breed
12
+
13
+ has_one :mother, class_name: 'Cat'
14
+ has_one :father, class_name: 'Cat'
15
+ end
16
+
17
+ class ResourceTest < MiniTest::Unit::TestCase
18
+ def setup
19
+ @post = Post.first
20
+ end
21
+
22
+ def test_model_name
23
+ assert_equal(PostResource._model_name, 'Post')
24
+ end
25
+
26
+ def test_model
27
+ assert_equal(PostResource._model_class, Post)
28
+ end
29
+
30
+ def test_model_alternate
31
+ assert_equal(ArticleResource._model_class, Post)
32
+ end
33
+
34
+ def test_class_attributes
35
+ attrs = CatResource._attributes
36
+ assert_kind_of(Set, attrs)
37
+ assert_equal(attrs.size, 3)
38
+ end
39
+
40
+ def test_class_assosications
41
+ associations = CatResource._associations
42
+ assert_kind_of(Hash, associations)
43
+ assert_equal(associations.size, 2)
44
+ end
45
+ end
@@ -0,0 +1,429 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+ require File.expand_path('../../../fixtures/active_record', __FILE__)
3
+ require 'jsonapi/resource_serializer'
4
+
5
+
6
+ class SerializerTest < MiniTest::Unit::TestCase
7
+ def setup
8
+ @post = Post.find(1)
9
+ @fred = Person.find_by(name: 'Fred Reader')
10
+ end
11
+
12
+ 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))
27
+ end
28
+
29
+ def test_serializer_limited_fieldset
30
+ assert_hash_equals({
31
+ posts: [{
32
+ id: 1,
33
+ title: 'New post',
34
+ links: {
35
+ author: 1
36
+ }
37
+ }]
38
+ }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), nil,
39
+ {posts: [:id, :title, :author]}))
40
+ end
41
+
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))
69
+ end
70
+
71
+ def test_serializer_include_sub_objects
72
+ 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
+ ]
130
+ }
131
+ }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), [:comments,'comments.tags'], nil))
132
+ end
133
+
134
+ def test_serializer_include_has_many_sub_objects_only
135
+ 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
+ ]
173
+ }
174
+ }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), ['comments.tags'], nil))
175
+ end
176
+
177
+ def test_serializer_include_has_one_sub_objects_only
178
+ 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
+ ]
204
+ }
205
+ }, JSONAPI::ResourceSerializer.new.serialize(PostResource.new(@post), ['author.comments'], nil))
206
+ end
207
+
208
+ def test_serializer_different_foreign_key
209
+ 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
+ ]
241
+ }
242
+ }, JSONAPI::ResourceSerializer.new.serialize(PersonResource.new(@fred), ['comments'], nil))
243
+ end
244
+
245
+ def test_serializer_array_of_resources
246
+
247
+ posts = []
248
+ Post.find(1,2).each do |post|
249
+ posts.push PostResource.new(post)
250
+ end
251
+
252
+ 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
+ {
281
+ id: 1,
282
+ name: 'short',
283
+ links: {
284
+ posts: :not_nil
285
+ }
286
+ },
287
+ {
288
+ id: 2,
289
+ name: 'whiny',
290
+ links: {
291
+ posts: :not_nil
292
+ }
293
+ },
294
+ {
295
+ id: 4,
296
+ name: 'happy',
297
+ links: {
298
+ posts: :not_nil
299
+ }
300
+ },
301
+ {
302
+ id: 5,
303
+ name: 'JR',
304
+ links: {
305
+ posts: [2,11]
306
+ }
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))
340
+ end
341
+
342
+ def test_serializer_array_of_resources_limited_fields
343
+
344
+ posts = []
345
+ Post.find(1,2).each do |post|
346
+ posts.push PostResource.new(post)
347
+ end
348
+
349
+ 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
+ }))
428
+ end
429
+ end