tpitale-mongo_mapper 0.6.9 → 0.6.10

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 (57) hide show
  1. data/README.rdoc +2 -17
  2. data/Rakefile +1 -1
  3. data/VERSION +1 -1
  4. data/lib/mongo_mapper/associations/base.rb +19 -10
  5. data/lib/mongo_mapper/associations/in_array_proxy.rb +137 -0
  6. data/lib/mongo_mapper/associations/one_proxy.rb +64 -0
  7. data/lib/mongo_mapper/associations/proxy.rb +7 -4
  8. data/lib/mongo_mapper/associations.rb +11 -3
  9. data/lib/mongo_mapper/callbacks.rb +30 -78
  10. data/lib/mongo_mapper/dirty.rb +5 -24
  11. data/lib/mongo_mapper/document.rb +117 -144
  12. data/lib/mongo_mapper/embedded_document.rb +7 -11
  13. data/lib/mongo_mapper/finder_options.rb +42 -30
  14. data/lib/mongo_mapper/mongo_mapper.rb +125 -0
  15. data/lib/mongo_mapper/pagination.rb +12 -1
  16. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +1 -0
  17. data/lib/mongo_mapper/serialization.rb +2 -2
  18. data/lib/mongo_mapper/serializers/json_serializer.rb +2 -46
  19. data/lib/mongo_mapper/support.rb +5 -2
  20. data/lib/mongo_mapper/validations.rb +1 -3
  21. data/lib/mongo_mapper.rb +8 -2
  22. data/mongo_mapper.gemspec +14 -8
  23. data/specs.watchr +3 -5
  24. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +8 -0
  25. data/test/functional/associations/test_belongs_to_proxy.rb +54 -9
  26. data/test/functional/associations/test_in_array_proxy.rb +309 -0
  27. data/test/functional/associations/test_many_documents_proxy.rb +103 -53
  28. data/test/functional/associations/test_many_embedded_proxy.rb +4 -14
  29. data/test/functional/associations/test_many_polymorphic_proxy.rb +4 -3
  30. data/test/functional/associations/test_one_proxy.rb +149 -0
  31. data/test/functional/test_binary.rb +13 -4
  32. data/test/functional/test_callbacks.rb +1 -5
  33. data/test/functional/test_dirty.rb +1 -4
  34. data/test/functional/test_document.rb +576 -640
  35. data/test/functional/test_embedded_document.rb +7 -20
  36. data/test/functional/test_modifiers.rb +238 -0
  37. data/test/functional/test_pagination.rb +1 -3
  38. data/test/functional/test_string_id_compatibility.rb +3 -8
  39. data/test/functional/test_validations.rb +13 -75
  40. data/test/models.rb +1 -1
  41. data/test/support/timing.rb +1 -1
  42. data/test/test_helper.rb +28 -0
  43. data/test/unit/associations/test_base.rb +54 -13
  44. data/test/unit/associations/test_proxy.rb +12 -0
  45. data/test/unit/test_document.rb +36 -26
  46. data/test/unit/test_embedded_document.rb +14 -51
  47. data/test/unit/test_finder_options.rb +36 -7
  48. data/test/unit/test_key.rb +1 -4
  49. data/test/unit/test_pagination.rb +6 -0
  50. data/test/unit/test_rails_compatibility.rb +4 -1
  51. data/test/unit/test_serializations.rb +1 -2
  52. data/test/unit/test_support.rb +4 -0
  53. data/test/unit/test_time_zones.rb +1 -2
  54. data/test/unit/test_validations.rb +3 -14
  55. metadata +12 -6
  56. data/lib/mongo_mapper/observing.rb +0 -50
  57. data/test/unit/test_observing.rb +0 -101
@@ -3,14 +3,11 @@ require 'models'
3
3
 
4
4
  class EmbeddedDocumentTest < Test::Unit::TestCase
5
5
  def setup
6
- @document = Class.new do
7
- include MongoMapper::Document
6
+ @document = Doc do
8
7
  set_collection_name 'users'
