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.
Files changed (44) hide show
  1. data/README.rdoc +38 -38
  2. data/Rakefile +87 -73
  3. data/VERSION +1 -1
  4. data/lib/mongomapper.rb +67 -71
  5. data/lib/mongomapper/associations.rb +86 -84
  6. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
  7. data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
  8. data/lib/mongomapper/associations/proxy.rb +74 -73
  9. data/lib/mongomapper/document.rb +342 -348
  10. data/lib/mongomapper/embedded_document.rb +354 -274
  11. data/lib/mongomapper/finder_options.rb +84 -84
  12. data/lib/mongomapper/key.rb +32 -76
  13. data/lib/mongomapper/rails_compatibility/document.rb +14 -14
  14. data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
  15. data/lib/mongomapper/support.rb +156 -29
  16. data/lib/mongomapper/validations.rb +69 -47
  17. data/test/custom_matchers.rb +48 -0
  18. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
  19. data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
  20. data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
  21. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
  22. data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
  23. data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
  24. data/test/functional/test_binary.rb +21 -0
  25. data/test/functional/test_document.rb +946 -952
  26. data/test/functional/test_embedded_document.rb +98 -0
  27. data/test/functional/test_pagination.rb +87 -80
  28. data/test/functional/test_rails_compatibility.rb +29 -29
  29. data/test/functional/test_validations.rb +262 -172
  30. data/test/models.rb +169 -169
  31. data/test/test_helper.rb +28 -66
  32. data/test/unit/serializers/test_json_serializer.rb +193 -193
  33. data/test/unit/test_document.rb +161 -123
  34. data/test/unit/test_embedded_document.rb +643 -547
  35. data/test/unit/test_finder_options.rb +183 -183
  36. data/test/unit/test_key.rb +175 -247
  37. data/test/unit/test_rails_compatibility.rb +38 -33
  38. data/test/unit/test_serializations.rb +52 -52
  39. data/test/unit/test_support.rb +268 -0
  40. data/test/unit/test_time_zones.rb +40 -0
  41. data/test/unit/test_validations.rb +499 -258
  42. metadata +22 -12
  43. data/History +0 -76
  44. 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
