mongo_mapper 0.12.0 → 0.13.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +35 -13
  3. data/bin/mmconsole +1 -1
  4. data/lib/mongo_mapper.rb +4 -0
  5. data/lib/mongo_mapper/connection.rb +17 -6
  6. data/lib/mongo_mapper/document.rb +1 -0
  7. data/lib/mongo_mapper/exceptions.rb +4 -1
  8. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  9. data/lib/mongo_mapper/extensions/boolean.rb +20 -23
  10. data/lib/mongo_mapper/extensions/date.rb +3 -3
  11. data/lib/mongo_mapper/extensions/integer.rb +5 -1
  12. data/lib/mongo_mapper/extensions/kernel.rb +2 -0
  13. data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
  14. data/lib/mongo_mapper/extensions/string.rb +2 -2
  15. data/lib/mongo_mapper/extensions/time.rb +7 -5
  16. data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
  17. data/lib/mongo_mapper/plugins.rb +1 -1
  18. data/lib/mongo_mapper/plugins/associations.rb +11 -5
  19. data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
  20. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
  22. data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
  25. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
  26. data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
  27. data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
  28. data/lib/mongo_mapper/plugins/clone.rb +4 -2
  29. data/lib/mongo_mapper/plugins/dirty.rb +22 -21
  30. data/lib/mongo_mapper/plugins/document.rb +4 -4
  31. data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
  32. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
  33. data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
  34. data/lib/mongo_mapper/plugins/keys.rb +133 -54
  35. data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
  36. data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
  37. data/lib/mongo_mapper/plugins/persistence.rb +15 -5
  38. data/lib/mongo_mapper/plugins/querying.rb +15 -40
  39. data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
  40. data/lib/mongo_mapper/plugins/rails.rb +22 -2
  41. data/lib/mongo_mapper/plugins/safe.rb +8 -5
  42. data/lib/mongo_mapper/plugins/sci.rb +26 -4
  43. data/lib/mongo_mapper/plugins/scopes.rb +5 -4
  44. data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
  45. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  46. data/lib/mongo_mapper/utils.rb +12 -0
  47. data/lib/mongo_mapper/version.rb +1 -1
  48. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
  49. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
  50. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
  51. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
  52. data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
  53. data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
  54. data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
  55. data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
  56. data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
  57. data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
  58. data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
  59. data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
  60. data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
  61. data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
  62. data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
  63. data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
  64. data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
  65. data/spec/functional/associations_spec.rb +48 -0
  66. data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
  67. data/spec/functional/caching_spec.rb +75 -0
  68. data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
  69. data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
  70. data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
  71. data/spec/functional/dumpable_spec.rb +24 -0
  72. data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
  73. data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
  74. data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
  75. data/spec/functional/extensions_spec.rb +16 -0
  76. data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
  77. data/spec/functional/indexes_spec.rb +48 -0
  78. data/spec/functional/keys_spec.rb +224 -0
  79. data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
  80. data/spec/functional/modifiers_spec.rb +550 -0
  81. data/spec/functional/pagination_spec.rb +89 -0
  82. data/spec/functional/protected_spec.rb +199 -0
  83. data/spec/functional/querying_spec.rb +1003 -0
  84. data/spec/functional/rails_spec.rb +55 -0
  85. data/spec/functional/safe_spec.rb +163 -0
  86. data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
  87. data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
  88. data/spec/functional/timestamps_spec.rb +97 -0
  89. data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
  90. data/spec/functional/userstamps_spec.rb +46 -0
  91. data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
  92. data/spec/spec_helper.rb +81 -0
  93. data/spec/support/matchers.rb +24 -0
  94. data/{test → spec/support}/models.rb +1 -6
  95. data/spec/unit/associations/base_spec.rb +146 -0
  96. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  97. data/spec/unit/associations/many_association_spec.rb +64 -0
  98. data/spec/unit/associations/one_association_spec.rb +48 -0
  99. data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
  100. data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
  101. data/spec/unit/config_generator_spec.rb +24 -0
  102. data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
  103. data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
  104. data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
  105. data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
  106. data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
  107. data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
  108. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  109. data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
  110. data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
  111. data/spec/unit/keys_spec.rb +155 -0
  112. data/spec/unit/model_generator_spec.rb +47 -0
  113. data/spec/unit/mongo_mapper_spec.rb +184 -0
  114. data/spec/unit/pagination_spec.rb +11 -0
  115. data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
  116. data/spec/unit/rails_compatibility_spec.rb +40 -0
  117. data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
  118. data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
  119. data/spec/unit/serialization_spec.rb +169 -0
  120. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  121. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  122. data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
  123. data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
  124. data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
  125. metadata +199 -179
  126. data/test/_NOTE_ON_TESTING +0 -1
  127. data/test/functional/test_associations.rb +0 -46
  128. data/test/functional/test_caching.rb +0 -77
  129. data/test/functional/test_indexes.rb +0 -50
  130. data/test/functional/test_modifiers.rb +0 -537
  131. data/test/functional/test_pagination.rb +0 -91
  132. data/test/functional/test_protected.rb +0 -201
  133. data/test/functional/test_querying.rb +0 -935
  134. data/test/functional/test_safe.rb +0 -76
  135. data/test/functional/test_timestamps.rb +0 -62
  136. data/test/functional/test_userstamps.rb +0 -44
  137. data/test/support/railtie.rb +0 -4
  138. data/test/support/railtie/autoloaded.rb +0 -2
  139. data/test/support/railtie/not_autoloaded.rb +0 -3
  140. data/test/support/railtie/parent.rb +0 -3
  141. data/test/test_active_model_lint.rb +0 -18
  142. data/test/test_helper.rb +0 -93
  143. data/test/unit/associations/test_base.rb +0 -146
  144. data/test/unit/associations/test_belongs_to_association.rb +0 -29
  145. data/test/unit/associations/test_many_association.rb +0 -63
  146. data/test/unit/associations/test_one_association.rb +0 -47
  147. data/test/unit/serializers/test_json_serializer.rb +0 -216
  148. data/test/unit/serializers/test_xml_serializer.rb +0 -196
  149. data/test/unit/test_identity_map_middleware.rb +0 -132
  150. data/test/unit/test_keys.rb +0 -65
  151. data/test/unit/test_mongo_mapper.rb +0 -157
  152. data/test/unit/test_pagination.rb +0 -11
  153. data/test/unit/test_rails_compatibility.rb +0 -38
  154. data/test/unit/test_serialization.rb +0 -166
