djsun-mongomapper 0.3.5.5 → 0.4.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +38 -38
- data/Rakefile +87 -73
- data/VERSION +1 -1
- data/lib/mongomapper.rb +67 -71
- data/lib/mongomapper/associations.rb +86 -84
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
- data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
- data/lib/mongomapper/associations/proxy.rb +74 -73
- data/lib/mongomapper/document.rb +342 -348
- data/lib/mongomapper/embedded_document.rb +354 -274
- data/lib/mongomapper/finder_options.rb +84 -84
- data/lib/mongomapper/key.rb +32 -76
- data/lib/mongomapper/rails_compatibility/document.rb +14 -14
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
- data/lib/mongomapper/support.rb +156 -29
- data/lib/mongomapper/validations.rb +69 -47
- data/test/custom_matchers.rb +48 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
- data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
- data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
- data/test/functional/test_binary.rb +21 -0
- data/test/functional/test_document.rb +946 -952
- data/test/functional/test_embedded_document.rb +98 -0
- data/test/functional/test_pagination.rb +87 -80
- data/test/functional/test_rails_compatibility.rb +29 -29
- data/test/functional/test_validations.rb +262 -172
- data/test/models.rb +169 -169
- data/test/test_helper.rb +28 -66
- data/test/unit/serializers/test_json_serializer.rb +193 -193
- data/test/unit/test_document.rb +161 -123
- data/test/unit/test_embedded_document.rb +643 -547
- data/test/unit/test_finder_options.rb +183 -183
- data/test/unit/test_key.rb +175 -247
- data/test/unit/test_rails_compatibility.rb +38 -33
- data/test/unit/test_serializations.rb +52 -52
- data/test/unit/test_support.rb +268 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +499 -258
- metadata +22 -12
- data/History +0 -76
- data/mongomapper.gemspec +0 -145
@@ -1,262 +1,261 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
clear_all_collections
|
7
|
-
end
|
8
|
-
|
9
|
-
should "default reader to empty array" do
|
10
|
-
Room.new.messages.should == []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
room
|
20
|
-
room.messages
|
21
|
-
room.messages.
|
22
|
-
room.messages.
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
messages
|
39
|
-
messages.
|
40
|
-
messages[
|
41
|
-
messages[
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
room
|
47
|
-
room.messages
|
48
|
-
room.messages.
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
messages
|
53
|
-
messages[
|
54
|
-
messages[
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
message
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
message
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
message
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
message
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
message
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
message
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
room
|
119
|
-
room.messages.create(:body => '
|
120
|
-
room.messages.create(:body => 'Other
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
@
|
130
|
-
@
|
131
|
-
@
|
132
|
-
@lounge.
|
133
|
-
|
134
|
-
|
135
|
-
@
|
136
|
-
@
|
137
|
-
@
|
138
|
-
@
|
139
|
-
@hall.
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
messages
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
messages
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
messages
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
messages
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
message
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
message
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
message
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
message
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
messages
|
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
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
clear_all_collections
|
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 "with :all" do
|
143
|
+
should "work" do
|
144
|
+
@lounge.messages.find(:all, :order => "position").should == [@lm1, @lm2]
|
145
|
+
end
|
146
|
+
|
147
|
+
should "work with conditions" do
|
148
|
+
messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'}, :order => "position")
|
149
|
+
messages.should == [@lm1]
|
150
|
+
end
|
151
|
+
|
152
|
+
should "work with order" do
|
153
|
+
messages = @lounge.messages.find(:all, :order => 'position desc')
|
154
|
+
messages.should == [@lm2, @lm1]
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "with #all" do
|
159
|
+
should "work" do
|
160
|
+
@lounge.messages.all(:order => "position").should == [@lm1, @lm2]
|
161
|
+
end
|
162
|
+
|
163
|
+
should "work with conditions" do
|
164
|
+
messages = @lounge.messages.all(:conditions => {:body => 'Loungin!'}, :order => "position")
|
165
|
+
messages.should == [@lm1]
|
166
|
+
end
|
167
|
+
|
168
|
+
should "work with order" do
|
169
|
+
messages = @lounge.messages.all(:order => 'position desc')
|
170
|
+
messages.should == [@lm2, @lm1]
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context "with :first" do
|
175
|
+
should "work" do
|
176
|
+
@lounge.messages.find(:first, :order => "position asc").should == @lm1
|
177
|
+
end
|
178
|
+
|
179
|
+
should "work with conditions" do
|
180
|
+
message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'}, :order => "position asc")
|
181
|
+
message.should == @lm2
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "with #first" do
|
186
|
+
should "work" do
|
187
|
+
@lounge.messages.first(:order => "position asc").should == @lm1
|
188
|
+
end
|
189
|
+
|
190
|
+
should "work with conditions" do
|
191
|
+
message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'}, :order => "position asc")
|
192
|
+
message.should == @lm2
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
context "with :last" do
|
197
|
+
should "work" do
|
198
|
+
@lounge.messages.find(:last, :order => "position asc").should == @lm2
|
199
|
+
end
|
200
|
+
|
201
|
+
should "work with conditions" do
|
202
|
+
message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'}, :order => "position asc")
|
203
|
+
message.should == @lm1
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context "with #last" do
|
208
|
+
should "work" do
|
209
|
+
@lounge.messages.last(:order => "position asc").should == @lm2
|
210
|
+
end
|
211
|
+
|
212
|
+
should "work with conditions" do
|
213
|
+
message = @lounge.messages.last(:conditions => {:body => 'Loungin!'}, :order => "position asc")
|
214
|
+
message.should == @lm1
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "with one id" do
|
219
|
+
should "work for id in association" do
|
220
|
+
@lounge.messages.find(@lm2.id).should == @lm2
|
221
|
+
end
|
222
|
+
|
223
|
+
should "not work for id not in association" do
|
224
|
+
lambda {
|
225
|
+
@lounge.messages.find(@hm2.id)
|
226
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context "with multiple ids" do
|
231
|
+
should "work for ids in association" do
|
232
|
+
messages = @lounge.messages.find(@lm1.id, @lm2.id)
|
233
|
+
messages.should == [@lm1, @lm2]
|
234
|
+
end
|
235
|
+
|
236
|
+
should "not work for ids not in association" do
|
237
|
+
lambda {
|
238
|
+
@lounge.messages.find(@lm1.id, @lm2.id, @hm2.id)
|
239
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context "with #paginate" do
|
244
|
+
setup do
|
245
|
+
@messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => 'position asc')
|
246
|
+
end
|
247
|
+
|
248
|
+
should "return total pages" do
|
249
|
+
@messages.total_pages.should == 2
|
250
|
+
end
|
251
|
+
|
252
|
+
should "return total entries" do
|
253
|
+
@messages.total_entries.should == 3
|
254
|
+
end
|
255
|
+
|
256
|
+
should "return the subject" do
|
257
|
+
@messages.should == [@hm1, @hm2]
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|