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.
- checksums.yaml +7 -0
- data/README.rdoc +35 -13
- data/bin/mmconsole +1 -1
- data/lib/mongo_mapper.rb +4 -0
- data/lib/mongo_mapper/connection.rb +17 -6
- data/lib/mongo_mapper/document.rb +1 -0
- data/lib/mongo_mapper/exceptions.rb +4 -1
- data/lib/mongo_mapper/extensions/binary.rb +1 -1
- data/lib/mongo_mapper/extensions/boolean.rb +20 -23
- data/lib/mongo_mapper/extensions/date.rb +3 -3
- data/lib/mongo_mapper/extensions/integer.rb +5 -1
- data/lib/mongo_mapper/extensions/kernel.rb +2 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
- data/lib/mongo_mapper/extensions/string.rb +2 -2
- data/lib/mongo_mapper/extensions/time.rb +7 -5
- data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
- data/lib/mongo_mapper/plugins.rb +1 -1
- data/lib/mongo_mapper/plugins/associations.rb +11 -5
- data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
- data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
- data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
- data/lib/mongo_mapper/plugins/clone.rb +4 -2
- data/lib/mongo_mapper/plugins/dirty.rb +22 -21
- data/lib/mongo_mapper/plugins/document.rb +4 -4
- data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
- data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
- data/lib/mongo_mapper/plugins/keys.rb +133 -54
- data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
- data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
- data/lib/mongo_mapper/plugins/persistence.rb +15 -5
- data/lib/mongo_mapper/plugins/querying.rb +15 -40
- data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
- data/lib/mongo_mapper/plugins/rails.rb +22 -2
- data/lib/mongo_mapper/plugins/safe.rb +8 -5
- data/lib/mongo_mapper/plugins/sci.rb +26 -4
- data/lib/mongo_mapper/plugins/scopes.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/utils.rb +12 -0
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
- data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
- data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
- data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
- data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
- data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
- data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
- data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
- data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
- data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
- data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
- data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
- data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
- data/spec/functional/associations_spec.rb +48 -0
- data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
- data/spec/functional/caching_spec.rb +75 -0
- data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
- data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
- data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
- data/spec/functional/dumpable_spec.rb +24 -0
- data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
- data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
- data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
- data/spec/functional/extensions_spec.rb +16 -0
- data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
- data/spec/functional/indexes_spec.rb +48 -0
- data/spec/functional/keys_spec.rb +224 -0
- data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
- data/spec/functional/modifiers_spec.rb +550 -0
- data/spec/functional/pagination_spec.rb +89 -0
- data/spec/functional/protected_spec.rb +199 -0
- data/spec/functional/querying_spec.rb +1003 -0
- data/spec/functional/rails_spec.rb +55 -0
- data/spec/functional/safe_spec.rb +163 -0
- data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
- data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
- data/spec/functional/timestamps_spec.rb +97 -0
- data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
- data/spec/functional/userstamps_spec.rb +46 -0
- data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
- data/spec/spec_helper.rb +81 -0
- data/spec/support/matchers.rb +24 -0
- data/{test → spec/support}/models.rb +1 -6
- data/spec/unit/associations/base_spec.rb +146 -0
- data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
- data/spec/unit/associations/many_association_spec.rb +64 -0
- data/spec/unit/associations/one_association_spec.rb +48 -0
- data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
- data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
- data/spec/unit/config_generator_spec.rb +24 -0
- data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
- data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
- data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
- data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
- data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
- data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
- data/spec/unit/identity_map_middleware_spec.rb +134 -0
- data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
- data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
- data/spec/unit/keys_spec.rb +155 -0
- data/spec/unit/model_generator_spec.rb +47 -0
- data/spec/unit/mongo_mapper_spec.rb +184 -0
- data/spec/unit/pagination_spec.rb +11 -0
- data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
- data/spec/unit/rails_compatibility_spec.rb +40 -0
- data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
- data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
- data/spec/unit/serialization_spec.rb +169 -0
- data/spec/unit/serializers/json_serializer_spec.rb +218 -0
- data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
- data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
- data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
- data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
- metadata +199 -179
- data/test/_NOTE_ON_TESTING +0 -1
- data/test/functional/test_associations.rb +0 -46
- data/test/functional/test_caching.rb +0 -77
- data/test/functional/test_indexes.rb +0 -50
- data/test/functional/test_modifiers.rb +0 -537
- data/test/functional/test_pagination.rb +0 -91
- data/test/functional/test_protected.rb +0 -201
- data/test/functional/test_querying.rb +0 -935
- data/test/functional/test_safe.rb +0 -76
- data/test/functional/test_timestamps.rb +0 -62
- data/test/functional/test_userstamps.rb +0 -44
- data/test/support/railtie.rb +0 -4
- data/test/support/railtie/autoloaded.rb +0 -2
- data/test/support/railtie/not_autoloaded.rb +0 -3
- data/test/support/railtie/parent.rb +0 -3
- data/test/test_active_model_lint.rb +0 -18
- data/test/test_helper.rb +0 -93
- data/test/unit/associations/test_base.rb +0 -146
- data/test/unit/associations/test_belongs_to_association.rb +0 -29
- data/test/unit/associations/test_many_association.rb +0 -63
- data/test/unit/associations/test_one_association.rb +0 -47
- data/test/unit/serializers/test_json_serializer.rb +0 -216
- data/test/unit/serializers/test_xml_serializer.rb +0 -196
- data/test/unit/test_identity_map_middleware.rb +0 -132
- data/test/unit/test_keys.rb +0 -65
- data/test/unit/test_mongo_mapper.rb +0 -157
- data/test/unit/test_pagination.rb +0 -11
- data/test/unit/test_rails_compatibility.rb +0 -38
- data/test/unit/test_serialization.rb +0 -166
@@ -1,8 +1,8 @@
|
|
1
|
-
require '
|
2
|
-
require 'models'
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/models'
|
3
3
|
|
4
|
-
|
5
|
-
|
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
|
-
|
15
|
+
before do
|
16
16
|
@document.key :tags, Array
|
17
17
|
end
|
18
18
|
|
19
|
-
should
|
19
|
+
it "should give correct default" do
|
20
20
|
doc = @document.new
|
21
21
|
doc.tags.should == []
|
22
22
|
end
|
23
23
|
|
24
|
-
should
|
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
|
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
|
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
|
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
|
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
|
-
|
62
|
+
before do
|
63
63
|
@document.key :foo, Hash
|
64
64
|
end
|
65
65
|
|
66
|
-
should
|
66
|
+
it "should give correct default" do
|
67
67
|
doc = @document.new
|
68
68
|
doc.foo.should == {}
|
69
69
|
end
|
70
70
|
|
71
|
-
should
|
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
|
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
|
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
|
-
|
97
|
+
before do
|
98
98
|
@document.key :window, WindowSize, :default => WindowSize.new(600, 480)
|
99
99
|
end
|
100
100
|
|
101
|
-
should
|
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
|
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
|
-
|
117
|
+
before do
|
118
118
|
@document.key :proc_default, String, :default => lambda { return 'string' }
|
119
119
|
end
|
120
120
|
|
121
|
-
should
|
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
|
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
|
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
|
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
|
-
|
142
|
+
before do
|
143
143
|
@doc1 = @document.create(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
|
144
144
|
end
|
145
145
|
|
146
|
-
should
|
146
|
+
it "should be true if deleted" do
|
147
147
|
@doc1.delete
|
148
|
-
|
148
|
+
@doc1.should be_destroyed
|
149
149
|
end
|
150
150
|
|
151
|
-
should
|
151
|
+
it "should be true if destroyed" do
|
152
152
|
@doc1.destroy
|
153
|
-
|
153
|
+
@doc1.should be_destroyed
|
154
154
|
end
|
155
155
|
|
156
|
-
should
|
157
|
-
|
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
|
-
|
162
|
+
before do
|
163
163
|
@doc = @document.new(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
|
164
164
|
end
|
165
165
|
|
166
|
-
should
|
166
|
+
it "should be false if new" do
|
167
167
|
@doc.should_not be_persisted
|
168
168
|
end
|
169
169
|
|
170
|
-
should
|
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
|
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
|
-
|
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
|
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
|
215
|
-
@instance.foos.
|
216
|
-
@instance.bars.
|
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
|
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
|
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
|
232
|
+
it "should reset nil one association" do
|
233
233
|
end
|
234
234
|
|
235
|
-
should
|
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
|
240
|
+
it "should return self" do
|
241
241
|
@instance.reload.object_id.should == @instance.object_id
|
242
242
|
end
|
243
243
|
|
244
|
-
should
|
244
|
+
it "should raise DocumentNotFound if not found" do
|
245
245
|
@instance.destroy
|
246
|
-
|
246
|
+
expect { @instance.reload }.to raise_error(MongoMapper::DocumentNotFound)
|
247
247
|
end
|
248
248
|
|
249
|
-
should
|
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
|
-
|
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
|
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
|
279
|
-
ObjectSpace.
|
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 '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb}
RENAMED
@@ -1,8 +1,8 @@
|
|
1
|
-
require '
|
2
|
-
require 'models'
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/models'
|
3
3
|
|
4
|
-
|
5
|
-
|
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
|
-
|
23
|
+
before do
|
24
24
|
@klass.key :foo, @address_class
|
25
25
|
end
|
26
26
|
|
27
|
-
should
|
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
|
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
|
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
|
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
|
-
|
65
|
+
before do
|
66
66
|
@klass.key :foo, @address_class
|
67
67
|
end
|
68
68
|
|
69
|
-
should
|
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
|
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
|
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
|
-
|
93
|
+
before do
|
94
94
|
@doc = @klass.new(:pets => [{:name => 'poo bear'}])
|
95
95
|
end
|
96
96
|
|
97
|
-
should
|
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
|
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
|
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
|
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
|
-
|
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
|
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
|
-
|
140
|
+
before do
|
141
141
|
@klass.one :address, :class => @address_class
|
142
142
|
@doc = @klass.new
|
143
143
|
end
|
144
144
|
|
145
|
-
should
|
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
|
-
|
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
|
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
|
-
|
171
|
+
before do
|
172
172
|
@doc = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
|
173
173
|
end
|
174
174
|
|
175
|
-
should
|
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
|
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
|
-
|
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
|
192
|
+
it "should be false if new" do
|
193
193
|
@doc.pets.first.should_not be_persisted
|
194
194
|
end
|
195
195
|
|
196
|
-
should
|
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
|
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
|
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
|
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.
|
229
|
+
person.should_receive(:save!)
|
230
230
|
pet.save!
|
231
231
|
end
|
232
232
|
|
233
|
-
should
|
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
|
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
|
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
|
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.
|
275
|
-
pet.
|
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
|
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
|
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
|