@@ -1,24 +1,23 @@
1
- require 'test_helper'
2
- require 'models'
1
+ require 'spec_helper'
3
2
 
4
- class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
5
- def setup
3
+ describe "OneEmbeddedPolymorhpicProxy" do
4
+ before do
6
5
  @post_class = Doc('Post') do
7
6
  key :title, String
8
7
  end
9
8
  end
10
9
 
11
- should "default to nil" do
10
+ it "should default to nil" do
12
11
  @post_class.one :author, :polymorphic => true, :class => Robot
13
12
  @post_class.new.author.should be_nil
14
13
  end
15
14
 
16
- should "return nil instead of a proxy" do
15
+ it "should return nil instead of a proxy" do
17
16
  @post_class.one :author, :polymorphic => true, :class => Robot
18
17
  nil.should === @post_class.new.author
19
18
  end
20
19
 
21
- should "be able to build" do
20
+ it "should be able to build" do
22
21
  @post_class.one :author, :polymorphic => true, :class => Robot
23
22
  post = @post_class.create
24
23
  author = post.build_author(:serial_number => "1B")
@@ -29,29 +28,29 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
29
28
  post.author.post.should == post
30
29
  end
31
30
 
32
- should "allow assignment of associated document using a hash" do
31
+ it "should allow assignment of associated document using a hash" do
33
32
  @post_class.one :author, :polymorphic => :true, :class => Robot
34
-
33
+
35
34
  post = @post_class.new('author' => { 'name' => 'Frank', '_type' => 'Human' })