9
-
10
8
  key :first_name, String
11
9
  key :last_name, String
12
10
  end
13
- @document.collection.remove
14
11
  end
15
12
 
16
13
  context "Saving a document with an embedded document" do
@@ -34,23 +31,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
34
31
  end
35
32
  end
36
33
 
37
- context "Instantiating single collection inherited embedded documents" do
38
- setup do
39
- @document = Class.new do
40
- include MongoMapper::Document
41
- key :message, Message
42
- end
43
- end
44
-
45
- should "work" do
46
- doc1 = @document.create(:message => Enter.new)
47
- doc2 = @document.create(:message => Exit.new)
48
- doc3 = @document.create(:message => Chat.new)
49
-
50
- doc1.reload.message.class.should be(Enter)
51
- doc2.reload.message.class.should be(Exit)
52
- doc3.reload.message.class.should be(Chat)
34
+ should "correctly instantiate single collection inherited embedded documents" do
35
+ document = Doc('Foo') do
36
+ key :message, Message
53
37
  end
38
+
39
+ doc1 = document.create(:message => Enter.new)
40
+ doc1.reload.message.class.should be(Enter)
54
41
  end
55
42
 
56
43
  context "new?" do
@@ -0,0 +1,238 @@
1
+ require 'test_helper'
2
+
3
+ class ModifierTest < Test::Unit::TestCase
4
+ def setup
5
+ @page_class = Doc do
6
+ key :title, String
7
+ key :day_count, Integer, :default => 0
8
+ key :week_count, Integer, :default => 0
9
+ key :month_count, Integer, :default => 0
10
+ key :tags, Array
11
+ end
12
+ end
13
+
14
+ def assert_page_counts(page, day_count, week_count, month_count)
15
+ page.reload
16
+ page.day_count.should == day_count
17
+ page.week_count.should == week_count
18
+ page.month_count.should == month_count
19
+ end
20
+
21
+ should "be able to increment with criteria and modifier hashes" do
22
+ page = @page_class.create(:title => 'Home')
23
+ page2 = @page_class.create(:title => 'Home')
24
+
25
+ @page_class.increment({:title => 'Home'}, {
26
+ :day_count => 1, :week_count => 2, :month_count => 3
27
+ })
28
+
29
+ assert_page_counts page, 1, 2, 3
30
+ assert_page_counts page2, 1, 2, 3
31
+ end
32
+
33
+ should "be able to increment with ids and modifier hash" do
34
+ page = @page_class.create(:title => 'Home')
35
+ page2 = @page_class.create(:title => 'Home')
36
+
37
+ @page_class.increment(page.id, page2.id, {
38
+ :day_count => 1, :week_count => 2, :month_count => 3
39
+ })
40
+
41
+ assert_page_counts page, 1, 2, 3
42
+ assert_page_counts page2, 1, 2, 3
43
+ end
44
+
45
+ should "be able to decrement with criteria and modifier hashes" do
46
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
47
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
48
+
49
+ @page_class.decrement({:title => 'Home'}, {
50
+ :day_count => 1, :week_count => 2, :month_count => 3
51
+ })
52
+
53
+ assert_page_counts page, 0, 0, 0
54
+ assert_page_counts page2, 0, 0, 0
55
+ end
56
+
57
+ should "be able to decrement with ids and modifier hash" do
58
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
59
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
60
+
61
+ @page_class.decrement(page.id, page2.id, {
62
+ :day_count => 1, :week_count => 2, :month_count => 3
63
+ })
64
+
65
+ assert_page_counts page, 0, 0, 0
66
+ assert_page_counts page2, 0, 0, 0
67
+ end
68
+
69
+ should "always decrement when decrement is called whether number is positive or negative" do
70
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
71
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
72
+
73
+ @page_class.decrement(page.id, page2.id, {
74
+ :day_count => -1, :week_count => 2, :month_count => -3
75
+ })
76
+
77
+ assert_page_counts page, 0, 0, 0
78
+ assert_page_counts page2, 0, 0, 0
79
+ end
80
+
81
+ should "be able to set with criteria and modifier hashes" do
82
+ page = @page_class.create(:title => 'Home')
83
+ page2 = @page_class.create(:title => 'Home')
84
+
85
+ @page_class.set({:title => 'Home'}, :title => 'Home Revised')
86
+
87
+ page.reload
88
+ page.title.should == 'Home Revised'
89
+
90
+ page2.reload
91
+ page2.title.should == 'Home Revised'
92
+ end
93
+
94
+ should "be able to set with ids and modifier hash" do
95
+ page = @page_class.create(:title => 'Home')
96
+ page2 = @page_class.create(:title => 'Home')
97
+
98
+ @page_class.set(page.id, page2.id, :title => 'Home Revised')
99
+
100
+ page.reload
101
+ page.title.should == 'Home Revised'
102
+
103
+ page2.reload
104
+ page2.title.should == 'Home Revised'
105
+ end
106
+
107
+ should "be able to push with criteria and modifier hashes" do
108
+ page = @page_class.create(:title => 'Home')
109
+ page2 = @page_class.create(:title => 'Home')
110
+
111
+ @page_class.push({:title => 'Home'}, :tags => 'foo')
112
+
113
+ page.reload
114
+ page.tags.should == %w(foo)
115
+
116
+ page2.reload
117
+ page.tags.should == %w(foo)
118
+ end
119
+
120
+ should "be able to push with ids and modifier hash" do
121
+ page = @page_class.create(:title => 'Home')
122
+ page2 = @page_class.create(:title => 'Home')
123
+
124
+ @page_class.push(page.id, page2.id, :tags => 'foo')
125
+
126
+ page.reload
127
+ page.tags.should == %w(foo)
128
+
129
+ page2.reload
130
+ page.tags.should == %w(foo)
131
+ end
132
+
133
+ should "be able to push all with criteria and modifier hashes" do
134
+ page = @page_class.create(:title => 'Home')
135
+ page2 = @page_class.create(:title => 'Home')
136
+ tags = %w(foo bar)
137
+
138
+ @page_class.push_all({:title => 'Home'}, :tags => tags)
139
+
140
+ page.reload
141
+ page.tags.should == tags
142
+
143
+ page2.reload
144
+ page.tags.should == tags
145
+ end
146
+
147
+ should "be able to push all with ids and modifier hash" do
148
+ page = @page_class.create(:title => 'Home')
149
+ page2 = @page_class.create(:title => 'Home')
150
+ tags = %w(foo bar)
151
+
152
+ @page_class.push_all(page.id, page2.id, :tags => tags)
153
+
154
+ page.reload
155
+ page.tags.should == tags
156
+
157
+ page2.reload
158
+ page.tags.should == tags
159
+ end
160
+
161
+ should "be able to pull with criteria and modifier hashes" do
162
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
163
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
164
+
165
+ @page_class.pull({:title => 'Home'}, :tags => 'foo')
166
+
167
+ page.reload
168
+ page.tags.should == %w(bar)
169
+
170
+ page2.reload
171
+ page.tags.should == %w(bar)
172
+ end
173
+
174
+ should "be able to pull with ids and modifier hash" do
175
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
176
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
177
+
178
+ @page_class.pull(page.id, page2.id, :tags => 'foo')
179
+
180
+ page.reload
181
+ page.tags.should == %w(bar)
182
+
183
+ page2.reload
184
+ page.tags.should == %w(bar)
185
+ end
186
+
187
+ should "be able to pull all with criteria and modifier hashes" do
188
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
189
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
190
+
191
+ @page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
192
+
193
+ page.reload
194
+ page.tags.should == %w(baz)
195
+
196
+ page2.reload
197
+ page.tags.should == %w(baz)
198
+ end
199
+
200
+ should "be able to pull all with ids and modifier hash" do
201
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
202
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
203
+
204
+ @page_class.pull_all(page.id, page2.id, :tags => %w(foo bar))
205
+
206
+ page.reload
207
+ page.tags.should == %w(baz)
208
+
209
+ page2.reload
210
+ page.tags.should == %w(baz)
211
+ end
212
+
213
+ should "be able to push uniq with criteria and modifier hash" do
214
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
215
+ page2 = @page_class.create(:title => 'Home')
216
+
217
+ @page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
218
+
219
+ page.reload
220
+ page.tags.should == %w(foo)
221
+
222
+ page2.reload
223
+ page.tags.should == %w(foo)
224
+ end
225
+
226
+ should "be able to push uniq with ids and modifier hash" do
227
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
228
+ page2 = @page_class.create(:title => 'Home')
229
+
230
+ @page_class.push_uniq(page.id, page2.id, :tags => 'foo')
231
+
232
+ page.reload
233
+ page.tags.should == %w(foo)
234
+
235
+ page2.reload
236
+ page.tags.should == %w(foo)
237
+ end
238
+ end
@@ -3,8 +3,7 @@ require 'test_helper'
3
3
  class PaginationTest < Test::Unit::TestCase
