djsun-mongomapper 0.3.1.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/History +20 -1
  2. data/Rakefile +5 -3
  3. data/VERSION +1 -1
  4. data/lib/mongomapper/associations/base.rb +3 -5
  5. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +5 -3
  6. data/lib/mongomapper/associations/belongs_to_proxy.rb +4 -4
  7. data/lib/mongomapper/associations/many_documents_proxy.rb +32 -14
  8. data/lib/mongomapper/associations/proxy.rb +2 -6
  9. data/lib/mongomapper/associations.rb +38 -15
  10. data/lib/mongomapper/document.rb +165 -95
  11. data/lib/mongomapper/dynamic_finder.rb +38 -0
  12. data/lib/mongomapper/embedded_document.rb +116 -88
  13. data/lib/mongomapper/finder_options.rb +3 -14
  14. data/lib/mongomapper/key.rb +12 -16
  15. data/lib/mongomapper/serializers/json_serializer.rb +15 -12
  16. data/lib/mongomapper/support.rb +30 -0
  17. data/lib/mongomapper.rb +7 -33
  18. data/mongomapper.gemspec +10 -7
  19. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +14 -0
  20. data/test/functional/associations/test_belongs_to_proxy.rb +10 -0
  21. data/test/functional/associations/test_many_polymorphic_proxy.rb +46 -52
  22. data/test/functional/associations/test_many_proxy.rb +71 -12
  23. data/test/functional/test_associations.rb +9 -2
  24. data/test/functional/test_document.rb +281 -20
  25. data/test/functional/test_rails_compatibility.rb +2 -3
  26. data/test/models.rb +39 -8
  27. data/test/unit/serializers/test_json_serializer.rb +46 -12
  28. data/test/unit/test_association_base.rb +10 -2
  29. data/test/unit/test_document.rb +7 -9
  30. data/test/unit/test_embedded_document.rb +180 -24
  31. data/test/unit/test_finder_options.rb +7 -38
  32. data/test/unit/test_key.rb +54 -24
  33. metadata +5 -5
  34. data/test/unit/test_mongo_id.rb +0 -35
@@ -27,30 +27,31 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
27
27
 
28
28
  lambda {
29
29
  room.messages = [
30
- Enter.new(:body => 'John entered room'),
31
- Chat.new(:body => 'Heyyyoooo!'),
32
- Exit.new(:body => 'John exited room')
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
33
  ]
34
34
  }.should change { Message.count }.by(3)
35
35
 
36
36
  from_db = Room.find(room.id)
37
- from_db.messages.size.should == 3
38
- from_db.messages[0].body.should == 'John entered room'
39
- from_db.messages[1].body.should == 'Heyyyoooo!'
40
- from_db.messages[2].body.should == 'John exited room'
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'
41
42
  end
42
-
43
- # Intermittent failure (DJ, 2009-08-03)
44
- should_eventually "correctly store type when using <<, push and concat" do
43
+
44
+ should "correctly store type when using <<, push and concat" do
45
45
  room = Room.new
46
- room.messages << Enter.new(:body => 'John entered the room')
47
- room.messages.push Exit.new(:body => 'John entered the room')
48
- room.messages.concat Chat.new(:body => 'Holla!')
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
49
 
50
50
  from_db = Room.find(room.id)
51
- from_db.messages[0]._type.should == 'Enter'
52
- from_db.messages[1]._type.should == 'Exit'
53
- from_db.messages[2]._type.should == 'Chat'
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'
54
55
  end
55
56
 
56
57
  context "build" do
@@ -125,97 +126,91 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
125
126
  context "Finding scoped to association" do
126
127
  setup do
127
128
  @lounge = Room.create(:name => 'Lounge')
128
- @lm1 = Message.create(:body => 'Loungin!')
129
- @lm2 = Message.create(:body => 'I love loungin!')
129
+ @lm1 = Message.create(:body => 'Loungin!', :position => 1)
130
+ @lm2 = Message.create(:body => 'I love loungin!', :position => 2)
130
131
  @lounge.messages = [@lm1, @lm2]