36
35
  post.author.name.should == 'Frank'
37
36
  post.author.class.should == Human
38
-
37
+
39
38
  post.save.should be_true
40
39
  post.reload
41
-
40
+
42
41
  post.author.name.should == 'Frank'
43
42
  post.author.class.should == Human
44
43
  end
45
44
 
46
45
  context "replacing the association" do
47
46
  context "with an object" do
48
- setup do
47
+ before do
49
48
  @post_class.one :author, :polymorphic => true, :class => Robot
50
49
  @post = @post_class.create
51
50
  @human = Human.new(:name => 'Frank')
52
51
  end
53
52
 
54
- should "work" do
53
+ it "should work" do
55
54
  @post.author = @human
56
55
  @post.save
57
56
  @post.reload
@@ -65,7 +64,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
65
64
  @post.author.should == new_human
66
65
  end
67
66
 
68
- should "generate a new proxy instead of modifying the existing one" do
67
+ it "should generate a new proxy instead of modifying the existing one" do
69
68
  @post.author = @human
70
69
  @post.save
71
70
  @post.reload
@@ -82,19 +81,19 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
82
81
  original_author.name.should == 'Frank'
83
82
  end
84
83
 
85
- should "assign _type" do
84
+ it "should assign _type" do
86
85
  @post.author = @human
87
86
  @post.author._type.should == "Human"
88
87
  end
89
88
  end
90
89
 
91
90
  context "with a Hash" do
92
- setup do
91
+ before do
93
92
  @post_class.one :author, :polymorphic => true, :class => Robot
94
93
  @post = @post_class.create
95
94
  end
96
95
 
97
- should "convert to an object of the class and work" do
96
+ it "should convert to an object of the class and work" do
98
97
  @post.author = {'serial_number' => '1B'}
99
98
  @post.save
100
99
  @post.reload
@@ -106,7 +105,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
106
105
  @post.author.serial_number.should == '2C'
107
106
  end
108
107
 
109
- should "convert to an object of _type if given" do
108
+ it "should convert to an object of _type if given" do
110
109
  @post.author = {'name' => 'Frank', '_type' => 'Human'}
111
110
  @post.author.name.should == 'Frank'
112
111
  @post.author.class.should == Human
@@ -117,7 +116,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
117
116
  @post.author.class.should == Human
118
117
  end
119
118
 
120
- should "assign _type" do
119
+ it "should assign _type" do
121
120
  @post.author = {'name' => 'Frank', '_type' => 'Human'}
122
121
  @post.save
123
122
  @post.reload
@@ -126,7 +125,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
126
125
  end
127
126
  end
128
127
 
129
- should "unset the association" do
128
+ it "should unset the association" do
130
129
  @post_class.one :author, :polymorphic => true, :class => Robot
131
130
  post = @post_class.create
132
131
  human = Human.new
@@ -136,7 +135,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
136
135
  post.author.should == nil
137
136
  end
138
137
 
139
- should "set modularized associated models correctly" do
138
+ it "should set modularized associated models correctly" do
140
139
  @post_class.one :author, :polymorphic => true, :class => Robot
141
140
 
142
141
  post = @post_class.new('author' => {'_type' => 'TrModels::Ambulance', 'license_plate' => 'GGG123', 'icu' => true})
@@ -152,7 +151,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
152
151
  post.author.icu.should be_true
153
152
  end
154
153
 
155
- should "not have problem loading root document if embedded one is nil" do
154
+ it "should not have problem loading root document if embedded one is nil" do
156
155
  @post_class.one :author, :polymorphic => true, :class => Robot
157
156
  post = @post_class.create
158
157
 
@@ -161,7 +160,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
161
160
  }.should_not raise_error
162
161
  end
163
162
 
164
- should "load the parent and root documents for nested embedded documents" do
163
+ it "should load the parent and root documents for nested embedded documents" do
165
164
  @address_class = EDoc('Address') do
166
165
  key :city, String
167
166
  key :state, String