4
4
  context "Paginating" do
5
5
  setup do
6
- @document = Class.new do
7
- include MongoMapper::Document
6
+ @document = Doc do
8
7
  set_collection_name 'users'
9
8
 
10
9
  key :first_name, String
@@ -13,7 +12,6 @@ class PaginationTest < Test::Unit::TestCase
13
12
 
14
13
  def self.per_page; 1 end
15
14
  end
16
- @document.collection.remove
17
15
 
18
16
  @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
19
17
  @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@@ -2,19 +2,17 @@ require 'test_helper'
2
2
 
3
3
  class StringIdCompatibilityTest < Test::Unit::TestCase
4
4
  def setup
5
- @note_class = Class.new do
6
- include MongoMapper::EmbeddedDocument
5
+ @note_class = EDoc do
7
6
  key :_id, String
8
7
  end
9
8
 
10
- @task_class = Class.new do
11
- include MongoMapper::Document
9
+ @task_class = Doc do
12
10
  key :_id, String
13
11
  key :project_id, String
14
12
  belongs_to :project
15
13
  end
16
14
 
17
- @project_class = Class.new do
15
+ @project_class = Doc do
18
16
  include MongoMapper::Document
19
17
  key :_id, String
20
18
  end
@@ -22,9 +20,6 @@ class StringIdCompatibilityTest < Test::Unit::TestCase
22
20
  @task_class.belongs_to :project, :class => @project_class
