ree_lib 1.0.67 → 1.0.69

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,981 @@
1
+ # frozen_string_literal: true
2
+ require 'faker'
3
+ require 'benchmark'
4
+
5
+ RSpec.describe :agg do
6
+ link :agg, from: :ree_dao
7
+ link :build_pg_connection, from: :ree_dao
8
+
9
+ after do
10
+ Ree.disable_irb_mode
11
+ end
12
+
13
+ before :all do
14
+ connection = build_pg_connection(ReeDaoAggTest::Db::DB_CONFIG)
15
+
16
+ connection.drop_table(:organizations, cascade: true) if connection.table_exists?(:organizations)
17
+ connection.drop_table(:users, cascade: true) if connection.table_exists?(:users)
18
+ connection.drop_table(:user_passports, cascade: true) if connection.table_exists?(:user_passports)
19
+ connection.drop_table(:books, cascade: true) if connection.table_exists?(:books)
20
+ connection.drop_table(:chapters, cascade: true) if connection.table_exists?(:chapters)
21
+ connection.drop_table(:avtorki, cascade: true) if connection.table_exists?(:avtorki)
22
+ connection.drop_table(:reviews, cascade: true) if connection.table_exists?(:reviews)
23
+ connection.drop_table(:review_authors, cascade: true) if connection.table_exists?(:review_authors)
24
+
25
+ connection.create_table :organizations do
26
+ primary_key :id
27
+
28
+ column :name, 'varchar(256)'
29
+ end
30
+
31
+ connection.create_table :users do
32
+ primary_key :id
33
+
34
+ column :name, 'varchar(256)'
35
+ column :age, :integer
36
+ foreign_key :organization_id, :organizations, null: false, on_delete: :cascade
37
+ end
38
+
39
+ connection.create_table :user_passports do
40
+ primary_key :id
41
+
42
+ foreign_key :user_id, :users, null: false, on_delete: :cascade
43
+ column :info, 'varchar(256)'
44
+ end
45
+
46
+ connection.create_table :books do
47
+ primary_key :id
48
+
49
+ foreign_key :user_id, :users, null: false, on_delete: :cascade
50
+ column :title, 'varchar(256)'
51
+ end
52
+
53
+ connection.create_table :chapters do
54
+ primary_key :id
55
+
56
+ foreign_key :book_id, :books, null: false, on_delete: :cascade
57
+ column :title, 'varchar(256)'
58
+ end
59
+
60
+ connection.create_table :reviews do
61
+ primary_key :id
62
+
63
+ foreign_key :book_id, :books, null: false, on_delete: :cascade
64
+ column :rating, :integer
65
+ end
66
+
67
+ connection.create_table :review_authors do
68
+ primary_key :id
69
+
70
+ foreign_key :review_id, :reviews, null: false, on_delete: :cascade
71
+ column :name, 'varchar(256)'
72
+ end
73
+
74
+ connection.create_table :avtorki do
75
+ primary_key :id
76
+
77
+ foreign_key :book_id, :books, null: false, on_delete: :cascade
78
+ column :name, 'varchar(256)'
79
+ end
80
+
81
+ connection.disconnect
82
+ end
83
+
84
+ require_relative 'ree_dao_agg_test'
85
+
86
+ class ReeDaoAggTest::AggUsers
87
+ include ReeDao::AggregateDSL
88
+
89
+ aggregate :agg_users do
90
+ link :users, from: :ree_dao_agg_test
91
+ link :organizations_dao, from: :ree_dao_agg_test
92
+ link :user_passports, from: :ree_dao_agg_test
93
+ link :books, from: :ree_dao_agg_test
94
+ link :chapters, from: :ree_dao_agg_test
95
+ link :authors, from: :ree_dao_agg_test
96
+ link :reviews, from: :ree_dao_agg_test
97
+ link :review_authors, from: :ree_dao_agg_test
98
+ link :agg, from: :ree_dao
99
+ end
100
+
101
+ def call(ids_or_scope, **opts)
102
+ agg(users, ids_or_scope, **opts) do |users_list|
103
+ belongs_to :organization
104
+ has_many :books do |books_list|
105
+ has_one :author
106
+ has_many :chapters
107
+
108
+ has_many :reviews, -> { reviews_opts } do |reviews_list|
109
+ has_one :review_author
110
+
111
+ field :review_calculatetable_field, -> { some_method(reviews_list) }
112
+ end
113
+
114
+ field :book_calculatetable_field, -> { change_book_titles(books_list) }
115
+ end
116
+
117
+ has_one :passport, -> { passport_opts }
118
+ has_one :custom_field, -> { custom_field_opts }
119
+
120
+ field :user_calculatetable_field, -> { some_method(users_list) }
121
+ end
122
+ end
123
+
124
+ private
125
+
126
+ def change_book_titles(books_list)
127
+ books_list.each do |book|
128
+ book.title = "#{book.title.upcase} changed"
129
+ end
130
+ end
131
+
132
+ def some_method(list)
133
+ # puts list.map(&:id)
134
+ # puts list.map { _1.class.name }
135
+ end
136
+
137
+ def passport_opts
138
+ {
139
+ foreign_key: :user_id,
140
+ scope: user_passports
141
+ }
142
+ end
143
+
144
+ def custom_field_opts
145
+ {
146
+ scope: books.where(title: "1984")
147
+ }
148
+ end
149
+
150
+ def reviews_opts
151
+ { autoload_children: true }
152
+ end
153
+ end
154
+
155
+ class ReeDaoAggTest::AggUsersWithDto
156
+ include ReeDao::AggregateDSL
157
+
158
+ aggregate :agg_users_with_dto do
159
+ link :users, from: :ree_dao_agg_test
160
+ link :books, from: :ree_dao_agg_test
161
+ link :authors, from: :ree_dao_agg_test
162
+ link :chapters, from: :ree_dao_agg_test
163
+ link :organizations_dao, from: :ree_dao_agg_test
164
+ link :agg, from: :ree_dao
165
+ end
166
+
167
+ def call(ids_or_scope, **opts)
168
+ agg(users, ids_or_scope, **opts) do
169
+ belongs_to :organization
170
+
171
+ has_many :books, -> { books_opts } do
172
+ has_one :author, -> { author_opts }
173
+ has_many :chapters, -> { chapters_opts }
174
+ end
175
+ end
176
+ end
177
+
178
+ private
179
+
180
+ def books_opts
181
+ {
182
+ to_dto: -> (book) { ReeDaoAggTest::BookDto.new(book) },
183
+ setter: -> (item, child_index) {
184
+ item.set_books(child_index[item.id] || [])
185
+ }
186
+ }
187
+ end
188
+
189
+ def author_opts
190
+ { to_dto: -> (author) { ReeDaoAggTest::AuthorDto.new(author) }}
191
+ end
192
+
193
+ def chapters_opts
194
+ { to_dto: -> (chapter) { ReeDaoAggTest::ChapterDto.new(chapter) }}
195
+ end
196
+ end
197
+
198
+ class ReeDaoAggTest::AggUsersAutoloadBooksChildren
199
+ include ReeDao::AggregateDSL
200
+
201
+ aggregate :agg_users_autoload_books_children do
202
+ link :users, from: :ree_dao_agg_test
203
+ link :books, from: :ree_dao_agg_test
204
+ link :chapters, from: :ree_dao_agg_test
205
+ link :authors, from: :ree_dao_agg_test
206
+ link :reviews, from: :ree_dao_agg_test
207
+ link :review_authors, from: :ree_dao_agg_test
208
+ link :agg, from: :ree_dao
209
+ end
210
+
211
+ def call(ids_or_scope, **opts)
212
+ agg(users, ids_or_scope, **opts) do
213
+ belongs_to :organization
214
+ has_many :books, -> { books_opts } do
215
+ has_one :author
216
+ has_many :chapters
217
+
218
+ has_many :reviews do
219
+ has_one :review_author
220
+ end
221
+ end
222
+ end
223
+ end
224
+
225
+ private
226
+
227
+ def books_opts
228
+ { autoload_children: true }
229
+ end
230
+ end
231
+
232
+ class ReeDaoAggTest::AggUsersAutoloadReviewsChildren
233
+ include ReeDao::AggregateDSL
234
+
235
+ aggregate :agg_users_autoload_reviews_children do
236
+ link :users, from: :ree_dao_agg_test
237
+ link :books, from: :ree_dao_agg_test
238
+ link :chapters, from: :ree_dao_agg_test
239
+ link :authors, from: :ree_dao_agg_test
240
+ link :reviews, from: :ree_dao_agg_test
241
+ link :review_authors, from: :ree_dao_agg_test
242
+ link :agg, from: :ree_dao
243
+ end
244
+
245
+ def call(ids_or_scope, **opts)
246
+ agg(users, ids_or_scope, **opts) do
247
+ belongs_to :organization
248
+ has_many :books do
249
+ has_one :author
250
+ has_many :chapters
251
+
252
+ has_many :reviews, -> { { autoload_children: true } } do
253
+ has_one :review_author
254
+ end
255
+ end
256
+ end
257
+ end
258
+
259
+ private
260
+
261
+ def reviews_opts
262
+ { autoload_children: true }
263
+ end
264
+ end
265
+
266
+ class ReeDaoAggTest::AggUsersBlockTest
267
+ include ReeDao::AggregateDSL
268
+
269
+ aggregate :agg_users_block_test do
270
+ link :users, from: :ree_dao_agg_test
271
+ link :organizations_dao, from: :ree_dao_agg_test
272
+ link :books, from: :ree_dao_agg_test
273
+ link :agg, from: :ree_dao
274
+ end
275
+
276
+ def call(ids_or_scope, **opts)
277
+ agg(users, ids_or_scope, **opts) do
278
+ belongs_to :organization
279
+ has_many :books, -> { books_opts }
280
+ end
281
+ end
282
+
283
+ private
284
+
285
+ def books_opts
286
+ {
287
+ setter: -> (item, items_index) {
288
+ b = items_index[item.id].each { |b| b.title = "Changed" }
289
+ item.set_books(b)
290
+ }
291
+ }
292
+ end
293
+ end
294
+
295
+ class ReeDaoAggTest::AggUsersScopeMethodTest
296
+ include ReeDao::AggregateDSL
297
+
298
+ aggregate :agg_users_scope_method_test do
299
+ link :users, from: :ree_dao_agg_test
300
+ link :organizations_dao, from: :ree_dao_agg_test
301
+ link :books, from: :ree_dao_agg_test
302
+ link :agg, from: :ree_dao
303
+ end
304
+
305
+ def call(ids_or_scope, **opts)
306
+ agg(users, ids_or_scope, **opts) do |agg_list|
307
+ some_id = agg_list.first.id
308
+ title = "1984"
309
+ belongs_to :organization
310
+
311
+ has_many :books, -> { books_opts(title) }
312
+ has_many :active_books, books
313
+ end
314
+ end
315
+
316
+ private
317
+
318
+ def books_opts(title)
319
+ { scope: books_scope(title) }
320
+ end
321
+
322
+ def books_scope(title)
323
+ books.where(title: title)
324
+ end
325
+ end
326
+
327
+ class ReeDaoAggTest::AggUsersOnlyDataset
328
+ include ReeDao::AggregateDSL
329
+
330
+ aggregate :agg_users_only_dataset do
331
+ link :users, from: :ree_dao_agg_test
332
+ link :books, from: :ree_dao_agg_test
333
+ link :agg, from: :ree_dao
334
+ end
335
+
336
+ def call(ids_or_scope, **opts)
337
+ agg(users, ids_or_scope, **opts) do
338
+ has_many :books, books.where(title: "1408")
339
+ end
340
+ end
341
+ end
342
+
343
+ class ReeDaoAggTest::AggUsersWithoutDao
344
+ include ReeDao::AggregateDSL
345
+
346
+ aggregate :agg_users_without_dao do
347
+ link :users, from: :ree_dao_agg_test
348
+ link :organizations_dao, from: :ree_dao_agg_test
349
+ link :books, from: :ree_dao_agg_test
350
+ link :agg, from: :ree_dao
351
+ end
352
+
353
+ def call(ids_or_scope, **opts)
354
+ agg(users, ids_or_scope, **opts) do
355
+ has_many :something
356
+ end
357
+ end
358
+ end
359
+
360
+ class ReeDaoAggTest::AggUsersOnlyExceptKeys
361
+ include ReeDao::AggregateDSL
362
+
363
+ aggregate :agg_users_only_except_keys do
364
+ link :users, from: :ree_dao_agg_test
365
+ link :organizations_dao, from: :ree_dao_agg_test
366
+ link :books, from: :ree_dao_agg_test
367
+ link :agg, from: :ree_dao
368
+ end
369
+
370
+ def call(ids_or_scope, **opts)
371
+ agg(users, ids_or_scope, **opts) do
372
+ belongs_to :organization
373
+ has_many :books
374
+ end
375
+ end
376
+ end
377
+
378
+ let(:agg_users) { ReeDaoAggTest::AggUsers.new }
379
+ let(:agg_users_block) { ReeDaoAggTest::AggUsersBlockTest.new }
380
+ let(:agg_users_scope_method) { ReeDaoAggTest::AggUsersScopeMethodTest.new }
381
+ let(:agg_users_autoload_books_children) { ReeDaoAggTest::AggUsersAutoloadBooksChildren.new }
382
+ let(:agg_users_autoload_reviews_children) { ReeDaoAggTest::AggUsersAutoloadReviewsChildren.new }
383
+ let(:agg_users_without_dao) { ReeDaoAggTest::AggUsersWithoutDao.new }
384
+ let(:agg_users_with_dto) { ReeDaoAggTest::AggUsersWithDto.new }
385
+ let(:agg_users_only_dataset) { ReeDaoAggTest::AggUsersOnlyDataset.new }
386
+ let(:user_agg_only_except_keys) { ReeDaoAggTest::AggUsersOnlyExceptCase.new }
387
+ let(:organizations) { ReeDaoAggTest::OrganizationsDao.new }
388
+ let(:users) { ReeDaoAggTest::Users.new }
389
+ let(:user_passports) { ReeDaoAggTest::UserPassports.new }
390
+ let(:books) { ReeDaoAggTest::Books.new }
391
+ let(:chapters) { ReeDaoAggTest::Chapters.new }
392
+ let(:authors) { ReeDaoAggTest::Authors.new }
393
+ let(:reviews) { ReeDaoAggTest::Reviews.new }
394
+ let(:review_authors) { ReeDaoAggTest::ReviewAuthors.new }
395
+
396
+ it {
397
+ organizations.delete_all
398
+ users.delete_all
399
+
400
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
401
+ organizations.put(organization)
402
+
403
+ user = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
404
+ users.put(user)
405
+
406
+ expect {
407
+ agg_users_without_dao.call(users.all)
408
+ }.to raise_error(ArgumentError)
409
+ }
410
+
411
+ it {
412
+ organizations.delete_all
413
+ users.delete_all
414
+ user_passports.delete_all
415
+ books.delete_all
416
+ chapters.delete_all
417
+
418
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
419
+ organizations.put(organization)
420
+
421
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
422
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
423
+ users.put(user_1)
424
+ users.put(user_2)
425
+
426
+ passport_1 = ReeDaoAggTest::UserPassport.new(user_id: user_1.id, info: "some info")
427
+ user_passports.put(passport_1)
428
+ user_passports.put(ReeDaoAggTest::UserPassport.new(user_id: user_2.id, info: "another info"))
429
+
430
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
431
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
432
+
433
+ books.put(book_1)
434
+ books.put(book_2)
435
+
436
+ chapter = ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "beginning")
437
+ chapters.put(chapter)
438
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "interlude"))
439
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "tragic ending"))
440
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "beginning"))
441
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "ending"))
442
+
443
+
444
+ authors.put(ReeDaoAggTest::Author.new(book_id: book_1.id, name: "George Orwell"))
445
+ review = ReeDaoAggTest::Review.new(book_id: book_1.id, rating: 10)
446
+ reviews.put(review)
447
+ reviews.put(ReeDaoAggTest::Review.new(book_id: book_1.id, rating: 7))
448
+ review_authors.put(ReeDaoAggTest::ReviewAuthor.new(review_id: review.id, name: "John Review"))
449
+
450
+ res = agg_users.call(
451
+ users.all,
452
+ only: [:books, :reviews],
453
+ except: [:review_author]
454
+ )
455
+
456
+ expect(res.first.books.first.reviews.first.review_author).to eq(nil)
457
+ }
458
+
459
+ it {
460
+ organizations.delete_all
461
+ users.delete_all
462
+
463
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
464
+ organizations.put(organization)
465
+
466
+ user = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
467
+ users.put(user)
468
+
469
+ expect {
470
+ agg_users_without_dao.call(users.all, only: [:books], except: [:books])
471
+ }.to raise_error(ArgumentError, "you can't use both :only and :except for \"books\" keys")
472
+ }
473
+
474
+ it {
475
+ organizations.delete_all
476
+ users.delete_all
477
+
478
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
479
+ organizations.put(organization)
480
+
481
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
482
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
483
+ users.put(user_1)
484
+ users.put(user_2)
485
+
486
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
487
+ books.put(book_1)
488
+
489
+ authors.put(ReeDaoAggTest::Author.new(book_id: book_1.id, name: "George Orwell"))
490
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "interlude"))
491
+
492
+ res = agg_users_with_dto.call(
493
+ users.all,
494
+ to_dto: -> (user) {
495
+ ReeDaoAggTest::UserDto.new(
496
+ id: user.id,
497
+ name: user.name,
498
+ organization_id: user.organization_id,
499
+ full_name: user.name
500
+ )
501
+ }
502
+ )
503
+
504
+ book = res.first.books.first
505
+
506
+ expect(res.first.class).to eq(ReeDaoAggTest::UserDto)
507
+ expect(book.class).to eq(ReeDaoAggTest::BookDto)
508
+ expect(book.author.class).to eq(ReeDaoAggTest::AuthorDto)
509
+ expect(book.chapters.first.class).to eq(ReeDaoAggTest::ChapterDto)
510
+ }
511
+
512
+ it {
513
+ organizations.delete_all
514
+ users.delete_all
515
+
516
+ org = ReeDaoAggTest::Organization.new(name: "Test Org")
517
+ organizations.put(org)
518
+
519
+ user = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: org.id)
520
+ users.put(user)
521
+
522
+ book_1 = ReeDaoAggTest::Book.new(user_id: user.id, title: "1984")
523
+ book_2 = ReeDaoAggTest::Book.new(user_id: user.id, title: "1408")
524
+ book_3 = ReeDaoAggTest::Book.new(user_id: user.id, title: "1408")
525
+
526
+ books.put(book_1)
527
+ books.put(book_2)
528
+ books.put(book_3)
529
+
530
+ res = agg_users_only_dataset.call(users.where(name: "John"))
531
+
532
+ user = res[0]
533
+ expect(user.books.map(&:title).uniq).to eq(["1408"])
534
+ }
535
+
536
+ it {
537
+ organizations.delete_all
538
+ users.delete_all
539
+ user_passports.delete_all
540
+ books.delete_all
541
+ chapters.delete_all
542
+
543
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
544
+ organizations.put(organization)
545
+
546
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
547
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
548
+ users.put(user_1)
549
+ users.put(user_2)
550
+
551
+ passport_1 = ReeDaoAggTest::UserPassport.new(user_id: user_1.id, info: "some info")
552
+ user_passports.put(passport_1)
553
+ user_passports.put(ReeDaoAggTest::UserPassport.new(user_id: user_2.id, info: "another info"))
554
+
555
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
556
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
557
+
558
+ books.put(book_1)
559
+ books.put(book_2)
560
+
561
+ chapter = ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "beginning")
562
+ chapters.put(chapter)
563
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "interlude"))
564
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "tragic ending"))
565
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "beginning"))
566
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "ending"))
567
+
568
+
569
+ authors.put(ReeDaoAggTest::Author.new(book_id: book_1.id, name: "George Orwell"))
570
+ review = ReeDaoAggTest::Review.new(book_id: book_1.id, rating: 10)
571
+ reviews.put(review)
572
+ reviews.put(ReeDaoAggTest::Review.new(book_id: book_1.id, rating: 7))
573
+ review_authors.put(ReeDaoAggTest::ReviewAuthor.new(review_id: review.id, name: "John Review"))
574
+
575
+ res = agg_users.call(
576
+ users.all,
577
+ chapters: -> (scope) { scope.ids(chapter.id) }
578
+ )
579
+
580
+ res_user = res[0]
581
+ expect(res_user.id).to eq(user_1.id)
582
+ expect(res_user.organization).to eq(organization)
583
+ expect(res_user.passport).to eq(passport_1)
584
+ expect(res_user.passport.info).to eq("some info")
585
+ expect(res_user.books.count).to eq(2)
586
+ expect(res_user.books.map(&:title)).to eq(["1984 changed", "1408 changed"])
587
+ expect(res_user.books[0].author.name).to eq("George Orwell")
588
+ expect(res_user.books[0].chapters.map(&:title)).to eq(["beginning"])
589
+ expect(res_user.books[0].reviews[0].review_author.name).to eq("John Review")
590
+ expect(res_user.custom_field).to_not eq(nil)
591
+ }
592
+
593
+ it {
594
+ organizations.delete_all
595
+ users.delete_all
596
+ user_passports.delete_all
597
+ books.delete_all
598
+ chapters.delete_all
599
+
600
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
601
+ organizations.put(organization)
602
+
603
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
604
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
605
+ users.put(user_1)
606
+ users.put(user_2)
607
+
608
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
609
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
610
+
611
+ books.put(book_1)
612
+ books.put(book_2)
613
+
614
+ author_1 = ReeDaoAggTest::Author.new(book_id: book_1.id, name: "George Orwell")
615
+ author_2 = ReeDaoAggTest::Author.new(book_id: book_2.id, name: "Stephen King")
616
+ authors.put(author_1)
617
+ authors.put(author_2)
618
+
619
+ res = agg_users.call(
620
+ users.all
621
+ )
622
+
623
+ expect(res[0].books.first.author).to_not eq(nil)
624
+
625
+ authors.delete(author_1)
626
+ authors.delete(author_2)
627
+
628
+ res = agg_users.call(
629
+ users.all
630
+ )
631
+
632
+ expect(res[0].books[0].author).to eq(nil)
633
+ expect(res[0].books[1].author).to eq(nil)
634
+ }
635
+
636
+ it {
637
+ organizations.delete_all
638
+ users.delete_all
639
+ user_passports.delete_all
640
+ books.delete_all
641
+ chapters.delete_all
642
+
643
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
644
+ organizations.put(organization)
645
+
646
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
647
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
648
+ users.put(user_1)
649
+ users.put(user_2)
650
+
651
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
652
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
653
+
654
+ books.put(book_1)
655
+ books.put(book_2)
656
+
657
+ res = agg_users.call(
658
+ users.all
659
+ )
660
+
661
+ expect(res[0].books).to_not eq([])
662
+ expect(res[1].books).to eq([])
663
+
664
+ books.delete(book_1)
665
+ books.delete(book_2)
666
+
667
+ res = agg_users.call(
668
+ users.all
669
+ )
670
+
671
+ expect(res[0].books).to eq([])
672
+ expect(res[1].books).to eq([])
673
+ }
674
+
675
+ it {
676
+ organizations.delete_all
677
+ users.delete_all
678
+ books.delete_all
679
+
680
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
681
+ organizations.put(organization)
682
+
683
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
684
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
685
+ users.put(user_1)
686
+ users.put(user_2)
687
+
688
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
689
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
690
+
691
+ books.put(book_1)
692
+ books.put(book_2)
693
+
694
+ res = agg_users_scope_method.call(users.all)
695
+
696
+ res_user = res[0]
697
+ expect(res_user.id).to eq(user_1.id)
698
+ expect(res_user.organization).to eq(organization)
699
+ expect(res_user.books.count).to eq(1)
700
+ expect(res_user.active_books.count).to eq(2)
701
+ }
702
+
703
+ it {
704
+ organizations.delete_all
705
+ users.delete_all
706
+ books.delete_all
707
+
708
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
709
+ organizations.put(organization)
710
+
711
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
712
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
713
+ users.put(user_1)
714
+ users.put(user_2)
715
+
716
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
717
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
718
+
719
+ books.put(book_1)
720
+ books.put(book_2)
721
+
722
+ expect {
723
+ agg_users_scope_method.call(users.where(name: "Another names"))
724
+ }.to_not raise_error
725
+ }
726
+
727
+ it {
728
+ organizations.delete_all
729
+ users.delete_all
730
+ user_passports.delete_all
731
+ books.delete_all
732
+ chapters.delete_all
733
+
734
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
735
+ organizations.put(organization)
736
+
737
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
738
+ users.put(user_1)
739
+
740
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
741
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
742
+
743
+ books.put(book_1)
744
+ books.put(book_2)
745
+
746
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "beginning"))
747
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "interlude"))
748
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "tragic ending"))
749
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "beginning"))
750
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "ending"))
751
+
752
+ res = agg_users.call(
753
+ users.all,
754
+ only: [:books, :chapters]
755
+ )
756
+
757
+ u = res[0]
758
+ expect(u.books.count).to eq(2)
759
+ expect(u.books[0].chapters.count).to_not eq(0)
760
+ }
761
+
762
+ it {
763
+ organizations.delete_all
764
+ users.delete_all
765
+ user_passports.delete_all
766
+ books.delete_all
767
+ chapters.delete_all
768
+
769
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
770
+ organizations.put(organization)
771
+
772
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
773
+ users.put(user_1)
774
+
775
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
776
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
777
+
778
+ books.put(book_1)
779
+ books.put(book_2)
780
+
781
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "beginning"))
782
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "interlude"))
783
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "tragic ending"))
784
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "beginning"))
785
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "ending"))
786
+
787
+ res = agg_users.call(
788
+ users.all,
789
+ only: [:books, :chapters]
790
+ )
791
+
792
+ u = res[0]
793
+ expect(u.books.count).to eq(2)
794
+ expect(u.books[0].chapters.count).to_not eq(0)
795
+ }
796
+
797
+ it {
798
+ organizations.delete_all
799
+ users.delete_all
800
+ user_passports.delete_all
801
+ books.delete_all
802
+ chapters.delete_all
803
+
804
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
805
+ organizations.put(organization)
806
+
807
+ user = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
808
+ users.put(user)
809
+
810
+ book = ReeDaoAggTest::Book.new(user_id: user.id, title: "1984")
811
+ books.put(book)
812
+
813
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book.id, title: "beginning"))
814
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book.id, title: "interlude"))
815
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book.id, title: "tragic ending"))
816
+
817
+ authors.put(ReeDaoAggTest::Author.new(book_id: book.id, name: "George Orwell"))
818
+
819
+ review = ReeDaoAggTest::Review.new(book_id: book.id, rating: 5)
820
+ reviews.put(review)
821
+ review_authors.put(ReeDaoAggTest::ReviewAuthor.new(review_id: review.id, name: "John"))
822
+
823
+ res = agg_users_autoload_books_children.call(
824
+ users.all,
825
+ only: [:books]
826
+ )
827
+
828
+ u = res[0]
829
+ expect(u.books).to_not eq(nil)
830
+ expect(u.books[0].chapters).to_not eq(nil)
831
+ expect(u.books[0].author).to_not eq(nil)
832
+ expect(u.books[0].reviews).to_not eq(nil)
833
+ expect(u.books[0].reviews[0].review_author).to eq(nil)
834
+ }
835
+
836
+ it {
837
+ organizations.delete_all
838
+ users.delete_all
839
+ user_passports.delete_all
840
+ books.delete_all
841
+ chapters.delete_all
842
+
843
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
844
+ organizations.put(organization)
845
+
846
+ user = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
847
+ users.put(user)
848
+
849
+ book = ReeDaoAggTest::Book.new(user_id: user.id, title: "1984")
850
+ books.put(book)
851
+
852
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book.id, title: "beginning"))
853
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book.id, title: "interlude"))
854
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book.id, title: "tragic ending"))
855
+
856
+ authors.put(ReeDaoAggTest::Author.new(book_id: book.id, name: "George Orwell"))
857
+
858
+ review = ReeDaoAggTest::Review.new(book_id: book.id, rating: 5)
859
+ reviews.put(review)
860
+ review_authors.put(ReeDaoAggTest::ReviewAuthor.new(review_id: review.id, name: "John"))
861
+
862
+ res = agg_users_autoload_reviews_children.call(
863
+ users.all,
864
+ only: [:books, :reviews]
865
+ )
866
+
867
+ u = res[0]
868
+ expect(u.books).to_not eq(nil)
869
+ expect(u.books[0].chapters).to eq(nil)
870
+ expect(u.books[0].author).to eq(nil)
871
+ expect(u.books[0].reviews).to_not eq(nil)
872
+ expect(u.books[0].reviews[0].review_author).to_not eq(nil)
873
+ }
874
+
875
+ it {
876
+ organizations.delete_all
877
+ users.delete_all
878
+ user_passports.delete_all
879
+ books.delete_all
880
+
881
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
882
+ organizations.put(organization)
883
+
884
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
885
+ users.put(user_1)
886
+
887
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
888
+
889
+ books.put(book_1)
890
+
891
+ res = agg_users_block.call(user_1.id)
892
+
893
+ u = res[0]
894
+ expect(u.books.map(&:title)).to eq(["Changed"])
895
+ }
896
+
897
+ it {
898
+ organizations.delete_all
899
+ users.delete_all
900
+ user_passports.delete_all
901
+ books.delete_all
902
+ chapters.delete_all
903
+
904
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
905
+ organizations.put(organization)
906
+
907
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
908
+ users.put(user_1)
909
+
910
+ book_1 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1984")
911
+ book_2 = ReeDaoAggTest::Book.new(user_id: user_1.id, title: "1408")
912
+
913
+ books.put(book_1)
914
+ books.put(book_2)
915
+
916
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "beginning"))
917
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "interlude"))
918
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_1.id, title: "tragic ending"))
919
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "beginning"))
920
+ chapters.put(ReeDaoAggTest::Chapter.new(book_id: book_2.id, title: "ending"))
921
+
922
+ res = agg_users.call(
923
+ users.all,
924
+ except: [:organization, :passport, :custom_field]
925
+ )
926
+
927
+ u = res[0]
928
+ expect(u.books.count).to eq(2)
929
+ expect(u.passport).to eq(nil)
930
+ expect(u.organization).to eq(nil)
931
+ expect(u.custom_field).to eq(nil)
932
+ }
933
+
934
+ it {
935
+ organizations.delete_all
936
+ users.delete_all
937
+
938
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
939
+ organizations.put(organization)
940
+
941
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
942
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
943
+ users.put(user_1)
944
+ users.put(user_2)
945
+
946
+ ids = [user_1, user_2].map(&:id)
947
+
948
+ res = agg(users, ids)
949
+ expect(res.count).to eq(2)
950
+ }
951
+
952
+ it {
953
+ organizations.delete_all
954
+ users.delete_all
955
+
956
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
957
+ organizations.put(organization)
958
+
959
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
960
+ users.put(user_1)
961
+
962
+ res = agg(users, user_1.id)
963
+ expect(res.count).to eq(1)
964
+ }
965
+
966
+ it {
967
+ organizations.delete_all
968
+ users.delete_all
969
+
970
+ organization = ReeDaoAggTest::Organization.new(name: "Test Org")
971
+ organizations.put(organization)
972
+
973
+ user_1 = ReeDaoAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
974
+ user_2 = ReeDaoAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
975
+ users.put(user_1)
976
+ users.put(user_2)
977
+
978
+ res = agg(users, users.where(organization_id: organization.id))
979
+ expect(res.count).to eq(2)
980
+ }
981
+ end