jnunemaker-mongomapper 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,29 +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
 
43
44
  should "correctly store type when using <<, push and concat" do
44
45
  room = Room.new
45
- room.messages << Enter.new(:body => 'John entered the room')
46
- room.messages.push Exit.new(:body => 'John entered the room')
47
- 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)
48
49
 
49
50
  from_db = Room.find(room.id)
50
- from_db.messages[0]._type.should == 'Enter'
51
- from_db.messages[1]._type.should == 'Exit'
52
- 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'
53
55
  end
54
56
 
55
57
  context "build" do
@@ -124,91 +126,91 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
124
126
  context "Finding scoped to association" do
125
127
  setup do
126
128
  @lounge = Room.create(:name => 'Lounge')
127
- @lm1 = Message.create(:body => 'Loungin!')
128
- @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)
129
131
  @lounge.messages = [@lm1, @lm2]
130
132
  @lounge.save
131
133
 
132
134
  @hall = Room.create(:name => 'Hall')
133
- @hm1 = Message.create(:body => 'Do not fall in the hall')
134
- @hm2 = Message.create(:body => 'Hall the king!')
135
- @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)
136
138
  @hall.messages = [@hm1, @hm2, @hm3]
137
139
  @hall.save
138
140
  end
139
141
 
140
142
  context "with :all" do
141
143
  should "work" do
142
- @lounge.messages.find(:all).should == [@lm1, @lm2]
144
+ @lounge.messages.find(:all, :order => "position").should == [@lm1, @lm2]
143
145
  end
144
146
 
145
147
  should "work with conditions" do
146
- messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'})
148
+ messages = @lounge.messages.find(:all, :conditions => {:body => 'Loungin!'}, :order => "position")
147
149
  messages.should == [@lm1]
148
150
  end
149
151
 
150
152
  should "work with order" do
151
- messages = @lounge.messages.find(:all, :order => '$natural desc')
153
+ messages = @lounge.messages.find(:all, :order => 'position desc')
152
154
  messages.should == [@lm2, @lm1]
153
155
  end
154
156
  end
155
157
 
156
158
  context "with #all" do
157
159
  should "work" do
158
- @lounge.messages.all.should == [@lm1, @lm2]
160
+ @lounge.messages.all(:order => "position").should == [@lm1, @lm2]
159
161
  end
160
162
 
161
163
  should "work with conditions" do
162
- messages = @lounge.messages.all(:conditions => {'body' => 'Loungin!'})
164
+ messages = @lounge.messages.all(:conditions => {:body => 'Loungin!'}, :order => "position")
163
165
  messages.should == [@lm1]
164
166
  end
165
167
 
166
168
  should "work with order" do
167
- messages = @lounge.messages.all(:order => '$natural desc')
169
+ messages = @lounge.messages.all(:order => 'position desc')
168
170
  messages.should == [@lm2, @lm1]
169
171
  end
170
172
  end
171
173
 
172
174
  context "with :first" do
173
175
  should "work" do
174
- @lounge.messages.find(:first).should == @lm1
176
+ @lounge.messages.find(:first, :order => "position asc").should == @lm1
175
177
  end
176
178
 
177
179
  should "work with conditions" do
178
- message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'})
180
+ message = @lounge.messages.find(:first, :conditions => {:body => 'I love loungin!'}, :order => "position asc")
179
181
  message.should == @lm2
180
182
  end
181
183
  end
182
184
 
183
185
  context "with #first" do
184
186
  should "work" do
185
- @lounge.messages.first.should == @lm1
187
+ @lounge.messages.first(:order => "position asc").should == @lm1
186
188
  end
187
189
 
188
190
  should "work with conditions" do
189
- message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'})
191
+ message = @lounge.messages.first(:conditions => {:body => 'I love loungin!'}, :order => "position asc")
190
192
  message.should == @lm2
191
193
  end
192
194
  end
193
195
 
194
196
  context "with :last" do
195
197
  should "work" do
196
- @lounge.messages.find(:last).should == @lm2
198
+ @lounge.messages.find(:last, :order => "position asc").should == @lm2
197
199
  end
198
200
 
199
201
  should "work with conditions" do
200
- message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'})
202
+ message = @lounge.messages.find(:last, :conditions => {:body => 'Loungin!'}, :order => "position asc")
201
203
  message.should == @lm1
202
204
  end
203
205
  end
204
206
 
205
207
  context "with #last" do
206
208
  should "work" do
207
- @lounge.messages.last.should == @lm2
209
+ @lounge.messages.last(:order => "position asc").should == @lm2
208
210
  end
209
211
 
210
212
  should "work with conditions" do
211
- message = @lounge.messages.last(:conditions => {:body => 'Loungin!'})
213
+ message = @lounge.messages.last(:conditions => {:body => 'Loungin!'}, :order => "position asc")
212
214
  message.should == @lm1
213
215
  end
214
216
  end
@@ -240,7 +242,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
240
242
 
241
243
  context "with #paginate" do
242
244
  setup do
243
- @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')
244
246
  end
245
247
 