@@ -169,31 +168,31 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
169
168
  @author_class = EDoc('EmbeddedAuthor')
170
169
  @author_class.one :address, :polymorphic => true, :class => @address_class
171
170
  @post_class.one :author, :polymorphic => true, :class => @author_class
172
-
171
+
173
172
  post = @post_class.create(:title => 'Post Title', :author => { :name => 'Frank', :address => { :city => 'Boston', :state => 'MA' } })
174
-
173
+
175
174
  post.author.address._parent_document.should == post.author
176
175
  post.author.address._root_document.should == post
177
176
  end
178
-
179
- should "have boolean method for testing presence" do
177
+
178
+ it "should have boolean method for testing presence" do
180
179
  @post_class.one :author, :polymorphic => true, :class => Robot
181
-
180
+
182
181
  post = @post_class.new
183
182
  post.author?.should be_false
184
-
183
+
185
184
  post.author = Human.new(:name => 'Frank')
186
185
  post.author?.should be_true
187
186
  end
188
187
 
189
- should "initialize id for nested embedded document created from hash" do
188
+ it "should initialize id for nested embedded document created from hash" do
190
189
  @address_class = EDoc('Address') do
191
190
  key :city, String
192
191
  key :state, String
193
192
  end
194
193
  @author_class = EDoc('EmbeddedAuthor')
195
194
  @author_class.one :address, :polymorphic => true, :class => @address_class
196
- @post_class.one :author, :polymorphic => true, :class => @author_class
195
+ @post_class.one :author, :polymorphic => true, :class => @author_class
197
196
 
198
197
  post = @post_class.create(:title => 'Post Title', :author => {
199
198
  :name => 'Frank',
@@ -202,7 +201,7 @@ class OneEmbeddedPolymorhpicProxyTest < Test::Unit::TestCase
202
201
  :state => 'MA'
203
202
  }
204
203
  })
205
-
204
+
206
205
  post.author.address.id.should_not be_nil
207
206
  end
208
207
  end
@@ -1,7 +1,7 @@
1
- require 'test_helper'
1
+ require 'spec_helper'
2
2
 
3
- class OneEmbeddedProxyTest < Test::Unit::TestCase
4
- def setup
3
+ describe "OneEmbeddedProxy" do
4
+ before do
5
5
  @post_class = Doc('Post') do
6
6
  key :title, String
7
7
  end
@@ -11,12 +11,12 @@ class OneEmbeddedProxyTest < Test::Unit::TestCase
11
11
  end
12
12
  end
13
13
 
14
- should "default to nil" do
14
+ it "should default to nil" do
15
15
  @post_class.one :author, :class => @author_class
16
16
  @post_class.new.author.should be_nil
17
17
  end
18
18
 
19
- should "be able to build" do
19
+ it "should be able to build" do
20
20
  @post_class.one :author, :class => @author_class
21
21
 
22
22
  post = @post_class.create
@@ -28,7 +28,7 @@ class OneEmbeddedProxyTest < Test::Unit::TestCase
28
28
  post.author.post.should == post
29
29
  end
30
30
 
31
- should "be able to replace the association" do
31
+ it "should be able to replace the association" do
32
32
  @post_class.one :author, :class => @author_class
33
33
 
34
34
  post = @post_class.new
@@ -45,7 +45,7 @@ class OneEmbeddedProxyTest < Test::Unit::TestCase
45
45
  post.author.should == new_author
46
46
  end
47
47
 
48
- should "not have problem loading root document if embedded one is nil" do
48
+ it "should not have problem loading root document if embedded one is nil" do
49
49
  @post_class.one :author, :class => @author_class
50
50
  post = @post_class.create
51
51
 
@@ -54,7 +54,7 @@ class OneEmbeddedProxyTest < Test::Unit::TestCase
54
54
  }.should_not raise_error
55
55
  end
56
56
 
57
- should "load the parent and root documents for nested embedded documents" do
57
+ it "should load the parent and root documents for nested embedded documents" do
58
58
  @address_class = EDoc('Address') do
59
59
  key :city, String
