mongo_mapper-unstable 2009.10.11
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +87 -0
- data/VERSION +1 -0
- data/bin/mmconsole +55 -0
- data/lib/mongo_mapper/associations/base.rb +83 -0
- data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
- data/lib/mongo_mapper/associations/belongs_to_proxy.rb +22 -0
- data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +27 -0
- data/lib/mongo_mapper/associations/many_documents_proxy.rb +116 -0
- data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
- data/lib/mongo_mapper/associations/many_embedded_proxy.rb +67 -0
- data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
- data/lib/mongo_mapper/associations/many_proxy.rb +6 -0
- data/lib/mongo_mapper/associations/proxy.rb +74 -0
- data/lib/mongo_mapper/associations.rb +86 -0
- data/lib/mongo_mapper/callbacks.rb +106 -0
- data/lib/mongo_mapper/dirty.rb +137 -0
- data/lib/mongo_mapper/document.rb +340 -0
- data/lib/mongo_mapper/dynamic_finder.rb +35 -0
- data/lib/mongo_mapper/embedded_document.rb +355 -0
- data/lib/mongo_mapper/finder_options.rb +98 -0
- data/lib/mongo_mapper/key.rb +36 -0
- data/lib/mongo_mapper/observing.rb +50 -0
- data/lib/mongo_mapper/pagination.rb +51 -0
- data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
- data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +27 -0
- data/lib/mongo_mapper/save_with_validation.rb +19 -0
- data/lib/mongo_mapper/serialization.rb +55 -0
- data/lib/mongo_mapper/serializers/json_serializer.rb +92 -0
- data/lib/mongo_mapper/support.rb +161 -0
- data/lib/mongo_mapper/validations.rb +69 -0
- data/lib/mongo_mapper.rb +111 -0
- data/mongo_mapper.gemspec +162 -0
- data/specs.watchr +32 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/custom_matchers.rb +55 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +49 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +244 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +132 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +174 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +297 -0
- data/test/functional/associations/test_many_proxy.rb +331 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +18 -0
- data/test/functional/test_callbacks.rb +85 -0
- data/test/functional/test_dirty.rb +138 -0
- data/test/functional/test_document.rb +1051 -0
- data/test/functional/test_embedded_document.rb +97 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_pagination.rb +87 -0
- data/test/functional/test_rails_compatibility.rb +30 -0
- data/test/functional/test_validations.rb +279 -0
- data/test/models.rb +195 -0
- data/test/test_helper.rb +30 -0
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_association_base.rb +144 -0
- data/test/unit/test_document.rb +184 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +656 -0
- data/test/unit/test_finder_options.rb +261 -0
- data/test/unit/test_key.rb +172 -0
- data/test/unit/test_mongomapper.rb +28 -0
- data/test/unit/test_observing.rb +101 -0
- data/test/unit/test_pagination.rb +109 -0
- data/test/unit/test_rails_compatibility.rb +39 -0
- data/test/unit/test_serializations.rb +52 -0
- data/test/unit/test_support.rb +291 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +503 -0
- metadata +210 -0
@@ -0,0 +1,297 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
Room.collection.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
should "default reader to empty array" do
|
10
|
+
Room.new.messages.should == []
|
11
|
+
end
|
12
|
+
|
13
|
+
should "add type key to polymorphic class base" do
|
14
|
+
Message.keys.keys.should include('_type')
|
15
|
+
end
|
16
|
+
|
17
|
+
should "allow adding to assiciation like it was an array" do
|
18
|
+
room = Room.new
|
19
|
+
room.messages << Enter.new
|
20
|
+
room.messages.push Exit.new
|
21
|
+
room.messages.concat Exit.new
|
22
|
+
room.messages.size.should == 3
|
23
|
+
end
|
24
|
+
|
25
|
+
should "be able to replace the association" do
|
26
|
+
room = Room.create(:name => 'Lounge')
|
27
|
+
|
28
|
+
lambda {
|
29
|
+
room.messages = [
|
30
|
+
Enter.new(:body => 'John entered room', :position => 1),
|
31
|
+
Chat.new(:body => 'Heyyyoooo!', :position => 2),
|
32
|
+
Exit.new(:body => 'John exited room', :position => 3)
|
33
|
+
]
|
34
|
+
}.should change { Message.count }.by(3)
|
35
|
+
|
36
|
+
from_db = Room.find(room.id)
|
37
|
+
messages = from_db.messages.all :order => "position"
|
38
|
+
messages.size.should == 3
|
39
|
+
messages[0].body.should == 'John entered room'
|
40
|
+
messages[1].body.should == 'Heyyyoooo!'
|
41
|
+
messages[2].body.should == 'John exited room'
|
42
|
+
end
|
43
|
+
|
44
|
+
should "correctly store type when using <<, push and concat" do
|
45
|
+
room = Room.new
|
46
|
+
room.messages << Enter.new(:body => 'John entered the room', :position => 1)
|
47
|
+
room.messages.push Exit.new(:body => 'John entered the room', :position => 2)
|
48
|
+
room.messages.concat Chat.new(:body => 'Holla!' , :position => 3)
|
49
|
+
|
50
|
+
from_db = Room.find(room.id)
|
51
|
+
messages = from_db.messages.all :order => "position"
|
52
|
+
messages[0]._type.should == 'Enter'
|
53
|
+
messages[1]._type.should == 'Exit'
|
54
|
+
messages[2]._type.should == 'Chat'
|
55
|
+
end
|
56
|
+
|
57
|
+
context "build" do
|
58
|
+
should "assign foreign key" do
|
59
|
+
room = Room.create
|
60
|
+
message = room.messages.build
|
61
|
+
message.room_id.should == room.id
|
62
|
+
end
|
63
|
+
|
64
|
+
should "assign _type" do
|
65
|
+
room = Room.create
|
66
|
+
message = room.messages.build
|
67
|
+
message._type.should == 'Message'
|
68
|
+
end
|
69
|
+
|
70
|
+
should "allow assigning attributes" do
|
71
|
+
room = Room.create
|
72
|
+
message = room.messages.build(:body => 'Foo!')
|
73
|
+
message.body.should == 'Foo!'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "create" do
|
78
|
+
should "assign foreign key" do
|
79
|
+
room = Room.create
|
80
|
+
message = room.messages.create
|
81
|
+
message.room_id.should == room.id
|
82
|
+
end
|
83
|
+
|
84
|
+
should "assign _type" do
|
85
|
+
room = Room.create
|
86
|
+
message = room.messages.create
|
87
|
+
message._type.should == 'Message'
|
88
|
+
end
|
89
|
+
|
90
|
+
should "save record" do
|
91
|
+
room = Room.create
|
92
|
+
lambda {
|
93
|
+
room.messages.create
|
94
|
+
}.should change { Message.count }
|
95
|
+
end
|
96
|
+
|
97
|
+
should "allow passing attributes" do
|
98
|
+
room = Room.create
|
99
|
+
message = room.messages.create(:body => 'Foo!')
|
100
|
+
message.body.should == 'Foo!'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "count" do
|
105
|
+
should "work scoped to association" do
|
106
|
+
room = Room.create
|
107
|
+
3.times { room.messages.create }
|
108
|
+
|
109
|
+
other_room = Room.create
|
110
|
+
2.times { other_room.messages.create }
|
111
|
+
|
112
|
+
room.messages.count.should == 3
|
113
|
+
other_room.messages.count.should == 2
|
114
|
+
end
|
115
|
+
|
116
|
+
should "work with conditions" do
|
117
|
+
room = Room.create
|
118
|
+
room.messages.create(:body => 'Foo')
|
119
|
+
room.messages.create(:body => 'Other 1')
|
120
|
+
room.messages.create(:body => 'Other 2')
|
121
|
+
|
122
|
+
room.messages.count(:body => 'Foo').should == 1
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "Finding scoped to association" do
|
127
|
+
setup do
|
128
|
+
@lounge = Room.create(:name => 'Lounge')
|
129
|
+
@lm1 = Message.create(:body => 'Loungin!', :position => 1)
|
130
|
+
@lm2 = Message.create(:body => 'I love loungin!', :position => 2)
|
131
|
+
@lounge.messages = [@lm1, @lm2]
|
132
|
+
@lounge.save
|
133
|
+
|
134
|
+
@hall = Room.create(:name => 'Hall')
|
135
|
+
@hm1 = Message.create(:body => 'Do not fall in the hall', :position => 1)
|
136
|
+
@hm2 = Message.create(:body => 'Hall the king!', :position => 2)
|
137
|
+
@hm3 = Message.create(:body => 'Loungin!', :position => 3)
|
138
|
+
@hall.messages = [@hm1, @hm2, @hm3]
|
139
|
+
@hall.save
|
140
|
+
end
|
141
|
+
|
142
|
+
context "dynamic finders" do
|
143
|
+
should "work with single key" do
|
144
|
+
@lounge.messages.find_by_position(1).should == @lm1
|
145
|
+
@hall.messages.find_by_position(2).should == @hm2
|
146
|
+
end
|
147
|
+
|
148
|
+
should "work with multiple keys" do
|
149
|
+
@lounge.messages.find_by_body_and_position('Loungin!', 1).should == @lm1
|
150
|
+
@lounge.messages.find_by_body_and_position('Loungin!', 2).should be_nil
|
151
|
+
end
|
152
|
+
|
153
|
+
should "raise error when using !" do
|
154
|
+
lambda {
|
155
|
+
@lounge.messages.find_by_position!(222)
|
156
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
157
|
+
end
|
158
|
+
|
159
|
+
context "find_or_create_by" do
|
160
|
+
should "not create document if found" do
|
161
|
+
lambda {
|
162
|
+
message = @lounge.messages.find_or_create_by_body('Loungin!')
|
163
|
+
message.room.should == @lounge
|
164
|
+
message.should == @lm1
|
165
|
+
}.should_not change { Message.count }
|
166
|
+
end
|
167
|
+
|
168
|
+
should "create document if not found" do
|
169
|
+
lambda {
|
170
|
+
message = @lounge.messages.find_or_create_by_body('Yo dawg!')
|
171
|
+
message.room.should == @lounge
|
172
|
+
message._type.should == 'Message'
|
173
|
+
}.should change { Message.count }.by(1)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context "with :all" do
|
179
|
+
should "work" do
|
180
|
+
@lounge.messages.find(:all, :order => "position").should == [@lm1, @lm2]
|
181
|
+
end
|
182
|
+
|
183
|
+
should "work with conditions" do
|
184
|
+
messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'}, :order => "position")
|
185
|
+
messages.should == [@lm1]
|
186
|
+
end
|
187
|
+
|
188
|
+
should "work with order" do
|
189
|
+
messages = @lounge.messages.find(:all, :order => 'position desc')
|
190
|
+
messages.should == [@lm2, @lm1]
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context "with #all" do
|
195
|
+
should "work" do
|
196
|
+
@lounge.messages.all(:order => "position").should == [@lm1, @lm2]
|
197
|
+
end
|
198
|
+
|
199
|
+
should "work with conditions" do
|
200
|
+
messages = @lounge.messages.all(:conditions => {:body => 'Loungin!'}, :order => "position")
|
201
|
+
messages.should == [@lm1]
|
202
|
+
end
|
203
|
+
|
204
|
+
should "work with order" do
|
205
|
+
messages = @lounge.messages.all(:order => 'position desc')
|
206
|
+
messages.should == [@lm2, @lm1]
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context "with :first" do
|
211
|
+
should "work" do
|
212
|
+
@lounge.messages.find(:first, :order => "position asc").should == @lm1
|
213
|
+
end
|
214
|
+
|
215
|
+
should "work with conditions" do
|
216
|
+
message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'}, :order => "position asc")
|
217
|
+
message.should == @lm2
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "with #first" do
|
222
|
+
should "work" do
|
223
|
+
@lounge.messages.first(:order => "position asc").should == @lm1
|
224
|
+
end
|
225
|
+
|
226
|
+
should "work with conditions" do
|
227
|
+
message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'}, :order => "position asc")
|
228
|
+
message.should == @lm2
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
context "with :last" do
|
233
|
+
should "work" do
|
234
|
+
@lounge.messages.find(:last, :order => "position asc").should == @lm2
|
235
|
+
end
|
236
|
+
|
237
|
+
should "work with conditions" do
|
238
|
+
message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'}, :order => "position asc")
|
239
|
+
message.should == @lm1
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context "with #last" do
|
244
|
+
should "work" do
|
245
|
+
@lounge.messages.last(:order => "position asc").should == @lm2
|
246
|
+
end
|
247
|
+
|
248
|
+
should "work with conditions" do
|
249
|
+
message = @lounge.messages.last(:conditions => {:body => 'Loungin!'}, :order => "position asc")
|
250
|
+
message.should == @lm1
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
context "with one id" do
|
255
|
+
should "work for id in association" do
|
256
|
+
@lounge.messages.find(@lm2.id).should == @lm2
|
257
|
+
end
|
258
|
+
|
259
|
+
should "not work for id not in association" do
|
260
|
+
lambda {
|
261
|
+
@lounge.messages.find(@hm2.id)
|
262
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context "with multiple ids" do
|
267
|
+
should "work for ids in association" do
|
268
|
+
messages = @lounge.messages.find(@lm1.id, @lm2.id)
|
269
|
+
messages.should == [@lm1, @lm2]
|
270
|
+
end
|
271
|
+
|
272
|
+
should "not work for ids not in association" do
|
273
|
+
lambda {
|
274
|
+
@lounge.messages.find(@lm1.id, @lm2.id, @hm2.id)
|
275
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
context "with #paginate" do
|
280
|
+
setup do
|
281
|
+
@messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => 'position asc')
|
282
|
+
end
|
283
|
+
|
284
|
+
should "return total pages" do
|
285
|
+
@messages.total_pages.should == 2
|
286
|
+
end
|
287
|
+
|
288
|
+
should "return total entries" do
|
289
|
+
@messages.total_entries.should == 3
|
290
|
+
end
|
291
|
+
|
292
|
+
should "return the subject" do
|
293
|
+
@messages.should == [@hm1, @hm2]
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
@@ -0,0 +1,331 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class ManyProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
Project.collection.clear
|
7
|
+
Status.collection.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
should "default reader to empty array" do
|
11
|
+
project = Project.new
|
12
|
+
project.statuses.should == []
|
13
|
+
end
|
14
|
+
|
15
|
+
should "allow adding to association like it was an array" do
|
16
|
+
project = Project.new
|
17
|
+
project.statuses << Status.new
|
18
|
+
project.statuses.push Status.new
|
19
|
+
project.statuses.concat Status.new
|
20
|
+
project.statuses.size.should == 3
|
21
|
+
end
|
22
|
+
|
23
|
+
should "be able to replace the association" do
|
24
|
+
project = Project.new
|
25
|
+
project.statuses = [Status.new("name" => "ready")]
|
26
|
+
project.save.should be_true
|
27
|
+
|
28
|
+
from_db = Project.find(project.id)
|
29
|
+
from_db.statuses.size.should == 1
|
30
|
+
from_db.statuses[0].name.should == "ready"
|
31
|
+
end
|
32
|
+
|
33
|
+
should "correctly assign foreign key when using <<, push and concat" do
|
34
|
+
project = Project.new
|
35
|
+
project.statuses << Status.new(:name => '<<')
|
36
|
+
project.statuses.push Status.new(:name => 'push')
|
37
|
+
project.statuses.concat Status.new(:name => 'concat')
|
38
|
+
|
39
|
+
from_db = Project.find(project.id)
|
40
|
+
from_db.statuses[0].project_id.should == project.id
|
41
|
+
from_db.statuses[1].project_id.should == project.id
|
42
|
+
from_db.statuses[2].project_id.should == project.id
|
43
|
+
end
|
44
|
+
|
45
|
+
context "build" do
|
46
|
+
should "assign foreign key" do
|
47
|
+
project = Project.create
|
48
|
+
status = project.statuses.build
|
49
|
+
status.project_id.should == project.id
|
50
|
+
end
|
51
|
+
|
52
|
+
should "allow assigning attributes" do
|
53
|
+
project = Project.create
|
54
|
+
status = project.statuses.build(:name => 'Foo')
|
55
|
+
status.name.should == 'Foo'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "create" do
|
60
|
+
should "assign foreign key" do
|
61
|
+
project = Project.create
|
62
|
+
status = project.statuses.create
|
63
|
+
status.project_id.should == project.id
|
64
|
+
end
|
65
|
+
|
66
|
+
should "save record" do
|
67
|
+
project = Project.create
|
68
|
+
lambda {
|
69
|
+
project.statuses.create
|
70
|
+
}.should change { Status.count }
|
71
|
+
end
|
72
|
+
|
73
|
+
should "allow passing attributes" do
|
74
|
+
project = Project.create
|
75
|
+
status = project.statuses.create(:name => 'Foo!')
|
76
|
+
status.name.should == 'Foo!'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "count" do
|
81
|
+
should "work scoped to association" do
|
82
|
+
project = Project.create
|
83
|
+
3.times { project.statuses.create }
|
84
|
+
|
85
|
+
other_project = Project.create
|
86
|
+
2.times { other_project.statuses.create }
|
87
|
+
|
88
|
+
project.statuses.count.should == 3
|
89
|
+
other_project.statuses.count.should == 2
|
90
|
+
end
|
91
|
+
|
92
|
+
should "work with conditions" do
|
93
|
+
project = Project.create
|
94
|
+
project.statuses.create(:name => 'Foo')
|
95
|
+
project.statuses.create(:name => 'Other 1')
|
96
|
+
project.statuses.create(:name => 'Other 2')
|
97
|
+
|
98
|
+
project.statuses.count(:name => 'Foo').should == 1
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "Unassociating documents" do
|
103
|
+
setup do
|
104
|
+
@project = Project.create
|
105
|
+
@project.statuses << Status.create(:name => '1')
|
106
|
+
@project.statuses << Status.create(:name => '2')
|
107
|
+
|
108
|
+
@project2 = Project.create
|
109
|
+
@project2.statuses << Status.create(:name => '1')
|
110
|
+
@project2.statuses << Status.create(:name => '2')
|
111
|
+
end
|
112
|
+
|
113
|
+
should "work with destroy all" do
|
114
|
+
@project.statuses.count.should == 2
|
115
|
+
@project.statuses.destroy_all
|
116
|
+
@project.statuses.count.should == 0
|
117
|
+
|
118
|
+
@project2.statuses.count.should == 2
|
119
|
+
Status.count.should == 2
|
120
|
+
end
|
121
|
+
|
122
|
+
should "work with destroy all and conditions" do
|
123
|
+
@project.statuses.count.should == 2
|
124
|
+
@project.statuses.destroy_all(:name => '1')
|
125
|
+
@project.statuses.count.should == 1
|
126
|
+
|
127
|
+
@project2.statuses.count.should == 2
|
128
|
+
Status.count.should == 3
|
129
|
+
end
|
130
|
+
|
131
|
+
should "work with delete all" do
|
132
|
+
@project.statuses.count.should == 2
|
133
|
+
@project.statuses.delete_all
|
134
|
+
@project.statuses.count.should == 0
|
135
|
+
|
136
|
+
@project2.statuses.count.should == 2
|
137
|
+
Status.count.should == 2
|
138
|
+
end
|
139
|
+
|
140
|
+
should "work with delete all and conditions" do
|
141
|
+
@project.statuses.count.should == 2
|
142
|
+
@project.statuses.delete_all(:name => '1')
|
143
|
+
@project.statuses.count.should == 1
|
144
|
+
|
145
|
+
@project2.statuses.count.should == 2
|
146
|
+
Status.count.should == 3
|
147
|
+
end
|
148
|
+
|
149
|
+
should "work with nullify" do
|
150
|
+
@project.statuses.count.should == 2
|
151
|
+
@project.statuses.nullify
|
152
|
+
@project.statuses.count.should == 0
|
153
|
+
|
154
|
+
@project2.statuses.count.should == 2
|
155
|
+
Status.count.should == 4
|
156
|
+
Status.count(:name => '1').should == 2
|
157
|
+
Status.count(:name => '2').should == 2
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context "Finding scoped to association" do
|
162
|
+
setup do
|
163
|
+
@project1 = Project.new(:name => 'Project 1')
|
164
|
+
@brand_new = Status.create(:name => 'New', :position => 1 )
|
165
|
+
@complete = Status.create(:name => 'Complete', :position => 2)
|
166
|
+
@project1.statuses = [@brand_new, @complete]
|
167
|
+
@project1.save
|
168
|
+
|
169
|
+
@project2 = Project.create(:name => 'Project 2')
|
170
|
+
@in_progress = Status.create(:name => 'In Progress')
|
171
|
+
@archived = Status.create(:name => 'Archived')
|
172
|
+
@another_complete = Status.create(:name => 'Complete')
|
173
|
+
@project2.statuses = [@in_progress, @archived, @another_complete]
|
174
|
+
@project2.save
|
175
|
+
end
|
176
|
+
|
177
|
+
context "dynamic finders" do
|
178
|
+
should "work with single key" do
|
179
|
+
@project1.statuses.find_by_name('New').should == @brand_new
|
180
|
+
@project2.statuses.find_by_name('In Progress').should == @in_progress
|
181
|
+
end
|
182
|
+
|
183
|
+
should "work with multiple keys" do
|
184
|
+
@project1.statuses.find_by_name_and_position('New', 1).should == @brand_new
|
185
|
+
@project1.statuses.find_by_name_and_position('New', 2).should be_nil
|
186
|
+
end
|
187
|
+
|
188
|
+
should "raise error when using !" do
|
189
|
+
lambda {
|
190
|
+
@project1.statuses.find_by_name!('Fake')
|
191
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
192
|
+
end
|
193
|
+
|
194
|
+
context "find_or_create_by" do
|
195
|
+
should "not create document if found" do
|
196
|
+
lambda {
|
197
|
+
status = @project1.statuses.find_or_create_by_name('New')
|
198
|
+
status.project.should == @project1
|
199
|
+
status.should == @brand_new
|
200
|
+
}.should_not change { Status.count }
|
201
|
+
end
|
202
|
+
|
203
|
+
should "create document if not found" do
|
204
|
+
lambda {
|
205
|
+
status = @project1.statuses.find_or_create_by_name('Delivered')
|
206
|
+
status.project.should == @project1
|
207
|
+
}.should change { Status.count }.by(1)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "with :all" do
|
213
|
+
should "work" do
|
214
|
+
@project1.statuses.find(:all, :order => "position asc").should == [@brand_new, @complete]
|
215
|
+
end
|
216
|
+
|
217
|
+
should "work with conditions" do
|
218
|
+
statuses = @project1.statuses.find(:all, :conditions => {'name' => 'Complete'})
|
219
|
+
statuses.should == [@complete]
|
220
|
+
end
|
221
|
+
|
222
|
+
should "work with order" do
|
223
|
+
statuses = @project1.statuses.find(:all, :order => 'name asc')
|
224
|
+
statuses.should == [@complete, @brand_new]
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context "with #all" do
|
229
|
+
should "work" do
|
230
|
+
@project1.statuses.all(:order => "position asc").should == [@brand_new, @complete]
|
231
|
+
end
|
232
|
+
|
233
|
+
should "work with conditions" do
|
234
|
+
statuses = @project1.statuses.all(:conditions => {'name' => 'Complete'})
|
235
|
+
statuses.should == [@complete]
|
236
|
+
end
|
237
|
+
|
238
|
+
should "work with order" do
|
239
|
+
statuses = @project1.statuses.all(:order => 'name asc')
|
240
|
+
statuses.should == [@complete, @brand_new]
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
context "with :first" do
|
245
|
+
should "work" do
|
246
|
+
@project1.statuses.find(:first, :order => 'name').should == @complete
|
247
|
+
end
|
248
|
+
|
249
|
+
should "work with conditions" do
|
250
|
+
status = @project1.statuses.find(:first, :conditions => {:name => 'Complete'})
|
251
|
+
status.should == @complete
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
context "with #first" do
|
256
|
+
should "work" do
|
257
|
+
@project1.statuses.first(:order => 'name').should == @complete
|
258
|
+
end
|
259
|
+
|
260
|
+
should "work with conditions" do
|
261
|
+
status = @project1.statuses.first(:conditions => {:name => 'Complete'})
|
262
|
+
status.should == @complete
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context "with :last" do
|
267
|
+
should "work" do
|
268
|
+
@project1.statuses.find(:last, :order => "position asc").should == @complete
|
269
|
+
end
|
270
|
+
|
271
|
+
should "work with conditions" do
|
272
|
+
status = @project1.statuses.find(:last, :order => 'position', :conditions => {:name => 'New'})
|
273
|
+
status.should == @brand_new
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
context "with #last" do
|
278
|
+
should "work" do
|
279
|
+
@project1.statuses.last(:order => "position asc").should == @complete
|
280
|
+
end
|
281
|
+
|
282
|
+
should "work with conditions" do
|
283
|
+
status = @project1.statuses.last(:order => 'position', :conditions => {:name => 'New'})
|
284
|
+
status.should == @brand_new
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
context "with one id" do
|
289
|
+
should "work for id in association" do
|
290
|
+
@project1.statuses.find(@complete.id).should == @complete
|
291
|
+
end
|
292
|
+
|
293
|
+
should "not work for id not in association" do
|
294
|
+
lambda {
|
295
|
+
@project1.statuses.find(@archived.id)
|
296
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
context "with multiple ids" do
|
301
|
+
should "work for ids in association" do
|
302
|
+
statuses = @project1.statuses.find(@brand_new.id, @complete.id)
|
303
|
+
statuses.should == [@brand_new, @complete]
|
304
|
+
end
|
305
|
+
|
306
|
+
should "not work for ids not in association" do
|
307
|
+
lambda {
|
308
|
+
@project1.statuses.find(@brand_new.id, @complete.id, @archived.id)
|
309
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
context "with #paginate" do
|
314
|
+
setup do
|
315
|
+
@statuses = @project2.statuses.paginate(:per_page => 2, :page => 1, :order => 'name asc')
|
316
|
+
end
|
317
|
+
|
318
|
+
should "return total pages" do
|
319
|
+
@statuses.total_pages.should == 2
|
320
|
+
end
|
321
|
+
|
322
|
+
should "return total entries" do
|
323
|
+
@statuses.total_entries.should == 3
|
324
|
+
end
|
325
|
+
|
326
|
+
should "return the subject" do
|
327
|
+
@statuses.collect(&:name).should == %w(Archived Complete)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class AssociationsTest < Test::Unit::TestCase
|
5
|
+
should "allow changing class names" do
|
6
|
+
class AwesomeUser
|
7
|
+
include MongoMapper::Document
|
8
|
+
|
9
|
+
many :posts, :class_name => 'AssociationsTest::AwesomePost', :foreign_key => :creator_id
|
10
|
+
end
|
11
|
+
AwesomeUser.collection.clear
|
12
|
+
|
13
|
+
class AwesomeTag
|
14
|
+
include MongoMapper::EmbeddedDocument
|
15
|
+
|
16
|
+
key :name, String
|
17
|
+
key :post_id, String
|
18
|
+
|
19
|
+
belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
|
20
|
+
end
|
21
|
+
|
22
|
+
class AwesomePost
|
23
|
+
include MongoMapper::Document
|
24
|
+
|
25
|
+
key :creator_id, String
|
26
|
+
|
27
|
+
belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
|
28
|
+
many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
|
29
|
+
end
|
30
|
+
|
31
|
+
AwesomeUser.collection.clear
|
32
|
+
AwesomePost.collection.clear
|
33
|
+
|
34
|
+
user = AwesomeUser.create
|
35
|
+
tag1 = AwesomeTag.new(:name => 'awesome')
|
36
|
+
tag2 = AwesomeTag.new(:name => 'grand')
|
37
|
+
post1 = AwesomePost.create(:creator => user, :tags => [tag1])
|
38
|
+
post2 = AwesomePost.create(:creator => user, :tags => [tag2])
|
39
|
+
user.posts.should == [post1, post2]
|
40
|
+
|
41
|
+
post1_from_db = AwesomePost.find(post1.id)
|
42
|
+
post1_from_db.tags.should == [tag1]
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BinaryTest < Test::Unit::TestCase
|
4
|
+
should "serialize and deserialize correctly" do
|
5
|
+
klass = Class.new do
|
6
|
+
include MongoMapper::Document
|
7
|
+
set_collection_name 'test'
|
8
|
+
key :contents, Binary
|
9
|
+
end
|
10
|
+
klass.collection.clear
|
11
|
+
|
12
|
+
doc = klass.new(:contents => '010101')
|
13
|
+
doc.save
|
14
|
+
|
15
|
+
doc = klass.find(doc.id)
|
16
|
+
doc.contents.to_s.should == ByteBuffer.new('010101').to_s
|
17
|
+
end
|
18
|
+
end
|