23
21
  @project_class.many :notes, :class => @note_class
24
22
  @project_class.many :tasks, :class => @task_class, :foreign_key => 'project_id'
25
-
26
- @project_class.collection.remove
27
- @task_class.collection.remove
28
23
  end
29
24
 
30
25
  should "assign correct _id for documents" do
@@ -3,12 +3,9 @@ require 'test_helper'
3
3
  class ValidationsTest < Test::Unit::TestCase
4
4
  context "Saving a new document that is invalid" do
5
5
  setup do
6
- @document = Class.new do
7
- include MongoMapper::Document
8
- set_collection_name 'test'
6
+ @document = Doc do
9
7
  key :name, String, :required => true
10
8
  end
11
- @document.collection.remove
12
9
  end
13
10
 
14
11
  should "not insert document" do
@@ -25,31 +22,11 @@ class ValidationsTest < Test::Unit::TestCase
25
22
  end
26
23
  end
27
24
 
28
- context "Skipping validations when saving" do
29
- setup do
30
- @document = Class.new do
31
- include MongoMapper::Document
32
- set_collection_name 'test'
33
- key :name, String, :required => true
34
- end
35
- @document.collection.remove
36
- end
37
-
38
- should "insert document" do
39
- doc = @document.new
40
- doc.save(false)
41
- @document.count.should == 1
42
- end
43
- end
44
-
45
25
  context "Saving a document that is invalid (destructive)" do