60
60
  key :state, String
@@ -68,7 +68,7 @@ class OneEmbeddedProxyTest < Test::Unit::TestCase
68
68
  post.author.address._root_document.should == post
69
69
  end
70
70
 
71
- should "have boolean method for testing presence" do
71
+ it "should have boolean method for testing presence" do
72
72
  @post_class.one :author, :class => @author_class
73
73
 
74
74
  post = @post_class.new
@@ -78,7 +78,7 @@ class OneEmbeddedProxyTest < Test::Unit::TestCase
78
78
  post.author?.should be_true
79
79
  end
80
80
 
81
- should "initialize id for nested embedded document created from hash" do
81
+ it "should initialize id for nested embedded document created from hash" do
82
82
  @address_class = EDoc('Address') do
83
83
  key :city, String
84
84
  key :state, String
@@ -1,25 +1,25 @@
1
- require 'test_helper'
2
- require 'models'
1
+ require 'spec_helper'
3
2
 
4
- class OneProxyTest < Test::Unit::TestCase
5
- def setup
3
+ describe "OneProxy" do
4
+ before do
6
5
  @post_class = Doc('Post')
7
6
  @author_class = Doc do
7
+ key :name, String
8
8
  key :post_id, ObjectId
9
9
  end
10
10
  end
11
11
 
12
- should "default to nil" do
12
+ it "should default to nil" do
13
13
  @post_class.one :author, :class => @author_class
14
14
  @post_class.new.author.nil?.should be_true
15
15
  end
16
16
 
17
- should "return nil instead of a proxy" do
17
+ it "should return nil instead of a proxy" do
18
18
  @post_class.one :author, :class => @author_class
19
19
  nil.should === @post_class.new.author
20
20
  end
21
21
 
22
- should "allow assignment of associated document using a hash" do
22
+ it "should allow assignment of associated document using a hash" do
23
23
  @post_class.one :author, :class => @author_class
24
24
 
25
25
  post = @post_class.new('author' => { 'name' => 'Frank' })
@@ -33,7 +33,7 @@ class OneProxyTest < Test::Unit::TestCase
33
33
 
34
34
  context "replacing the association" do
35
35
  context "with an object of the class" do
36
- should "work" do
36
+ it "should work" do
37
37
  @post_class.one :author, :class => @author_class
38
38
 
39
39
  post = @post_class.new
@@ -49,7 +49,7 @@ class OneProxyTest < Test::Unit::TestCase
49
49
  post.author.should == new_author
50
50
  end
51
51
 
52
- should "generate a new proxy instead of modifying the existing one" do
52
+ it "should generate a new proxy instead of modifying the existing one" do
53
53
  @post_class.one :author, :class => @author_class
54
54
 
55
55
  post = @post_class.new
@@ -71,7 +71,7 @@ class OneProxyTest < Test::Unit::TestCase
71
71
  end
72
72
 
73
73
  context "with a Hash" do
74
- should "convert to an object of the class and work" do
74
+ it "should convert to an object of the class and work" do
75
75
  @post_class.one :author, :class => @author_class
76
76
 
77
77
  post = @post_class.new
@@ -85,35 +85,35 @@ class OneProxyTest < Test::Unit::TestCase
85
85
  post.author.name.should == 'Emily'
86
86
  end
87
87
  end
88
-
88
+
89
89
  context "with :dependent" do
90
90
  context "=> delete" do
91
- setup do
91
+ before do
92
92
  @post_class.one :author, :class => @author_class, :dependent => :delete
93
93
 
94
94
  @post = @post_class.create
95
95
  @author = @author_class.new
96
96
  @post.author = @author
97
97
  end
98
-
99
- should "call delete on the existing document" do
100
- @author_class.any_instance.expects(:delete).once
98
+
99
+ it "should call delete on the existing document" do
100
+ @author_class.any_instance.should_receive(:delete).once
101
101
  @post.author = @author_class.new
102
102
  end
103
-
104
- should "remove the existing document from the database" do
103
+
104
+ it "should remove the existing document from the database" do
105
105
  @post.author = @author_class.new
