/
relationship_spec.rb
1045 lines (843 loc) · 33.7 KB
/
relationship_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
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
class RelationshipWithNoProperty < Neo4j::Rails::Relationship
end
class NodeWithRelationship < Neo4j::Rails::Model
has_one(:other_node)
has_one(:foobar).relationship(RelationshipWithNoProperty)
has_n(:baaz)
def to_s
"node object_id: #{object_id} #{self._java_node ? neo_id : "not persisted"}"
end
end
describe "Neo4j::Model Relationships" do
describe "incoming" do
it "created incoming relationship can be accessed from outgoing node" do
clazz = create_model
a = clazz.create
b = clazz.create
a.incoming(:foo) << b
a.save
# then
a.incoming(:foo).size.should == 1
a.incoming(:foo).should include(b)
b.outgoing(:foo).size.should == 1
b.outgoing(:foo).to_a.should include(a)
# reload
a = clazz.find(a.id)
b = clazz.find(b.id)
a.incoming(:foo).size.should == 1
a.incoming(:foo).should include(b)
b.outgoing(:foo).size.should == 1
b.outgoing(:foo).should include(a)
end
end
describe "has_n from" do
before(:each) do
@b_clazz = create_model
@b_clazz.has_n :friends
@a_clazz = create_model
@a_clazz.has_n(:known_by).from(:friends)
@a = @a_clazz.create
@b = @b_clazz.create
@a.known_by << @b
@a.save
end
it "should find the node both ways" do
@a.known_by.size.should == 1
@a.known_by.should include(@b)
@b.friends.size.should == 1
@b.friends.should include(@a)
end
it "when reloaded it can also be found" do
a = @a_clazz.find(@a.id)
b = @b_clazz.find(@b.id)
a.known_by.size.should == 1
a.known_by.should include(b)
b.friends.size.should == 1
b.friends.should include(a)
end
it "can be found using incoming and outgoing" do
@b.outgoing(:friends).size.should == 1
@b.outgoing(:friends).should include(@a)
@a.incoming(:friends).size.should == 1
@a.incoming(:friends).should include(@b)
end
it "can be found using incoming and outgoing also when reloaded nodes" do
a = @a_clazz.find(@a.id)
b = @b_clazz.find(@b.id)
b.outgoing(:friends).size.should == 1
b.outgoing(:friends).should include(a)
a.incoming(:friends).size.should == 1
a.incoming(:friends).should include(b)
end
end
describe "has_n generated setter" do
let(:sugar) { Ingredient.new :name => 'sugar'}
let(:brown_sugar) { Ingredient.new :name => 'brown sugar' }
context "when node is new" do
it "should create relations to associated nodes" do
icecream = IceCream.new
icecream.ingredients = [sugar, brown_sugar]
icecream.ingredients.to_a.should =~ [sugar, brown_sugar]
end
end
context "creating a node with associated nodes" do
it "should create relations to associated nodes" do
icecream = IceCream.create!(:flavour => "Yummy!", :ingredients => [sugar, brown_sugar])
icecream.ingredients.to_a.should =~ [sugar, brown_sugar]
icecream.reload.ingredients.to_a.should =~ [sugar, brown_sugar]
end
end
context "assinging associated nodes to node with existing associations" do
it "should delete old relations and create new relations to associated nodes" do
icecream = IceCream.create!(:flavour => "Yummy!", :ingredients => [brown_sugar])
icecream.ingredients = [sugar]
icecream.ingredients.to_a.should == [sugar]
icecream.save!
icecream.reload.ingredients.to_a.should == [sugar]
end
end
end
describe "has_one, has_n, outgoing" do
it "node.friends << MyModel.create; node.save! should work" do
clazz = create_model
clazz.has_n(:friends)
a = clazz.create
b = clazz.create
c = clazz.create
a.friends << b << c
a.save!
a.friends.size.should == 2
a.friends.should include(b, c)
# it should be persisted
x = clazz.find(a.neo_id)
x.friends.size.should == 2
end
it "should find the relationship using #outgoing method" do
clazz = create_model
clazz.has_n(:friends)
a = clazz.create
b = clazz.create
c = clazz.create
a.friends << b << c
a.save!
a.outgoing(:friends).size.should == 2
a.outgoing(:friends).should include(b, c)
end
it "should find the relationship using #has_n method when created with outgoing method" do
clazz = create_model
clazz.has_n(:friends)
a = clazz.create
b = clazz.create
c = clazz.create
# need to create a new transaction since the node is already peristed
new_tx
a.outgoing(:friends) << b << c
finish_tx
a.friends.should include(b, c)
a.save!
a.friends.should include(b, c)
end
it "should find the relationship using #has_n friends_rels method when created with outgoing method" do
clazz = create_model
clazz.has_n(:friends)
a = clazz.create
b = clazz.create
c = clazz.create
a.friends << b << c
a.friends_rels.to_a.size.should == 2
a.save!
a.friends_rels.to_a.size.should == 2
end
it "should only find both persisted and none persisted relationship before saving it" do
clazz = create_model
clazz.has_n(:friends)
a = clazz.create
b = clazz.create
c = clazz.create
a.friends << b << c
a.save!
d = clazz.new
a.friends << d
a.friends_rels.to_a.size.should == 3
a.friends.to_a.size.should == 3
# only one is none persisted
a.friends.collect { |x| x.persisted? }.should == [false, true, true]
a.save!
a.friends_rels.to_a.size.should == 3
end
it "#save is necessary to create relationships" do
clazz = create_model
clazz.has_n(:friends)
a = clazz.create
b = clazz.create
c = clazz.create
a.friends << b << c
a.friends.size.should == 2
a.friends.should include(b, c)
# it should not be persiested
x = clazz.find(a.neo_id)
x.friends.size.should == 0
end
it "#del deletes the relationship without needing to call save" do
clazz = create_model
clazz.has_n(:friends)
a = clazz.create
b = clazz.create
a.friends << b
a.save
a.outgoing(:friends).should include(b)
rel = a.rels(:friends).outgoing.find(b)
Neo4j::Transaction.run { rel.del }
a.outgoing(:friends).should_not include(b)
end
it "has_n: should be empty when it has no relationships" do
clazz = create_model
clazz.has_n(:knows)
jack = clazz.new
jack.knows.should be_empty
end
it "has_one: should be empty when it has no relationships" do
clazz = create_model
clazz.has_one :thing
jack = clazz.new
jack.thing.should be_nil
end
it "add nodes to a has_one method with the #new method" do
member = Member.new
avatar = Avatar.new
member.avatar = avatar
member.avatar.should be_kind_of(Avatar)
member.save
member.avatar.id.should_not be_nil
end
it "has_one should allow assignments to nil" do
member = Member.new
member.avatar = nil
member.avatar.should be_nil
member.save
member.reload.avatar.should be_nil
end
it "has_one should allow changing a value to nil from a non nil value" do
member = Member.new
member.avatar = Avatar.new
member.save
member.reload.avatar = nil
member.save
member.reload.avatar.should be_nil
end
it "adding nodes to a has_n method created with the #new method" do
icecream = IceCream.new
sugar = Ingredient.new :name => 'sugar'
icecream.ingredients << sugar
icecream.ingredients.should include(sugar)
end
it "adding nodes using outgoing should work for models created with the #new method" do
icecream = IceCream.new
sugar = Ingredient.new :name => 'sugar'
icecream.outgoing(:ingredients) << sugar
icecream.outgoing(:ingredients).should include(sugar)
end
it "saving the node should create all the nested nodes" do
icecream = IceCream.new(:flavour => 'vanilla')
sugar = Ingredient.new :name => 'sugar'
butter = Ingredient.new :name => 'butter'
icecream.ingredients << sugar << butter
icecream.ingredients.should include(sugar, butter)
sugar.neo_id.should == nil
icecream.save.should be_true
# then
sugar.neo_id.should_not be_nil
icecream.ingredients.should include(sugar, butter)
# make sure the nested nodes were properly saved
ice = IceCream.load(icecream.neo_id)
ice.ingredients.should include(sugar, butter)
icecream.ingredients.first.should be_kind_of(Ingredient)
end
it "should not save nested nodes if it was not valid" do
icecream = IceCream.new # not valid
sugar = Ingredient.new :name => 'sugar'
butter = Ingredient.new :name => 'butter'
icecream.ingredients << sugar << butter
icecream.ingredients.should include(sugar, butter)
sugar.neo_id.should == nil
icecream.save.should be_false
# then
icecream.ingredients.should include(sugar, butter)
sugar.neo_id.should == nil
end
describe "nested nodes two level deep" do
before(:all) do
@clazz = create_model do
property :name
has_n :knows
validates :name, :presence => true
end
end
it "deleting relationships only when save is called" do
jack = @clazz.new(:name => 'jack')
carol = @clazz.new(:name => 'carol')
bob = @clazz.new(:name => 'bob')
jack.knows << carol
carol.knows << bob
jack.knows << bob
jack.knows_rels.first.delete
jack.knows_rels.to_a.size.should == 2
jack.save.should be_true
# the knows_rels only support persisted relationships for now
jack.knows_rels.to_a.size.should == 2
jack.knows_rels.first.destroy
jack.reload
end
end
describe "accepts_nested_attributes_for" do
class Avatar < Neo4j::Model
property :icon
end
class Post < Neo4j::Model
property :title
end
class Description < Neo4j::Model
property :title
property :text
validates_presence_of :title
def to_s
"Description title: #{title} text:#{text}"
end
end
class Member < Neo4j::Model
has_n(:posts).to(Post)
has_n(:valid_posts).to(Post)
has_n(:valid_posts2).to(Post)
has_n(:descriptions).to(Description)
has_one(:avatar).to(Avatar)
has_one(:thing)
accepts_nested_attributes_for :descriptions
accepts_nested_attributes_for :avatar, :allow_destroy => true
accepts_nested_attributes_for :posts, :allow_destroy => true
accepts_nested_attributes_for :valid_posts, :reject_if => proc { |attributes| attributes[:title].blank? }
accepts_nested_attributes_for :valid_posts2, :reject_if => :reject_posts
validates_associated :descriptions
def reject_posts(attributed)
attributed[:title].blank?
end
end
it "create one-to-one " do
params = {:member => {:name => 'Jack', :avatar_attributes => {:icon => 'smiling'}}}
member = Member.create(params[:member])
member.avatar.icon.should == 'smiling'
member = Member.new(params[:member])
member.save
member.avatar.icon.should == 'smiling'
end
it "create one-to-one - it also allows you to update the avatar through the member:" do
params = {:member => {:name => 'Jack', :avatar_attributes => {:icon => 'smiling'}}}
member = Member.create(params[:member])
params = {:member => {:avatar_attributes => {:id => member.avatar.id, :icon => 'sad'}}}
member.update_attributes params[:member]
member.avatar.icon.should == 'sad'
end
it "create one-to-one - allows assigning avatars that have already been created" do
avatar = Avatar.create(:icon => 'angry')
params = {:member => {:name => 'Jack', :avatar_attributes => {:id => avatar.id}}}
member = Member.create(params[:member])
member.avatar.should == avatar
member.avatar.icon.should == 'angry'
end
it "create one-to-one - allows updating avatars that have already been created" do
avatar = Avatar.create(:icon => 'angry')
params = {:member => {:name => 'Jack', :avatar_attributes => {:id => avatar.id, :icon => 'happy'}}}
member = Member.create(params[:member])
member.avatar.id.should == avatar.id
member.avatar.icon.should == 'happy'
end
it "create one-to-one - when you add the _destroy key to the attributes hash, with a value that evaluates to true, you will destroy the associated model" do
params = {:member => {:name => 'Jack', :avatar_attributes => {:icon => 'smiling'}}}
member = Member.create(params[:member])
member.avatar.should_not be_nil
# when
member.avatar_attributes = {:id => member.avatar.id, :_destroy => '1'}
member.save
member.avatar.should be_nil
end
it "create one-to-one - when you add the _destroy key of value '0' to the attributes hash you will NOT destroy the associated model" do
params = {:member => {:name => 'Jack', :avatar_attributes => {:icon => 'smiling'}}}
member = Member.create(params[:member])
member.avatar.should_not be_nil
# when
member.avatar_attributes = {:id => member.avatar.id, :_destroy => '0'}
member.save
member.avatar.should_not be_nil
end
it "create one-to_many - You can now set or update attributes on an associated post model through the attribute hash" do
# For each hash that does not have an id key a new record will be instantiated, unless the hash also contains a _destroy key that evaluates to true.
params = {:member => {
:name => 'joe', :posts_attributes => [
{:title => 'Kari, the awesome Ruby documentation browser!'},
{:title => 'The egalitarian assumption of the modern citizen'},
{:title => '', :_destroy => '1'} # this will be ignored
]
}}
member = Member.create(params[:member])
member.posts.size.should == 2
member.posts.first.title.should == 'Kari, the awesome Ruby documentation browser!'
member.posts[1].title.should == 'The egalitarian assumption of the modern citizen'
member = Member.new(params[:member])
member.posts.first.title.should == 'Kari, the awesome Ruby documentation browser!'
member.posts[1].title.should == 'The egalitarian assumption of the modern citizen'
end
it ":reject_if proc will silently ignore any new record hashes if they fail to pass your criteria." do
params = {:member => {
:name => 'joe', :valid_posts_attributes => [
{:title => 'Kari, the awesome Ruby documentation browser!'},
{:title => 'The egalitarian assumption of the modern citizen'},
{:title => ''} # this will be ignored because of the :reject_if proc
]
}}
member = Member.create(params[:member])
member.valid_posts.length.should == 2
member.valid_posts.first.title.should == 'Kari, the awesome Ruby documentation browser!'
member.valid_posts[1].title.should == 'The egalitarian assumption of the modern citizen'
end
it ":reject_if also accepts a symbol for using methods" do
params = {:member => {
:name => 'joe', :valid_posts2_attributes => [
{:title => 'Kari, the awesome Ruby documentation browser!'},
{:title => 'The egalitarian assumption of the modern citizen'},
{:title => ''} # this will be ignored because of the :reject_if proc
]
}}
member = Member.create(params[:member])
member.valid_posts2.length.should == 2
member.valid_posts2.first.title.should == 'Kari, the awesome Ruby documentation browser!'
member.valid_posts2[1].title.should == 'The egalitarian assumption of the modern citizen'
member = Member.new(params[:member])
member.valid_posts2.first.title.should == 'Kari, the awesome Ruby documentation browser!'
member.valid_posts2[1].title.should == 'The egalitarian assumption of the modern citizen'
member.save
member.valid_posts2.length.should == 2
member.valid_posts2.first.title.should == 'Kari, the awesome Ruby documentation browser!'
member.valid_posts2[1].title.should == 'The egalitarian assumption of the modern citizen'
end
it "If the hash contains an id key that matches an already associated record, the matching record will be modified:" do
params = {:member => {
:name => 'joe', :posts_attributes => [
{:title => 'Kari, the awesome Ruby documentation browser!'},
{:title => 'The egalitarian assumption of the modern citizen'},
{:title => '', :_destroy => '1'} # this will be ignored
]
}}
member = Member.create(params[:member])
# when
id1 = member.posts[0].id
id2 = member.posts[1].id
member.attributes = {
:name => 'Joe',
:posts_attributes => [
{:id => id1, :title => '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!'},
{:id => id2, :title => '[UPDATED] other post'}
]
}
# then
member.posts.first.title.should == '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!'
member.posts[1].title.should == '[UPDATED] other post'
end
it "If the hash contains an id key that matches existing records, the matching records will associated" do
post1 = Post.create(:title => "First Old post")
post2 = Post.create(:title => "Second Old post")
params = {:member => {
:name => 'joe', :posts_attributes => [
{:id => post1.id},
{:id => post2.id}
]
}}
member = Member.create(params[:member])
member.posts[0].id.should == post1.id
member.posts[1].id.should == post2.id
member.posts.first.title.should == 'First Old post'
member.posts[1].title.should == 'Second Old post'
end
it "If the hash contains a mix of existing and new records, the existing record will be updated, and a new record will be created" do
post1 = Post.create(:title => "First Old post")
params = {:member => {
:name => 'joe', :posts_attributes => [
{:id => post1.id, :title => "Updated first Old post"},
{:title => "New post"}
]
}}
member = Member.create(params[:member])
member.posts[0].id.should == post1.id
member.posts.first.title.should == 'Updated first Old post'
member.posts[1].title.should == 'New post'
end
end
end
end
describe RelationshipWithNoProperty do
before(:each) do
@start_node = Neo4j::Model.new
@end_node = Neo4j::Model.new
end
subject do
RelationshipWithNoProperty.new(:foo, @start_node, @end_node)
end
it "should persist" do
subject.save
RelationshipWithNoProperty._all.size.should == 1
subject.class.all.to_a.size.should == 1
end
it_should_behave_like "a new model"
it_should_behave_like "a loadable model"
it_should_behave_like "a saveable model"
it_should_behave_like "a creatable relationship model"
it_should_behave_like "a destroyable model"
it_should_behave_like "an updatable model"
context "when there's lots of them" do
before(:each) do
subject.class.create!(:foo, @start_node, @end_node)
subject.class.create!(:foo, @start_node, @end_node)
subject.class.create!(:foo, @start_node, @end_node)
end
it "should be possible to #count" do
subject.class.count.should == 4
end
it "should be possible to #destroy_all" do
subject.class.all.to_a.size.should == 4
subject.class.destroy_all
subject.class.all.to_a.should be_empty
end
end
end
class RelationshipWithProperty < Neo4j::Rails::Relationship
property :flavour
index :flavour
property :required_on_create
property :required_on_update
property :created
attr_reader :saved
validates :flavour, :presence => true
validates :required_on_create, :presence => true, :on => :create
validates :required_on_update, :presence => true, :on => :update
before_create :timestamp
after_create :mark_saved
protected
def timestamp
self.created = "yep"
end
def mark_saved
@saved = true
end
end
describe RelationshipWithProperty do
before(:each) do
@start_node = Neo4j::Model.new
@end_node = Neo4j::Model.new
end
subject do
RelationshipWithProperty.new(:foo, @start_node, @end_node)
end
context "when valid" do
before :each do
subject.flavour = "vanilla"
subject.required_on_create = "true"
subject.required_on_update = "true"
subject["new_attribute"] = "newun"
end
it_should_behave_like "a new model"
it_should_behave_like "a loadable model"
it_should_behave_like "a saveable model"
it_should_behave_like "a creatable relationship model"
it_should_behave_like "a destroyable model"
it_should_behave_like "an updatable model"
context "after being saved" do
before { subject.save }
it { should == subject.class.find('flavour: vanilla') }
it "should render as XML" do
subject.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<relationship-with-property>\n <flavour>vanilla</flavour>\n <required-on-create>true</required-on-create>\n <required-on-update>true</required-on-update>\n <new-attribute>newun</new-attribute>\n <created>yep</created>\n</relationship-with-property>\n"
end
it "should be able to modify one of its named attributes" do
lambda { subject.update_attributes!(:flavour => 'horse') }.should_not raise_error
subject.flavour.should == 'horse'
end
it "should not have the extended property" do
subject.attributes.should_not include("extended_property")
end
it "should have the new attribute" do
subject.attributes.should include("new_attribute")
subject.attributes["new_attribute"].should == "newun"
subject["new_attribute"].should == "newun"
end
it "should have the new attribute after find" do
obj = subject.class.find('flavour: vanilla')
obj.attributes.should include("new_attribute")
obj.attributes["new_attribute"].should == "newun"
end
it "should respond to class.all" do
subject.class.respond_to?(:all)
end
it "should respond to class#all(:flavour => 'vanilla')" do
subject.class.all('flavour: vanilla').should include(subject)
end
it "should also be included in the rules for the parent class" do
pending
subject.class.superclass.all.to_a.should include(subject)
end
context "and then made invalid" do
before { subject.required_on_update = nil }
it "shouldn't be updatable" do
subject.update_attributes(:flavour => "fish").should_not be_true
end
it "should have the same attribute values after an unsuccessful update and reload" do
subject.update_attributes(:flavour => "fish")
subject.reload.flavour.should == "vanilla"
subject.required_on_update.should_not be_nil
end
it "shouldn't have a new attribute after an unsuccessful update and reload" do
subject["this_is_new"] = "test"
subject.attributes.should include("this_is_new")
subject.update_attributes(:flavour => "fish")
subject.reload.flavour.should == "vanilla"
subject.required_on_update.should_not be_nil
subject.attributes.should_not include("this_is_new")
end
end
end
context "after create" do
before :each do
@obj = subject.class.create!(:foo, @start_node, @end_node, subject.attributes)
end
it "should have run the #timestamp callback" do
@obj.created.should_not be_nil
end
it "should have run the #mark_saved callback" do
@obj.saved.should_not be_nil
end
end
end
context "when invalid" do
it_should_behave_like "a new model"
it_should_behave_like "an unsaveable model"
it_should_behave_like "an uncreatable model"
it_should_behave_like "a non-updatable model"
end
end
describe "SettingRelationship" do
context "created with Neo4j::Rails::Relationship" do
before(:all) do
@actor_class = create_model(Neo4j::Model)
@actor_class.property :name
@movie_class = create_model(Neo4j::Model)
@movie_class.property :title
@actor_class.has_n(:acted_in)
@movie_class.has_n(:actors).from(:acted_in)
end
it "can be created with Neo4j::Rails::Relationship.create" do
actor = @actor_class.new
movie = @movie_class.new
Neo4j::Rails::Relationship.new(@actor_class.acted_in, actor, movie)
actor.acted_in.size.should == 1
actor.acted_in.should include(movie)
movie.actors.size.should == 1
movie.actors.should include(actor)
end
end
context "create a Neo4j::Rails::Relationship" do
before(:each) do
@start_node = NodeWithRelationship.new
@end_node = Neo4j::Rails::Model.new
end
it "has_n" do
@start_node.outgoing(:baaz) << Neo4j::Rails::Model.new
@start_node.baaz.size.should == 1
@start_node.save
@start_node.baaz.size.should == 1
@start_node.outgoing(:baaz).to_a.size.should == 1
@start_node.outgoing(:baaz) << Neo4j::Rails::Model.new
@start_node.outgoing(:baaz).to_a.size.should == 2
@start_node.baaz.size.should == 2
@start_node.save
@start_node.baaz.size.should == 2
end
it "add an outgoing should set the incoming" do
@start_node.outgoing(:other_node) << @end_node
@end_node.incoming(:other_node).first.should == @start_node
end
it "add an incoming should set the outgoing" do
@start_node.incoming(:other_node) << @end_node
@end_node.outgoing(:other_node).first.should == @start_node
@start_node.save
@end_node.should be_persisted
@start_node.should be_persisted
@end_node.outgoing(:other_node).first.should == @start_node
@start_node.incoming(:other_node).first.should == @end_node
@end_node.outgoing(:other_node).to_a.size.should == 1
@start_node.incoming(:other_node).to_a.size.should == 1
# and again with
@start_node = Neo4j::Node.load(@start_node.neo_id)
@end_node = Neo4j::Node.load(@end_node.neo_id)
@end_node.should be_persisted
@start_node.should be_persisted
@end_node.outgoing(:other_node).first.should == @start_node
@start_node.incoming(:other_node).first.should == @end_node
@end_node.outgoing(:other_node).to_a.size.should == 1
@start_node.incoming(:other_node).to_a.size.should == 1
end
it "add an outgoing should set the outgoing" do
@start_node.outgoing(:other_node) << @end_node
@end_node.incoming(:other_node).first.should == @start_node
@start_node.save
@end_node.should be_persisted
@start_node.should be_persisted
@end_node.incoming(:other_node).first.should == @start_node
@start_node.outgoing(:other_node).first.should == @end_node
end
it "adding many different relationships" do
@start_node.outgoing(:foo) << (c = Neo4j::Rails::Model.new)
@start_node.outgoing(:other_node) << (a = Neo4j::Rails::Model.new)
@start_node.outgoing(:other_node) << (b = Neo4j::Rails::Model.new)
@start_node.outgoing(:foo).size.should == 1
@start_node.outgoing(:foo).should include(c)
@start_node.outgoing(:other_node).size.should == 2
@start_node.outgoing(:other_node).should include(a, b)
c.incoming(:foo).size.should == 1
c.incoming(:foo).should include(@start_node)
b.incoming(:other_node).size.should == 1
b.incoming(:other_node).should include(@start_node)
a.incoming(:other_node).size.should == 1
a.incoming(:other_node).should include(@start_node)
@start_node.save
@start_node.outgoing(:foo).size.should == 1
@start_node.outgoing(:foo).should include(c)
@start_node.outgoing(:other_node).size.should == 2
@start_node.outgoing(:other_node).should include(a, b)
c.incoming(:foo).size.should == 1
c.incoming(:foo).should include(@start_node)
b.incoming(:other_node).size.should == 1
b.incoming(:other_node).should include(@start_node)
a.incoming(:other_node).size.should == 1
a.incoming(:other_node).should include(@start_node)
end
it "add an outgoing twice should set the outgoing" do
@start_node.outgoing(:other_node) << @end_node
@start_node.outgoing(:other_node).size.should == 1
@end_node.incoming(:other_node).size.should == 1
@start_node.save
@start_node.outgoing(:other_node).size.should == 1
@end_node.incoming(:other_node).size.should == 1
@start_node.outgoing(:other_node) << Neo4j::Rails::Model.new
@start_node.outgoing(:other_node).size.should == 2
@end_node.incoming(:other_node).size.should == 1
@start_node.save
@end_node.should be_persisted
@start_node.should be_persisted
@start_node.outgoing(:other_node).size.should == 2
@end_node.incoming(:other_node).size.should == 1
end
it "should find the relationship using #incoming method" do
clazz = create_model
a = clazz.create
b = clazz.create
c = clazz.create
a.outgoing(:qwe) << b
a.outgoing(:qwe) << c
a.outgoing(:qwe).size.should == 2
b.incoming(:qwe).size.should == 1
b.outgoing(:qwe).size.should == 0
c.incoming(:qwe).size.should == 1
c.outgoing(:qwe).size.should == 0
b.incoming(:qwe).should include(a)
c.incoming(:qwe).should include(a)
a.save
a.outgoing(:qwe).size.should == 2
b._java_node.incoming(:qwe).size.should == 1
b.incoming(:qwe).size.should == 1
b.outgoing(:qwe).size.should == 0
c.incoming(:qwe).size.should == 1
c.outgoing(:qwe).size.should == 0
b.incoming(:qwe).should include(a)
c.incoming(:qwe).should include(a)
end
end
context "setting a Neo4j::Rails::Relationship" do
subject { @start_node.other_node_rel } #@start_node.other_node_rel }
before(:each) do
@start_node = NodeWithRelationship.new
@end_node = Neo4j::Rails::Model.new
@start_node.other_node = @end_node
end
it { should be_kind_of(Neo4j::Rails::Relationship) }
it_should_behave_like "a relationship model"
end
context "setting has many" do
it "bla" do
@start_node = NodeWithRelationship.new
@end_node_1 = Neo4j::Rails::Model.new
@end_node_2 = Neo4j::Rails::Model.new
@start_node.baaz << @end_node_1 << @end_node_2
@start_node.baaz_rels.each do |rel|
rel.should be_kind_of(Neo4j::Rails::Relationship)
end
@start_node.baaz_rels.to_a.size.should == 2
@start_node.baaz.should include(@end_node_1, @end_node_2)
@start_node.save
@start_node.baaz_rels.each do |rel|
rel.should be_kind_of(Neo4j::Rails::Relationship)
end
@start_node.baaz_rels.to_a.size.should == 2
@start_node.baaz.should include(@end_node_1, @end_node_2)
end
end
context "setting RelationshipWithNoProperty" do
subject { @start_node.foobar_rel } #@start_node.other_node_rel }
before(:each) do
@start_node = NodeWithRelationship.new
@end_node = Neo4j::Rails::Model.new
@start_node.foobar = @end_node
end
it { should be_kind_of(RelationshipWithNoProperty) }
it "should create the correct relationship class after save" do
@start_node.foobar_rel.should be_kind_of(RelationshipWithNoProperty)
@start_node.save
@start_node.foobar_rel.should be_kind_of(RelationshipWithNoProperty)
end