active_mocker 1.2.4 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -11
- data/active_mocker.gemspec +1 -1
- data/lib/active_hash/ar_api.rb +23 -4
- data/lib/active_hash/init.rb +5 -1
- data/lib/active_mocker.rb +0 -1
- data/lib/active_mocker/collection/association.rb +12 -0
- data/lib/active_mocker/collection/base.rb +65 -0
- data/lib/active_mocker/collection/queries.rb +105 -0
- data/lib/active_mocker/collection/relation.rb +11 -0
- data/lib/active_mocker/mock_instance_methods.rb +3 -1
- data/lib/active_mocker/mock_requires.rb +3 -1
- data/lib/active_mocker/mock_template.erb +6 -6
- data/lib/active_mocker/version.rb +1 -1
- data/sample_app_rails_4/config/application.rb +1 -1
- data/sample_app_rails_4/db/schema.rb +1 -0
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +496 -42
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +19 -10
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +8 -8
- data/sample_app_rails_4/spec/mocks/user_mock.rb +22 -22
- data/sample_app_rails_4/spec/user_mock_spec.rb +0 -1
- data/spec/lib/active_mocker/{collection_association_spec.rb → collection.rb} +4 -5
- data/spec/lib/active_mocker/generate_spec.rb +19 -30
- metadata +12 -11
- data/lib/active_hash/find_by.rb +0 -31
- data/lib/active_hash/update.rb +0 -18
- data/lib/active_mocker/collection_association.rb +0 -41
@@ -19,8 +19,6 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
19
19
|
Micropost.destroy_all
|
20
20
|
end
|
21
21
|
|
22
|
-
USER_CLASSES = [User, UserMock]
|
23
|
-
|
24
22
|
|
25
23
|
let(:attributes) { {name: 'Dustin Zeisler', email: 'dustin@example.com'} }
|
26
24
|
let(:attributes_with_admin) { {name: 'Dustin Zeisler', email: 'dustin@example.com', admin: true} }
|
@@ -46,6 +44,52 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
46
44
|
UserMock.create(create_attributes)
|
47
45
|
end
|
48
46
|
|
47
|
+
context 'new with block' do
|
48
|
+
|
49
|
+
def create_with_block(klass)
|
50
|
+
user = klass.new do |u|
|
51
|
+
u.name = "David"
|
52
|
+
u.admin = true
|
53
|
+
end
|
54
|
+
|
55
|
+
expect(user.name).to eq 'David'
|
56
|
+
expect(user.admin).to eq true
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'User' do
|
61
|
+
create_with_block(User)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'UserMock' do
|
65
|
+
create_with_block(UserMock)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'create with block' do
|
71
|
+
|
72
|
+
def create_with_block(klass)
|
73
|
+
user = klass.create do |u|
|
74
|
+
u.name = "David"
|
75
|
+
u.admin = true
|
76
|
+
end
|
77
|
+
|
78
|
+
expect(user.name).to eq 'David'
|
79
|
+
expect(user.admin).to eq true
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'User' do
|
84
|
+
create_with_block(User)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'UserMock' do
|
88
|
+
create_with_block(UserMock)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
49
93
|
end
|
50
94
|
|
51
95
|
describe '#attributes' do
|
@@ -63,6 +107,74 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
63
107
|
|
64
108
|
end
|
65
109
|
|
110
|
+
describe '::all' do
|
111
|
+
|
112
|
+
def klass_all(klass)
|
113
|
+
array = [klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
114
|
+
expect(klass.all).to eq array
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'User' do
|
118
|
+
klass_all(User)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'UserMock' do
|
122
|
+
klass_all(UserMock)
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
describe '::average' do
|
128
|
+
|
129
|
+
def klass_method_average(klass)
|
130
|
+
[klass.create!(credits: 12, email: '1'), klass.create!(credits: 2, email: '2'), klass.create!(credits: 8, email: '3'), klass.create!(credits: 4, email: '4')]
|
131
|
+
expect(klass.average(:credits).to_s).to eq "6.5"
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'User' do
|
135
|
+
klass_method_average(User)
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'UserMock' do
|
139
|
+
klass_method_average(UserMock)
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
describe '::minimum' do
|
145
|
+
|
146
|
+
def klass_method_minimum(klass)
|
147
|
+
[klass.create!(credits: 12, email: '1'), klass.create!(credits: 2, email: '2'), klass.create!(credits: 8, email: '3'), klass.create!(credits: 4, email: '4')]
|
148
|
+
expect(klass.minimum(:credits).to_s).to eq "2.0"
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'User' do
|
152
|
+
klass_method_minimum(User)
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'UserMock' do
|
156
|
+
klass_method_minimum(UserMock)
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
describe '::maximum' do
|
162
|
+
|
163
|
+
def klass_method_maximum(klass)
|
164
|
+
[klass.create!(credits: 12, email: '1'), klass.create!(credits: 2, email: '2'), klass.create!(credits: 8, email: '3'), klass.create!(credits: 4, email: '4')]
|
165
|
+
expect(klass.maximum(:credits).to_s).to eq "12.0"
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'User' do
|
169
|
+
klass_method_maximum(User)
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'UserMock' do
|
173
|
+
klass_method_maximum(UserMock)
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
|
66
178
|
describe 'associations' do
|
67
179
|
|
68
180
|
let(:micropost){ Micropost.create(content: 'post')}
|
@@ -91,6 +203,14 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
91
203
|
|
92
204
|
end
|
93
205
|
|
206
|
+
describe 'attribute_names' do
|
207
|
+
|
208
|
+
it 'they are the same' do
|
209
|
+
expect(UserMock.attribute_names).to eq User.attribute_names
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
94
214
|
describe '::find_by' do
|
95
215
|
|
96
216
|
let!(:ar_record){User.create(attributes)}
|
@@ -108,16 +228,16 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
108
228
|
|
109
229
|
describe '::where' do
|
110
230
|
|
111
|
-
let(:ar_record){User.create(attributes)}
|
112
|
-
let(:mock_record){UserMock.create(attributes)}
|
113
|
-
let(:mock_record_2){UserMock.create(attributes_with_admin)}
|
231
|
+
let!(:ar_record){User.create(attributes)}
|
232
|
+
let!(:mock_record){UserMock.create(attributes)}
|
233
|
+
let!(:mock_record_2){UserMock.create(attributes_with_admin)}
|
114
234
|
|
115
235
|
it 'AR' do
|
116
236
|
expect([ar_record]).to eq User.where(attributes)
|
117
237
|
end
|
118
238
|
|
119
239
|
it 'Mock' do
|
120
|
-
expect(
|
240
|
+
expect(UserMock.where(attributes)).to eq [mock_record, mock_record_2]
|
121
241
|
end
|
122
242
|
|
123
243
|
it 'Mock will not take sql string needs to be mocked' do
|
@@ -125,6 +245,94 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
125
245
|
expect{UserMock.where("name = 'Dustin Zeisler'")}.to raise_error
|
126
246
|
end
|
127
247
|
|
248
|
+
context 'by association not only attribute'do
|
249
|
+
|
250
|
+
def where_by_association(user_class, micropost_class)
|
251
|
+
user = user_class.create!(email: '1')
|
252
|
+
micropost = micropost_class.create(user: user)
|
253
|
+
expect(micropost_class.where(user: user)).to eq [micropost]
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'User' do
|
257
|
+
where_by_association(User, Micropost)
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'UserMock' do
|
261
|
+
where_by_association(UserMock, MicropostMock)
|
262
|
+
end
|
263
|
+
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
context 'will return all if no options passed' do
|
268
|
+
|
269
|
+
def where_no_options(klass, where_klass)
|
270
|
+
records = [klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
271
|
+
expect(klass.where.class).to eq(where_klass)
|
272
|
+
end
|
273
|
+
|
274
|
+
it 'User' do
|
275
|
+
where_no_options(User, ActiveRecord::QueryMethods::WhereChain)
|
276
|
+
end
|
277
|
+
|
278
|
+
it 'UserMock' do
|
279
|
+
where_no_options(UserMock, ActiveMocker::Collection::Queries::WhereNotChain)
|
280
|
+
end
|
281
|
+
|
282
|
+
end
|
283
|
+
|
284
|
+
context 'multiple wheres' do
|
285
|
+
|
286
|
+
def where_where(klass)
|
287
|
+
records = [klass.create!(email: '1', name: 'fred', admin: true), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
288
|
+
expect(klass.where(name: 'fred').where(admin: true)).to eq([records[0]])
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'User' do
|
292
|
+
where_where(User)
|
293
|
+
end
|
294
|
+
|
295
|
+
it 'UserMock' do
|
296
|
+
where_where(UserMock)
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
describe '::where.not' do
|
304
|
+
|
305
|
+
def where_not(klass)
|
306
|
+
records = [klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
307
|
+
expect(klass.where.not(name: 'fred')).to eq([records[2]])
|
308
|
+
end
|
309
|
+
|
310
|
+
it 'User' do
|
311
|
+
where_not(User)
|
312
|
+
end
|
313
|
+
|
314
|
+
it 'UserMock' do
|
315
|
+
where_not(UserMock)
|
316
|
+
end
|
317
|
+
|
318
|
+
end
|
319
|
+
|
320
|
+
describe '::update_all' do
|
321
|
+
|
322
|
+
def klass_update_all(klass)
|
323
|
+
[klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
324
|
+
klass.update_all(name: 'John')
|
325
|
+
expect(klass.all.map{|a| a.name}).to eq(['John', 'John', 'John'])
|
326
|
+
end
|
327
|
+
|
328
|
+
it 'User' do
|
329
|
+
klass_update_all(User)
|
330
|
+
end
|
331
|
+
|
332
|
+
it 'UserMock' do
|
333
|
+
klass_update_all(UserMock)
|
334
|
+
end
|
335
|
+
|
128
336
|
end
|
129
337
|
|
130
338
|
describe 'type coercion' do
|
@@ -160,33 +368,246 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
160
368
|
|
161
369
|
let(:support_array_methods) { [:<<, :take, :push, :clear, :first, :last, :concat, :replace, :distinct, :uniq, :count, :size, :length, :empty?, :any?, :include?] }
|
162
370
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
371
|
+
context 'supported array methods' do
|
372
|
+
|
373
|
+
def supported_array_methods(user_class, micropost, collection_klass)
|
374
|
+
mp1 = micropost.create!(content: 'text')
|
375
|
+
mp2 = micropost.create!(content: 'text')
|
376
|
+
user = user_class.create(microposts: [mp1, mp2])
|
377
|
+
expect(user.microposts.take(1).count).to eq(1)
|
378
|
+
expect(user.microposts.class).to eq(collection_klass)
|
379
|
+
expect(user.microposts.methods).to include *support_array_methods
|
380
|
+
end
|
381
|
+
|
382
|
+
it 'User' do
|
383
|
+
supported_array_methods(User, Micropost, Micropost::ActiveRecord_Associations_CollectionProxy)
|
384
|
+
end
|
385
|
+
|
386
|
+
it 'UserMock' do
|
387
|
+
supported_array_methods(UserMock, MicropostMock, ActiveMocker::Collection::Association)
|
388
|
+
end
|
389
|
+
|
390
|
+
end
|
391
|
+
|
392
|
+
describe '#find' do
|
393
|
+
|
394
|
+
context 'single id passed' do
|
395
|
+
|
396
|
+
def collection_find(user_class, micropost, collection_klass)
|
397
|
+
microposts = [micropost.create, micropost.create]
|
398
|
+
user = user_class.create!(email: '1', name: 'fred', microposts: microposts)
|
399
|
+
expect(user.microposts.find(microposts.first.id)).to eq microposts.first
|
400
|
+
end
|
401
|
+
|
402
|
+
it 'User' do
|
403
|
+
collection_find(User, Micropost, Micropost::ActiveRecord_Associations_CollectionProxy)
|
404
|
+
end
|
405
|
+
|
406
|
+
it 'UserMock' do
|
407
|
+
collection_find(UserMock, MicropostMock, ActiveMocker::Collection::Association)
|
408
|
+
end
|
167
409
|
|
168
|
-
|
169
|
-
mpm2 = MicropostMock.create
|
170
|
-
user_mock = UserMock.create(microposts: [mpm1, mpm2])
|
410
|
+
end
|
171
411
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
412
|
+
context 'multiple ids passed' do
|
413
|
+
|
414
|
+
def collection_finds(user_class, micropost, collection_klass)
|
415
|
+
microposts = [micropost.create(id: 1), micropost.create(id: 2)]
|
416
|
+
user = user_class.create!(email: '1', name: 'fred', microposts: microposts)
|
417
|
+
expect(user.microposts.find([microposts.first.id, microposts.last.id])).to include *microposts.first, microposts.last
|
418
|
+
end
|
419
|
+
|
420
|
+
it 'User' do
|
421
|
+
collection_finds(User, Micropost, Micropost::ActiveRecord_Associations_CollectionProxy)
|
422
|
+
end
|
423
|
+
|
424
|
+
it 'UserMock' do
|
425
|
+
collection_finds(UserMock, MicropostMock, ActiveMocker::Collection::Association)
|
426
|
+
end
|
427
|
+
|
428
|
+
end
|
176
429
|
|
177
430
|
end
|
178
431
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
432
|
+
describe '#sum' do
|
433
|
+
|
434
|
+
def collection_association_sum(user_class, micropost)
|
435
|
+
mpm1 = micropost.create!(up_votes: 5)
|
436
|
+
mpm2 = micropost.create!(up_votes: 5)
|
437
|
+
user_mock = user_class.create!(microposts: [mpm1, mpm2])
|
438
|
+
expect(user_mock.microposts.sum(:up_votes)).to eq 10
|
439
|
+
end
|
184
440
|
|
185
|
-
|
186
|
-
|
187
|
-
|
441
|
+
it 'User' do
|
442
|
+
collection_association_sum(User, Micropost)
|
443
|
+
end
|
188
444
|
|
189
|
-
|
445
|
+
it 'UserMock' do
|
446
|
+
collection_association_sum(UserMock, MicropostMock)
|
447
|
+
end
|
448
|
+
|
449
|
+
end
|
450
|
+
|
451
|
+
context 'can delete unsaved object from collection' do
|
452
|
+
|
453
|
+
def delete_object(klasses)
|
454
|
+
mp1 = klasses.first.create!(content: 'text')
|
455
|
+
mp2 = klasses.first.create!(content: 'text')
|
456
|
+
user = klasses.last.new(microposts: [mp1, mp2])
|
457
|
+
user.microposts.delete(mp1)
|
458
|
+
expect(user.microposts).to eq [mp2]
|
459
|
+
end
|
460
|
+
|
461
|
+
it 'UserMock' do
|
462
|
+
delete_object([MicropostMock, UserMock])
|
463
|
+
end
|
464
|
+
|
465
|
+
it 'User' do
|
466
|
+
delete_object([Micropost, User])
|
467
|
+
end
|
468
|
+
|
469
|
+
end
|
470
|
+
|
471
|
+
end
|
472
|
+
|
473
|
+
describe 'Collections'do
|
474
|
+
|
475
|
+
context 'delete_all' do
|
476
|
+
|
477
|
+
context 'deletes all records from result' do
|
478
|
+
|
479
|
+
def collections_delete_all(klass)
|
480
|
+
[klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
481
|
+
klass.where(name: 'fred').delete_all
|
482
|
+
expect(klass.count).to eq 1
|
483
|
+
end
|
484
|
+
|
485
|
+
it 'User' do
|
486
|
+
collections_delete_all(User)
|
487
|
+
end
|
488
|
+
|
489
|
+
it 'UserMock' do
|
490
|
+
collections_delete_all(UserMock)
|
491
|
+
end
|
492
|
+
|
493
|
+
end
|
494
|
+
|
495
|
+
context 'deletes all records association' do
|
496
|
+
|
497
|
+
def association_collections_delete_all(user_class, micropost)
|
498
|
+
user = user_class.create!(email: '1', name: 'fred', microposts: [micropost.create, micropost.create])
|
499
|
+
user.microposts.delete_all
|
500
|
+
expect(user_class.count).to eq 1
|
501
|
+
end
|
502
|
+
|
503
|
+
it 'User' do
|
504
|
+
association_collections_delete_all(User, Micropost)
|
505
|
+
end
|
506
|
+
|
507
|
+
it 'UserMock' do
|
508
|
+
association_collections_delete_all(UserMock, MicropostMock)
|
509
|
+
end
|
510
|
+
|
511
|
+
end
|
512
|
+
|
513
|
+
end
|
514
|
+
|
515
|
+
context 'where' do
|
516
|
+
|
517
|
+
context 'all.where' do
|
518
|
+
|
519
|
+
def collections_all_where(klass)
|
520
|
+
records = [klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
521
|
+
expect(klass.all.where(name: 'fred')).to eq([records[0], records[1]])
|
522
|
+
end
|
523
|
+
|
524
|
+
it 'User' do
|
525
|
+
collections_all_where(User)
|
526
|
+
end
|
527
|
+
|
528
|
+
it 'UserMock' do
|
529
|
+
collections_all_where(UserMock)
|
530
|
+
end
|
531
|
+
|
532
|
+
end
|
533
|
+
|
534
|
+
end
|
535
|
+
|
536
|
+
context 'order' do
|
537
|
+
|
538
|
+
context 'where.order' do
|
539
|
+
|
540
|
+
def collections_where_order(klass)
|
541
|
+
records = [klass.create!(email: '2', name: 'fred'), klass.create!(email: '1', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
542
|
+
expect(klass.where(name: 'fred').order(:email)).to eq([records[1], records[0]])
|
543
|
+
end
|
544
|
+
|
545
|
+
it 'User' do
|
546
|
+
collections_where_order(User)
|
547
|
+
end
|
548
|
+
|
549
|
+
it 'UserMock' do
|
550
|
+
collections_where_order(UserMock)
|
551
|
+
end
|
552
|
+
|
553
|
+
end
|
554
|
+
|
555
|
+
context 'where.order.reverse_order' do
|
556
|
+
|
557
|
+
def collections_where_order_reverse_order(klass)
|
558
|
+
records = [klass.create!(email: '2', name: 'fred'), klass.create!(email: '1', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
559
|
+
expect(klass.where(name: 'fred').order(:email).reverse_order).to eq([records[0], records[1]])
|
560
|
+
end
|
561
|
+
|
562
|
+
it 'User' do
|
563
|
+
collections_where_order_reverse_order(User)
|
564
|
+
end
|
565
|
+
|
566
|
+
it 'UserMock' do
|
567
|
+
collections_where_order_reverse_order(UserMock)
|
568
|
+
end
|
569
|
+
|
570
|
+
end
|
571
|
+
|
572
|
+
end
|
573
|
+
|
574
|
+
context 'update_all' do
|
575
|
+
|
576
|
+
context 'where.update_all' do
|
577
|
+
|
578
|
+
def collection_where_update_all(klass)
|
579
|
+
[klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
580
|
+
klass.where(name: 'fred' ).update_all(name: 'John')
|
581
|
+
expect(klass.all.map { |a| a.name }).to eq(['John', 'John', 'Sam'])
|
582
|
+
end
|
583
|
+
|
584
|
+
it 'User' do
|
585
|
+
collection_where_update_all(User)
|
586
|
+
end
|
587
|
+
|
588
|
+
it 'UserMock' do
|
589
|
+
collection_where_update_all(UserMock)
|
590
|
+
end
|
591
|
+
|
592
|
+
end
|
593
|
+
|
594
|
+
context 'all.update_all' do
|
595
|
+
|
596
|
+
def collection_all_update_all(klass)
|
597
|
+
[klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'fred'), klass.create!(email: '3', name: 'Sam')]
|
598
|
+
klass.all.update_all(name: 'John')
|
599
|
+
expect(klass.all.map { |a| a.name }).to eq(['John', 'John', 'John'])
|
600
|
+
end
|
601
|
+
|
602
|
+
it 'User' do
|
603
|
+
collection_all_update_all(User)
|
604
|
+
end
|
605
|
+
|
606
|
+
it 'UserMock' do
|
607
|
+
collection_all_update_all(UserMock)
|
608
|
+
end
|
609
|
+
|
610
|
+
end
|
190
611
|
|
191
612
|
end
|
192
613
|
|
@@ -240,7 +661,6 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
240
661
|
|
241
662
|
end
|
242
663
|
|
243
|
-
|
244
664
|
end
|
245
665
|
|
246
666
|
describe 'delete' do
|
@@ -355,23 +775,57 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
355
775
|
|
356
776
|
end
|
357
777
|
|
358
|
-
describe
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
expect(
|
364
|
-
expect(User.where(id: user1.id).class).to eq User::ActiveRecord_Relation
|
365
|
-
expect(User.count).to eq 1
|
778
|
+
describe '::limit' do
|
779
|
+
|
780
|
+
def klass_limit(klass)
|
781
|
+
records = [klass.create!(email: '1', name: 'fred'), klass.create!(email: '2', name: 'Dan'), klass.create!(email: '3', name: 'Sam')]
|
782
|
+
expect(klass.limit(2)).to eq [records[0], records[1]]
|
783
|
+
expect(klass.limit(2).where(name: 'fred')).to eq [records[0]]
|
366
784
|
end
|
367
785
|
|
368
|
-
it
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
786
|
+
it 'User' do
|
787
|
+
klass_limit(User)
|
788
|
+
end
|
789
|
+
|
790
|
+
it 'UserMock' do
|
791
|
+
klass_limit(UserMock)
|
792
|
+
end
|
793
|
+
|
794
|
+
end
|
795
|
+
|
796
|
+
describe '::find_by!' do
|
797
|
+
|
798
|
+
context 'will raise exception if not found' do
|
799
|
+
|
800
|
+
def find_by_exception(user_class, error)
|
801
|
+
expect{user_class.find_by!(name: 'Matz')}.to raise_error(error)
|
802
|
+
end
|
803
|
+
|
804
|
+
it 'User' do
|
805
|
+
find_by_exception(User, ActiveRecord::RecordNotFound)
|
806
|
+
end
|
807
|
+
|
808
|
+
it 'UserMock' do
|
809
|
+
find_by_exception(UserMock, ActiveMocker::RecordNotFound)
|
810
|
+
end
|
811
|
+
|
812
|
+
end
|
813
|
+
|
814
|
+
context 'will find one record by conditions' do
|
815
|
+
|
816
|
+
def find_by_one_result(user_class)
|
817
|
+
user = user_class.create!(email: '1', name: 'fred')
|
818
|
+
expect(user_class.find_by!(name: 'fred') ).to eq user
|
819
|
+
end
|
820
|
+
|
821
|
+
it 'User' do
|
822
|
+
find_by_one_result(User)
|
823
|
+
end
|
824
|
+
|
825
|
+
it 'UserMock' do
|
826
|
+
find_by_one_result(UserMock)
|
827
|
+
end
|
828
|
+
|
375
829
|
end
|
376
830
|
|
377
831
|
end
|