106
106
  lambda { @author.reload }.should raise_error(MongoMapper::DocumentNotFound)
107
107
  end
108
-
109
- should "do nothing if it's the same document" do
110
- @author_class.any_instance.expects(:delete).never
108
+
109
+ it "should do nothing if it's the same document" do
110
+ @author_class.any_instance.should_receive(:delete).never
111
111
  @post.author = @author
112
- end
112
+ end
113
113
  end
114
-
114
+
115
115
  context "=> destory" do
116
- setup do
116
+ before do
117
117
  @post_class.one :author, :class => @author_class, :dependent => :destroy
118
118
 
119
119
  @post = @post_class.create
@@ -121,32 +121,32 @@ class OneProxyTest < Test::Unit::TestCase
121
121
  @post.author = @author
122
122
  end
123
123
 
124
- should "call destroy the existing document" do
125
- @author_class.any_instance.expects(:destroy).once
124
+ it "should call destroy the existing document" do
125
+ @author_class.any_instance.should_receive(:destroy).once
126
126
  @post.author = @author_class.new
127
127
  end
128
-
129
- should "remove the existing document from the database" do
128
+
129
+ it "should remove the existing document from the database" do
130
130
  @post.author = @author_class.new
131
131
  lambda { @author.reload }.should raise_error(MongoMapper::DocumentNotFound)
132
132
  end
133
-
134
- should "do nothing if it's the same document" do
135
- @author_class.any_instance.expects(:destroy).never
133
+
134
+ it "should do nothing if it's the same document" do
135
+ @author_class.any_instance.should_receive(:destroy).never
136
136
  @post.author = @author
137
- end
137
+ end
138
138
  end
139
139
 
140
140
  context "=> nullify" do
141
- setup do
141
+ before do
142
142
  @post_class.one :author, :class => @author_class, :dependent => :nullify
143
143
 
144
144
  @post = @post_class.create
145
145
  @author = @author_class.new
146
146
  @post.author = @author
147
147
  end
148
-
149
- should "nullify the existing document" do
148
+
149
+ it "should nullify the existing document" do
150
150
  @author.reload
151
151
  @author.post_id.should == @post.id
152
152
 
@@ -155,16 +155,16 @@ class OneProxyTest < Test::Unit::TestCase
155
155
  @author.reload
156
156
  @author.post_id.should be_nil
157
157
  end
158
-
159
- should "work when it's the same document" do
158
+
159
+ it "should work when it's the same document" do
160
160
  old_author = @post.author
161
161
  @post.author = @author
162
162
  old_author.should == @post.author
163
- end
163
+ end
164
164
  end
165
-
165
+
166
166
  context "unspecified" do
167
- should "nullify the existing document" do
167
+ it "should nullify the existing document" do
168
168
  @post_class.one :author, :class => @author_class
169
169
 
170
170
  post = @post_class.create
@@ -180,30 +180,30 @@ class OneProxyTest < Test::Unit::TestCase
180
180
  end
181
181
  end
182
182
  end
183
-
183
+
184
184
  context "with nil" do
185
- setup do
185
+ before do
186
186
  @post_class.one :author, :class => @author_class
187
187
 
188
188
  @post = @post_class.new
189
- @author = @author_class.new(:name => 'Frank')
189
+ @author = @author_class.new(:name => 'Frank')
190
190
  @post.author = @author
191
191
  end
192
-
193
- should "nullify the existing document" do
192
+
193
+ it "should nullify the existing document" do
194
194
  @post.author = nil
195
195
  @author.reload
196
196
  @author.post_id.should be_nil
197
197
  end
198
198
 
199
- should "set the target to nil" do
199
+ it "should set the target to nil" do
200
200
  @post.author = nil
201
201
  @post.author.should == nil
202
202
  end
203
203
  end
204
204
  end
205
205
 
206
- should "have boolean method for testing presence" do
206
+ it "should have boolean method for testing presence" do
207
207
  @post_class.one :author, :class => @author_class
