mongo_mapper 0.12.0 → 0.13.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- 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,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
module KeyOverride
|
4
4
|
def other_child
|
@@ -10,9 +10,9 @@ module KeyOverride
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
describe "EmbeddedDocument" do
|
14
14
|
context "EmbeddedDocuments" do
|
15
|
-
|
15
|
+
before do
|
16
16
|
class ::Grandparent
|
17
17
|
include MongoMapper::EmbeddedDocument
|
18
18
|
key :grandparent, String
|
@@ -47,7 +47,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
|
50
|
+
after do
|
51
51
|
Object.send :remove_const, 'Grandparent' if defined?(::Grandparent)
|
52
52
|
Object.send :remove_const, 'Parent' if defined?(::Parent)
|
53
53
|
Object.send :remove_const, 'Child' if defined?(::Child)
|
@@ -57,43 +57,43 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
57
57
|
end
|
58
58
|
|
59
59
|
context "Including MongoMapper::EmbeddedDocument in a class" do
|
60
|
-
|
60
|
+
before do
|
61
61
|
@klass = EDoc()
|
62
62
|
end
|
63
63
|
|
64
|
-
should
|
64
|
+
it "should add _id key" do
|
65
65
|
@klass.keys['_id'].should_not be_nil
|
66
66
|
end
|
67
67
|
|
68
|
-
should
|
68
|
+
it "should know it is using object id" do
|
69
69
|
@klass.using_object_id?.should be_true
|
70
70
|
end
|
71
71
|
|
72
|
-
should
|
72
|
+
it "should know it is not using object id if _id type is changed" do
|
73
73
|
@klass.key :_id, String
|
74
74
|
@klass.using_object_id?.should be_false
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
78
|
context "Class Methods" do
|
79
|
-
should
|
79
|
+
it "should include logger" do
|
80
80
|
@klass = EDoc()
|
81
81
|
@klass.logger.should == MongoMapper.logger
|
82
82
|
@klass.logger.should be_instance_of(Logger)
|
83
83
|
end
|
84
84
|
|
85
|
-
should
|
85
|
+
it "should return false for embeddable" do
|
86
86
|
EDoc().embeddable?.should be_true
|
87
87
|
end
|
88
88
|
|
89
89
|
context "#to_mongo" do
|
90
|
-
|
90
|
+
before { @klass = EDoc() }
|
91
91
|
|
92
|
-
should
|
92
|
+
it "should be nil if nil" do
|
93
93
|
@klass.to_mongo(nil).should be_nil
|
94
94
|
end
|
95
95
|
|
96
|
-
should
|
96
|
+
it "should convert to_mongo for other values" do
|
97
97
|
doc = @klass.new(:foo => 'bar')
|
98
98
|
to_mongo = @klass.to_mongo(doc)
|
99
99
|
to_mongo.is_a?(Hash).should be_true
|
@@ -102,18 +102,18 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
102
102
|
end
|
103
103
|
|
104
104
|
context "#from_mongo" do
|
105
|
-
|
105
|
+
before { @klass = EDoc() }
|
106
106
|
|
107
|
-
should
|
107
|
+
it "should be nil if nil" do
|
108
108
|
@klass.from_mongo(nil).should be_nil
|
109
109
|
end
|
110
110
|
|
111
|
-
should
|
111
|
+
it "should be instance if instance of class" do
|
112
112
|
doc = @klass.new
|
113
113
|
@klass.from_mongo(doc).should == doc
|
114
114
|
end
|
115
115
|
|
116
|
-
should
|
116
|
+
it "should be instance if hash of attributes" do
|
117
117
|
doc = @klass.from_mongo({:foo => 'bar'})
|
118
118
|
doc.instance_of?(@klass).should be_true
|
119
119
|
doc.foo.should == 'bar'
|
@@ -121,35 +121,35 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
121
121
|
end
|
122
122
|
|
123
123
|
context "defining a key" do
|
124
|
-
|
124
|
+
before do
|
125
125
|
@document = EDoc()
|
126
126
|
end
|
127
127
|
|
128
|
-
should
|
128
|
+
it "should work with name" do
|
129
129
|
key = @document.key(:name)
|
130
130
|
key.name.should == 'name'
|
131
131
|
end
|
132
132
|
|
133
|
-
should
|
133
|
+
it "should work with name and type" do
|
134
134
|
key = @document.key(:name, String)
|
135
135
|
key.name.should == 'name'
|
136
136
|
key.type.should == String
|
137
137
|
end
|
138
138
|
|
139
|
-
should
|
139
|
+
it "should work with name, type and options" do
|
140
140
|
key = @document.key(:name, String, :required => true)
|
141
141
|
key.name.should == 'name'
|
142
142
|
key.type.should == String
|
143
143
|
key.options[:required].should be_true
|
144
144
|
end
|
145
145
|
|
146
|
-
should
|
146
|
+
it "should work with name and options" do
|
147
147
|
key = @document.key(:name, :required => true)
|
148
148
|
key.name.should == 'name'
|
149
149
|
key.options[:required].should be_true
|
150
150
|
end
|
151
151
|
|
152
|
-
should
|
152
|
+
it "should be tracked per document" do
|
153
153
|
@document.key(:name, String)
|
154
154
|
@document.key(:age, Integer)
|
155
155
|
@document.keys['name'].name.should == 'name'
|
@@ -158,32 +158,32 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
158
158
|
@document.keys['age'].type.should == Integer
|
159
159
|
end
|
160
160
|
|
161
|
-
should
|
161
|
+
it "should be redefinable" do
|
162
162
|
@document.key(:foo, String)
|
163
163
|
@document.keys['foo'].type.should == String
|
164
164
|
@document.key(:foo, Integer)
|
165
165
|
@document.keys['foo'].type.should == Integer
|
166
166
|
end
|
167
167
|
|
168
|
-
should
|
168
|
+
it "should create reader method" do
|
169
169
|
@document.new.should_not respond_to(:foo)
|
170
170
|
@document.key(:foo, String)
|
171
171
|
@document.new.should respond_to(:foo)
|
172
172
|
end
|
173
173
|
|
174
|
-
should
|
174
|
+
it "should create reader before type cast method" do
|
175
175
|
@document.new.should_not respond_to(:foo_before_type_cast)
|
176
176
|
@document.key(:foo, String)
|
177
177
|
@document.new.should respond_to(:foo_before_type_cast)
|
178
178
|
end
|
179
179
|
|
180
|
-
should
|
180
|
+
it "should create writer method" do
|
181
181
|
@document.new.should_not respond_to(:foo=)
|
182
182
|
@document.key(:foo, String)
|
183
183
|
@document.new.should respond_to(:foo=)
|
184
184
|
end
|
185
185
|
|
186
|
-
should
|
186
|
+
it "should create boolean method" do
|
187
187
|
@document.new.should_not respond_to(:foo?)
|
188
188
|
@document.key(:foo, String)
|
189
189
|
@document.new.should respond_to(:foo?)
|
@@ -191,13 +191,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
191
191
|
end
|
192
192
|
|
193
193
|
context "keys" do
|
194
|
-
should
|
194
|
+
it "should be inherited" do
|
195
195
|
Grandparent.keys.keys.sort.should == ['_id', '_type', 'grandparent']
|
196
196
|
Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
|
197
197
|
Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
|
198
198
|
end
|
199
199
|
|
200
|
-
should
|
200
|
+
it "should propogate to descendants if key added after class definition" do
|
201
201
|
Grandparent.key :foo, String
|
202
202
|
|
203
203
|
Grandparent.keys.keys.sort.should == ['_id', '_type', 'foo', 'grandparent']
|
@@ -205,21 +205,21 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
205
205
|
Child.keys.keys.sort.should == ['_id', '_type', 'child', 'foo', 'grandparent', 'parent']
|
206
206
|
end
|
207
207
|
|
208
|
-
should
|
208
|
+
it "should not add anonymous objects to the ancestor tree" do
|
209
209
|
OtherChild.ancestors.any? { |a| a.name.blank? }.should be_false
|
210
210
|
end
|
211
211
|
|
212
|
-
should
|
212
|
+
it "should not include descendant keys" do
|
213
213
|
lambda { Parent.new.other_child }.should raise_error
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
217
217
|
context "descendants" do
|
218
|
-
should
|
218
|
+
it "should default to an empty array" do
|
219
219
|
Child.descendants.should == []
|
220
220
|
end
|
221
221
|
|
222
|
-
should
|
222
|
+
it "should be recorded" do
|
223
223
|
Grandparent.direct_descendants.should == [Parent]
|
224
224
|
Grandparent.descendants.to_set.should == [Parent, Child, OtherChild].to_set
|
225
225
|
|
@@ -229,38 +229,38 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
229
229
|
end
|
230
230
|
|
231
231
|
context "An instance of an embedded document" do
|
232
|
-
|
232
|
+
before do
|
233
233
|
@document = EDoc do
|
234
234
|
key :name, String
|
235
235
|
key :age, Integer
|
236
236
|
end
|
237
237
|
end
|
238
238
|
|
239
|
-
should
|
239
|
+
it "should respond to cache_key" do
|
240
240
|
@document.new.should respond_to(:cache_key)
|
241
241
|
end
|
242
242
|
|
243
|
-
should
|
243
|
+
it "should have access to class logger" do
|
244
244
|
doc = @document.new
|
245
245
|
doc.logger.should == @document.logger
|
246
246
|
doc.logger.should be_instance_of(Logger)
|
247
247
|
end
|
248
248
|
|
249
|
-
should
|
249
|
+
it "should automatically have an _id key" do
|
250
250
|
@document.keys.keys.should include('_id')
|
251
251
|
end
|
252
252
|
|
253
|
-
should
|
253
|
+
it "should create id during initialization" do
|
254
254
|
@document.new._id.should be_instance_of(BSON::ObjectId)
|
255
255
|
end
|
256
256
|
|
257
|
-
should
|
257
|
+
it "should have id method returns _id" do
|
258
258
|
id = BSON::ObjectId.new
|
259
259
|
doc = @document.new(:_id => id)
|
260
260
|
doc.id.should == id
|
261
261
|
end
|
262
262
|
|
263
|
-
should
|
263
|
+
it "should convert string object id to mongo object id when assigning id with _id object id type" do
|
264
264
|
id = BSON::ObjectId.new
|
265
265
|
doc = @document.new(:id => id.to_s)
|
266
266
|
doc._id.should == id
|
@@ -271,19 +271,19 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
271
271
|
end
|
272
272
|
|
273
273
|
context "_parent_document" do
|
274
|
-
should
|
274
|
+
it "should default to nil" do
|
275
275
|
@document.new._parent_document.should be_nil
|
276
276
|
@document.new._root_document.should be_nil
|
277
277
|
end
|
278
278
|
|
279
|
-
should
|
279
|
+
it "should set _root_document when setting _parent_document" do
|
280
280
|
root = Doc().new
|
281
281
|
doc = @document.new(:_parent_document => root)
|
282
282
|
doc._parent_document.should be(root)
|
283
283
|
doc._root_document.should be(root)
|
284
284
|
end
|
285
285
|
|
286
|
-
should
|
286
|
+
it "should set _root_document when setting _parent_document on embedded many" do
|
287
287
|
root = Doc().new
|
288
288
|
klass = EDoc { many :children }
|
289
289
|
parent = klass.new(:_parent_document => root, :children => [{}])
|
@@ -294,54 +294,54 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
294
294
|
end
|
295
295
|
|
296
296
|
context "being initialized" do
|
297
|
-
should
|
297
|
+
it "should accept a hash that sets keys and values" do
|
298
298
|
doc = @document.new(:name => 'John', :age => 23)
|
299
299
|
doc.attributes.keys.sort.should == ['_id', 'age', 'name']
|
300
300
|
doc.attributes['name'].should == 'John'
|
301
301
|
doc.attributes['age'].should == 23
|
302
302
|
end
|
303
303
|
|
304
|
-
should
|
304
|
+
it "should be able to assign keys dynamically" do
|
305
305
|
doc = @document.new(:name => 'John', :skills => ['ruby', 'rails'])
|
306
306
|
doc.name.should == 'John'
|
307
307
|
doc.skills.should == ['ruby', 'rails']
|
308
308
|
end
|
309
309
|
|
310
|
-
should
|
311
|
-
|
310
|
+
it "should not throw error if initialized with nil" do
|
311
|
+
expect { @document.new(nil) }.to_not raise_error
|
312
312
|
end
|
313
313
|
end
|
314
314
|
|
315
315
|
context "initialized when _type key present" do
|
316
|
-
|
316
|
+
before do
|
317
317
|
@klass = EDoc('FooBar') { key :_type, String }
|
318
318
|
end
|
319
319
|
|
320
|
-
should
|
320
|
+
it "should set _type to class name" do
|
321
321
|
@klass.new._type.should == 'FooBar'
|
322
322
|
end
|
323
323
|
|
324
|
-
should
|
324
|
+
it "should ignore _type attribute and always use class" do
|
325
325
|
@klass.new(:_type => 'Foo')._type.should == 'FooBar'
|
326
326
|
end
|
327
327
|
end
|
328
328
|
|
329
329
|
context "attributes=" do
|
330
|
-
should
|
330
|
+
it "should update values for keys provided" do
|
331
331
|
doc = @document.new(:name => 'foobar', :age => 10)
|
332
332
|
doc.attributes = {:name => 'new value', :age => 5}
|
333
333
|
doc.attributes[:name].should == 'new value'
|
334
334
|
doc.attributes[:age].should == 5
|
335
335
|
end
|
336
336
|
|
337
|
-
should
|
337
|
+
it "should not update values for keys that were not provided" do
|
338
338
|
doc = @document.new(:name => 'foobar', :age => 10)
|
339
339
|
doc.attributes = {:name => 'new value'}
|
340
340
|
doc.attributes[:name].should == 'new value'
|
341
341
|
doc.attributes[:age].should == 10
|
342
342
|
end
|
343
343
|
|
344
|
-
should
|
344
|
+
it "should work with pre-defined methods" do
|
345
345
|
@document.class_eval do
|
346
346
|
attr_writer :password
|
347
347
|
|
@@ -354,7 +354,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
354
354
|
doc.passwd.should == 'secret'
|
355
355
|
end
|
356
356
|
|
357
|
-
should
|
357
|
+
it "should type cast key values" do
|
358
358
|
doc = @document.new(:name => 1234, :age => '21')
|
359
359
|
doc.name.should == '1234'
|
360
360
|
doc.age.should == 21
|
@@ -362,13 +362,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
362
362
|
end
|
363
363
|
|
364
364
|
context "attributes" do
|
365
|
-
should
|
365
|
+
it "should default to hash with all keys" do
|
366
366
|
doc = @document.new
|
367
367
|
doc.keys.keys.sort.should == ['_id', 'age', 'name']
|
368
368
|
doc.attributes.keys.sort.should == ['_id']
|
369
369
|
end
|
370
370
|
|
371
|
-
should
|
371
|
+
it "should return all keys with values" do
|
372
372
|
doc = @document.new(:name => 'string', :age => nil)
|
373
373
|
doc.attributes.keys.sort.should == ['_id', 'name']
|
374
374
|
doc.keys.keys.sort.should == ['_id', 'age', 'name']
|
@@ -376,7 +376,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
376
376
|
doc.attributes.values.should_not include(nil)
|
377
377
|
end
|
378
378
|
|
379
|
-
should
|
379
|
+
it "should have indifferent access" do
|
380
380
|
doc = @document.new(:name => 'string')
|
381
381
|
doc.attributes[:name].should == 'string'
|
382
382
|
doc.attributes['name'].should == 'string'
|
@@ -384,13 +384,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
384
384
|
end
|
385
385
|
|
386
386
|
context "to_mongo" do
|
387
|
-
should
|
387
|
+
it "should default to hash with _id key" do
|
388
388
|
doc = @document.new
|
389
389
|
doc.to_mongo.keys.sort.should == ['_id']
|
390
390
|
doc.keys.keys.sort.should == ['_id', 'age', 'name']
|
391
391
|
end
|
392
392
|
|
393
|
-
should
|
393
|
+
it "should return all keys" do
|
394
394
|
doc = @document.new(:name => 'string', :age => nil)
|
395
395
|
doc.keys.keys.sort.should == ['_id', 'age', 'name']
|
396
396
|
doc.to_mongo.keys.sort.should == ['_id','name']
|
@@ -401,32 +401,32 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
401
401
|
|
402
402
|
context "key shorcut access" do
|
403
403
|
context "[]" do
|
404
|
-
should
|
404
|
+
it "should work when key found" do
|
405
405
|
doc = @document.new(:name => 'string')
|
406
406
|
doc[:name].should == 'string'
|
407
407
|
end
|
408
408
|
|
409
|
-
should
|
409
|
+
it "should return nil when not found" do
|
410
410
|
doc = @document.new(:name => 'string')
|
411
411
|
doc[:not_here].should be_nil
|
412
412
|
end
|
413
413
|
end
|
414
414
|
|
415
415
|
context "[]=" do
|
416
|
-
should
|
416
|
+
it "should write key value for existing key" do
|
417
417
|
doc = @document.new
|
418
418
|
doc[:name] = 'string'
|
419
419
|
doc[:name].should == 'string'
|
420
420
|
end
|
421
421
|
|
422
|
-
should
|
422
|
+
it "should create key and write value for missing key" do
|
423
423
|
doc = @document.new
|
424
424
|
doc[:foo] = 'string'
|
425
425
|
doc.class.keys.include?('foo').should be_true
|
426
426
|
doc[:foo].should == 'string'
|
427
427
|
end
|
428
428
|
|
429
|
-
should
|
429
|
+
it "should share the new key with the class" do
|
430
430
|
doc = @document.new
|
431
431
|
doc[:foo] = 'string'
|
432
432
|
@document.keys.should include('foo')
|
@@ -435,17 +435,17 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
435
435
|
end
|
436
436
|
|
437
437
|
context "reading a key" do
|
438
|
-
should
|
438
|
+
it "should work for defined keys" do
|
439
439
|
doc = @document.new(:name => 'string')
|
440
440
|
doc.name.should == 'string'
|
441
441
|
end
|
442
442
|
|
443
|
-
should
|
443
|
+
it "should raise no method error for undefined keys" do
|
444
444
|
doc = @document.new
|
445
445
|
lambda { doc.fart }.should raise_error(NoMethodError)
|
446
446
|
end
|
447
447
|
|
448
|
-
should
|
448
|
+
it "should be accessible for use in the model" do
|
449
449
|
@document.class_eval do
|
450
450
|
def name_and_age
|
451
451
|
"#{self[:name]} (#{self[:age]})"
|
@@ -456,7 +456,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
456
456
|
doc.name_and_age.should == 'John (27)'
|
457
457
|
end
|
458
458
|
|
459
|
-
should
|
459
|
+
it "should set instance variable" do
|
460
460
|
@document.key :foo, Array
|
461
461
|
doc = @document.new
|
462
462
|
doc.instance_variable_get("@foo").should be_nil
|
@@ -464,7 +464,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
464
464
|
doc.instance_variable_get("@foo").should == []
|
465
465
|
end
|
466
466
|
|
467
|
-
should
|
467
|
+
it "should be overrideable by modules" do
|
468
468
|
@document = Doc do
|
469
469
|
key :other_child, String
|
470
470
|
end
|
@@ -480,42 +480,36 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
480
480
|
end
|
481
481
|
|
482
482
|
context "reading a key before typcasting" do
|
483
|
-
should
|
483
|
+
it "should work for defined keys" do
|
484
484
|
doc = @document.new(:name => 12)
|
485
485
|
doc.name_before_type_cast.should == 12
|
486
486
|
end
|
487
487
|
|
488
|
-
should
|
488
|
+
it "should raise no method error for undefined keys" do
|
489
489
|
doc = @document.new
|
490
490
|
lambda { doc.foo_before_type_cast }.should raise_error(NoMethodError)
|
491
491
|
end
|
492
492
|
|
493
|
-
should
|
494
|
-
@document.class_eval do
|
495
|
-
def untypcasted_name
|
496
|
-
read_key_before_type_cast(:name)
|
497
|
-
end
|
498
|
-
end
|
499
|
-
|
493
|
+
it "should be accessible for use in a document" do
|
500
494
|
doc = @document.new(:name => 12)
|
501
495
|
doc.name.should == '12'
|
502
|
-
doc.
|
496
|
+
doc.name_before_type_cast.should == 12
|
503
497
|
end
|
504
498
|
end
|
505
499
|
|
506
500
|
context "writing a key" do
|
507
|
-
should
|
501
|
+
it "should work for defined keys" do
|
508
502
|
doc = @document.new
|
509
503
|
doc.name = 'John'
|
510
504
|
doc.name.should == 'John'
|
511
505
|
end
|
512
506
|
|
513
|
-
should
|
507
|
+
it "should raise no method error for undefined keys" do
|
514
508
|
doc = @document.new
|
515
509
|
lambda { doc.fart = 'poof!' }.should raise_error(NoMethodError)
|
516
510
|
end
|
517
511
|
|
518
|
-
should
|
512
|
+
it "should type cast value" do
|
519
513
|
doc = @document.new
|
520
514
|
doc.name = 1234
|
521
515
|
doc.name.should == '1234'
|
@@ -523,7 +517,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
523
517
|
doc.age.should == 21
|
524
518
|
end
|
525
519
|
|
526
|
-
should
|
520
|
+
it "should be accessible for use in the model" do
|
527
521
|
@document.class_eval do
|
528
522
|
def name_and_age=(new_value)
|
529
523
|
new_value.match(/([^\(\s]+) \((.*)\)/)
|
@@ -538,7 +532,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
538
532
|
doc.age.should == 62
|
539
533
|
end
|
540
534
|
|
541
|
-
should
|
535
|
+
it "should be overrideable by modules" do
|
542
536
|
@document = Doc do
|
543
537
|
key :other_child, String
|
544
538
|
end
|
@@ -554,63 +548,63 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
554
548
|
end # writing a key
|
555
549
|
|
556
550
|
context "checking if a keys value is present" do
|
557
|
-
should
|
551
|
+
it "should work for defined keys" do
|
558
552
|
doc = @document.new
|
559
553
|
doc.name?.should be_false
|
560
554
|
doc.name = 'John'
|
561
555
|
doc.name?.should be_true
|
562
556
|
end
|
563
557
|
|
564
|
-
should
|
558
|
+
it "should raise no method error for undefined keys" do
|
565
559
|
doc = @document.new
|
566
560
|
lambda { doc.fart? }.should raise_error(NoMethodError)
|
567
561
|
end
|
568
562
|
end
|
569
563
|
|
570
564
|
context "equality" do
|
571
|
-
|
565
|
+
before do
|
572
566
|
@oid = BSON::ObjectId.new
|
573
567
|
end
|
574
568
|
|
575
|
-
should
|
569
|
+
it "should delegate hash to _id" do
|
576
570
|
doc = @document.new
|
577
571
|
doc.hash.should == doc._id.hash
|
578
572
|
end
|
579
573
|
|
580
|
-
should
|
574
|
+
it "should delegate eql to ==" do
|
581
575
|
doc = @document.new
|
582
576
|
other = @document.new
|
583
577
|
doc.eql?(other).should == (doc == other)
|
584
578
|
doc.eql?(doc).should == (doc == doc)
|
585
579
|
end
|
586
580
|
|
587
|
-
should
|
581
|
+
it "should know if same object as another" do
|
588
582
|
doc = @document.new
|
589
583
|
doc.should equal(doc)
|
590
584
|
doc.should_not equal(@document.new)
|
591
585
|
end
|
592
586
|
|
593
|
-
should
|
587
|
+
it "should allow set operations on array of documents" do
|
594
588
|
doc = @document.new
|
595
589
|
([doc] & [doc]).should == [doc]
|
596
590
|
end
|
597
591
|
|
598
|
-
should
|
592
|
+
it "should be equal if id and class are the same" do
|
599
593
|
(@document.new('_id' => @oid) == @document.new('_id' => @oid)).should be_true
|
600
594
|
end
|
601
595
|
|
602
|
-
should
|
596
|
+
it "should not be equal if class same but id different" do
|
603
597
|
(@document.new('_id' => @oid) == @document.new('_id' => BSON::ObjectId.new)).should be_false
|
604
598
|
end
|
605
599
|
|
606
|
-
should
|
600
|
+
it "should not be equal if id same but class different" do
|
607
601
|
another_document = Doc()
|
608
602
|
(@document.new('_id' => @oid) == another_document.new('_id' => @oid)).should be_false
|
609
603
|
end
|
610
604
|
end
|
611
605
|
|
612
606
|
context "reading keys with default values" do
|
613
|
-
|
607
|
+
before do
|
614
608
|
@document = EDoc do
|
615
609
|
key :name, String, :default => 'foo'
|
616
610
|
key :age, Integer, :default => 20
|
@@ -624,30 +618,30 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
624
618
|
@doc = @document.new
|
625
619
|
end
|
626
620
|
|
627
|
-
should
|
621
|
+
it "should work for strings" do
|
628
622
|
@doc.name.should == 'foo'
|
629
623
|
end
|
630
624
|
|
631
|
-
should
|
625
|
+
it "should work for integers" do
|
632
626
|
@doc.age.should == 20
|
633
627
|
end
|
634
628
|
|
635
|
-
should
|
629
|
+
it "should work for floats" do
|
636
630
|
@doc.net_worth.should == 100.00
|
637
631
|
end
|
638
632
|
|
639
|
-
should
|
633
|
+
it "should work for booleans" do
|
640
634
|
@doc.active.should == true
|
641
635
|
@doc.smart.should == false
|
642
636
|
end
|
643
637
|
|
644
|
-
should
|
638
|
+
it "should work for arrays" do
|
645
639
|
@doc.skills.should == [1]
|
646
640
|
@doc.skills << 2
|
647
641
|
@doc.skills.should == [1, 2]
|
648
642
|
end
|
649
643
|
|
650
|
-
should
|
644
|
+
it "should work for hashes" do
|
651
645
|
@doc.options['foo'].should == 'bar'
|
652
646
|
@doc.options['baz'] = 'wick'
|
653
647
|
@doc.options['baz'].should == 'wick'
|
@@ -655,25 +649,25 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
655
649
|
end
|
656
650
|
|
657
651
|
context "#save!" do
|
658
|
-
|
652
|
+
before do
|
659
653
|
@root = DocWithAValidation.create(:name => "Root")
|
660
654
|
@doc = @root.e_doc_with_a_validations.build :name => "Embedded"
|
661
655
|
end
|
662
656
|
|
663
|
-
|
657
|
+
it "should should save when valid" do
|
664
658
|
@doc.save!
|
665
659
|
@root.reload.e_doc_with_a_validations.first.should == @doc
|
666
660
|
end
|
667
661
|
|
668
|
-
|
662
|
+
it "should should raise errors when invalid" do
|
669
663
|
@doc.name = ''
|
670
|
-
|
664
|
+
expect { @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
|
671
665
|
end
|
672
666
|
|
673
|
-
|
667
|
+
it "should should raise errors when root document is invalid" do
|
674
668
|
@root.name = ''
|
675
669
|
@root.save(:validate => false)
|
676
|
-
|
670
|
+
expect{ @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
|
677
671
|
end
|
678
672
|
end
|
679
673
|
end # instance of a embedded document
|