246
248
  should "return total pages" do
@@ -256,4 +258,4 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
256
258
  end
257
259
  end
258
260
  end
259
- end
261
+ end
@@ -160,8 +160,8 @@ class ManyProxyTest < Test::Unit::TestCase
160
160
  context "Finding scoped to association" do
161
161
  setup do
162
162
  @project1 = Project.new(:name => 'Project 1')
163
- @brand_new = Status.create(:name => 'New')
164
- @complete = Status.create(:name => 'Complete')
163
+ @brand_new = Status.create(:name => 'New', :position => 1 )
164
+ @complete = Status.create(:name => 'Complete', :position => 2)
165
165
  @project1.statuses = [@brand_new, @complete]
166
166
  @project1.save
167
167
 
@@ -175,7 +175,7 @@ class ManyProxyTest < Test::Unit::TestCase
175
175
 
176
176
  context "with :all" do
177
177
  should "work" do
178
- @project1.statuses.find(:all).should == [@brand_new, @complete]
178
+ @project1.statuses.find(:all, :order => "position asc").should == [@brand_new, @complete]
179
179
  end
180
180
 
181
181
  should "work with conditions" do
@@ -191,7 +191,7 @@ class ManyProxyTest < Test::Unit::TestCase
191
191
 
192
192
  context "with #all" do
193
193
  should "work" do
194
- @project1.statuses.all.should == [@brand_new, @complete]
194
+ @project1.statuses.all(:order => "position asc").should == [@brand_new, @complete]
195
195
  end
196
196
 
197
197
  should "work with conditions" do
@@ -229,7 +229,7 @@ class ManyProxyTest < Test::Unit::TestCase
229
229
 
230
230
  context "with :last" do
231
231
  should "work" do
232
- @project1.statuses.find(:last).should == @complete
232
+ @project1.statuses.find(:last, :order => "position asc").should == @complete
233
233
  end
234
234
 
235
235
  should "work with conditions" do
@@ -240,7 +240,7 @@ class ManyProxyTest < Test::Unit::TestCase
240
240
 
241
241
  context "with #last" do
242
242
  should "work" do
243
- @project1.statuses.last.should == @complete
243
+ @project1.statuses.last(:order => "position asc").should == @complete
244
244
  end
245
245
 
246
246
  should "work with conditions" do
@@ -292,4 +292,4 @@ class ManyProxyTest < Test::Unit::TestCase
292
292
  end
293
293
  end
294
294
  end
295
- 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
@@ -331,13 +331,13 @@ class DocumentTest < Test::Unit::TestCase
331
331
 
332
332
  context "with :last" do
333
333
  should "find last document" do
334
- @document.find(:last, :order => 'age desc').should == @doc2
334
+ @document.find(:last, :order => 'age').should == @doc2
335
335
  end
336
336
  end
337
337
 
338
338
  context "with #last" do
339
339
  should "find last document based on criteria" do
340
- @document.last(:order => 'age desc').should == @doc2
340
+ @document.last(:order => 'age').should == @doc2
341
341
  @document.last(:conditions => {:age => 28}).should == @doc2
342
342
  end
343
343
  end
@@ -375,7 +375,7 @@ class DocumentTest < Test::Unit::TestCase
375
375
  end
376
376
 
377
377
  should "find last document based on arguments" do
378
- doc = @document.find_last_by_last_name('Nunemaker', :order => 'age desc')
378
+ doc = @document.find_last_by_last_name('Nunemaker', :order => 'age')
379
379
  doc.should == @doc1
380
380
  end
381
381
 
@@ -561,6 +561,7 @@ class DocumentTest < Test::Unit::TestCase
561
561
  context "many" do
562
562
  context "=> destroy" do
563
563
  setup do
564
+ Property.key :thing_id, String
564
565
  Property.belongs_to :thing, :dependent => :destroy
565
566
  Thing.many :properties, :dependent => :destroy
566
567
 
@@ -583,6 +584,7 @@ class DocumentTest < Test::Unit::TestCase
583
584
 
584
585
  context "=> delete_all" do
585
586
  setup do
587
+ Property.key :thing_id, String
586
588
  Property.belongs_to :thing
587
589
  Thing.has_many :properties, :dependent => :delete_all
588
590
 
@@ -605,6 +607,7 @@ class DocumentTest < Test::Unit::TestCase
605
607
 
606
608
  context "=> nullify" do
607
609
  setup do
610
+ Property.key :thing_id, String
608
611
  Property.belongs_to :thing
609
612
  Thing.has_many :properties, :dependent => :nullify
610
613
 
@@ -629,6 +632,7 @@ class DocumentTest < Test::Unit::TestCase
629
632
  context "belongs_to" do
630
633
  context "=> destroy" do
631
634
  setup do
635
+ Property.key :thing_id, String
632
636
  Property.belongs_to :thing, :dependent => :destroy
633
637
  Thing.has_many :properties
634
638
 
@@ -713,7 +717,7 @@ class DocumentTest < Test::Unit::TestCase
713
717
  index_name = @document.ensure_index [[:first_name, 1], [:last_name, -1]]