208
208
 
209
209
  post = @post_class.new
@@ -213,7 +213,7 @@ class OneProxyTest < Test::Unit::TestCase
213
213
  post.author?.should be_true
214
214
  end
215
215
 
216
- should "work with criteria" do
216
+ it "should work with criteria" do
217
217
  @post_class.one :primary_author, :class => @author_class, :primary => true
218
218
  @post_class.one :author, :class => @author_class, :primary => false
219
219
 
@@ -225,7 +225,7 @@ class OneProxyTest < Test::Unit::TestCase
225
225
  post.primary_author.should == primary
226
226
  end
227
227
 
228
- should "unset the association" do
228
+ it "should unset the association" do
229
229
  @post_class.one :author, :class => @author_class
230
230
  post = @post_class.create
231
231
  author = @author_class.create
@@ -234,10 +234,10 @@ class OneProxyTest < Test::Unit::TestCase
234
234
  post.author = nil
235
235
  post.author.nil?.should be_true
236
236
  end
237
-
237
+
238
238
  context "destroying parent with :dependent" do
239
239
  context "=> destroy" do
240
- setup do
240
+ before do
241
241
  @post_class.one :author, :class => @author_class, :dependent => :destroy
242
242
 
243
243
  @post = @post_class.create
@@ -245,12 +245,12 @@ class OneProxyTest < Test::Unit::TestCase
245
245
  @post.author = @author
246
246
  end
247
247
 
248
- should "should call destroy on the associated documents" do
249
- @author_class.any_instance.expects(:destroy).once
248
+ it "should should call destroy on the associated documents" do
249
+ @author_class.any_instance.should_receive(:destroy).once
250
250
  @post.destroy
251
251
  end
252
-
253
- should "should remove the associated documents" do
252
+
253
+ it "should should remove the associated documents" do
254
254
  @author_class.count.should == 1
255
255
  @post.destroy
256
256
  @post.author.should == nil
@@ -259,7 +259,7 @@ class OneProxyTest < Test::Unit::TestCase
259
259
  end
260
260
 
261
261
  context "=> delete" do
262
- setup do
262
+ before do
263
263
  @post_class.one :author, :class => @author_class, :dependent => :delete
264
264
 
265
265
  @post = @post_class.create
@@ -267,12 +267,12 @@ class OneProxyTest < Test::Unit::TestCase
267
267
  @post.author = @author
268
268
  end
269
269
 
270
- should "should call delete the associated documents" do
271
- @author_class.any_instance.expects(:delete).once
270
+ it "should should call delete the associated documents" do
271
+ @author_class.any_instance.should_receive(:delete).once
272
272
  @post.destroy
273
273
  end
274
274
 
275
- should "remove the associated documents" do
275
+ it "should remove the associated documents" do
276
276
  @author_class.count.should == 1
277
277
  @post.destroy
278
278
  @post.author.should == nil
@@ -281,7 +281,7 @@ class OneProxyTest < Test::Unit::TestCase
281
281
  end
282
282
 
283
283
  context "=> nullify" do
284
- should "should nullify the relationship but not destroy the associated document" do
284
+ it "should should nullify the relationship but not destroy the associated document" do
285
285
  @post_class.one :author, :class => @author_class, :dependent => :nullify
286
286
 
287
287
  post = @post_class.create
@@ -297,9 +297,9 @@ class OneProxyTest < Test::Unit::TestCase
297
297
  author.post_id.should == nil
298
298
  end
299
299
  end
300
-
300
+
301
301
  context "unspecified" do
302
- should "should nullify the relationship but not destroy the associated document" do
302
+ it "should should nullify the relationship but not destroy the associated document" do
303
303
  @post_class.one :author, :class => @author_class
304
304
 
305
305
  post = @post_class.create
@@ -316,65 +316,88 @@ class OneProxyTest < Test::Unit::TestCase
316
316
  end
317
317
  end
318
318
  end
319
-
320
319
 