46
26
  setup do
47
- @document = Class.new do
48
- include MongoMapper::Document
49
- set_collection_name 'test'
27
+ @document = Doc do
50
28
  key :name, String, :required => true
51
29
  end
52
- @document.collection.remove
53
30
  end
54
31
 
55
32
  should "raise error" do
@@ -60,12 +37,9 @@ class ValidationsTest < Test::Unit::TestCase
60
37
 
61
38
  context "Creating a document that is invalid (destructive)" do
62
39
  setup do
63
- @document = Class.new do
64
- include MongoMapper::Document
65
- set_collection_name 'test'
40
+ @document = Doc do
66
41
  key :name, String, :required => true
67
42
  end
68
- @document.collection.remove
69
43
  end
70
44
 
71
45
  should "raise error" do
@@ -80,12 +54,9 @@ class ValidationsTest < Test::Unit::TestCase
80
54
 
81
55
  context "Saving an existing document that is invalid" do
82
56
  setup do
83
- @document = Class.new do
84
- include MongoMapper::Document
85
- set_collection_name 'test'
57
+ @document = Doc do
86
58
  key :name, String, :required => true
87
59
  end
88
- @document.collection.remove
89
60
 
90
61
  @doc = @document.create(:name => 'John Nunemaker')
91
62
  end
@@ -105,16 +76,12 @@ class ValidationsTest < Test::Unit::TestCase
105
76
 
106
77
  context "Adding validation errors" do
107
78
  setup do
108
- @document = Class.new do
109
- include MongoMapper::Document
110
- set_collection_name 'test'
111
-
79
+ @document = Doc do
112
80
  key :action, String
113
81
  def action_present
114
82
  errors.add(:action, 'is invalid') if action.blank?
115
83
  end
116
84
  end
117
- @document.collection.remove
118
85
  end
119
86
 
120
87
  should "work with validate_on_create callback" do
@@ -150,14 +117,10 @@ class ValidationsTest < Test::Unit::TestCase
150
117
 
151
118
  context "validating uniqueness of" do
152
119
  setup do
153
- @document = Class.new do
154
- include MongoMapper::Document
155
- set_collection_name 'test'
156
-
120
+ @document = Doc do
157
121
  key :name, String
158
122
  validates_uniqueness_of :name
159
123
  end
160
- @document.collection.remove
161
124
  end
162
125
 
163
126
  should "not fail if object is new" do
@@ -166,10 +129,7 @@ class ValidationsTest < Test::Unit::TestCase
166
129
  end
167
130
 
168
131
  should "not fail when new object is out of scope" do
169
- document = Class.new do
170
- include MongoMapper::Document
171
- set_collection_name 'test'
172
-
132
+ document = Doc do
173
133
  key :name
174
134
  key :adult
175
135
  validates_uniqueness_of :name, :scope => :adult
@@ -179,7 +139,6 @@ class ValidationsTest < Test::Unit::TestCase
179
139
 
180
140
  doc2 = document.new("name" => "joe", :adult => false)
181
141
  doc2.should be_valid
182
-
183
142
  end
184
143
 
185
144
  should "allow to update an object" do
@@ -210,10 +169,7 @@ class ValidationsTest < Test::Unit::TestCase
210
169
  end
211
170
 
212
171
  should "allow multiple blank entries if :allow_blank => true" do
213
- document = Class.new do
214
- include MongoMapper::Document
215
- set_collection_name 'test'
216
-
172
+ document = Doc do
217
173
  key :name
218
174
  validates_uniqueness_of :name, :allow_blank => :true
219
175
  end
@@ -231,10 +187,7 @@ class ValidationsTest < Test::Unit::TestCase
231
187
  end
232
188
 
233
189
  should "allow entries that differ only in case by default" do
234
- document = Class.new do
235
- include MongoMapper::Document
236
- set_collection_name 'test'
237
-
190
+ document = Doc do
238
191
  key :name
