mongo_mapper 0.12.0 → 0.13.0.beta1

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 (154) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +35 -13
  3. data/bin/mmconsole +1 -1
  4. data/lib/mongo_mapper.rb +4 -0
  5. data/lib/mongo_mapper/connection.rb +17 -6
  6. data/lib/mongo_mapper/document.rb +1 -0
  7. data/lib/mongo_mapper/exceptions.rb +4 -1
  8. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  9. data/lib/mongo_mapper/extensions/boolean.rb +20 -23
  10. data/lib/mongo_mapper/extensions/date.rb +3 -3
  11. data/lib/mongo_mapper/extensions/integer.rb +5 -1
  12. data/lib/mongo_mapper/extensions/kernel.rb +2 -0
  13. data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
  14. data/lib/mongo_mapper/extensions/string.rb +2 -2
  15. data/lib/mongo_mapper/extensions/time.rb +7 -5
  16. data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
  17. data/lib/mongo_mapper/plugins.rb +1 -1
  18. data/lib/mongo_mapper/plugins/associations.rb +11 -5
  19. data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
  20. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
  22. data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
  25. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
  26. data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
  27. data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
  28. data/lib/mongo_mapper/plugins/clone.rb +4 -2
  29. data/lib/mongo_mapper/plugins/dirty.rb +22 -21
  30. data/lib/mongo_mapper/plugins/document.rb +4 -4
  31. data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
  32. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
  33. data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
  34. data/lib/mongo_mapper/plugins/keys.rb +133 -54
  35. data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
  36. data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
  37. data/lib/mongo_mapper/plugins/persistence.rb +15 -5
  38. data/lib/mongo_mapper/plugins/querying.rb +15 -40
  39. data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
  40. data/lib/mongo_mapper/plugins/rails.rb +22 -2
  41. data/lib/mongo_mapper/plugins/safe.rb +8 -5
  42. data/lib/mongo_mapper/plugins/sci.rb +26 -4
  43. data/lib/mongo_mapper/plugins/scopes.rb +5 -4
  44. data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
  45. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  46. data/lib/mongo_mapper/utils.rb +12 -0
  47. data/lib/mongo_mapper/version.rb +1 -1
  48. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
  49. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
  50. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
  51. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
  52. data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
  53. data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
  54. data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
  55. data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
  56. data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
  57. data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
  58. data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
  59. data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
  60. data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
  61. data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
  62. data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
  63. data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
  64. data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
  65. data/spec/functional/associations_spec.rb +48 -0
  66. data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
  67. data/spec/functional/caching_spec.rb +75 -0
  68. data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
  69. data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
  70. data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
  71. data/spec/functional/dumpable_spec.rb +24 -0
  72. data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
  73. data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
  74. data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
  75. data/spec/functional/extensions_spec.rb +16 -0
  76. data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
  77. data/spec/functional/indexes_spec.rb +48 -0
  78. data/spec/functional/keys_spec.rb +224 -0
  79. data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
  80. data/spec/functional/modifiers_spec.rb +550 -0
  81. data/spec/functional/pagination_spec.rb +89 -0
  82. data/spec/functional/protected_spec.rb +199 -0
  83. data/spec/functional/querying_spec.rb +1003 -0
  84. data/spec/functional/rails_spec.rb +55 -0
  85. data/spec/functional/safe_spec.rb +163 -0
  86. data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
  87. data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
  88. data/spec/functional/timestamps_spec.rb +97 -0
  89. data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
  90. data/spec/functional/userstamps_spec.rb +46 -0
  91. data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
  92. data/spec/spec_helper.rb +81 -0
  93. data/spec/support/matchers.rb +24 -0
  94. data/{test → spec/support}/models.rb +1 -6
  95. data/spec/unit/associations/base_spec.rb +146 -0
  96. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  97. data/spec/unit/associations/many_association_spec.rb +64 -0
  98. data/spec/unit/associations/one_association_spec.rb +48 -0
  99. data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
  100. data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
  101. data/spec/unit/config_generator_spec.rb +24 -0
  102. data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
  103. data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
  104. data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
  105. data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
  106. data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
  107. data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
  108. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  109. data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
  110. data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
  111. data/spec/unit/keys_spec.rb +155 -0
  112. data/spec/unit/model_generator_spec.rb +47 -0
  113. data/spec/unit/mongo_mapper_spec.rb +184 -0
  114. data/spec/unit/pagination_spec.rb +11 -0
  115. data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
  116. data/spec/unit/rails_compatibility_spec.rb +40 -0
  117. data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
  118. data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
  119. data/spec/unit/serialization_spec.rb +169 -0
  120. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  121. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  122. data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
  123. data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
  124. data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
  125. metadata +199 -179
  126. data/test/_NOTE_ON_TESTING +0 -1
  127. data/test/functional/test_associations.rb +0 -46
  128. data/test/functional/test_caching.rb +0 -77
  129. data/test/functional/test_indexes.rb +0 -50
  130. data/test/functional/test_modifiers.rb +0 -537
  131. data/test/functional/test_pagination.rb +0 -91
  132. data/test/functional/test_protected.rb +0 -201
  133. data/test/functional/test_querying.rb +0 -935
  134. data/test/functional/test_safe.rb +0 -76
  135. data/test/functional/test_timestamps.rb +0 -62
  136. data/test/functional/test_userstamps.rb +0 -44
  137. data/test/support/railtie.rb +0 -4
  138. data/test/support/railtie/autoloaded.rb +0 -2
  139. data/test/support/railtie/not_autoloaded.rb +0 -3
  140. data/test/support/railtie/parent.rb +0 -3
  141. data/test/test_active_model_lint.rb +0 -18
  142. data/test/test_helper.rb +0 -93
  143. data/test/unit/associations/test_base.rb +0 -146
  144. data/test/unit/associations/test_belongs_to_association.rb +0 -29
  145. data/test/unit/associations/test_many_association.rb +0 -63
  146. data/test/unit/associations/test_one_association.rb +0 -47
  147. data/test/unit/serializers/test_json_serializer.rb +0 -216
  148. data/test/unit/serializers/test_xml_serializer.rb +0 -196
  149. data/test/unit/test_identity_map_middleware.rb +0 -132
  150. data/test/unit/test_keys.rb +0 -65
  151. data/test/unit/test_mongo_mapper.rb +0 -157
  152. data/test/unit/test_pagination.rb +0 -11
  153. data/test/unit/test_rails_compatibility.rb +0 -38
  154. data/test/unit/test_serialization.rb +0 -166
