-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcircular_association_test.rb
320 lines (270 loc) · 7.71 KB
/
circular_association_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# frozen_string_literal: true
# Based on https://github.com/jsonapi-serializer/comparisons
require_relative '../test_helper'
require 'securerandom'
require 'ffaker'
class CircularAssociationTest < Minitest::Test
class Author
attr_accessor :id, :first_name, :last_name, :books, :book_ids
def initialize(id, first_name, last_name, books, book_ids)
@id = id
@first_name = first_name
@last_name = last_name
@books = books
@book_ids = book_ids
end
end
class Genre
attr_accessor :id, :title, :description, :books, :book_ids
def initialize(id, title, description, books, book_ids)
@id = id
@title = title
@description = description
@books = books
@book_ids = book_ids
end
end
class Book
attr_accessor :id, :title, :description, :published_at, :authors, :author_ids, :genre, :genre_id
def initialize(id, title, description, published_at, authors, author_ids, genre, genre_id)
@id = id
@title = title
@description = description
@published_at = published_at
@authors = authors
@author_ids = author_ids
@genre = genre
@genre_id = genre_id
end
def sync
self.author_ids = authors.map do |a|
a.books << self
a.book_ids << id
a.id
end
self.genre_id = genre.id
genre.books << self
genre.book_ids << id
self
end
end
class AuthorResource
include Alba::Resource
root_key!
attributes :id, :first_name, :last_name
has_many :books, resource: 'CircularAssociationTest::BookResource'
end
class GenreResource
include Alba::Resource
root_key!
attributes :id, :title, :description
has_many :books, resource: 'CircularAssociationTest::BookResource'
end
class BookResource
include Alba::Resource
root_key!
attributes :id, :title, :description, :published_at
has_many :authors, resource: 'CircularAssociationTest::AuthorResource'
one :genre, resource: 'CircularAssociationTest::GenreResource'
end
def setup
Alba.inflector = :active_support
@authors = Array.new(100) do
Author.new(
SecureRandom.uuid,
FFaker::Name.first_name,
FFaker::Name.last_name,
[],
[]
)
end
@genres = Array.new(10) do
Genre.new(
SecureRandom.uuid,
FFaker::Book.genre,
FFaker::Book.description,
[],
[]
)
end
@books = Array.new(100) do
Book.new(
SecureRandom.uuid,
FFaker::Book.title,
FFaker::Book.description,
FFaker::Time.datetime,
@authors.sample(rand(1..5)),
[],
@genres.sample,
nil
).sync
end
end
def teardown
Alba.inflector = nil
end
def test_within_option_works_for_serialize
book = @books.sample
result = JSON.parse(BookResource.new(book, within: {authors: :books, genre: :books}).serialize)
assert result['book']['authors'][0]['books']
assert books = result.dig('book', 'genre', 'books')
refute books.first['authors']
end
def test_within_option_that_deeply_nested
book = @books.sample
result = JSON.parse(BookResource.new(book, within: {authors: {books: {authors: :books}}, genre: :books}).serialize)
assert result['book']['authors'][0]['books'][0]['authors'][0]['books']
obj = result['book']['authors'][0]['books'][0]['authors'][0]['books'][0]
refute obj.key?('authors')
refute result['book']['authors'][0]['books'][0]['authors'][0]['books'][0]['authors']
end
def test_within_ignores_typo
book = @books.sample
result = JSON.parse(BookResource.new(book, within: {authors: :boks, genre: :boks}).serialize)
assert result['book']['authors']
refute result['book']['authors'][0]['books']
refute result.dig('book', 'genre', 'books')
end
def test_within_option_with_nil_value_works_for_serialize
book = @books.sample
result = JSON.parse(BookResource.new(book, within: nil).serialize)
assert result['book']
refute result['book'].key?('authors')
refute result.dig('book', 'authors')
end
def test_within_option_with_false_value_works_for_serialize
book = @books.sample
result = JSON.parse(BookResource.new(book, within: false).serialize)
assert result['book']
refute result.dig('book', 'authors')
end
def test_within_option_works_for_serialize_with_collection
books = @books.sample(3)
result = JSON.parse(BookResource.new(books, within: {authors: :books, genre: :books}).serialize(root_key: :books))
assert books = result['books']
assert books[0]['authors'][0]['books']
refute books[0]['authors'][0]['books'][0]['authors']
end
def test_within_option_with_array_end_works_for_serialize
book = @books.sample
result = JSON.parse(BookResource.new(book, within: [:authors, :genre]).serialize)
assert authors = result.dig('book', 'authors')
refute authors[0]['books']
refute result.dig('book', 'genre', 'books')
end
def test_within_option_with_array_end_that_does_not_include_all_associations
book = @books.sample
result = JSON.parse(BookResource.new(book, within: [:authors]).serialize)
assert result['book']['authors']
refute result['book']['genre']
end
def test_within_option_with_invalid_type
book = @books.sample
assert_raises Alba::Error do
BookResource.new(book, within: 'book').serialize # This is not supported
end
end
class User
attr_accessor :id, :full_name, :company
def type
'users'
end
end
class Company
attr_accessor :id, :name, :users
def type
'companies'
end
end
class UserResource
include Alba::Resource
attributes :type, :id
nested_attribute :attributes do
attributes :full_name
end
nested_attribute :relationships do
has_one :company, resource: CompanyResource
end
end
class CompanyResource
include Alba::Resource
attributes :type, :id
nested_attribute :attributes do
attributes :name
end
nested_attribute :relationships do
has_many :users, resource: UserResource
end
end
# rubocop:disable Style/StringHashKeys
def test_within_for_nested_attributes
company = Company.new.tap do |c|
c.id = 42
c.name = 'a_new_company'
end
users = [
User.new.tap do |u|
u.id = 43
u.full_name = 'a_new_user_0'
end
]
company.users = users
users.each { |u| u.company = company }
result1 = {
'type' => 'companies',
'id' => 42,
'attributes' => {
'name' => 'a_new_company'
},
'relationships' => {
'users' => [
{
'type' => 'users',
'id' => 43,
'attributes' => {
'full_name' => 'a_new_user_0'
},
'relationships' => {}
}
]
}
}
assert_equal(
result1,
CompanyResource.new(company, within: [:users]).to_h
)
result2 = {
'type' => 'companies',
'id' => 42,
'attributes' => {
'name' => 'a_new_company'
},
'relationships' => {
'users' => [
{
'type' => 'users',
'id' => 43,
'attributes' => {
'full_name' => 'a_new_user_0'
},
'relationships' => {
'company' => {
'type' => 'companies',
'id' => 42,
'attributes' => {
'name' => 'a_new_company'
},
'relationships' => {}
}
}
}
]
}
}
assert_equal(
result2,
CompanyResource.new(company, within: {users: :company}).to_h
)
end
# rubocop:enable Style/StringHashKeys
end