239
192
  validates_uniqueness_of :name
240
193
  end
@@ -248,10 +201,7 @@ class ValidationsTest < Test::Unit::TestCase
248
201
 
249
202
  context "with :case_sensitive => false" do
250
203
  setup do
251
- @document = Class.new do
252
- include MongoMapper::Document
253
- set_collection_name 'test'
254
-
204
+ @document = Doc do
255
205
  key :name
256
206
  validates_uniqueness_of :name, :case_sensitive => false
257
207
  end
@@ -273,15 +223,11 @@ class ValidationsTest < Test::Unit::TestCase
273
223
 
274
224
  context "scoped by a single attribute" do
275
225
  setup do
276
- @document = Class.new do
277
- include MongoMapper::Document
278
- set_collection_name 'test'
279
-
226
+ @document = Doc do
280
227
  key :name, String
281
228
  key :scope, String
282
229
  validates_uniqueness_of :name, :scope => :scope
283
230
  end
284
- @document.collection.remove
285
231
  end
286
232
 
287
233
  should "fail if the same name exists in the scope" do
@@ -313,16 +259,12 @@ class ValidationsTest < Test::Unit::TestCase
313
259
 
314
260
  context "scoped by a multiple attributes" do
315
261
  setup do
316
- @document = Class.new do
317
- include MongoMapper::Document
318
- set_collection_name 'test'
319
-
262
+ @document = Doc do
320
263
  key :name, String
321
264
  key :first_scope, String
322
265
  key :second_scope, String
323
266
  validates_uniqueness_of :name, :scope => [:first_scope, :second_scope]
324
267
  end
325
- @document.collection.remove
326
268
  end
327
269
 
328
270
  should "fail if the same name exists in the scope" do
@@ -355,13 +297,9 @@ class ValidationsTest < Test::Unit::TestCase
355
297
 
356
298
  context "validates uniqueness of with :unique shortcut" do
357
299
  should "work" do
358
- @document = Class.new do
359
- include MongoMapper::Document
360
- set_collection_name 'test'
361
-
300
+ @document = Doc do
362
301
  key :name, String, :unique => true
363
302
  end
364
- @document.collection.remove
365
303
 
366
304
  doc = @document.create(:name => 'John')
367
305
  doc.should_not have_error_on(:name)
data/test/models.rb CHANGED
@@ -100,7 +100,7 @@ class Account
100
100
 
101
101
  belongs_to :room
102
102
  end
103
- class User < Account; end
103
+ class AccountUser < Account; end
104
104
  class Bot < Account; end
105
105
 
106
106
  class Answer
@@ -5,7 +5,7 @@ class Test::Unit::TestCase
5
5
  end_time = Time.now
6
6
 
7
7
  duration = end_time - begin_time
8
- threshold = 0.5
8
+ threshold = 1.0
9
9
 
10
10
  if duration > threshold
11
11
  puts "\nSLOW TEST: #{duration} - #{self.name}"
data/test/test_helper.rb CHANGED
@@ -16,6 +16,34 @@ require 'support/timing'
16
16
 
17
17
  class Test::Unit::TestCase
18
18
  include CustomMatchers
19
+
20
+ cattr_accessor :mm_document_count
21
+ self.mm_document_count = 0
22
+
23
+ def Doc(name=nil, &block)
24
+ Test::Unit::TestCase.mm_document_count += 1
25
+
26
+ klass = Class.new do
27
+ include MongoMapper::Document
28
+ set_collection_name "test#{rand(20)}"
29
+
30
+ if name
31
+ class_eval "def self.name; '#{name}' end"
32
+ class_eval "def self.to_s; '#{name}' end"
33
+ end
34
+
35
+ class_eval(&block) if block_given?
36
+ end
37
+ klass.collection.remove
38
+ klass
39
+ end
40
+
41
+ def EDoc(&block)
42
+ Class.new do
43
+ include MongoMapper::EmbeddedDocument
44
+ instance_eval(&block) if block_given?
45
+ end
46
+ end
19
47
  end