@@ -1,8 +1,8 @@
1
- require 'test_helper'
2
- require 'models'
1
+ require 'spec_helper'
2
+ require 'support/models'
3
3
 
4
- class DocumentTest < Test::Unit::TestCase
5
- def setup
4
+ describe "Document" do
5
+ before do
6
6
  @document = Doc do
7
7
  key :first_name, String
8
8
  key :last_name, String
@@ -12,22 +12,22 @@ class DocumentTest < Test::Unit::TestCase
12
12
  end
13
13
 
14
14
  context "array key" do
15
- setup do
15
+ before do
16
16
  @document.key :tags, Array
17
17
  end
18
18
 
19
- should "give correct default" do
19
+ it "should give correct default" do
20
20
  doc = @document.new
21
21
  doc.tags.should == []
22
22
  end
23
23
 
24
- should "work with assignment" do
24
+ it "should work with assignment" do
25
25
  doc = @document.new
26
26
  doc.tags = %w(foo bar)
27
27
  doc.tags.should == %w(foo bar)
28
28
  end
29
29
 
30
- should "work with assignment after saving" do
30
+ it "should work with assignment after saving" do
31
31
  doc = @document.new
32
32
  doc.tags = %w(foo bar)
33
33
  doc.save
@@ -35,20 +35,20 @@ class DocumentTest < Test::Unit::TestCase
35
35
  doc.reload.tags.should == %w(foo bar)
36
36
  end
37
37
 
38
- should "work with assignment then <<" do
38
+ it "should work with assignment then <<" do
39
39
  doc = @document.new