714
718
  }.should change { @document.collection.index_information.size }.by(1)
715
719
 
716
- index_name.should == 'first_name_1_last_name_-1'
720
+ [ 'first_name_1_last_name_-1', 'last_name_-1_first_name_1' ].should include(index_name)
717
721
 
718
722
  index = @document.collection.index_information[index_name]
719
723
  index.should_not be_nil
@@ -910,6 +914,10 @@ class DocumentTest < Test::Unit::TestCase
910
914
  end
911
915
 
912
916
  context "timestamping" do
917
+ setup do
918
+ @document.timestamps!
919
+ end
920
+
913
921
  should "set created_at and updated_at on create" do
914
922
  doc = @document.new(:first_name => 'John', :age => 27)
915
923
  doc.created_at.should be(nil)
data/test/models.rb CHANGED
@@ -1,5 +1,30 @@
1
+ class Post
2
+ include MongoMapper::Document
3
+
4
+ key :title, String
5
+ key :body, String
6
+
7
+ has_many :comments, :as => :commentable, :class_name => 'PostComment'
8
+
9
+ timestamps!
10
+ end
11
+
12
+ class PostComment
13
+ include MongoMapper::Document
14
+
15
+ key :username, String, :default => 'Anonymous'
16
+ key :body, String
17
+
18
+ key :commentable_id, String
19
+ key :commentable_type, String
20
+ belongs_to :commentable, :polymorphic => true
21
+
22
+ timestamps!
23
+ end
24
+
1
25
  class Address
2
26
  include MongoMapper::EmbeddedDocument
27
+
3
28
  key :address, String
4
29
  key :city, String
5
30
  key :state, String
@@ -8,22 +33,35 @@ end
8
33
 
9
34
  class Message
10
35
  include MongoMapper::Document
36
+
11
37
  key :body, String
38
+ key :position, Integer
39
+ key :_type, String
40
+ key :room_id, String
41
+
12
42
  belongs_to :room
13
43
  end
14
44
 
45
+ class Answer
46
+ include MongoMapper::Document
47
+
48
+ key :body, String
49
+ end
50
+
15
51
  class Enter < Message; end
16
52
  class Exit < Message; end
17
53
  class Chat < Message; end
18
54
 
19
55
  class Room
20
56
  include MongoMapper::Document
57
+
21
58
  key :name, String
22
59
  many :messages, :polymorphic => true
23
60
  end
24
61
 
25
62
  class Project
26
63
  include MongoMapper::Document
64
+
27
65
  key :name, String
28
66
  many :statuses
29
67
  many :addresses
@@ -31,13 +69,20 @@ end
31
69
 
32
70
  class Status
33
71
  include MongoMapper::Document
72
+
73
+ key :project_id, String
74
+ key :target_id, String
75
+ key :target_type, String
76
+ key :name, String
77
+ key :position, Integer
78
+
34
79
  belongs_to :project
35
80
  belongs_to :target, :polymorphic => true
36
- key :name, String
37
81
  end
38
82
 
39
83
  class RealPerson
40
84
  include MongoMapper::Document
85
+
41
86
  many :pets
42
87
  key :name, String
43
88
 
@@ -48,19 +93,24 @@ end
48
93
 
49
94
  class Person
50
95
  include MongoMapper::EmbeddedDocument
96
+
51
97
  key :name, String
52
98
  key :child, Person
99
+
53
100
  many :pets
54
101
  end
55
102
 
56
103
  class Pet
57
104
  include MongoMapper::EmbeddedDocument
105
+
58
106
  key :name, String
59
107
  key :species, String
60
108
  end
61
109
 
62
110
  class Media
63
111
  include MongoMapper::EmbeddedDocument
112
+
113
+ key :_type, String
64
114
  key :file, String
65
115
  end
66
116
 
@@ -79,34 +129,41 @@ end
79
129
 
80
130
  class Catalog
81
131
  include MongoMapper::Document
132
+
82
133
  many :medias, :polymorphic => true
83
134
  end
84
135
 
85
136
  module TrModels
86
137
  class Transport
87
138
  include MongoMapper::EmbeddedDocument
139
+
140
+ key :_type, String
88
141
  key :license_plate, String
89
142
  end
90
143
 
91
144
  class Car < TrModels::Transport
92
145
  include MongoMapper::EmbeddedDocument
146
+
93
147
  key :model, String
94
148
  key :year, Integer
95
149
  end
96
150
 
97
151
  class Bus < TrModels::Transport
98
152
  include MongoMapper::EmbeddedDocument
153
+
99
154
  key :max_passengers, Integer
100
155
  end
101
156
 
102
157
  class Ambulance < TrModels::Transport
103
158
  include MongoMapper::EmbeddedDocument
159
+
104
160
  key :icu, Boolean
105
161
  end
106
162
 
107
163
  class Fleet
108
164
  include MongoMapper::Document
165
+
109
166
  many :transports, :polymorphic => true, :class_name => "TrModels::Transport"
110
167
  key :name, String
111
168
  end
112
- end
169
+ end