- Room.new.messages.inspect.should == "[]"
12
- end
13
-
14
- should "add type key to polymorphic class base" do
15
- Message.keys.keys.should include('_type')
16
- end
17
-
18
- should "allow adding to assiciation like it was an array" do
19
- room = Room.new
20
- room.messages << Enter.new
21
- room.messages.push Exit.new
22
- room.messages.concat Exit.new
23
- room.messages.size.should == 3
24
- end
25
-
26
- should "be able to replace the association" do
27
- room = Room.create(:name => 'Lounge')
28
-
29
- lambda {
30
- room.messages = [
31
- Enter.new(:body => 'John entered room', :position => 1),
32
- Chat.new(:body => 'Heyyyoooo!', :position => 2),
33
- Exit.new(:body => 'John exited room', :position => 3)
34
- ]
35
- }.should change { Message.count }.by(3)
36
-
37
- from_db = Room.find(room.id)
38
- messages = from_db.messages.all :order => "position"
39
- messages.size.should == 3
40
- messages[0].body.should == 'John entered room'
41
- messages[1].body.should == 'Heyyyoooo!'
42
- messages[2].body.should == 'John exited room'
43
- end
44
-
45
- should "correctly store type when using <<, push and concat" do
46
- room = Room.new
47
- room.messages << Enter.new(:body => 'John entered the room', :position => 1)
48
- room.messages.push Exit.new(:body => 'John entered the room', :position => 2)
49
- room.messages.concat Chat.new(:body => 'Holla!' , :position => 3)
50
-
51
- from_db = Room.find(room.id)
52
- messages = from_db.messages.all :order => "position"
53
- messages[0]._type.should == 'Enter'
54
- messages[1]._type.should == 'Exit'
55
- messages[2]._type.should == 'Chat'
56
- end
57
-
58
- context "build" do
59
- should "assign foreign key" do
60
- room = Room.create
61
- message = room.messages.build
62
- message.room_id.should == room.id
63
- end
64
-
65
- should "assign _type" do
66
- room = Room.create
67
- message = room.messages.build
68
- message._type.should == 'Message'
69
- end
70
-
71
- should "allow assigning attributes" do
72
- room = Room.create
73
- message = room.messages.build(:body => 'Foo!')
74
- message.body.should == 'Foo!'
75
- end
76
- end
77
-
78
- context "create" do
79
- should "assign foreign key" do
80
- room = Room.create
81
- message = room.messages.create
82
- message.room_id.should == room.id
83
- end
84
-
85
- should "assign _type" do
86
- room = Room.create
87
- message = room.messages.create
88
- message._type.should == 'Message'
89
- end
90
-
91
- should "save record" do
92
- room = Room.create
93
- lambda {
94
- room.messages.create
95
- }.should change { Message.count }
96
- end
97
-
98
- should "allow passing attributes" do
99
- room = Room.create
100
- message = room.messages.create(:body => 'Foo!')
101
- message.body.should == 'Foo!'
102
- end
103
- end
104
-
105
- context "count" do
106
- should "work scoped to association" do
107
- room = Room.create
108
- 3.times { room.messages.create }
109
-
110
- other_room = Room.create
111
- 2.times { other_room.messages.create }
112
-
113
- room.messages.count.should == 3
114
- other_room.messages.count.should == 2
115
- end
116
-
117
- should "work with conditions" do
118
- room = Room.create
119
- room.messages.create(:body => 'Foo')
120
- room.messages.create(:body => 'Other 1')
121
- room.messages.create(:body => 'Other 2')
122
-
123
- room.messages.count(:body => 'Foo').should == 1
124
- end
125
- end
126
-
127
- context "Finding scoped to association" do
128
- setup do
129
- @lounge = Room.create(:name => 'Lounge')
130
- @lm1 = Message.create(:body => 'Loungin!', :position => 1)
131
- @lm2 = Message.create(:body => 'I love loungin!', :position => 2)
132
- @lounge.messages = [@lm1, @lm2]
133
- @lounge.save
134
-
135
- @hall = Room.create(:name => 'Hall')
136
- @hm1 = Message.create(:body => 'Do not fall in the hall', :position => 1)
137
- @hm2 = Message.create(:body => 'Hall the king!', :position => 2)
138
- @hm3 = Message.create(:body => 'Loungin!', :position => 3)
139
- @hall.messages = [@hm1, @hm2, @hm3]
140
- @hall.save
141
- end
142
-
143
- context "with :all" do
144
- should "work" do
145
- @lounge.messages.find(:all, :order => "position").should == [@lm1, @lm2]
146
- end
147
-
148
- should "work with conditions" do
149
- messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'}, :order => "position")
150
- messages.should == [@lm1]
151
- end
152
-
153
- should "work with order" do
154
- messages = @lounge.messages.find(:all, :order => 'position desc')
155
- messages.should == [@lm2, @lm1]
156
- end
157
- end
158
-
159
- context "with #all" do
160
- should "work" do
161
- @lounge.messages.all(:order => "position").should == [@lm1, @lm2]
162
- end
163
-
164
- should "work with conditions" do
165
- messages = @lounge.messages.all(:conditions => {:body => 'Loungin!'}, :order => "position")
166
- messages.should == [@lm1]
167
- end
168
-
169
- should "work with order" do
170
- messages = @lounge.messages.all(:order => 'position desc')
171
- messages.should == [@lm2, @lm1]
172
- end
173
- end
174
-
175
- context "with :first" do
176
- should "work" do
177
- @lounge.messages.find(:first, :order => "position asc").should == @lm1
178
- end
179
-
180
- should "work with conditions" do
181
- message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'}, :order => "position asc")
182
- message.should == @lm2
183
- end
184
- end
185
-
186
- context "with #first" do
187
- should "work" do
188
- @lounge.messages.first(:order => "position asc").should == @lm1
189
- end
190
-
191
- should "work with conditions" do
192
- message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'}, :order => "position asc")
193
- message.should == @lm2
194
- end
195
- end
196
-
197
- context "with :last" do
198
- should "work" do
199
- @lounge.messages.find(:last, :order => "position asc").should == @lm2
200
- end
201
-
202
- should "work with conditions" do
203
- message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'}, :order => "position asc")
204
- message.should == @lm1
205
- end
206
- end
207
-
208
- context "with #last" do
209
- should "work" do
210
- @lounge.messages.last(:order => "position asc").should == @lm2
211
- end
212
-
213
- should "work with conditions" do
214
- message = @lounge.messages.last(:conditions => {:body => 'Loungin!'}, :order => "position asc")
215
- message.should == @lm1
216
- end
217
- end
218
-
219
- context "with one id" do
220
- should "work for id in association" do
221
- @lounge.messages.find(@lm2.id).should == @lm2
222
- end
223
-
224
- should "not work for id not in association" do
225
- lambda {
226
- @lounge.messages.find(@hm2.id)
227
- }.should raise_error(MongoMapper::DocumentNotFound)
228
- end
229
- end
230
-
231
- context "with multiple ids" do
232
- should "work for ids in association" do
233
- messages = @lounge.messages.find(@lm1.id, @lm2.id)
234
- messages.should == [@lm1, @lm2]
235
- end
236
-
237
- should "not work for ids not in association" do
238
- lambda {
239
- @lounge.messages.find(@lm1.id, @lm2.id, @hm2.id)
240
- }.should raise_error(MongoMapper::DocumentNotFound)
241
- end
242
- end
243
-
244
- context "with #paginate" do
245
- setup do
246
- @messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => 'position asc')
247
- end
248
-
249
- should "return total pages" do
250
- @messages.total_pages.should == 2
251
- end
252
-
253
- should "return total entries" do
254
- @messages.total_entries.should == 3
255
- end
256
-
257
- should "return the subject" do
258
- @messages.should == [@hm1, @hm2]
259
- end
260
- end
261
- end
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