40
40
  doc.tags = []
41
41
  doc.tags << "foo"
42
42
  doc.tags.should == ["foo"]
43
43
  end
44
44
 
45
- should "work with <<" do
45
+ it "should work with <<" do
46
46
  doc = @document.new
47
47
  doc.tags << "foo"
48
48
  doc.tags.should == ["foo"]
49
49
  end
50
50
 
51
- should "work with << then save" do
51
+ it "should work with << then save" do
52
52
  doc = @document.new
53
53
  doc.tags << "foo"
54
54
  doc.tags << "bar"
@@ -59,30 +59,30 @@ class DocumentTest < Test::Unit::TestCase
59
59
  end
60
60
 
61
61
  context "hash key" do
62
- setup do
62
+ before do
63
63
  @document.key :foo, Hash
64
64
  end
65
65
 
66
- should "give correct default" do
66
+ it "should give correct default" do
67
67
  doc = @document.new
68
68
  doc.foo.should == {}
69
69
  end
70
70
 
71
- should "work with []=" do
71
+ it "should work with []=" do
72
72
  doc = @document.new
73
73
  doc.foo["quux"] = "bar"
74
74
  doc.foo["quux"].should == "bar"
75
75
  doc.foo.should == { "quux" => "bar" }
76
76
  end
77
77
 
78
- should "work with indifferent access" do
78
+ it "should work with indifferent access" do
79
79
  doc = @document.new
80
80
  doc.foo = {:baz => 'bar'}
81
81
  doc.foo[:baz].should == 'bar'
82
82
  doc.foo['baz'].should == 'bar'
83
83
  end
84
84
 
85
- should "work with indifferent access after save" do
85
+ it "should work with indifferent access after save" do
86
86
  doc = @document.new
87
87
  doc.foo = {:baz => 'bar'}
88
88
  doc.save
@@ -94,17 +94,17 @@ class DocumentTest < Test::Unit::TestCase
94
94
  end
95
95
 
96
96
  context "custom type key with default" do
97
- setup do
97
+ before do
98
98
  @document.key :window, WindowSize, :default => WindowSize.new(600, 480)
99
99
  end
100
100
 
101
- should "default to default" do
101
+ it "should default to default" do
102
102
  doc = @document.new
103
103
  doc.window.should == WindowSize.new(600, 480)
104
104
 
105
105
  end
106
106
 
107
- should "save and load from mongo" do
107
+ it "should save and load from mongo" do
108
108
  doc = @document.new
109
109
  doc.save
110
110
 
@@ -114,74 +114,74 @@ class DocumentTest < Test::Unit::TestCase
114
114
  end
115
115
 
116
116
  context "key with proc default value" do
117
- setup do
117
+ before do
118
118
  @document.key :proc_default, String, :default => lambda { return 'string' }
119
119
  end
120
120
 
121
- should "detect and run proc default" do
121
+ it "should detect and run proc default" do
122
122
  doc = @document.new
123
123
  doc.proc_default.should == 'string'
124
124
  end
125
125
 
126
- should "save and load from mongo" do
126
+ it "should save and load from mongo" do
127
127
  doc = @document.create
128
128
  doc = doc.reload
129
129
  doc.proc_default.should == 'string'
130
130
  end
131
131
  end
132
132
 
133
- should "have instance method for collection" do
133
+ it "should have instance method for collection" do
134
134
  @document.new.collection.name.should == @document.collection.name
135
135
  end
136
136
 
137
- should "have instance method for database" do
137
+ it "should have instance method for database" do
138
138
  @document.new.database.should == @document.database
139
139
  end
140
140
 
141
141
  context "#destroyed?" do
