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.
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,4 +1,4 @@
1
- require 'test_helper'
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
- class EmbeddedDocumentTest < Test::Unit::TestCase
13
+ describe "EmbeddedDocument" do
14
14
  context "EmbeddedDocuments" do
15
- setup do
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
- teardown do
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
- setup do
60
+ before do
61
61
  @klass = EDoc()
62
62
  end
63
63
 
64
- should "add _id key" do
64
+ it "should add _id key" do
65
65
  @klass.keys['_id'].should_not be_nil
66
66
  end
67
67
 
68
- should "know it is using object id" do
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 "know it is not using object id if _id type is changed" do
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 "include logger" do
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 "return false for embeddable" do
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
- setup { @klass = EDoc() }
90
+ before { @klass = EDoc() }
91
91
 
92
- should "be nil if nil" do
92
+ it "should be nil if nil" do
93
93
  @klass.to_mongo(nil).should be_nil
94
94
  end
95
95
 
96
- should "convert to_mongo for other values" do
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
- setup { @klass = EDoc() }
105
+ before { @klass = EDoc() }
106
106
 
107
- should "be nil if nil" do
107
+ it "should be nil if nil" do
108
108
  @klass.from_mongo(nil).should be_nil
109
109
  end
110
110
 
111
- should "be instance if instance of class" do
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 "be instance if hash of attributes" do
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
- setup do
124
+ before do
125
125
  @document = EDoc()
126
126
  end
127
127
 
128
- should "work with name" do
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 "work with name and type" do
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 "work with name, type and options" do
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 "work with name and options" do
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 "be tracked per document" do
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 "be redefinable" do
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 "create reader method" do
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 "create reader before type cast method" do
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 "create writer method" do
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 "create boolean method" do
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 "be inherited" do
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 "propogate to descendants if key added after class definition" do
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 "not add anonymous objects to the ancestor tree" do
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 "not include descendant keys" do
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 "default to an empty array" do
218
+ it "should default to an empty array" do
219
219
  Child.descendants.should == []
220
220
  end
221
221
 
222
- should "be recorded" do
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
- setup do
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 "respond to cache_key" do
239
+ it "should respond to cache_key" do
240
240
  @document.new.should respond_to(:cache_key)
241
241
  end
242
242
 
243
- should "have access to class logger" do
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 "automatically have an _id key" do
249
+ it "should automatically have an _id key" do
250
250
  @document.keys.keys.should include('_id')
251
251
  end
252
252
 
253
- should "create id during initialization" do
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 "have id method returns _id" do
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 "convert string object id to mongo object id when assigning id with _id object id type" do
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 "default to nil" do
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 "set _root_document when setting _parent_document" do
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 "set _root_document when setting _parent_document on embedded many" do
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 "accept a hash that sets keys and values" do
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 "be able to assign keys dynamically" do
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 "not throw error if initialized with nil" do
311
- assert_nothing_raised { @document.new(nil) }
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
- setup do
316
+ before do
317
317
  @klass = EDoc('FooBar') { key :_type, String }
318
318
  end
319
319
 
320
- should "set _type to class name" do
320
+ it "should set _type to class name" do
321
321
  @klass.new._type.should == 'FooBar'
322
322
  end
323
323
 
324
- should "ignore _type attribute and always use class" do
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 "update values for keys provided" do
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 "not update values for keys that were not provided" do
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 "work with pre-defined methods" do
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 "type cast key values" do
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 "default to hash with all keys" do
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 "return all keys with values" do
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 "have indifferent access" do
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 "default to hash with _id key" do
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 "return all keys" do
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 "work when key found" do
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 "return nil when not found" do
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 "write key value for existing key" do
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 "create key and write value for missing key" do
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 "share the new key with the class" do
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 "work for defined keys" do
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 "raise no method error for undefined keys" do
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 "be accessible for use in the model" do
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 "set instance variable" do
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 "be overrideable by modules" do
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 "work for defined keys" do
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 "raise no method error for undefined keys" do
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 "be accessible for use in a document" do
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.untypcasted_name.should == 12
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 "work for defined keys" do
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 "raise no method error for undefined keys" do
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 "type cast value" do
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 "be accessible for use in the model" do
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 "be overrideable by modules" do
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 "work for defined keys" do
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 "raise no method error for undefined keys" do
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
- setup do
565
+ before do
572
566
  @oid = BSON::ObjectId.new
573
567
  end
574
568
 
575
- should "delegate hash to _id" do
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 "delegate eql to ==" do
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 "know if same object as another" do
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 "allow set operations on array of documents" do
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 "be equal if id and class are the same" do
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 "not be equal if class same but id different" do
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 "not be equal if id same but class different" do
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
- setup do
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 "work for strings" do
621
+ it "should work for strings" do
628
622
  @doc.name.should == 'foo'
629
623
  end
630
624
 
631
- should "work for integers" do
625
+ it "should work for integers" do
632
626
  @doc.age.should == 20
633
627
  end
634
628
 
635
- should "work for floats" do
629
+ it "should work for floats" do
636
630
  @doc.net_worth.should == 100.00
637
631
  end
638
632
 
639
- should "work for booleans" do
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 "work for arrays" do
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 "work for hashes" do
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
- setup do
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
- should "should save when valid" do
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
- should "should raise errors when invalid" do
662
+ it "should should raise errors when invalid" do
669
663
  @doc.name = ''
670
- lambda{ @doc.save! }.should raise_error(MongoMapper::DocumentNotValid, "Validation failed: Name can't be empty")
664
+ expect { @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
671
665
  end
672
666
 
673
- should "should raise errors when root document is invalid" do
667
+ it "should should raise errors when root document is invalid" do
674
668
  @root.name = ''
675
669
  @root.save(:validate => false)
676
- lambda{ @doc.save! }.should raise_error(MongoMapper::DocumentNotValid, "Foo")
670
+ expect{ @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
677
671
  end
678
672
  end
679
673
  end # instance of a embedded document