/
one_to_one_composite_spec.rb
150 lines (100 loc) · 4.77 KB
/
one_to_one_composite_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
require 'spec_helper'
describe "Person.has(1, :profile) with CPK;" do
before(:all) do
class ::Person
include DataMapper::Resource
extend ConstraintSupport
property :id, Serial, :key => true
property :audit_id, Integer, :key => true, :min => 0
property :name, String, :required => true
has 1, :profile, constraint(:destroy)
has 1, :address, :through => :profile
end
class ::Profile
include DataMapper::Resource
property :person_id, Integer, :key => true, :min => 0
property :person_audit_id, Integer, :key => true, :min => 0
property :nick, String, :required => true
belongs_to :person
has 1, :address
end
class ::Address
include DataMapper::Resource
property :id, Serial
property :audit_id, Integer, :key => true, :min => 0
property :profile_id, Integer, :required => true, :unique => :profile, :unique_index => true, :min => 0
property :profile_audit_id, Integer, :required => true, :unique => :profile, :unique_index => true, :min => 0
property :body, String, :required => true
belongs_to :profile
end
DataMapper.auto_migrate!
end
before(:each) do
Address.all.destroy!
Profile.all.destroy!
Person.all.destroy!
end
describe "Person.accepts_nested_attributes_for(:profile)" do
before(:all) do
Person.accepts_nested_attributes_for :profile
end
it_should_behave_like "every accessible one_to_one composite association"
it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
end
describe "Person.accepts_nested_attributes_for(:profile, :allow_destroy => false)" do
before(:all) do
Person.accepts_nested_attributes_for :profile, :allow_destroy => false
end
it_should_behave_like "every accessible one_to_one composite association"
it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
end
describe "Person.accepts_nested_attributes_for(:profile, :allow_destroy => true)" do
before(:all) do
Person.accepts_nested_attributes_for :profile, :allow_destroy => true
end
it_should_behave_like "every accessible one_to_one composite association"
it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => true"
end
describe "Person.accepts_nested_attributes_for(:profile, :reject_if => lambda { |attrs| true })" do
before(:all) do
Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| true }
end
it_should_behave_like "every accessible one_to_one composite association"
it_should_behave_like "every accessible one_to_one composite association with a valid reject_if proc"
it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
end
describe "Person.accepts_nested_attributes_for(:profile, :reject_if => lambda { |attrs| false })" do
before(:all) do
Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| false }
end
it_should_behave_like "every accessible one_to_one composite association"
it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
end
describe "Profile.accepts_nested_attributes_for(:person)" do
before(:all) do
Profile.accepts_nested_attributes_for :person
end
it "should allow to update an existing profile via Person#profile_attributes" do
person = Person.create(:audit_id => 1, :name => 'Martin')
profile = Profile.create(:person => person, :nick => 'snusnu')
person.reload
Person.all.size.should == 1
Profile.all.size.should == 1
profile.person_attributes = { :id => person.id, :audit_id => 1, :name => 'Martin Gamsjaeger' }
profile.save.should be_true
Person.all.size.should == 1
Profile.all.size.should == 1
Person.first.name.should == 'Martin Gamsjaeger'
end
it "should return the attributes written to Person#profile_attributes from the Person#profile_attributes reader" do
profile = Profile.new :nick => 'snusnu'
profile.person_attributes.should be_nil
profile.person_attributes = { :name => 'Martin' }
profile.person_attributes.should == { :name => 'Martin' }
end
end
end