142
- setup do
142
+ before do
143
143
  @doc1 = @document.create(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
144
144
  end
145
145
 
146
- should "be true if deleted" do
146
+ it "should be true if deleted" do
147
147
  @doc1.delete
148
- assert @doc1.destroyed?
148
+ @doc1.should be_destroyed
149
149
  end
150
150
 
151
- should "be true if destroyed" do
151
+ it "should be true if destroyed" do
152
152
  @doc1.destroy
153
- assert @doc1.destroyed?
153
+ @doc1.should be_destroyed
154
154
  end
155
155
 
156
- should "be false if not deleted or destroyed" do
157
- assert ! @doc1.destroyed?
156
+ it "should be false if not deleted or destroyed" do
157
+ @doc1.should_not be_destroyed
158
158
  end
159
159
  end
160
160
 
161
161
  context "#persisted?" do
162
- setup do
162
+ before do
163
163
  @doc = @document.new(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
164
164
  end
165
165
 
166
- should "be false if new" do
166
+ it "should be false if new" do
167
167
  @doc.should_not be_persisted
168
168
  end
169
169
 
170
- should "be false if destroyed" do
170
+ it "should be false if destroyed" do
171
171
  @doc.save
172
172
  @doc.destroy
173
173
  @doc.should be_destroyed
174
174
  @doc.should_not be_persisted
175
175
  end
176
176
 
177
- should "be true if not new or destroyed" do
177
+ it "should be true if not new or destroyed" do
178
178
  @doc.save
179
179
  @doc.should be_persisted
180
180
  end
181
181
  end
182
182
 
183
183
  context "#reload" do
184
- setup do
184
+ before do
185
185
  @foo_class = Doc do
186
186
  key :name
187
187
  end
@@ -204,56 +204,56 @@ class DocumentTest < Test::Unit::TestCase
204
204
  })
205
205
  end
206
206
 
207
- should "reload keys from the database" do
207
+ it "should reload keys from the database" do
208
208
  @instance.age = 37
209
209
  @instance.age.should == 37
210
210
  @instance.reload
211
211
  @instance.age.should == 39
212
212
  end
213
213
 
214
- should "reset many associations" do
215
- @instance.foos.expects(:reset).at_least_once
216
- @instance.bars.expects(:reset).at_least_once
214
+ it "should reset many associations" do
215
+ @instance.foos.should_receive(:reset).at_least(1).times
216
+ @instance.bars.should_receive(:reset).at_least(1).times
217
217
  @instance.reload
218
218
  end
219
219
 
220
- should "reset belongs_to association" do
220
+ it "should reset belongs_to association" do
221
221
  @instance.foo = nil
222
222
  @instance.reload
223
223
  @instance.foo.should_not be_nil
224
224
  end
225
225
 
226
- should "reset one association" do
226
+ it "should reset one association" do
227
227
  @instance.bar = nil
228
228
  @instance.reload
229
229
  @instance.bar.should_not be_nil
230
230
  end
231
231
 
232
- should "reset nil one association" do
232
+ it "should reset nil one association" do
233
233
  end
234
234
 
235
- should "reinstantiate embedded associations" do
235
+ it "should reinstantiate embedded associations" do
236
236
  @instance.reload
237
237
  @instance.bars.first.name.should == '1'
238
238
  end
239
239
 
240
- should "return self" do
240
+ it "should return self" do
241
241
  @instance.reload.object_id.should == @instance.object_id
242
242
  end
243
243
 
244
- should "raise DocumentNotFound if not found" do
244
+ it "should raise DocumentNotFound if not found" do
245
245
  @instance.destroy
246
- assert_raises(MongoMapper::DocumentNotFound) { @instance.reload }
246
+ expect { @instance.reload }.to raise_error(MongoMapper::DocumentNotFound)
247
247
  end
248
248
 
249
- should "clear keys that were removed from the database" do
249
+ it "should clear keys that were removed from the database" do
250
250
  @instance.unset(:age)
251
251
  @instance.reload.age.should be_nil
252
252
  end
253
253
  end
254
254
 
255
255
  context "database has keys not defined in model" do
256
- setup do
256
+ before do
257
257
  @id = BSON::ObjectId.new
258
258
  @document.collection.insert({
259
259
  :_id => @id,
@@ -265,7 +265,7 @@ class DocumentTest < Test::Unit::TestCase
265
265
  })
266
266
  end
267
267
 
