-
Notifications
You must be signed in to change notification settings - Fork 276
/
query_proxy_methods_spec.rb
1002 lines (825 loc) · 35 KB
/
query_proxy_methods_spec.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
describe 'query_proxy_methods' do
# goofy names to differentiate from same classes used elsewhere
before(:each) do
clear_model_memory_caches
stub_node_class('Student') do
property :name
has_many :out, :lessons, model_class: 'Lesson', rel_class: 'EnrolledIn'
has_many :out, :things, model_class: false, type: 'lessons'
end
stub_node_class('Lesson') do
property :name
has_many :in, :students, model_class: 'Student', rel_class: 'EnrolledIn'
has_many :in, :teachers, model_class: 'Teacher', origin: :lessons
end
stub_node_class('Teacher') do
property :name
property :age, type: Integer
has_many :out, :lessons, model_class: 'Lesson', type: 'teaching_lesson'
scope :ident, -> { as(identity) }
end
stub_node_class('EmptyClass') do
has_many :out, :lessons, type: nil, model_class: 'Lesson'
end
stub_relationship_class('EnrolledIn') do
from_class 'Student'
to_class 'Lesson'
type 'lessons'
property :absence_count, type: Integer, default: 0
after_destroy :destroy_called
def destroy_called
end
end
end
let!(:jimmy) { Student.create(name: 'Jimmy') }
let!(:math) { Lesson.create(name: 'math') }
let!(:science) { Lesson.create(name: 'science') }
let!(:mr_jones) { Teacher.create }
let!(:mr_adams) { Teacher.create }
shared_examples 'is initialized correctly with associations' do
it { should be_a(ActiveGraph::Node) }
its(:name) { should eq(student_name) }
its(:lessons) { should include(philosophy) }
end
shared_examples 'finds existing records or initializes them with relations' do
let(:frank) { Student.create(name: 'Frank') }
let(:philosophy) { Lesson.create(name: 'philosophy') }
before { philosophy.students << frank }
context 'when it already exists' do
let(:student_name) { 'Frank' }
it_should_behave_like 'is initialized correctly with associations'
it { should be_persisted }
end
context 'when it\'s a new record' do
let(:student_name) { 'Jacob' }
it_should_behave_like 'is initialized correctly with associations'
it { should_not be_persisted }
it 'can be saved' do
expect { subject.save }.to change { philosophy.reload.students.count }.by(1)
end
end
end
shared_examples 'is initialized correctly with attributes' do
it { should be_a(ActiveGraph::Node) }
its(:name) { should eq(teacher_name) }
its(:age) { should eq(teacher_age) }
end
shared_examples 'finds existing records or initializes them with attributes' do
let!(:old_emily) { Teacher.create(name: 'Emily', age: 72) }
let!(:young_emily) { Teacher.create(name: 'Emily', age: 24) }
context 'when it already exists' do
let(:teacher_name) { 'Emily' }
let(:teacher_age) { 24 }
it_should_behave_like 'is initialized correctly with attributes'
it { should be_persisted }
end
context 'when it\'s a new record' do
let(:teacher_name) { 'Emily' }
let(:teacher_age) { 37 }
it_should_behave_like 'is initialized correctly with attributes'
it { should_not be_persisted }
it 'can be saved' do
expect { subject.save }.to change { Teacher.count }.by(1)
subject.destroy
end
end
end
describe 'find_or_create_by' do
let(:emily) { Student.create(name: 'Emily') }
let(:philosophy) { Lesson.create(name: 'philosophy') }
let(:mixology) { Lesson.create(name: 'mixology') }
before do
philosophy.students << jimmy
end
it 'returns the correct node if it can be found' do
expect(philosophy.students.find_or_create_by(name: jimmy.name)).to eq(jimmy)
end
it 'creates and associates a new node if one is not found' do
expect(philosophy.students.where(name: 'Rebecca').blank?).to be_truthy
expect { philosophy.students.find_or_create_by(name: 'Rebecca') }.to change { Student.all.count }
expect(philosophy.students.where(name: 'Rebecca').blank?).to be_falsey
end
it 'returns the node after creating' do
expect(philosophy.students.find_or_create_by(name: 'Jacob')).to be_a(ActiveGraph::Node)
end
it 'does not look outside of scope' do
mixology.students.find_or_create_by(name: 'Emily')
expect { philosophy.students.find_or_create_by(name: 'Emily') }.to change { Student.all.count }
end
end
describe 'find_or_initialize_by on an association' do
subject { philosophy.students.find_or_initialize_by(name: student_name) }
it_should_behave_like 'finds existing records or initializes them with relations'
end
describe 'find_or_initialize_by on query proxy' do
subject { Teacher.where(name: teacher_name).find_or_initialize_by(age: teacher_age) }
it_should_behave_like 'finds existing records or initializes them with attributes'
context 'when a block is passed' do
let(:teacher_name) { 'Donna' }
let(:teacher_age) { 92 }
subject { Teacher.where(name: 'Emily').find_or_initialize_by(age: teacher_age) { |t| t.name = teacher_name } }
it_should_behave_like 'is initialized correctly with attributes'
it { should_not be_persisted }
end
end
describe 'first_or_initialize on relations' do
subject { philosophy.students.where(name: student_name).first_or_initialize }
it_should_behave_like 'finds existing records or initializes them with relations'
end
describe 'first_or_initialize on query proxy' do
subject { Teacher.where(name: teacher_name, age: teacher_age).first_or_initialize }
it_should_behave_like 'finds existing records or initializes them with attributes'
context 'when a block is passed' do
let(:teacher_name) { 'Donna' }
let(:teacher_age) { 92 }
subject { Teacher.where(name: 'Emily', age: teacher_age).first_or_initialize { |t| t.name = teacher_name } }
it_should_behave_like 'is initialized correctly with attributes'
it { should_not be_persisted }
end
end
describe 'first and last' do
it 'returns different objects' do
Student.create
expect(Student.all.count).to be > 1
expect(Student.all.first).to_not eq(Student.all.last)
end
it 'returns objects across multiple associations' do
jimmy.lessons << science
science.teachers << mr_adams
expect(jimmy.lessons.teachers.first).to eq mr_adams
expect(mr_adams.lessons.students.last).to eq jimmy
end
end
describe 'union', version: '>4.1' do
let!(:bartemius) { Teacher.create(name: 'Bartemius Crouch Jr', age: 34) }
let!(:amycus) { Teacher.create(name: 'Amycus Carrow', age: 39) }
let!(:snape) { Teacher.create(name: 'Severus Snape', age: 38) }
let(:potter) { Student.create(name: 'Harry Potter') }
context 'when common starting query' do
it 'result contains all records from two simple queries' do
bartemius_query_proxy = Teacher.where(name: bartemius.name)
amycus_query_proxy = Teacher.where(name: amycus.name)
loyal_death_eaters = Teacher.union(-> { bartemius_query_proxy }, -> { amycus_query_proxy })
expect(loyal_death_eaters.distinct).to contain_exactly(bartemius, amycus)
end
it 'result contains all records from multiple complex queries' do
quirinus = Teacher.create(name: 'Quirinus Quirrell')
teachers = [bartemius, amycus, snape, quirinus]
Lesson.create(name: 'Defence Against The Dark Arts', students: [potter], teachers: teachers)
bartemius_query_proc = -> { potter.lessons.teachers.where(name: bartemius.name).as(:res_teacher) }
amycus_query_proc = -> { Student.where(name: potter.name).lessons.teachers.where(name: amycus.name).as(:res_teacher) }
quirinus_query_proc = -> { Teacher.all.lessons.teachers.where(name: quirinus.name) }
loyal_to_voldy = Teacher.union(bartemius_query_proc, amycus_query_proc, quirinus_query_proc)
expect(loyal_to_voldy.distinct).to contain_exactly(bartemius, amycus, quirinus)
end
it 'is further queryable' do
bartemius_query_proxy = Teacher.where(name: bartemius.name)
amycus_query_proxy = Teacher.where(name: amycus.name)
loyal_death_eaters = Teacher.union(-> { bartemius_query_proxy }, -> { amycus_query_proxy })
expect(loyal_death_eaters.where(age: 34).distinct).to contain_exactly(bartemius)
end
it 'works with self reference' do
amycus_query_proxy = Teacher.where(name: amycus.name)
loyal_death_eaters = Teacher.where(name: bartemius.name).union(-> {}, -> { amycus_query_proxy })
expect(loyal_death_eaters.distinct).to contain_exactly(bartemius, amycus)
end
it 'allows continuation of original query' do
amycus_query_proxy = Teacher.where(name: amycus.name)
bartemius_name = bartemius.name
loyal_death_eaters = Teacher.union(-> { where(name: bartemius_name) }, -> { amycus_query_proxy })
expect(loyal_death_eaters.distinct).to contain_exactly(bartemius, amycus)
end
it 'works with branches' do
Lesson.create(name: 'Occlumency', students: [potter], teachers: [snape])
amycus_query_proxy = Teacher.where(name: amycus.name)
bartemius_name = bartemius.name
snape_proxy = Teacher.all.branch { lessons.students }
all_teachers = Teacher.union(-> { where(name: bartemius_name) }, -> { amycus_query_proxy }, -> { snape_proxy })
expect(all_teachers.distinct).to contain_exactly(bartemius, amycus, snape)
end
it 'works after converting in core query and back' do
start_of_query = Teacher.all
bartemius_query_proxy = start_of_query.query.where("#{start_of_query.identity}.name": bartemius.name).proxy_as(Teacher, start_of_query.identity)
amycus_query_proxy = Teacher.where(name: amycus.name)
loyal_death_eaters = Teacher.union(-> { bartemius_query_proxy }, -> { amycus_query_proxy })
expect(loyal_death_eaters.distinct).to contain_exactly(bartemius, amycus)
end
end
context 'with common starting query' do
it 'result contains all records from two queries respecting the common starting query' do
bartemius_query_proc = -> { Teacher.where(name: bartemius.name) }
name_arr = [amycus.name, snape.name]
amycus_snape_query_proc = -> { where(name: name_arr) }
loyal_death_eaters = Teacher.where_not(age: 38).union(bartemius_query_proc, amycus_snape_query_proc)
expect(loyal_death_eaters.distinct).to contain_exactly(bartemius, amycus)
end
it 'works with self reference' do
amycus_query_proxy = Teacher.where(age: 39)
loyal_death_eaters = Teacher.where(name: bartemius.name).union(-> {}, -> { amycus_query_proxy })
expect(loyal_death_eaters.distinct).to contain_exactly(bartemius, amycus)
end
it 'works with nested union' do
amycus_query_proxy = Teacher.where(age: 39)
loyal_death_eaters = Teacher.where(name: bartemius.name).union(-> {}, -> { amycus_query_proxy })
all_death_eaters = Teacher.union(-> { Teacher.where(name: snape.name) }, -> { loyal_death_eaters })
expect(all_death_eaters.distinct).to contain_exactly(snape, bartemius, amycus)
end
end
end
describe 'include?' do
it 'correctly reports when a node is included in a query result' do
jimmy.lessons << science
science.teachers << mr_adams
expect(jimmy.lessons.include?(science)).to be_truthy
expect(jimmy.lessons.include?(math)).to be_falsey
expect(jimmy.lessons.teachers.include?(mr_jones)).to be_falsey
expect(jimmy.lessons.where(name: 'science').teachers.include?(mr_jones)).to be_falsey
expect(jimmy.lessons.where(name: 'science').teachers.include?(mr_adams)).to be_truthy
expect(Teacher.all.include?(mr_jones)).to be_truthy
expect(Teacher.all.include?(math)).to be_falsey
end
it 'works with multiple relationships to the same object' do
jimmy.lessons << science
jimmy.lessons << science
expect(jimmy.lessons.include?(science)).to be_truthy
end
it 'returns correctly when model_class is false' do
woodworking = Lesson.create(name: 'woodworking')
expect(jimmy.things.include?(woodworking)).to be_falsey
jimmy.lessons << woodworking
expect(jimmy.things.include?(woodworking)).to be_truthy
woodworking.destroy
end
it 'allows you to check for an identifier in the middle of a chain' do
jimmy.lessons << science
science.teachers << mr_adams
expect(Lesson.as(:l).students.where(name: 'Jimmy').include?(science, :l)).to be_truthy
end
it 'can find by primary key/uuid' do
expect(jimmy.lessons.include?(science.id)).to be_falsey
jimmy.lessons << science
expect(jimmy.lessons.include?(science.id)).to be_truthy
expect(jimmy.lessons.include?(id_property_value(science))).to be_truthy
expect(jimmy.lessons.include?(science)).to be_truthy
end
it 'does not break when the query has been ordered' do
expect(jimmy.lessons.order(created_at: :desc).include?(science)).to eq jimmy.lessons.include?(science)
end
end
describe 'exists?' do
context 'class methods' do
it 'can run by a class' do
expect(EmptyClass.empty?).to be_truthy
expect(Lesson.empty?).to be_falsey
end
it 'does not fail from an ordered context' do
expect(Lesson.order(:name).empty?).to eq false
end
it 'can be called with a property and value' do
expect(Lesson.exists?(name: 'math')).to eq true
expect(Lesson.exists?(name: 'boat repair')).to eq false
end
it 'can be called on the class with a neo_id' do
expect(Lesson.exists?(math.neo_id)).to eq true
expect(Lesson.exists?(8_675_309)).to eq false
end
it 'can be called on a class with a primary key value' do
expect(Lesson.exists?(math.id)).to eq true
expect(Lesson.exists?('this_other_value')).to eq false
end
it 'raises an error if something other than a neo id is given' do
expect { Lesson.exists?(:fooooo) }.to raise_error(ActiveGraph::InvalidParameterError)
end
end
context 'QueryProxy methods' do
it 'can be called on a query' do
expect(Lesson.where(name: 'history').exists?).to eq false
expect(Lesson.where(name: 'math').exists?).to eq true
end
it 'can be called with property and value' do
expect(jimmy.lessons.exists?(name: 'science')).to eq false
jimmy.lessons << science
expect(jimmy.lessons.exists?(name: 'science')).to eq true
expect(jimmy.lessons.exists?(name: 'bomb disarming')).to eq false
end
it 'can be called with a neo_id' do
expect(Lesson.where(name: 'math').exists?(math.neo_id)).to eq true
expect(Lesson.where(name: 'math').exists?(science.neo_id)).to eq false
end
it 'can be called with a primary key' do
expect(Lesson.where(name: 'math').exists?(math.id)).to eq true
expect(Lesson.where(name: 'math').exists?(science.id)).to eq false
end
it 'is called by :blank? and :empty?' do
expect(jimmy.lessons.blank?).to eq true
expect(jimmy.lessons.empty?).to eq true
jimmy.lessons << science
expect(jimmy.lessons.blank?).to eq false
expect(jimmy.lessons.empty?).to eq false
end
it 'does not fail from an ordered context' do
expect(jimmy.lessons.order(:name).blank?).to eq true
expect(jimmy.lessons.order(:name).empty?).to eq true
end
end
end
describe 'count' do
before(:each) do
[Student, Lesson].each(&:delete_all)
@john = Student.create(name: 'John')
@history = Lesson.create(name: 'history')
3.times { @john.lessons << @history }
end
it 'tells you the number of matching objects' do
expect(@john.lessons.count).to eq(3)
end
it 'can tell you the number of distinct matching objects' do
expect(@john.lessons.count(:distinct)).to eq 1
end
it 'raises an exception if a bad parameter is passed' do
expect { @john.lessons.count(:foo) }.to raise_error(ActiveGraph::InvalidParameterError)
end
it 'works on an object earlier in the chain' do
expect(Student.as(:s).lessons.where(name: 'history').count(:distinct, :s)).to eq 1
end
it 'works with order clause' do
expect { Student.order(name: :asc).count }.not_to raise_error
end
it 'is aliased by length and size' do
expect(@john.lessons.size).to eq(3)
expect(@john.lessons.length).to eq(3)
end
context 'with limit' do
before do
Student.delete_all
10.times { Student.create }
end
it 'adds a :with and returns the limited count' do
expect(Student.as(:s).limit(5).count).to eq 5
expect(Student.as(:s).limit(11).count).to eq 10
end
end
end
describe 'distinct' do
let(:frank) { Student.create(name: 'Frank') }
let(:bill) { Teacher.create(name: 'Bill') }
let(:philosophy) { Lesson.create(name: 'philosophy') }
let(:science) { Lesson.create(name: 'science') }
before do
philosophy.students << frank
science.students << frank
philosophy.teachers << bill
science.teachers << bill
end
context 'when building the query' do
before do
@subscription = ActiveGraph::Base.subscribe_to_query do |query|
expect(query).to include('DISTINCT')
end
end
after do
ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription
end
it 'adds distinct to a select query' do
frank.lessons.teachers.distinct.to_a
end
it 'branch perserves distinct in a select query' do
frank.lessons.teachers.distinct.branch { lessons }.to_a
end
it 'with_associations perserves distinct in a select query' do
frank.lessons.teachers.distinct.with_associations(:lessons).to_a
end
end
it 'counts values without duplicates' do
expect(frank.lessons.teachers.count).to eq(2)
expect(frank.lessons.teachers.distinct.count).to eq(1)
end
it 'counts values without duplicates in a branch query' do
expect(frank.lessons.teachers.distinct.branch { lessons }.count).to eq(1)
end
it 'selects values without duplicates' do
expect(frank.lessons.teachers.distinct.to_a).to eq(frank.lessons.teachers.to_a.uniq)
end
end
describe 'query counts for count, size, and length' do
describe 'size' do
it 'always queries' do
proxy = Student.all
expect_queries(1) { proxy.count }
expect_queries(1) { proxy.to_a }
expect_queries(1) { proxy.count }
end
end
describe 'size' do
it 'always queries' do
proxy = Student.all
expect_queries(1) { proxy.size }
expect_queries(1) { proxy.to_a }
expect_queries(0) { proxy.size }
end
end
# Always loads the data
describe 'length' do
it 'always queries' do
proxy = Student.all
expect_queries(1) { proxy.length }
expect_queries(0) { proxy.to_a }
expect_queries(0) { proxy.length }
end
end
end
describe '#update_all' do
let!(:jimmy_clone) { Student.create(name: 'Jimmy') }
let!(:john) { Student.create(name: 'John') }
let(:changing_students) { Student.where(name: 'Bob') }
it 'updates all students' do
expect(Student.update_all(name: 'Bob')).to eq(Student.count)
expect(Student.all.map(&:name)).to be_all { |age| age == 'Bob' }
end
it 'updates students' do
expect do
expect(Student.as(:p).where('p.name = "Jimmy"').update_all(name: 'Bob')).to eq(2)
end.to change(changing_students, :count).by(2)
end
it 'updates people with age < 30 (using string parameter)' do
expect do
expect(Student.as(:p).where('p.name = "Jimmy"').update_all('p.name = $new_name', new_name: 'Bob')).to eq(2)
end.to change(changing_students, :count).by(2)
end
it 'updates nothing when matching nothing' do
expect do
expect(Student.as(:n).where('n.name = "Frank"').update_all(name: 'Bob')).to eq(0)
end.not_to change(changing_students, :count)
end
it 'raises error on invalid argument' do
expect do
Student.update_all(7)
end.to raise_error ArgumentError
end
end
describe '#update_all_rels' do
before do
science.students << jimmy
math.students << jimmy
end
it 'updates all jimmy\'s lessions absence' do
count = Student.all.match_to(jimmy).lessons(:l)
.update_all_rels(absence_count: 3)
expect(count).to eq(2)
end
it 'updates all jimmy\'s lessions absence (with string parameter)' do
count = Student.all.match_to(jimmy).lessons(:l)
.update_all_rels('rel1.absence_count = 3')
expect(count).to eq(2)
end
it 'raises error on invalid argument' do
expect do
Student.all.match_to(jimmy).lessons(:l).update_all(7)
end.to raise_error ArgumentError
end
end
describe 'delete_all' do
it 'deletes from Model' do
Student.delete_all
expect(Student.count).to eq(0)
end
it 'deletes from Model.all' do
Student.all.delete_all
expect(Student.count).to eq(0)
end
context 'Student has lessons which have teachers' do
before do
[Student, Lesson, Teacher].each(&:delete_all)
@tom = Student.create(name: 'Tom')
@math = Lesson.create(name: 'Math')
@science = Lesson.create(name: 'Science')
@adams = Teacher.create(name: 'Mr Adams')
@johnson = Teacher.create(name: 'Mrs Johnson')
@tom.lessons << @math
@tom.lessons << @science
@math.teachers << @adams
@science.teachers << @johnson
end
it 'removes the last link in the QueryProxy chain' do
expect(@tom.lessons.teachers.include?(@adams)).to be_truthy
@tom.lessons.teachers.delete_all
expect(@adams.exist?).to be_falsey
expect(@johnson.exist?).to be_falsey
expect(@tom.lessons.teachers).to be_empty
end
it 'does not touch earlier portions of the chain' do
expect(@tom.lessons.include?(@math)).to be_truthy
@tom.lessons.teachers.delete_all
expect(@math.persisted?).to be_truthy
end
it 'works when called from a class' do
expect(@tom.lessons.teachers.include?(@adams)).to be_truthy
Student.all.lessons.teachers.delete_all
expect(@adams.exist?).to be_falsey
end
it 'can target a specific identifier' do
@tom.lessons(:l).teachers.where(name: 'Mr Adams').delete_all(:l)
expect(@tom.lessons.include?(@math)).to be_falsey
expect(@math.exist?).to be false
expect(@tom.lessons.include?(@science)).to be_truthy
end
it 'can target relationships' do
@tom.lessons(:l, :r).teachers.where(name: 'Mr Adams').delete_all(:r)
expect(@tom.lessons.include?(@math)).to be_falsey
expect(@math).to be_persisted
end
end
end
describe 'limit_value' do
it 'returns nil when limit is not specified' do
expect(Student.all.limit_value).to be_nil
end
it 'returns the limit number when set' do
expect(Student.all.limit(10).limit_value).to eq 10
end
end
describe 'match_to and first_rel_to' do
before(:each) do
@john = Student.create(name: 'Paul')
@history = Lesson.create(name: 'history')
@math = Lesson.create(name: 'math')
@john.lessons << @history
end
describe 'match_to' do
it 'returns a QueryProxy object' do
expect(@john.lessons.match_to(@history)).to be_a(ActiveGraph::Node::Query::QueryProxy)
expect(@john.lessons.match_to(@history.id)).to be_a(ActiveGraph::Node::Query::QueryProxy)
expect(@john.lessons.match_to(nil)).to be_a(ActiveGraph::Node::Query::QueryProxy)
end
context 'with a valid node' do
it 'generates a match to the given node' do
expect(@john.lessons.match_to(@history).to_cypher).to include('WHERE (ID(result_lessons3) =')
end
it 'matches the object' do
expect(@john.lessons.match_to(@history).limit(1).first).to eq @history
end
end
context 'with an id' do
it 'generates cypher using the primary key' do
expect(@john.lessons.match_to(@history.id).to_cypher).to include(if Lesson.primary_key == :neo_id
'WHERE (ID(result_lessons3) ='
else
'WHERE (result_lessons3.uuid ='
end)
end
it 'matches' do
expect(@john.lessons.match_to(@history.id).limit(1).first).to eq @history
end
end
context 'with an array' do
context 'of nodes' do
after(:each) do
@john.lessons.first_rel_to(@math).destroy
end
it 'generates cypher using IN with the IDs of contained nodes' do
expect(@john.lessons.match_to([@history, @math]).to_cypher).to include('ID(result_lessons3) IN')
expect(@john.lessons.match_to([@history, @math]).to_a).to eq [@history]
@john.lessons << @math
expect(@john.lessons.match_to([@history, @math]).to_a.size).to eq 2
expect(@john.lessons.match_to([@history, @math]).to_a).to include(@history, @math)
end
end
context 'of IDs' do
it 'allows an array of IDs' do
expect(@john.lessons.match_to([@history.id]).to_a).to eq [@history]
end
end
end
context 'with a null object' do
it 'generates cypher with 1 = 2' do
expect(@john.lessons.match_to(nil).to_cypher).to include('WHERE (1 = 2')
end
it 'matches nil' do
expect(@john.lessons.match_to(nil).first).to be_nil
end
end
context 'on Model.all' do
it 'works with a node' do
expect(Lesson.all.match_to(@history).first).to eq @history
end
it 'works with an id' do
expect(Lesson.all.match_to(@history.id).first).to eq @history
end
end
describe 'complex chains' do
before do
jimmy.lessons << math
math.teachers << mr_jones
mr_jones.age = 40
mr_jones.save
jimmy.lessons << science
science.teachers << mr_adams
mr_adams.age = 50
mr_adams.save
end
it 'works with a chain starting with `all`' do
expect(Student.all.match_to(jimmy).lessons(:l).match_to(math).teachers.where(age: 40).first).to eq mr_jones
end
end
end
describe 'first_rel_to' do
it 'returns the first relationship across a QueryProxy chain to a given node' do
expect(@john.lessons.first_rel_to(@history)).to be_a EnrolledIn
end
it 'returns nil when nothing matches' do
expect(@john.lessons.first_rel_to(@math)).to be_nil
end
end
# also aliased as `all_rels_to`
describe 'rels_to' do
before { 3.times { @john.lessons << @history } }
it 'returns all relationships across a QueryProxy chain to a given node' do
all_rels = @john.lessons.rels_to(@history)
expect(all_rels).to be_a(Enumerable)
expect(all_rels.count).to eq @john.lessons.match_to(@history).count
@john.lessons.all_rels_to(@history).map(&:destroy)
@john.association_proxy_cache.clear
expect(@john.lessons.all_rels_to(@history)).to be_empty
end
end
describe 'as' do
it 'changes query variable' do
query = Student.as(:stud)
expect(query.to_cypher).to include('(stud:`Student`)')
query.to_a
end
it 'keeps the current context' do
query = Student.where(name: 'John').as(:stud)
expect(query.to_cypher).to include('(stud:`Student`)', ' WHERE ')
query.to_a
end
it 'keeps the current context with associations' do
query = Student.all.lessons.as(:less)
expect(query.to_cypher).to include('`Student`)', '(less:`Lesson`)')
query.to_a
end
end
describe 'delete, destroy' do
before { @john.lessons << @history unless @john.lessons.include?(@history) }
describe 'delete' do
it 'removes relationships between a node and the last link of the QP chain from the server' do
expect_any_instance_of(EnrolledIn).not_to receive(:destroy_called)
expect(@john.lessons.include?(@history)).to be_truthy
@john.lessons.delete(@history)
expect(@john.lessons.include?(@history)).to be_falsey
# let's just be sure it's not deleting the nodes...
expect(@john).to be_persisted
expect(@history).to be_persisted
end
it 'accepts an array' do
@john.lessons << @math
@john.lessons.delete([@math, @history])
expect(@john.lessons.to_a).not_to include(@math, @history)
end
end
describe 'destroy' do
it 'returns relationships and destroys them in Ruby, executing callbacks in the process' do
expect(@john.lessons.include?(@history)).to be_truthy
expect_any_instance_of(EnrolledIn).to receive(:destroy_called)
@john.lessons.destroy(@history)
expect(@john.lessons.include?(@history)).to be_falsey
# let's just be sure it's not deleting the nodes...
expect(@john).to be_persisted
expect(@history).to be_persisted
end
it 'accepts an array' do
@john.lessons << @math
@john.lessons.destroy([@math, @history])
expect(@john.lessons.to_a).not_to include(@math, @history)
end
end
end
end
context 'matching by relations' do
before(:each) do
[Student, Lesson, Teacher].each(&:delete_all)
@john = Student.create(name: 'John')
@bill = Student.create(name: 'Bill')
@frank = Student.create(name: 'Frank')
@history = Lesson.create(name: 'history')
@john.lessons << @history
EnrolledIn.create(from_node: @frank, to_node: @history, absence_count: 10)
end
describe 'having_rel' do
it 'returns students having at least a lesson' do
expect(Student.all.having_rel(:lessons)).to contain_exactly(@john, @frank)
end
it 'returns students having at least a lesson with no absences' do
expect(Student.all.having_rel(:lessons, absence_count: 0)).to contain_exactly(@john)
end
it 'returns no student when absence is negative' do
expect(Student.all.having_rel(:lessons, absence_count: -1)).to eq([])
end
it 'returns no lesson having teacher, since there\'s none' do
expect(Lesson.all.having_rel(:teachers)).to eq([])
end
it 'raises ArgumentError when passing a missing relation' do
expect { Lesson.all.having_rel(:friends) }.to raise_error(ArgumentError)
end
end
describe 'not_having_rel' do
it 'returns students having at least a lesson' do
expect(Student.all.not_having_rel(:lessons)).to contain_exactly(@bill)
end
it 'returns students having at least a lesson with no absences' do
expect(Student.all.not_having_rel(:lessons, absence_count: 0)).to contain_exactly(@bill, @frank)
end
it 'returns no lesson having no students, since there\'s none' do
expect(Lesson.all.not_having_rel(:students)).to eq([])
end
it 'raises ArgumentError when passing a missing relation' do
expect { Lesson.all.not_having_rel(:friends) }.to raise_error(ArgumentError)
end
end
end
describe 'branch' do
before(:each) do
[Student, Lesson, Teacher].each(&:delete_all)
@john = Student.create(name: 'John')
@bill = Student.create(name: 'Bill')
@history = Lesson.create(name: 'history')
@math = Lesson.create(name: 'math')
@jim = Teacher.create(name: 'Jim', age: 40)
@john.lessons << @history
@history.teachers << @jim
@math.teachers << @jim
end
it 'returns a QueryProxy object' do
expect(@john.lessons.branch { teachers }).to be_a(ActiveGraph::Node::Query::QueryProxy)
end
it 'keeps identity to the external chain - node' do
expect(@john.lessons(:l).branch { teachers(:t) }.identity).to eq(:l)
end
it 'keeps identity to the external chain - rel' do
expect(@history.students(:student, :enrolledIn).branch { all }.rel_var).to eq(:enrolledIn)
end
it 'queries lessions' do
expect(@john.lessons(:l).branch { teachers(:t) }.to_a.first).to be_a(Lesson)
end
it 'applies the query in the block' do
expect(@john.lessons.branch { teachers(:t) }.to_cypher).to include('(t:`Teacher`)')
end
it 'applies scope identity used within branch' do
expect(@john.lessons.branch { teachers.ident }.to_cypher).to include('(result_teachers4:`Teacher`)')
end
it 'returns only records matching the relation' do
students_with_lessons = Student.all.branch { lessons }.to_a
expect(students_with_lessons).to include(@john)
expect(students_with_lessons).not_to include(@bill)
end
it 'uses the right cypher variable through the branch' do
job = Teacher.create(name: 'Job', age: 28)
jil = Teacher.create(name: 'Jil', age: 35)
sceince = Lesson.create(name: 'Sceince', teachers: [job])
muth = Lesson.create(name: 'Muth', teachers: [jil])
jummy = Student.create(name: 'Jummy', lessons: [sceince, muth])
Student.create(name: 'Rundom')
result = jummy.lessons.branch { teachers.where(age: 28) }.students
# We should only get Jummy. Failure in choosing the identity would give all
expect(result).to eq [jummy]
end
it 'returns johns lessons whose teachers not only teach history' do
expect(@john.lessons.branch { teachers.lessons.where_not(name: 'history') }).to include(@history)
end
it 'raises LocalJumpError when no block is passed' do
expect { @john.lessons.branch }.to raise_error LocalJumpError
end
end
describe 'optional' do
before(:each) do
@lauren = Student.create(name: 'Lauren')
@math = Lesson.create(name: 'Math')
@science = Lesson.create(name: 'Science')
@johnson = Teacher.create(name: 'Mr. Johnson', age: 40)
@clancy = Teacher.create(name: 'Mr. Clancy', age: 50)
@lauren.lessons << [@math, @science]
@math.teachers << @johnson
@science.teachers << @clancy
end
it 'starts a new optional match' do
result = @lauren.lessons(:l).optional(:teachers, :t).where(age: 40).query.order(l: :name).pluck('distinct l, t')
expect(result).to eq [[@math, @johnson],
[@science, nil]]
end
end
describe '#inspect' do
before(:each) do
Student.create(name: 'Lauren')
Student.create(name: 'Bob')
Student.create(name: 'Bill')
end
context 'when inspecting a query proxy' do
let(:query_proxy) { Student.all }
let(:inspected_elements) { query_proxy.inspect }
it 'returns the list of resulting elements' do
expect(inspected_elements).to include('#<QueryProxy Student')
expect(inspected_elements).to include(query_proxy.to_a.inspect)
end
end