321
- should "be able to build" do
322
- @post_class.one :author, :class => @author_class
323
-
324
- post = @post_class.create
325
- author = post.build_author(:name => 'John')
326
- post.author.should be_instance_of(@author_class)
327
- post.author.should be_new
328
- post.author.name.should == 'John'
329
- post.author.should == author
330
- post.author.post_id.should == post.id
331
- end
320
+ context "when building associations" do
321
+ before do
322
+ @post_class.one :author, :class => @author_class
323
+ end
324
+ let(:post) { @post_class.create }
325
+
326
+ context "#build" do
327
+ it "should work" do
328
+ author = post.build_author(:name => 'John')
329
+ post.author.should be_instance_of(@author_class)
330
+ post.author.should be_new
331
+ post.author.name.should == 'John'
332
+ post.author.should == author
333
+ post.author.post_id.should == post.id
334
+ end
332
335
 
333
- should "be able to create" do
334
- @post_class.one :author, :class => @author_class
336
+ it "should allow a block" do
337
+ author = post.build_author do |doc|
338
+ doc.name = "John"
339
+ end
340
+ author.name.should == "John"
341
+ end
342
+ end
335
343
 
336
- post = @post_class.create
337
- author = post.create_author(:name => 'John')
338
- post.author.should be_instance_of(@author_class)
339
- post.author.should_not be_new
340
- post.author.name.should == 'John'
341
- post.author.should == author
342
- post.author.post_id.should == post.id
343
- end
344
+ context "#create" do
345
+ it "should work" do
346
+ author = post.create_author(:name => 'John')
347
+ post.author.should be_instance_of(@author_class)
348
+ post.author.should_not be_new
349
+ post.author.name.should == 'John'
350
+ post.author.should == author
351
+ post.author.post_id.should == post.id
352
+ end
344
353
 
345
- context "#create!" do
346
- setup do
347
- @author_class.key :name, String, :required => true
348
- @post_class.one :author, :class => @author_class
354
+ it "should allow a block" do
355
+ author = post.create_author do |doc|
356
+ doc.name = "John"
357
+ end
358
+ author.name.should == "John"
359
+ end
349
360
  end
350
361
 
351
- should "raise exception if invalid" do
352
- post = @post_class.create
353
- assert_raises(MongoMapper::DocumentNotValid) do
354
- post.create_author!
362
+
363
+ context "#create!" do
364
+ before do
365
+ @author_class.key :name, String, :required => true
366
+ end
367
+
368
+ it "should raise exception if invalid" do
369
+ expect {
370
+ post.create_author!
371
+ }.to raise_error(MongoMapper::DocumentNotValid)
372
+ end
373
+
374
+ it "should work if valid" do
375
+ author = post.create_author!(:name => 'John')
376
+ post.author.should be_instance_of(@author_class)
377
+ post.author.should_not be_new
378
+ post.author.name.should == 'John'
379
+ post.author.should == author
380
+ post.author.post_id.should == post.id
355
381
  end
356
- end
357
382
 
358
- should "work if valid" do
359
- post = @post_class.create
360
- author = post.create_author!(:name => 'John')
361
- post.author.should be_instance_of(@author_class)
362
- post.author.should_not be_new
363
- post.author.name.should == 'John'
364
- post.author.should == author
365
- post.author.post_id.should == post.id
383
+ it "should accept a block" do
384
+ author = post.create_author! do |doc|
385
+ doc.name = "John"
386
+ end
387
+ author.name.should == "John"
388
+ end
366
389
  end
367
390
  end
368
391
 
369
392
  context "namespaced foreign keys" do
370
- setup do
393
+ before do
371
394
  News::Paper.one :article, :class_name => 'News::Article'
372
395
  News::Article.belongs_to :paper, :class_name => 'News::Paper'
373
396
 
374
397
  @paper = News::Paper.create
375
398
  end
376
399
 
377
- should "properly infer the foreign key" do
400
+ it "should properly infer the foreign key" do
378
401
  article = @paper.create_article
379
402
  article.should respond_to(:paper_id)
380
403
  article.paper_id.should == @paper.id