268
- should "assign all keys from database" do
268
+ it "should assign all keys from database" do
269
269
  doc = @document.find(@id)
270
270
  doc.first_name.should == 'John'
271
271
  doc.last_name.should == 'Nunemaker'
@@ -275,8 +275,8 @@ class DocumentTest < Test::Unit::TestCase
275
275
  end
276
276
  end
277
277
 
278
- should "not walk ObjectSpace when creating a model" do
279
- ObjectSpace.expects(:each_object).never
278
+ it "should not walk ObjectSpace when creating a model" do
279
+ ObjectSpace.should_receive(:each_object).never
280
280
  Doc()
281
281
  end
282
282
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'active_support/cache/memory_store'
3
+
4
+ describe "Documents with the Dumpable plugin" do
5
+ let(:doc) { Doc { key :foo, String } }
6
+ let(:answer) { Answer.create(:body => "answer body") }
7
+ let(:store) { ActiveSupport::Cache::MemoryStore.new(:size => 1.megabyte) }
8
+
9
+ it "should be able to be marshalled" do
10
+ expect { Marshal.dump(answer) }.to_not raise_error
11
+ end
12
+
13
+ it "should be able to be unmarshalled" do
14
+ dumped = Marshal.dump(answer)
15
+ reconstituted_answer = Marshal.load(dumped)
16
+ reconstituted_answer.attributes.should == answer.attributes
17
+ end
18
+
19
+ it "should be able to be saved in a Rails cache" do
20
+ fetched = store.fetch("foo") { Answer.find(answer._id) }
21
+ fetched.body.should == "answer body"
22
+ fetched._id.should == answer._id
23
+ end
24
+ end
@@ -1,7 +1,7 @@
1
- require 'test_helper'
1
+ require 'spec_helper'
2
2
 
3
- class DynamicQueryingTest < Test::Unit::TestCase
4
- def setup
3
+ describe "Dynamic Querying" do
4
+ before do
5
5
  @document = Doc do
6
6
  scope :nunes, where(:last_name => 'Nunemaker')
7
7
 
@@ -16,59 +16,59 @@ class DynamicQueryingTest < Test::Unit::TestCase
16
16
  @doc3 = @document.create(:first_name => 'Steph', :last_name => 'Nunemaker', :age => 26)
17
17
  end
18
18
 
19
- should "find document based on argument" do
19
+ it "should find document based on argument" do
20
20
  @document.find_by_first_name('John').should == @doc1
21
21
  @document.find_by_last_name('Nunemaker', :order => 'age desc').should == @doc1
22
22
  @document.find_by_age(27).should == @doc1
23
23
  end
24
24
 
25
- should "not raise error" do
25
+ it "should not raise error" do
26
26
  @document.find_by_first_name('Mongo').should be_nil
27
27
  end
28
28
 
29
- should "define a method for each key" do
29
+ it "should define a method for each key" do
30
30
  @document.methods(false).select { |e| e =~ /^find_by_/ }.size == @document.keys.size
31
31
  end
32
32
 
33
- should "find document based on all arguments" do
33
+ it "should find document based on all arguments" do
34
34
  @document.find_by_first_name_and_last_name_and_age('John', 'Nunemaker', 27).should == @doc1
35
35
  end
36
36
 
37
- should "not find the document if an argument is wrong" do
37
+ it "should not find the document if an argument is wrong" do
38
38
  @document.find_by_first_name_and_last_name_and_age('John', 'Nunemaker', 28).should be_nil
39
39
  end
40
40
 
41
- should "find all documents based on arguments" do
41
+ it "should find all documents based on arguments" do
42
42
  docs = @document.find_all_by_last_name('Nunemaker')
43
43
  docs.should be_kind_of(Array)
44
44
  docs.should include(@doc1)
45
45
  docs.should include(@doc3)
46
46
  end
47
47
 
48
- should "initialize document with given arguments" do
48
+ it "should initialize document with given arguments" do
49
49
  doc = @document.find_or_initialize_by_first_name_and_last_name('David', 'Cuadrado')