131
132
  @lounge.save
132
133
 
133
134
  @hall = Room.create(:name => 'Hall')
134
- @hm1 = Message.create(:body => 'Do not fall in the hall')
135
- @hm2 = Message.create(:body => 'Hall the king!')
136
- @hm3 = Message.create(:body => 'Loungin!')
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)
137
138
  @hall.messages = [@hm1, @hm2, @hm3]
138
139
  @hall.save
139
140
  end
140
141
 
141
142
  context "with :all" do
142
- # Intermittent failure (DJ, 2009-08-03)
143
- should_eventually "work" do
144
- @lounge.messages.find(:all).should == [@lm1, @lm2]
143
+ should "work" do
144
+ @lounge.messages.find(:all, :order => "position").should == [@lm1, @lm2]
145
145
  end
146
146
 
147
147
  should "work with conditions" do
148
- messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'})
148
+ messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'}, :order => "position")
149
149
  messages.should == [@lm1]
150
150
  end
151
151
 
152
- # Intermittent failure (DJ, 2009-08-03)
153
- should_eventually "work with order" do
154
- messages = @lounge.messages.find(:all, :order => '$natural desc')
152
+ should "work with order" do
153
+ messages = @lounge.messages.find(:all, :order => 'position desc')
155
154
  messages.should == [@lm2, @lm1]
156
155
  end
157
156
  end
158
157
 
159
158
  context "with #all" do
160
159
  should "work" do
161
- @lounge.messages.all.should == [@lm1, @lm2]
160
+ @lounge.messages.all(:order => "position").should == [@lm1, @lm2]
162
161
  end
163
162
 
164
163
  should "work with conditions" do
165
- messages = @lounge.messages.all(:conditions => {'body' => 'Loungin!'})
164
+ messages = @lounge.messages.all(:conditions => {:body => 'Loungin!'}, :order => "position")
166
165
  messages.should == [@lm1]
167
166
  end
168
167
 
169
- # Intermittent failure (DJ, 2009-08-03)
170
- should_eventually "work with order" do
171
- messages = @lounge.messages.all(:order => '$natural desc')
168
+ should "work with order" do
169
+ messages = @lounge.messages.all(:order => 'position desc')
172
170
  messages.should == [@lm2, @lm1]
173
171
  end
174
172
  end
175
173
 
176
174
  context "with :first" do
177
- # Intermittent failure (DJ, 2009-08-03)
178
- should_eventually "work" do
179
- @lounge.messages.find(:first).should == @lm1
175
+ should "work" do
176
+ @lounge.messages.find(:first, :order => "position asc").should == @lm1
180
177
  end
181
178
 
182
179
  should "work with conditions" do
183
- message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'})
180
+ message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'}, :order => "position asc")
184
181
  message.should == @lm2
185
182
  end
186
183
  end
187
184
 
188
185
  context "with #first" do
189
- # Intermittent failure (DJ, 2009-08-03)
190
- should_eventually "work" do
191
- @lounge.messages.first.should == @lm1
186
+ should "work" do
187
+ @lounge.messages.first(:order => "position asc").should == @lm1
192
188
  end
193
189
 
194
190
  should "work with conditions" do
195
- message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'})
191
+ message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'}, :order => "position asc")
196
192
  message.should == @lm2
197
193
  end
198
194
  end
199
195
 
200
196
  context "with :last" do
201
197
  should "work" do
202
- @lounge.messages.find(:last).should == @lm2
198
+ @lounge.messages.find(:last, :order => "position asc").should == @lm2
203
199
  end
204
200
 
205
201
  should "work with conditions" do
206
- message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'})
202
+ message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'}, :order => "position asc")
207
203
  message.should == @lm1
208
204
  end
209
205
  end
210
-
206
+
211
207
  context "with #last" do
212
- # Intermittent failure (DJ, 2009-08-03)
213
- should_eventually "work" do
214
- @lounge.messages.last.should == @lm2
208
+ should "work" do
209
+ @lounge.messages.last(:order => "position asc").should == @lm2
215
210
  end
