forked from mongodb/mongoid
/
has_one_spec.rb
257 lines (186 loc) · 5.9 KB
/
has_one_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
require "spec_helper"
describe Mongoid::Associations::HasOne do
before do
@attributes = { "mixed_drink" => {
"name" => "Jack and Coke", "_type" => "MixedDrink" },
"writer" => { "speed" => 50, "_type" => "HtmlWriter" }
}
@document = stub(:raw_attributes => @attributes, :update => true)
end
describe "#build_*" do
context "when attributes provided" do
before do
@association = Mongoid::Associations::HasOne.new(
@document,
@attributes["mixed_drink"],
Mongoid::Associations::Options.new(:name => :mixed_drink)
)
end
it "replaces the existing has_one" do
drink = @association.send(:build, { :name => "Sapphire and Tonic" })
drink.name.should == "Sapphire and Tonic"
end
end
context "when a type is supplied" do
before do
@association = Mongoid::Associations::HasOne.new(
@document,
@attributes["writer"],
Mongoid::Associations::Options.new(:name => :writer)
)
end
it "instantiates a class of that type" do
writer = @association.send(:build, { :speed => 500 }, HtmlWriter)
writer.should be_a_kind_of(HtmlWriter)
writer.speed.should == 500
end
end
context "setting the parent relationship" do
before do
@person = Person.new
end
it "happens before any other operation" do
name = @person.build_name(:set_parent => true, :street => "Madison Ave")
name._parent.should == @person
@person.name.should == name
end
end
end
describe ".instantiate" do
context "when the attributes are nil" do
before do
@document = Person.new
@association = Mongoid::Associations::HasOne.instantiate(
@document,
Mongoid::Associations::Options.new(:name => :name)
)
end
it "returns nil" do
@association.should be_nil
end
end
context "when attributes are empty" do
before do
@document = stub(:raw_attributes => { "name" => {} })
@association = Mongoid::Associations::HasOne.instantiate(
@document,
Mongoid::Associations::Options.new(:name => :name)
)
end
it "returns nil" do
@association.should be_nil
end
end
context "when attributes exist" do
before do
@document = stub(:raw_attributes => { "name" => { "first_name" => "Test" } })
@options = Mongoid::Associations::Options.new(:name => :name)
end
it "delegates to new" do
Mongoid::Associations::HasOne.expects(:new).with(@document, { "first_name" => "Test" }, @options)
Mongoid::Associations::HasOne.instantiate(@document, @options)
end
end
end
describe "#method_missing" do
before do
@association = Mongoid::Associations::HasOne.new(
@document,
@attributes["mixed_drink"],
Mongoid::Associations::Options.new(:name => :mixed_drink)
)
end
context "when getting values" do
it "delegates to the document" do
@association.name.should == "Jack and Coke"
end
end
context "when setting values" do
it "delegates to the document" do
@association.name = "Jack and Coke"
@association.name.should == "Jack and Coke"
end
end
end
describe "#nested_build" do
context "when attributes provided" do
before do
@association = Mongoid::Associations::HasOne.new(
@document,
@attributes["mixed_drink"],
Mongoid::Associations::Options.new(:name => :mixed_drink)
)
end
it "replaces the existing has_one" do
drink = @association.nested_build({ :name => "Sapphire and Tonic" })
drink.name.should == "Sapphire and Tonic"
end
end
end
describe ".macro" do
it "returns :has_one" do
Mongoid::Associations::HasOne.macro.should == :has_one
end
end
describe ".update" do
context "when setting to a non-nil value" do
before do
@name = Name.new(:first_name => "Donald")
@person = Person.new(:title => "Sir")
@association = Mongoid::Associations::HasOne.update(
@name,
@person,
Mongoid::Associations::Options.new(:name => :name)
)
end
it "parentizes the child document" do
@name._parent.should == @person
end
it "sets the attributes of the child on the parent" do
@person.attributes[:name].should ==
{ "_id" => "donald", "first_name" => "Donald", "_type" => "Name" }
end
it "returns the proxy" do
@association.target.should == @name
end
end
context "when setting the object to nil" do
before do
@name = Name.new(:first_name => "Donald")
@person = Person.new(:title => "Sir")
Mongoid::Associations::HasOne.update(
nil,
@person,
Mongoid::Associations::Options.new(:name => :name)
)
end
it "clears out the association" do
@person.name.should be_nil
end
end
end
describe "#to_a" do
before do
@association = Mongoid::Associations::HasOne.new(
@document,
@attributes["mixed_drink"],
Mongoid::Associations::Options.new(:name => :mixed_drink)
)
end
it "returns the target in a new array" do
@association.to_a.first.should be_a_kind_of(MixedDrink)
end
end
describe "#valid?" do
context "when the document is not nil" do
before do
@document = stub(:raw_attributes => { "name" => { "first_name" => "Test" } }, :update => true)
@options = Mongoid::Associations::Options.new(:name => :name)
@association = Mongoid::Associations::HasOne.instantiate(@document, @options)
end
it "validates the document" do
@association.valid?.should be_true
end
end
end
end