50
50
  doc.should be_new
51
51
  doc.first_name.should == 'David'
52
52
  end
53
53
 
54
- should "not initialize document if document is found" do
54
+ it "should not initialize document if document is found" do
55
55
  doc = @document.find_or_initialize_by_first_name('John')
56
56
  doc.should_not be_new
57
57
  end
58
58
 
59
- should "create document with given arguments" do
59
+ it "should create document with given arguments" do
60
60
  doc = @document.find_or_create_by_first_name_and_last_name('David', 'Cuadrado')
61
61
  doc.should_not be_new
62
62
  doc.first_name.should == 'David'
63
63
  end
64
64
 
65
- should "raise error if document is not found when using !" do
65
+ it "should raise error if document is not found when using !" do
66
66
  lambda {
67
67
  @document.find_by_first_name_and_last_name!(1,2)
68
68
  }.should raise_error(MongoMapper::DocumentNotFound)
69
69
  end
70
70
 
71
- should "work on scopes" do
71
+ it "should work on scopes" do
72
72
  @document.nunes.find_by_first_name('Steph').should == @doc3
73
73
  @document.nunes.find_all_by_first_name('Steph').should == [@doc3]
74
74
  end
@@ -1,8 +1,8 @@
1
- require 'test_helper'
2
- require 'models'
1
+ require 'spec_helper'
2
+ require 'support/models'
3
3
 
4
- class EmbeddedDocumentTest < Test::Unit::TestCase
5
- def setup
4
+ describe "EmbeddedDocument" do
5
+ before do
6
6
  @klass = Doc('Person') do
7
7
  key :name, String
8
8
  end
@@ -20,11 +20,11 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
20
20
  end
21
21
 
22
22
  context "Saving a document with a key that is an embedded document" do
23
- setup do
23
+ before do
24
24
  @klass.key :foo, @address_class
25
25
  end
26
26
 
27
- should "embed embedded document" do
27
+ it "should embed embedded document" do
28
28
  address = @address_class.new(:city => 'South Bend', :state => 'IN')
29
29
  doc = @klass.create(:foo => address)
30
30
  doc.foo.city.should == 'South Bend'
@@ -35,7 +35,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
35
35
  doc.foo.state.should == 'IN'
36
36
  end
37
37
 
38
- should "assign _parent_document and _root_document" do
38
+ it "should assign _parent_document and _root_document" do
39
39
  address = @address_class.new(:city => 'South Bend', :state => 'IN')
40
40
  address._parent_document.should be_nil
41
41
  doc = @klass.create(:foo => address)
@@ -43,7 +43,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
43
43
  address._root_document.should be(doc)
44
44
  end
45
45
 
46
- should "assign _parent_document and _root_document when loading" do
46
+ it "should assign _parent_document and _root_document when loading" do
47
47
  address = @address_class.new(:city => 'South Bend', :state => 'IN')
48
48
  doc = @klass.create(:foo => address)
49
49
  doc.reload
@@ -52,7 +52,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
52
52
  end
53
53
  end
54
54
 
55
- should "correctly instantiate single collection inherited embedded documents" do
55
+ it "should correctly instantiate single collection inherited embedded documents" do
56
56
  document = Doc('Foo') do
57
57
  key :message, Message
58
58
  end
@@ -62,24 +62,24 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
62
62
  end
63
63
 
64
64
  context "new? (embedded key)" do
65
- setup do
65
+ before do
66
66
  @klass.key :foo, @address_class
67
67
  end
68
68
 
69
- should "be true until document is created" do
69
+ it "should be true until document is created" do
70
70
  address = @address_class.new(:city => 'South Bend', :state => 'IN')
71
71
  doc = @klass.new(:foo => address)
72
72
  address.new?.should be_true
73
73
  end
74
74
 
75
- should "be false after document is saved" do
75
+ it "should be false after document is saved" do
76
76
  address = @address_class.new(:city => 'South Bend', :state => 'IN')
77
77
  doc = @klass.new(:foo => address)
78
78
  doc.save
79
79
  doc.foo.new?.should be_false