216
211
 
217
212
  should "work with conditions" do
218
- message = @lounge.messages.last(:conditions => {:body => 'Loungin!'})
213
+ message = @lounge.messages.last(:conditions => {:body => 'Loungin!'}, :order => "position asc")
219
214
  message.should == @lm1
220
215
  end
221
216
  end
@@ -247,7 +242,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
247
242
 
248
243
  context "with #paginate" do
249
244
  setup do
250
- @messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => '$natural asc')
245
+ @messages = @hall.messages.paginate(:per_page => 2, :page => 1, :order => 'position asc')
251
246
  end
252
247
 
253
248
  should "return total pages" do
@@ -258,10 +253,9 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
258
253
  @messages.total_entries.should == 3
259
254
  end
260
255
 
261
- # Intermittent failure (DJ, 2009-08-03)
262
- should_eventually "return the subject" do
256
+ should "return the subject" do
263
257
  @messages.should == [@hm1, @hm2]
264
258
  end
265
259
  end
266
260
  end
267
- end
261
+ end
@@ -98,11 +98,70 @@ class ManyProxyTest < Test::Unit::TestCase
98
98
  end
99
99
  end
100
100
 
101
+ context "Unassociating documents" do
102
+ setup do
103
+ @project = Project.create
104
+ @project.statuses << Status.create(:name => '1')
105
+ @project.statuses << Status.create(:name => '2')
106
+
107
+ @project2 = Project.create
108
+ @project2.statuses << Status.create(:name => '1')
109
+ @project2.statuses << Status.create(:name => '2')
110
+ end
111
+
112
+ should "work with destroy all" do
113
+ @project.statuses.count.should == 2
114
+ @project.statuses.destroy_all
115
+ @project.statuses.count.should == 0
116
+
117
+ @project2.statuses.count.should == 2
118
+ Status.count.should == 2
119
+ end
120
+
121
+ should "work with destroy all and conditions" do
122
+ @project.statuses.count.should == 2
123
+ @project.statuses.destroy_all(:name => '1')
124
+ @project.statuses.count.should == 1
125
+
126
+ @project2.statuses.count.should == 2
127
+ Status.count.should == 3
128
+ end
129
+
130
+ should "work with delete all" do
131
+ @project.statuses.count.should == 2
132
+ @project.statuses.delete_all
133
+ @project.statuses.count.should == 0
134
+
135
+ @project2.statuses.count.should == 2
136
+ Status.count.should == 2
137
+ end
138
+
139
+ should "work with delete all and conditions" do
140
+ @project.statuses.count.should == 2
141
+ @project.statuses.delete_all(:name => '1')
142
+ @project.statuses.count.should == 1
143
+
144
+ @project2.statuses.count.should == 2
145
+ Status.count.should == 3
146
+ end
147
+
148
+ should "work with nullify" do
149
+ @project.statuses.count.should == 2
150
+ @project.statuses.nullify
151
+ @project.statuses.count.should == 0
152
+
153
+ @project2.statuses.count.should == 2
154
+ Status.count.should == 4
155
+ Status.count(:name => '1').should == 2
156
+ Status.count(:name => '2').should == 2
157
+ end
158
+ end
159
+
101
160
  context "Finding scoped to association" do
102
161
  setup do
103
162
  @project1 = Project.new(:name => 'Project 1')
104
- @brand_new = Status.create(:name => 'New')
105
- @complete = Status.create(:name => 'Complete')
163
+ @brand_new = Status.create(:name => 'New', :position => 1 )
164
+ @complete = Status.create(:name => 'Complete', :position => 2)
106
165
  @project1.statuses = [@brand_new, @complete]
107
166
  @project1.save
108
167
 
@@ -116,7 +175,7 @@ class ManyProxyTest < Test::Unit::TestCase
116
175
 
117
176
  context "with :all" do
118
177
  should "work" do
119
- @project1.statuses.find(:all).should == [@brand_new, @complete]
178
+ @project1.statuses.find(:all, :order => "position asc").should == [@brand_new, @complete]
120
179
  end
