mongo_mapper 0.12.0 → 0.13.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.rdoc +35 -13
- data/bin/mmconsole +1 -1
- data/lib/mongo_mapper.rb +4 -0
- data/lib/mongo_mapper/connection.rb +17 -6
- data/lib/mongo_mapper/document.rb +1 -0
- data/lib/mongo_mapper/exceptions.rb +4 -1
- data/lib/mongo_mapper/extensions/binary.rb +1 -1
- data/lib/mongo_mapper/extensions/boolean.rb +20 -23
- data/lib/mongo_mapper/extensions/date.rb +3 -3
- data/lib/mongo_mapper/extensions/integer.rb +5 -1
- data/lib/mongo_mapper/extensions/kernel.rb +2 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
- data/lib/mongo_mapper/extensions/string.rb +2 -2
- data/lib/mongo_mapper/extensions/time.rb +7 -5
- data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
- data/lib/mongo_mapper/plugins.rb +1 -1
- data/lib/mongo_mapper/plugins/associations.rb +11 -5
- data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
- data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
- data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
- data/lib/mongo_mapper/plugins/clone.rb +4 -2
- data/lib/mongo_mapper/plugins/dirty.rb +22 -21
- data/lib/mongo_mapper/plugins/document.rb +4 -4
- data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
- data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
- data/lib/mongo_mapper/plugins/keys.rb +133 -54
- data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
- data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
- data/lib/mongo_mapper/plugins/persistence.rb +15 -5
- data/lib/mongo_mapper/plugins/querying.rb +15 -40
- data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
- data/lib/mongo_mapper/plugins/rails.rb +22 -2
- data/lib/mongo_mapper/plugins/safe.rb +8 -5
- data/lib/mongo_mapper/plugins/sci.rb +26 -4
- data/lib/mongo_mapper/plugins/scopes.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/utils.rb +12 -0
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
- data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
- data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
- data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
- data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
- data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
- data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
- data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
- data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
- data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
- data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
- data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
- data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
- data/spec/functional/associations_spec.rb +48 -0
- data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
- data/spec/functional/caching_spec.rb +75 -0
- data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
- data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
- data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
- data/spec/functional/dumpable_spec.rb +24 -0
- data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
- data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
- data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
- data/spec/functional/extensions_spec.rb +16 -0
- data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
- data/spec/functional/indexes_spec.rb +48 -0
- data/spec/functional/keys_spec.rb +224 -0
- data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
- data/spec/functional/modifiers_spec.rb +550 -0
- data/spec/functional/pagination_spec.rb +89 -0
- data/spec/functional/protected_spec.rb +199 -0
- data/spec/functional/querying_spec.rb +1003 -0
- data/spec/functional/rails_spec.rb +55 -0
- data/spec/functional/safe_spec.rb +163 -0
- data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
- data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
- data/spec/functional/timestamps_spec.rb +97 -0
- data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
- data/spec/functional/userstamps_spec.rb +46 -0
- data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
- data/spec/spec_helper.rb +81 -0
- data/spec/support/matchers.rb +24 -0
- data/{test → spec/support}/models.rb +1 -6
- data/spec/unit/associations/base_spec.rb +146 -0
- data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
- data/spec/unit/associations/many_association_spec.rb +64 -0
- data/spec/unit/associations/one_association_spec.rb +48 -0
- data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
- data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
- data/spec/unit/config_generator_spec.rb +24 -0
- data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
- data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
- data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
- data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
- data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
- data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
- data/spec/unit/identity_map_middleware_spec.rb +134 -0
- data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
- data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
- data/spec/unit/keys_spec.rb +155 -0
- data/spec/unit/model_generator_spec.rb +47 -0
- data/spec/unit/mongo_mapper_spec.rb +184 -0
- data/spec/unit/pagination_spec.rb +11 -0
- data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
- data/spec/unit/rails_compatibility_spec.rb +40 -0
- data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
- data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
- data/spec/unit/serialization_spec.rb +169 -0
- data/spec/unit/serializers/json_serializer_spec.rb +218 -0
- data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
- data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
- data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
- data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
- metadata +199 -179
- data/test/_NOTE_ON_TESTING +0 -1
- data/test/functional/test_associations.rb +0 -46
- data/test/functional/test_caching.rb +0 -77
- data/test/functional/test_indexes.rb +0 -50
- data/test/functional/test_modifiers.rb +0 -537
- data/test/functional/test_pagination.rb +0 -91
- data/test/functional/test_protected.rb +0 -201
- data/test/functional/test_querying.rb +0 -935
- data/test/functional/test_safe.rb +0 -76
- data/test/functional/test_timestamps.rb +0 -62
- data/test/functional/test_userstamps.rb +0 -44
- data/test/support/railtie.rb +0 -4
- data/test/support/railtie/autoloaded.rb +0 -2
- data/test/support/railtie/not_autoloaded.rb +0 -3
- data/test/support/railtie/parent.rb +0 -3
- data/test/test_active_model_lint.rb +0 -18
- data/test/test_helper.rb +0 -93
- data/test/unit/associations/test_base.rb +0 -146
- data/test/unit/associations/test_belongs_to_association.rb +0 -29
- data/test/unit/associations/test_many_association.rb +0 -63
- data/test/unit/associations/test_one_association.rb +0 -47
- data/test/unit/serializers/test_json_serializer.rb +0 -216
- data/test/unit/serializers/test_xml_serializer.rb +0 -196
- data/test/unit/test_identity_map_middleware.rb +0 -132
- data/test/unit/test_keys.rb +0 -65
- data/test/unit/test_mongo_mapper.rb +0 -157
- data/test/unit/test_pagination.rb +0 -11
- data/test/unit/test_rails_compatibility.rb +0 -38
- data/test/unit/test_serialization.rb +0 -166
@@ -1,8 +1,8 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
describe "InArrayProxy" do
|
4
4
|
context "description" do
|
5
|
-
|
5
|
+
before do
|
6
6
|
class ::List
|
7
7
|
include MongoMapper::Document
|
8
8
|
key :name, String, :required => true
|
@@ -18,16 +18,16 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
18
18
|
List.collection.remove
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
after do
|
22
22
|
Object.send :remove_const, 'List' if defined?(::List)
|
23
23
|
Object.send :remove_const, 'User' if defined?(::User)
|
24
24
|
end
|
25
25
|
|
26
|
-
should
|
26
|
+
it "should default reader to empty array" do
|
27
27
|
User.new.lists.should == []
|
28
28
|
end
|
29
29
|
|
30
|
-
should
|
30
|
+
it "should allow adding to association like it was an array" do
|
31
31
|
user = User.new(:name => 'John')
|
32
32
|
user.lists << List.new(:name => 'Foo1!')
|
33
33
|
user.lists.push List.new(:name => 'Foo2!')
|
@@ -35,7 +35,7 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
35
35
|
user.lists.size.should == 3
|
36
36
|
end
|
37
37
|
|
38
|
-
should
|
38
|
+
it "should ignore adding duplicate ids" do
|
39
39
|
user = User.create(:name => 'John')
|
40
40
|
list = List.create(:name => 'Foo')
|
41
41
|
user.lists << list
|
@@ -46,7 +46,7 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
46
46
|
user.lists.count.should == 1
|
47
47
|
end
|
48
48
|
|
49
|
-
should
|
49
|
+
it "should be able to replace the association" do
|
50
50
|
user = User.new(:name => 'John')
|
51
51
|
list = List.new(:name => 'Foo')
|
52
52
|
user.lists = [list]
|
@@ -59,29 +59,29 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
59
59
|
end
|
60
60
|
|
61
61
|
context "create" do
|
62
|
-
|
62
|
+
before do
|
63
63
|
@user = User.create(:name => 'John')
|
64
64
|
@list = @user.lists.create(:name => 'Foo!')
|
65
65
|
end
|
66
66
|
|
67
|
-
should
|
67
|
+
it "should add id to key" do
|
68
68
|
@user.list_ids.should include(@list.id)
|
69
69
|
end
|
70
70
|
|
71
|
-
should
|
71
|
+
it "should persist id addition to key in database" do
|
72
72
|
@user.reload
|
73
73
|
@user.list_ids.should include(@list.id)
|
74
74
|
end
|
75
75
|
|
76
|
-
should
|
76
|
+
it "should add doc to association" do
|
77
77
|
@user.lists.should include(@list)
|
78
78
|
end
|
79
79
|
|
80
|
-
should
|
80
|
+
it "should save doc" do
|
81
81
|
@list.should_not be_new
|
82
82
|
end
|
83
83
|
|
84
|
-
should
|
84
|
+
it "should reset cache" do
|
85
85
|
@user.lists.size.should == 1
|
86
86
|
@user.lists.create(:name => 'Moo!')
|
87
87
|
@user.lists.size.should == 2
|
@@ -89,35 +89,35 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
89
89
|
end
|
90
90
|
|
91
91
|
context "create!" do
|
92
|
-
|
92
|
+
before do
|
93
93
|
@user = User.create(:name => 'John')
|
94
94
|
@list = @user.lists.create!(:name => 'Foo!')
|
95
95
|
end
|
96
96
|
|
97
|
-
should
|
97
|
+
it "should add id to key" do
|
98
98
|
@user.list_ids.should include(@list.id)
|
99
99
|
end
|
100
100
|
|
101
|
-
should
|
101
|
+
it "should persist id addition to key in database" do
|
102
102
|
@user.reload
|
103
103
|
@user.list_ids.should include(@list.id)
|
104
104
|
end
|
105
105
|
|
106
|
-
should
|
106
|
+
it "should add doc to association" do
|
107
107
|
@user.lists.should include(@list)
|
108
108
|
end
|
109
109
|
|
110
|
-
should
|
110
|
+
it "should save doc" do
|
111
111
|
@list.should_not be_new
|
112
112
|
end
|
113
113
|
|
114
|
-
should
|
115
|
-
|
114
|
+
it "should raise exception if invalid" do
|
115
|
+
expect {
|
116
116
|
@user.lists.create!
|
117
|
-
|
117
|
+
}.to raise_error(MongoMapper::DocumentNotValid)
|
118
118
|
end
|
119
119
|
|
120
|
-
should
|
120
|
+
it "should reset cache" do
|
121
121
|
@user.lists.size.should == 1
|
122
122
|
@user.lists.create!(:name => 'Moo!')
|
123
123
|
@user.lists.size.should == 2
|
@@ -125,7 +125,7 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
125
125
|
end
|
126
126
|
|
127
127
|
context "Finding scoped to association" do
|
128
|
-
|
128
|
+
before do
|
129
129
|
@user = User.create(:name => 'John')
|
130
130
|
@user2 = User.create(:name => 'Brandon')
|
131
131
|
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
@@ -134,135 +134,135 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
134
134
|
end
|
135
135
|
|
136
136
|
context "all" do
|
137
|
-
should
|
137
|
+
it "should work" do
|
138
138
|
@user.lists.all(:order => :position.asc).should == [@list1, @list2]
|
139
139
|
end
|
140
140
|
|
141
|
-
should
|
141
|
+
it "should work with conditions" do
|
142
142
|
@user.lists.all(:name => 'Foo 1').should == [@list1]
|
143
143
|
end
|
144
144
|
|
145
|
-
should
|
145
|
+
it "should not hit the database if ids key is empty" do
|
146
146
|
@user.list_ids = []
|
147
|
-
@user.lists.
|
147
|
+
@user.lists.should_receive(:query).never
|
148
148
|
@user.lists.all.should == []
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
152
|
context "first" do
|
153
|
-
should
|
153
|
+
it "should work" do
|
154
154
|
@user.lists.first(:order => 'position').should == @list1
|
155
155
|
end
|
156
156
|
|
157
|
-
should
|
157
|
+
it "should work with conditions" do
|
158
158
|
@user.lists.first(:position => 2).should == @list2
|
159
159
|
end
|
160
160
|
|
161
|
-
should
|
161
|
+
it "should not hit the database if ids key is empty" do
|
162
162
|
@user.list_ids = []
|
163
|
-
@user.lists.
|
163
|
+
@user.lists.should_receive(:query).never
|
164
164
|
@user.lists.first.should be_nil
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
168
168
|
context "last" do
|
169
|
-
should
|
169
|
+
it "should work" do
|
170
170
|
@user.lists.last(:order => 'position').should == @list2
|
171
171
|
end
|
172
172
|
|
173
|
-
should
|
173
|
+
it "should work with conditions" do
|
174
174
|
@user.lists.last(:position => 2, :order => 'position').should == @list2
|
175
175
|
end
|
176
176
|
|
177
|
-
should
|
177
|
+
it "should not hit the database if ids key is empty" do
|
178
178
|
@user.list_ids = []
|
179
|
-
@user.lists.
|
179
|
+
@user.lists.should_receive(:query).never
|
180
180
|
@user.lists.last.should be_nil
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
184
|
context "with one id" do
|
185
|
-
should
|
185
|
+
it "should work for id in association" do
|
186
186
|
@user.lists.find(@list1.id).should == @list1
|
187
187
|
end
|
188
188
|
|
189
|
-
should
|
189
|
+
it "should work with string ids" do
|
190
190
|
@user.lists.find(@list1.id.to_s).should == @list1
|
191
191
|
end
|
192
192
|
|
193
|
-
should
|
193
|
+
it "should not work for id not in association" do
|
194
194
|
@user.lists.find(@list3.id).should be_nil
|
195
195
|
end
|
196
196
|
|
197
|
-
should
|
198
|
-
|
197
|
+
it "should raise error when using ! and not found" do
|
198
|
+
expect {
|
199
199
|
@user.lists.find!(@list3.id)
|
200
|
-
|
200
|
+
}.to raise_error(MongoMapper::DocumentNotFound)
|
201
201
|
end
|
202
202
|
end
|
203
203
|
|
204
204
|
context "with multiple ids" do
|
205
|
-
should
|
205
|
+
it "should work for ids in association" do
|
206
206
|
@user.lists.find(@list1.id, @list2.id).should == [@list1, @list2]
|
207
207
|
end
|
208
208
|
|
209
|
-
should
|
209
|
+
it "should not work for ids not in association" do
|
210
210
|
@user.lists.find(@list1.id, @list2.id, @list3.id).should == [@list1, @list2]
|
211
211
|
end
|
212
212
|
end
|
213
213
|
|
214
214
|
context "with #paginate" do
|
215
|
-
|
215
|
+
before do
|
216
216
|
@lists = @user.lists.paginate(:per_page => 1, :page => 1, :order => 'position')
|
217
217
|
end
|
218
218
|
|
219
|
-
should
|
219
|
+
it "should return total pages" do
|
220
220
|
@lists.total_pages.should == 2
|
221
221
|
end
|
222
222
|
|
223
|
-
should
|
223
|
+
it "should return total entries" do
|
224
224
|
@lists.total_entries.should == 2
|
225
225
|
end
|
226
226
|
|
227
|
-
should
|
227
|
+
it "should return the subject" do
|
228
228
|
@lists.collect(&:name).should == ['Foo 1']
|
229
229
|
end
|
230
230
|
|
231
|
-
should
|
231
|
+
it "should not hit the database if ids key is empty" do
|
232
232
|
@user.list_ids = []
|
233
|
-
@user.lists.
|
233
|
+
@user.lists.should_receive(:query).never
|
234
234
|
@user.lists.paginate(:page => 1).should == []
|
235
235
|
end
|
236
236
|
end
|
237
237
|
|
238
238
|
context "dynamic finders" do
|
239
|
-
should
|
239
|
+
it "should work with single key" do
|
240
240
|
@user.lists.find_by_name('Foo 1').should == @list1
|
241
241
|
@user.lists.find_by_name!('Foo 1').should == @list1
|
242
242
|
@user.lists.find_by_name('Foo 3').should be_nil
|
243
243
|
end
|
244
244
|
|
245
|
-
should
|
245
|
+
it "should work with multiple keys" do
|
246
246
|
@user.lists.find_by_name_and_position('Foo 1', 1).should == @list1
|
247
247
|
@user.lists.find_by_name_and_position!('Foo 1', 1).should == @list1
|
248
248
|
@user.lists.find_by_name_and_position('Foo 3', 1).should be_nil
|
249
249
|
end
|
250
250
|
|
251
|
-
should
|
252
|
-
|
251
|
+
it "should raise error when using ! and not found" do
|
252
|
+
expect {
|
253
253
|
@user.lists.find_by_name!('Foo 3')
|
254
|
-
|
254
|
+
}.to raise_error(MongoMapper::DocumentNotFound)
|
255
255
|
end
|
256
256
|
|
257
257
|
context "find_or_create_by" do
|
258
|
-
should
|
258
|
+
it "should not create document if found" do
|
259
259
|
lambda {
|
260
260
|
list = @user.lists.find_or_create_by_name('Foo 1')
|
261
261
|
list.should == @list1
|
262
262
|
}.should_not change { List.count }
|
263
263
|
end
|
264
264
|
|
265
|
-
should
|
265
|
+
it "should create document if not found" do
|
266
266
|
lambda {
|
267
267
|
list = @user.lists.find_or_create_by_name('Home')
|
268
268
|
@user.lists.should include(list)
|
@@ -273,7 +273,7 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
273
273
|
end
|
274
274
|
|
275
275
|
context "count" do
|
276
|
-
|
276
|
+
before do
|
277
277
|
@user = User.create(:name => 'John')
|
278
278
|
@user2 = User.create(:name => 'Brandon')
|
279
279
|
@list1 = @user.lists.create!(:name => 'Foo 1')
|
@@ -281,25 +281,25 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
281
281
|
@list3 = @user2.lists.create!(:name => 'Foo 3')
|
282
282
|
end
|
283
283
|
|
284
|
-
should
|
284
|
+
it "should return number of ids" do
|
285
285
|
@user.lists.count.should == 2
|
286
286
|
@user2.lists.count.should == 1
|
287
287
|
end
|
288
288
|
|
289
|
-
should
|
289
|
+
it "should return correct count when given criteria" do
|
290
290
|
@user.lists.count(:name => 'Foo 1').should == 1
|
291
291
|
@user2.lists.count(:name => 'Foo 1').should == 0
|
292
292
|
end
|
293
293
|
|
294
|
-
should
|
294
|
+
it "should not hit the database if ids key is empty" do
|
295
295
|
@user.list_ids = []
|
296
|
-
@user.lists.
|
296
|
+
@user.lists.should_receive(:query).never
|
297
297
|
@user.lists.count(:name => 'Foo 1').should == 0
|
298
298
|
end
|
299
299
|
end
|
300
300
|
|
301
301
|
context "Removing documents" do
|
302
|
-
|
302
|
+
before do
|
303
303
|
@user = User.create(:name => 'John')
|
304
304
|
@user2 = User.create(:name => 'Brandon')
|
305
305
|
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
@@ -308,13 +308,13 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
308
308
|
end
|
309
309
|
|
310
310
|
context "destroy_all" do
|
311
|
-
should
|
311
|
+
it "should work" do
|
312
312
|
@user.lists.count.should == 2
|
313
313
|
@user.lists.destroy_all
|
314
314
|
@user.lists.count.should == 0
|
315
315
|
end
|
316
316
|
|
317
|
-
should
|
317
|
+
it "should work with conditions" do
|
318
318
|
@user.lists.count.should == 2
|
319
319
|
@user.lists.destroy_all(:name => 'Foo 1')
|
320
320
|
@user.lists.count.should == 1
|
@@ -322,20 +322,20 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
322
322
|
end
|
323
323
|
|
324
324
|
context "delete_all" do
|
325
|
-
should
|
325
|
+
it "should work" do
|
326
326
|
@user.lists.count.should == 2
|
327
327
|
@user.lists.delete_all
|
328
328
|
@user.lists.count.should == 0
|
329
329
|
end
|
330
330
|
|
331
|
-
should
|
331
|
+
it "should work with conditions" do
|
332
332
|
@user.lists.count.should == 2
|
333
333
|
@user.lists.delete_all(:name => 'Foo 1')
|
334
334
|
@user.lists.count.should == 1
|
335
335
|
end
|
336
336
|
end
|
337
337
|
|
338
|
-
should
|
338
|
+
it "should work with nullify" do
|
339
339
|
@user.lists.count.should == 2
|
340
340
|
|
341
341
|
lambda {
|
@@ -1,24 +1,23 @@
|
|
1
|
-
require '
|
2
|
-
require 'models'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
describe "ManyDocumentsAsProxy" do
|
4
|
+
before do
|
6
5
|
Post.collection.remove
|
7
6
|
PostComment.collection.remove
|
8
7
|
end
|
9
8
|
|
10
|
-
should
|
9
|
+
it "should default reader to empty array" do
|
11
10
|
Post.new.comments.should == []
|
12
11
|
end
|
13
12
|
|
14
|
-
should
|
13
|
+
it "should add type and id key to polymorphic class base" do
|
15
14
|
PostComment.keys.keys.should include('commentable_type')
|
16
15
|
PostComment.keys['commentable_type'].type.should == String
|
17
16
|
PostComment.keys.keys.should include('commentable_id')
|
18
17
|
PostComment.keys['commentable_id'].type.should == ObjectId
|
19
18
|
end
|
20
19
|
|
21
|
-
should
|
20
|
+
it "should allow adding to association like it was an array" do
|
22
21
|
post = Post.new
|
23
22
|
post.comments << PostComment.new(:body => 'foo bar')
|
24
23
|
post.comments << PostComment.new(:body => 'baz')
|
@@ -27,7 +26,7 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
27
26
|
post.comments.size.should == 3
|
28
27
|
end
|
29
28
|
|
30
|
-
should
|
29
|
+
it "should be able to replace the association" do
|
31
30
|
post = Post.new
|
32
31
|
|
33
32
|
lambda {
|
@@ -47,19 +46,19 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
47
46
|
end
|
48
47
|
|
49
48
|
context "build" do
|
50
|
-
should
|
49
|
+
it "should assign foreign key" do
|
51
50
|
post = Post.new
|
52
51
|
comment = post.comments.build
|
53
52
|
comment.commentable_id.should == post._id
|
54
53
|
end
|
55
54
|
|
56
|
-
should
|
55
|
+
it "should assign _type" do
|
57
56
|
post = Post.new
|
58
57
|
comment = post.comments.build
|
59
58
|
comment.commentable_type.should == "Post"
|
60
59
|
end
|
61
60
|
|
62
|
-
should
|
61
|
+
it "should allow assigning attributes" do
|
63
62
|
post = Post.new
|
64
63
|
comment = post.comments.build(:body => 'foo bar')
|
65
64
|
comment.body.should == 'foo bar'
|
@@ -67,26 +66,26 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
67
66
|
end
|
68
67
|
|
69
68
|
context "create" do
|
70
|
-
should
|
69
|
+
it "should assign foreign key" do
|
71
70
|
post = Post.new
|
72
71
|
comment = post.comments.create
|
73
72
|
comment.commentable_id.should == post._id
|
74
73
|
end
|
75
74
|
|
76
|
-
should
|
75
|
+
it "should assign _type" do
|
77
76
|
post = Post.new
|
78
77
|
comment = post.comments.create
|
79
78
|
comment.commentable_type.should == "Post"
|
80
79
|
end
|
81
80
|
|
82
|
-
should
|
81
|
+
it "should save record" do
|
83
82
|
post = Post.new
|
84
83
|
lambda {
|
85
84
|
post.comments.create(:body => 'baz')
|
86
85
|
}.should change { PostComment.count }
|
87
86
|
end
|
88
87
|
|
89
|
-
should
|
88
|
+
it "should allow passing attributes" do
|
90
89
|
post = Post.create
|
91
90
|
comment = post.comments.create(:body => 'foo bar')
|
92
91
|
comment.body.should == 'foo bar'
|
@@ -94,7 +93,7 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
94
93
|
end
|
95
94
|
|
96
95
|
context "count" do
|
97
|
-
should
|
96
|
+
it "should work scoped to association" do
|
98
97
|
post = Post.create
|
99
98
|
3.times { post.comments.create(:body => 'foo bar') }
|
100
99
|
|
@@ -105,7 +104,7 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
105
104
|
other_post.comments.count.should == 2
|
106
105
|
end
|
107
106
|
|
108
|
-
should
|
107
|
+
it "should work with conditions" do
|
109
108
|
post = Post.create
|
110
109
|
post.comments.create(:body => 'foo bar')
|
111
110
|
post.comments.create(:body => 'baz')
|
@@ -116,7 +115,7 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
116
115
|
end
|
117
116
|
|
118
117
|
context "Finding scoped to association" do
|
119
|
-
|
118
|
+
before do
|
120
119
|
@post = Post.new
|
121
120
|
|
122
121
|
@comment1 = PostComment.create(:body => 'comment1', :name => 'John')
|
@@ -134,66 +133,66 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
134
133
|
end
|
135
134
|
|
136
135
|
context "with #all" do
|
137
|
-
should
|
136
|
+
it "should work" do
|
138
137
|
@post.comments.all.should include(@comment1)
|
139
138
|
@post.comments.all.should include(@comment2)
|
140
139
|
end
|
141
140
|
|
142
|
-
should
|
141
|
+
it "should work with conditions" do
|
143
142
|
comments = @post.comments.all(:body => 'comment1')
|
144
143
|
comments.should == [@comment1]
|
145
144
|
end
|
146
145
|
|
147
|
-
should
|
146
|
+
it "should work with order" do
|
148
147
|
comments = @post.comments.all(:order => 'body desc')
|
149
148
|
comments.should == [@comment2, @comment1]
|
150
149
|
end
|
151
150
|
end
|
152
151
|
|
153
152
|
context "with one id" do
|
154
|
-
should
|
153
|
+
it "should work for id in association" do
|
155
154
|
@post.comments.find(@comment2._id).should == @comment2
|
156
155
|
end
|
157
156
|
|
158
|
-
should
|
159
|
-
|
157
|
+
it "should not work for id not in association" do
|
158
|
+
expect {
|
160
159
|
@post.comments.find!(@comment5._id)
|
161
|
-
|
160
|
+
}.to raise_error(MongoMapper::DocumentNotFound)
|
162
161
|
end
|
163
162
|
end
|
164
163
|
|
165
164
|
context "with multiple ids" do
|
166
|
-
should
|
165
|
+
it "should work for ids in association" do
|
167
166
|
posts = @post.comments.find!(@comment1._id, @comment2._id)
|
168
167
|
posts.should == [@comment1, @comment2]
|
169
168
|
end
|
170
169
|
|
171
|
-
should
|
172
|
-
|
170
|
+
it "should not work for ids not in association" do
|
171
|
+
expect {
|
173
172
|
@post.comments.find!(@comment1._id, @comment2._id, @comment4._id)
|
174
|
-
|
173
|
+
}.to raise_error(MongoMapper::DocumentNotFound)
|
175
174
|
end
|
176
175
|
end
|
177
176
|
|
178
177
|
context "dynamic finders" do
|
179
|
-
should
|
178
|
+
it "should work with single key" do
|
180
179
|
@post.comments.find_by_body('comment1').should == @comment1
|
181
180
|
@post2.comments.find_by_body('comment1').should == @comment4
|
182
181
|
end
|
183
182
|
|
184
|
-
should
|
183
|
+
it "should work with multiple keys" do
|
185
184
|
@post.comments.find_by_body_and_name('comment1', 'John').should == @comment1
|
186
185
|
@post.comments.find_by_body_and_name('comment1', 'Frank').should be_nil
|
187
186
|
end
|
188
187
|
|
189
|
-
should
|
188
|
+
it "should raise error when using !" do
|
190
189
|
lambda {
|
191
190
|
@post.comments.find_by_body!('asdf')
|
192
191
|
}.should raise_error(MongoMapper::DocumentNotFound)
|
193
192
|
end
|
194
193
|
|
195
194
|
context "find_or_create_by" do
|
196
|
-
should
|
195
|
+
it "should not create document if found" do
|
197
196
|
lambda {
|
198
197
|
comment = @post.comments.find_or_create_by_name('Steve')
|
199
198
|
comment.commentable.should == @post
|
@@ -201,7 +200,7 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
201
200
|
}.should_not change { PostComment.count }
|
202
201
|
end
|
203
202
|
|
204
|
-
should
|
203
|
+
it "should create document if not found" do
|
205
204
|
lambda {
|
206
205
|
@post.comments.find_or_create_by_name('Chas')
|
207
206
|
}.should change { PostComment.count }.by(1)
|
@@ -210,19 +209,19 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
210
209
|
end
|
211
210
|
|
212
211
|
context "with #paginate" do
|
213
|
-
|
212
|
+
before do
|
214
213
|
@comments = @post2.comments.paginate(:per_page => 2, :page => 1, :order => 'name')
|
215
214
|
end
|
216
215
|
|
217
|
-
should
|
216
|
+
it "should return total pages" do
|
218
217
|
@comments.total_pages.should == 2
|
219
218
|
end
|
220
219
|
|
221
|
-
should
|
220
|
+
it "should return total entries" do
|
222
221
|
@comments.total_entries.should == 3
|
223
222
|
end
|
224
223
|
|
225
|
-
should
|
224
|
+
it "should return the subject" do
|
226
225
|
@comments.should include(@comment4)
|
227
226
|
@comments.should include(@comment5)
|
228
227
|
end
|