80
80
  end
81
81
 
82
- should "be false when loaded from database" do
82
+ it "should be false when loaded from database" do
83
83
  address = @address_class.new(:city => 'South Bend', :state => 'IN')
84
84
  doc = @klass.new(:foo => address)
85
85
  doc.save
@@ -90,28 +90,28 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
90
90
  end
91
91
 
92
92
  context "new? (embedded many association)" do
93
- setup do
93
+ before do
94
94
  @doc = @klass.new(:pets => [{:name => 'poo bear'}])
95
95
  end
96
96
 
97
- should "be true until document is saved" do
97
+ it "should be true until document is saved" do
98
98
  @doc.should be_new
99
99
  @doc.pets.first.should be_new
100
100
  end
101
101
 
102
- should "be false after document is saved" do
102
+ it "should be false after document is saved" do
103
103
  @doc.save
104
104
  @doc.pets.first.should_not be_new
105
105
  end
106
106
 
107
- should "be false when loaded from database" do
107
+ it "should be false when loaded from database" do
108
108
  @doc.save
109
109
  @doc.pets.first.should_not be_new
110
110
  @doc.reload
111
111
  @doc.pets.first.should_not be_new
112
112
  end
113
113
 
114
- should "be true until existing document is saved" do
114
+ it "should be true until existing document is saved" do
115
115
  @doc.save
116
116
  pet = @doc.pets.build(:name => 'Rasmus')
117
117
  pet.new?.should be_true
@@ -121,14 +121,14 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
121
121
  end
122
122
 
123
123
  context "new? (nested embedded many association)" do
124
- setup do
124
+ before do
125
125
  @pet_klass.many :addresses, :class=> @address_class
126
126
  @doc = @klass.new
127
127
  @doc.pets.build(:name => 'Rasmus')
128
128
  @doc.save
129
129
  end
130
130
 
131
- should "be true until existing document is saved" do
131
+ it "should be true until existing document is saved" do
132
132
  address = @doc.pets.first.addresses.build(:city => 'Holland', :state => 'MI')
133
133
  address.new?.should be_true
134
134
  @doc.save
@@ -137,12 +137,12 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
137
137
  end
138
138
 
139
139
  context "new? (embedded one association)" do
140
- setup do
140
+ before do
141
141
  @klass.one :address, :class => @address_class
142
142
  @doc = @klass.new
143
143
  end
144
144
 
145
- should "be true until existing document is saved" do
145
+ it "should be true until existing document is saved" do
146
146
  @doc.save
147
147
  @doc.build_address(:city => 'Holland', :state => 'MI')
148
148
  @doc.address.new?.should be_true
@@ -152,14 +152,14 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
152
152
  end
153
153
 
154
154
  context "new? (nested embedded one association)" do
155
- setup do
155
+ before do
156
156
  @pet_klass.one :address, :class => @address_class
157
157
  @doc = @klass.new
158
158
  @doc.pets.build(:name => 'Rasmus')
159
159
  @doc.save
160
160
  end
161
161
 
162
- should "be true until existing document is saved" do
162
+ it "should be true until existing document is saved" do
163
163
  address = @doc.pets.first.build_address(:city => 'Holland', :stats => 'MI')
164
164
  address.new?.should be_true
165
165
  @doc.save
@@ -168,16 +168,16 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
168
168
  end
169
169
 
170
170
  context "#destroyed?" do
171
- setup do
171
+ before do
172
172
  @doc = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
173
173
  end
174
174
 
175
- should "be false if root document is not destroyed" do
175
+ it "should be false if root document is not destroyed" do
176
176
  @doc.should_not be_destroyed
177
177
  @doc.pets.first.should_not be_destroyed
178
178
  end
179
179
 
180
- should "be true if root document is destroyed" do
180
+ it "should be true if root document is destroyed" do
181
181
  @doc.destroy
182
182
  @doc.should be_destroyed
183
183
  @doc.pets.first.should be_destroyed
@@ -185,28 +185,28 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
185
185
  end
186
186
 
187
187
  context "#persisted?" do