20
48
 
21
49
  test_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp')
@@ -50,23 +50,17 @@ class AssociationBaseTest < Test::Unit::TestCase
50
50
 
51
51
  should "be false if not many" do
52
52
  Base.new(:belongs_to, :foo).many?.should be_false
53
+ Base.new(:one, :foo).many?.should be_false
53
54
  end
54
55
  end
55
56
 
56
- context "finder_options" do
57
- should "default to empty hash" do
58
- base = Base.new(:many, :foos)
59
- base.finder_options.should == {}
57
+ context "one?" do
58
+ should "be true if one" do
59
+ Base.new(:one, :foo).one?.should be_true
60
60
  end
61
61
 
62
- should "work with order" do
63
- base = Base.new(:many, :foos, :order => 'position')
64
- base.finder_options.should == {:order => 'position'}
65
- end
66
-
67
- should "correctly parse from options" do
68
- base = Base.new(:many, :foos, :order => 'position', :somekey => 'somevalue')
69
- base.finder_options.should == {:order => 'position', :somekey => 'somevalue'}
62
+ should "be false if not one" do
63
+ Base.new(:many, :foo).one?.should be_false
70
64
  end
71
65
  end
72
66
 
@@ -90,6 +84,43 @@ class AssociationBaseTest < Test::Unit::TestCase
90
84
  end
91
85
  end
92
86
 
87
+ context "as?" do
88
+ should "be true if one" do
89
+ Base.new(:one, :foo, :as => :commentable).as?.should be_true
90
+ end
91
+
92
+ should "be false if not one" do
93
+ Base.new(:many, :foo).as?.should be_false
94
+ end
95
+ end
96
+
97
+ context "in_array?" do
98
+ should "be true if one" do
99
+ Base.new(:one, :foo, :in => :list_ids).in_array?.should be_true
100
+ end
101
+
102
+ should "be false if not one" do
103
+ Base.new(:many, :foo).in_array?.should be_false
104
+ end
105
+ end
106
+
107
+ context "finder_options" do
108
+ should "default to empty hash" do
109
+ base = Base.new(:many, :foos)
110
+ base.finder_options.should == {}
111
+ end
112
+
113
+ should "work with order" do
114
+ base = Base.new(:many, :foos, :order => 'position')
115
+ base.finder_options.should == {:order => 'position'}
116
+ end
117
+
118
+ should "correctly parse from options" do
119
+ base = Base.new(:many, :foos, :order => 'position', :somekey => 'somevalue')
120
+ base.finder_options.should == {:order => 'position', :somekey => 'somevalue'}
121
+ end
122
+ end
123
+
93
124
  context "type_key_name" do
94
125
  should "be _type for many" do
95
126
  Base.new(:many, :foos).type_key_name.should == '_type'
@@ -101,7 +132,7 @@ class AssociationBaseTest < Test::Unit::TestCase
101
132
  end
102
133
 
103
134
  context "foreign_key" do
104
- should "default to assocation_name_id" do
135
+ should "default to assocation name _id for belongs to" do
105
136
  base = Base.new(:belongs_to, :foo)
106
137
  base.foreign_key.should == 'foo_id'
107
138
  end
@@ -161,6 +192,16 @@ class AssociationBaseTest < Test::Unit::TestCase
161
192
  base = Base.new(:belongs_to, :target, :polymorphic => true)
162
193
  base.proxy_class.should == BelongsToPolymorphicProxy
163
194
  end
195
+
196
+ should "be OneProxy for one" do
197
+ base = Base.new(:one, :target, :polymorphic => true)
198
+ base.proxy_class.should == OneProxy
199
+ end
200
+
201
+ should "be InArrayProxy for many with :in option" do
202
+ base = Base.new(:many, :messages, :in => :message_ids)
203
+ base.proxy_class.should == InArrayProxy
204
+ end
164
205
  end
165
206
 
166
207
  end