121
180
 
122
181
  should "work with conditions" do
@@ -131,8 +190,8 @@ class ManyProxyTest < Test::Unit::TestCase
131
190
  end
132
191
 
133
192
  context "with #all" do
134
- should_eventually "work" do
135
- @project1.statuses.all.should == [@brand_new, @complete]
193
+ should "work" do
194
+ @project1.statuses.all(:order => "position asc").should == [@brand_new, @complete]
136
195
  end
137
196
 
138
197
  should "work with conditions" do
@@ -148,7 +207,7 @@ class ManyProxyTest < Test::Unit::TestCase
148
207
 
149
208
  context "with :first" do
150
209
  should "work" do
151
- @project1.statuses.find(:first).should == @brand_new
210
+ @project1.statuses.find(:first, :order => 'name').should == @complete
152
211
  end
153
212
 
154
213
  should "work with conditions" do
@@ -159,7 +218,7 @@ class ManyProxyTest < Test::Unit::TestCase
159
218
 
160
219
  context "with #first" do
161
220
  should "work" do
162
- @project1.statuses.first.should == @brand_new
221
+ @project1.statuses.first(:order => 'name').should == @complete
163
222
  end
164
223
 
165
224
  should "work with conditions" do
@@ -170,7 +229,7 @@ class ManyProxyTest < Test::Unit::TestCase
170
229
 
171
230
  context "with :last" do
172
231
  should "work" do
173
- @project1.statuses.find(:last).should == @complete
232
+ @project1.statuses.find(:last, :order => "position asc").should == @complete
174
233
  end
175
234
 
176
235
  should "work with conditions" do
@@ -181,7 +240,7 @@ class ManyProxyTest < Test::Unit::TestCase
181
240
 
182
241
  context "with #last" do
183
242
  should "work" do
184
- @project1.statuses.last.should == @complete
243
+ @project1.statuses.last(:order => "position asc").should == @complete
185
244
  end
186
245
 
187
246
  should "work with conditions" do
@@ -217,7 +276,7 @@ class ManyProxyTest < Test::Unit::TestCase
217
276
 
218
277
  context "with #paginate" do
219
278
  setup do
220
- @statuses = @project2.statuses.paginate(:per_page => 2, :page => 1, :order => '$natural asc')
279
+ @statuses = @project2.statuses.paginate(:per_page => 2, :page => 1, :order => 'name asc')
221
280
  end
222
281
 
223
282
  should "return total pages" do
@@ -229,8 +288,8 @@ class ManyProxyTest < Test::Unit::TestCase
229
288
  end
230
289
 
231
290
  should "return the subject" do
232
- @statuses.should == [@in_progress, @archived]
291
+ @statuses.collect(&:name).should == %w(Archived Complete)
233
292
  end
234
293
  end
235
294
  end
236
- end
295
+ end
@@ -5,21 +5,28 @@ class AssociationsTest < Test::Unit::TestCase
5
5
  def setup
6
6
  clear_all_collections
7
7
  end
8
-
8
+
9
9
  should "allow changing class names" do
10
10
  class AwesomeUser
11
11
  include MongoMapper::Document
12
+
12
13
  many :posts, :class_name => 'AssociationsTest::AwesomePost', :foreign_key => :creator_id
13
14
  end
14
15
 
15
16
  class AwesomeTag
16
17
  include MongoMapper::EmbeddedDocument
18
+
17
19
  key :name, String
20
+ key :post_id, String
21
+
18
22
  belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
19
23
  end
20
24
 
21
25
  class AwesomePost
22
26
  include MongoMapper::Document
27
+
28
+ key :creator_id, String
29
+
23
30
  belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
24
31
  many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
25
32
  end
@@ -36,5 +43,5 @@ class AssociationsTest < Test::Unit::TestCase
36
43
 
37
44
  post1_from_db = AwesomePost.find(post1.id)
38
45
  post1_from_db.tags.should == [tag1]
39
- end
46
+ end
40
47
  end