188
- setup do
188
+ before do
189
189
  @doc = @klass.new(:name => 'persisted doc', :pets => [@pet_klass.new(:name => 'persisted pet')])
190
190
  end
191
191
 
192
- should "be false if new" do
192
+ it "should be false if new" do
193
193
  @doc.pets.first.should_not be_persisted
194
194
  end
195
195
 
196
- should "be false if destroyed" do
196
+ it "should be false if destroyed" do
197
197
  @doc.save
198
198
  @doc.destroy
199
199
  @doc.pets.first.should be_destroyed
200
200
  @doc.pets.first.should_not be_persisted
201
201
  end
202
202
 
203
- should "be true if not new or destroyed" do
203
+ it "should be true if not new or destroyed" do
204
204
  @doc.save
205
205
  @doc.pets.first.should be_persisted
206
206
  end
207
207
  end
208
208
 
209
- should "be able to save" do
209
+ it "should be able to save" do
210
210
  person = @klass.create
211
211
 
212
212
  pet = @pet_klass.new(:name => 'sparky')
@@ -219,18 +219,18 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
219
219
  person.pets.first.should == pet
220
220
  end
221
221
 
222
- should "be able to save!" do
222
+ it "should be able to save!" do
223
223
  person = @klass.create
224
224
 
225
225
  pet = @pet_klass.new(:name => 'sparky')
226
226
  person.pets << pet
227
227
  pet.should be_new
228
228
 
229
- person.expects(:save!)
229
+ person.should_receive(:save!)
230
230
  pet.save!
231
231
  end
232
232
 
233
- should "be able to dynamically add new keys and save" do
233
+ it "should be able to dynamically add new keys and save" do
234
234
  person = @klass.create
235
235
 
236
236
  pet = @pet_klass.new(:name => 'sparky', :crazy_key => 'crazy')
@@ -241,7 +241,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
241
241
  person.pets.first.crazy_key.should == 'crazy'
242
242
  end
243
243
 
244
- should "be able to update_attribute" do
244
+ it "should be able to update_attribute" do
245
245
  pet = @pet_klass.new(:name => 'sparky')
246
246
  person = @klass.create(:pets => [pet])
247
247
  person.reload
@@ -253,7 +253,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
253
253
  person.pets.first.name.should == 'koda'
254
254
  end
255
255
 
256
- should "be able to update_attributes" do
256
+ it "should be able to update_attributes" do
257
257
  pet = @pet_klass.new(:name => 'sparky')
258
258
  person = @klass.create(:pets => [pet])
259
259
  person.reload
@@ -265,24 +265,33 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
265
265
  person.pets.first.name.should == 'koda'
266
266
  end
267
267
 
268
- should "be able to update_attributes!" do
268
+ it "should be able to update_attributes!" do
269
269
  person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
270
270
  person.reload
271
271
  pet = person.pets.first
272
272
 
273
273
  attributes = {:name => 'koda'}
274
- pet.expects(:attributes=).with(attributes)
275
- pet.expects(:save!)
274
+ pet.should_receive(:attributes=).with(attributes)
275
+ pet.should_receive(:save!)
276
276
  pet.update_attributes!(attributes)
277
277
  end
278
278
 
279
- should "have database instance method that is equal to root document" do
279
+ it "should have database instance method that is equal to root document" do
280
280
  person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
281
281
  person.pets.first.database.should == person.database
282
282
  end
283
283
 
284
- should "have collection instance method that is equal to root document" do
284
+ it "should have collection instance method that is equal to root document" do
285
285
  person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
286
286
  person.pets.first.collection.name.should == person.collection.name
287
287
  end
288
+
289
+ it "should not fail if the source document contains nils in the embedded document list" do
290
+ @klass.collection.insert(:pets => [nil, {:name => "Sasha"}])
291
+ expect { @klass.all.first.pets }.to_not raise_error
292
+ @klass.all.first.pets.tap do |pets|
293
+ pets.length.should == 1
294
+ pets[0].name.should == "Sasha"
295
+ end
296
+ end
288
297
  end