mongo_mapper-unstable 2009.10.16 → 2009.10.31
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -1
- data/README.rdoc +3 -0
- data/Rakefile +31 -65
- data/VERSION +1 -1
- data/lib/mongo_mapper/associations/base.rb +31 -4
- data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +0 -2
- data/lib/mongo_mapper/associations/many_documents_proxy.rb +21 -15
- data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +2 -2
- data/lib/mongo_mapper/associations/many_embedded_proxy.rb +21 -36
- data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +1 -1
- data/lib/mongo_mapper/associations/proxy.rb +1 -0
- data/lib/mongo_mapper/associations.rb +114 -17
- data/lib/mongo_mapper/callbacks.rb +18 -0
- data/lib/mongo_mapper/document.rb +230 -95
- data/lib/mongo_mapper/dynamic_finder.rb +1 -1
- data/lib/mongo_mapper/embedded_document.rb +7 -3
- data/lib/mongo_mapper/finder_options.rb +88 -56
- data/lib/mongo_mapper/pagination.rb +2 -0
- data/lib/mongo_mapper/serialization.rb +2 -3
- data/lib/mongo_mapper/serializers/json_serializer.rb +1 -1
- data/lib/mongo_mapper/support.rb +9 -0
- data/lib/mongo_mapper/validations.rb +14 -42
- data/lib/mongo_mapper.rb +15 -13
- data/mongo_mapper.gemspec +13 -13
- data/specs.watchr +2 -2
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
- data/test/functional/associations/test_belongs_to_proxy.rb +28 -30
- data/test/functional/associations/test_many_documents_as_proxy.rb +4 -4
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +27 -3
- data/test/functional/associations/test_many_embedded_proxy.rb +58 -38
- data/test/functional/associations/test_many_polymorphic_proxy.rb +49 -7
- data/test/functional/associations/test_many_proxy.rb +65 -15
- data/test/functional/test_associations.rb +3 -3
- data/test/functional/test_binary.rb +1 -1
- data/test/functional/test_callbacks.rb +1 -1
- data/test/functional/test_dirty.rb +3 -3
- data/test/functional/test_document.rb +178 -57
- data/test/functional/test_embedded_document.rb +1 -1
- data/test/functional/test_pagination.rb +18 -18
- data/test/functional/test_rails_compatibility.rb +1 -1
- data/test/functional/test_validations.rb +80 -27
- data/test/models.rb +93 -17
- data/test/support/{test_timing.rb → timing.rb} +1 -1
- data/test/test_helper.rb +8 -11
- data/test/unit/test_association_base.rb +23 -1
- data/test/unit/test_document.rb +29 -12
- data/test/unit/test_embedded_document.rb +13 -4
- data/test/unit/test_finder_options.rb +74 -58
- data/test/unit/test_mongomapper.rb +2 -2
- data/test/unit/test_pagination.rb +4 -0
- metadata +7 -7
@@ -1,73 +1,89 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'models'
|
2
3
|
|
3
4
|
class FinderOptionsTest < Test::Unit::TestCase
|
4
5
|
include MongoMapper
|
5
6
|
|
6
7
|
should "raise error if provided something other than a hash" do
|
7
|
-
lambda { FinderOptions.new }.should raise_error(ArgumentError)
|
8
|
-
lambda { FinderOptions.new(1) }.should raise_error(ArgumentError)
|
8
|
+
lambda { FinderOptions.new(Room) }.should raise_error(ArgumentError)
|
9
|
+
lambda { FinderOptions.new(Room, 1) }.should raise_error(ArgumentError)
|
9
10
|
end
|
10
11
|
|
11
12
|
should "symbolize the keys of the hash provided" do
|
12
|
-
FinderOptions.new('offset' => 1).options.keys.map do |key|
|
13
|
+
FinderOptions.new(Room, 'offset' => 1).options.keys.map do |key|
|
13
14
|
key.should be_instance_of(Symbol)
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
17
|
-
context "
|
18
|
-
should "
|
19
|
-
FinderOptions.
|
20
|
-
|
18
|
+
context "Converting conditions to criteria" do
|
19
|
+
should "not add _type to query if model does not have superclass that is single collection inherited" do
|
20
|
+
FinderOptions.new(Message, :foo => 'bar').criteria.should == {
|
21
|
+
:foo => 'bar'
|
22
|
+
}
|
21
23
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
|
25
|
+
should "not add _type to nested conditions" do
|
26
|
+
FinderOptions.new(Enter, :foo => 'bar', :age => {'$gt' => 21}).criteria.should == {
|
27
|
+
:foo => 'bar',
|
28
|
+
:age => {'$gt' => 21},
|
29
|
+
:_type => 'Enter'
|
30
|
+
}
|
28
31
|
end
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
|
33
|
+
%w{gt lt gte lte ne in nin mod size where exists}.each do |operator|
|
34
|
+
should "convert #{operator} conditions" do
|
35
|
+
FinderOptions.new(Room, :age.send(operator) => 21).criteria.should == {
|
36
|
+
:age => {"$#{operator}" => 21}
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
should "automatically add _type to query if model is single collection inherited" do
|
42
|
+
FinderOptions.new(Enter, :foo => 'bar').criteria.should == {
|
43
|
+
:foo => 'bar',
|
44
|
+
:_type => 'Enter'
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
32
48
|
should "work with simple criteria" do
|
33
|
-
FinderOptions.
|
49
|
+
FinderOptions.new(Room, :foo => 'bar').criteria.should == {
|
34
50
|
:foo => 'bar'
|
35
51
|
}
|
36
52
|
|
37
|
-
FinderOptions.
|
53
|
+
FinderOptions.new(Room, :foo => 'bar', :baz => 'wick').criteria.should == {
|
38
54
|
:foo => 'bar',
|
39
55
|
:baz => 'wick'
|
40
56
|
}
|
41
57
|
end
|
42
58
|
|
43
59
|
should "convert id to _id" do
|
44
|
-
FinderOptions.
|
60
|
+
FinderOptions.new(Room, :id => '1').criteria.should == {
|
45
61
|
:_id => '1'
|
46
62
|
}
|
47
63
|
end
|
48
64
|
|
49
65
|
should "use $in for arrays" do
|
50
|
-
FinderOptions.
|
66
|
+
FinderOptions.new(Room, :foo => [1,2,3]).criteria.should == {
|
51
67
|
:foo => {'$in' => [1,2,3]}
|
52
68
|
}
|
53
69
|
end
|
54
70
|
|
55
71
|
should "not use $in for arrays if already using array operator" do
|
56
|
-
FinderOptions.
|
72
|
+
FinderOptions.new(Room, :foo => {'$all' => [1,2,3]}).criteria.should == {
|
57
73
|
:foo => {'$all' => [1,2,3]}
|
58
74
|
}
|
59
75
|
|
60
|
-
FinderOptions.
|
76
|
+
FinderOptions.new(Room, :foo => {'$any' => [1,2,3]}).criteria.should == {
|
61
77
|
:foo => {'$any' => [1,2,3]}
|
62
78
|
}
|
63
79
|
end
|
64
80
|
|
65
81
|
should "work arbitrarily deep" do
|
66
|
-
FinderOptions.
|
82
|
+
FinderOptions.new(Room, :foo => {:bar => [1,2,3]}).criteria.should == {
|
67
83
|
:foo => {:bar => {'$in' => [1,2,3]}}
|
68
84
|
}
|
69
85
|
|
70
|
-
FinderOptions.
|
86
|
+
FinderOptions.new(Room, :foo => {:bar => {'$any' => [1,2,3]}}).criteria.should == {
|
71
87
|
:foo => {:bar => {'$any' => [1,2,3]}}
|
72
88
|
}
|
73
89
|
end
|
@@ -76,166 +92,166 @@ class FinderOptionsTest < Test::Unit::TestCase
|
|
76
92
|
context "ordering" do
|
77
93
|
should "single field with ascending direction" do
|
78
94
|
sort = [['foo', 1]]
|
79
|
-
FinderOptions.
|
80
|
-
FinderOptions.
|
95
|
+
FinderOptions.new(Room, :order => 'foo asc').options[:sort].should == sort
|
96
|
+
FinderOptions.new(Room, :order => 'foo ASC').options[:sort].should == sort
|
81
97
|
end
|
82
98
|
|
83
99
|
should "single field with descending direction" do
|
84
100
|
sort = [['foo', -1]]
|
85
|
-
FinderOptions.
|
86
|
-
FinderOptions.
|
101
|
+
FinderOptions.new(Room, :order => 'foo desc').options[:sort].should == sort
|
102
|
+
FinderOptions.new(Room, :order => 'foo DESC').options[:sort].should == sort
|
87
103
|
end
|
88
104
|
|
89
105
|
should "convert field without direction to ascending" do
|
90
106
|
sort = [['foo', 1]]
|
91
|
-
FinderOptions.
|
107
|
+
FinderOptions.new(Room, :order => 'foo').options[:sort].should == sort
|
92
108
|
end
|
93
109
|
|
94
110
|
should "convert multiple fields with directions" do
|
95
111
|
sort = [['foo', -1], ['bar', 1], ['baz', -1]]
|
96
|
-
FinderOptions.
|
112
|
+
FinderOptions.new(Room, :order => 'foo desc, bar asc, baz desc').options[:sort].should == sort
|
97
113
|
end
|
98
114
|
|
99
115
|
should "convert multiple fields with some missing directions" do
|
100
116
|
sort = [['foo', -1], ['bar', 1], ['baz', 1]]
|
101
|
-
FinderOptions.
|
117
|
+
FinderOptions.new(Room, :order => 'foo desc, bar, baz').options[:sort].should == sort
|
102
118
|
end
|
103
119
|
|
104
120
|
should "just use sort if sort and order are present" do
|
105
121
|
sort = [['$natural', 1]]
|
106
|
-
FinderOptions.
|
122
|
+
FinderOptions.new(Room, :sort => sort, :order => 'foo asc').options[:sort].should == sort
|
107
123
|
end
|
108
124
|
|
109
125
|
should "convert natural in order to proper" do
|
110
126
|
sort = [['$natural', 1]]
|
111
|
-
FinderOptions.
|
127
|
+
FinderOptions.new(Room, :order => '$natural asc').options[:sort].should == sort
|
112
128
|
sort = [['$natural', -1]]
|
113
|
-
FinderOptions.
|
129
|
+
FinderOptions.new(Room, :order => '$natural desc').options[:sort].should == sort
|
114
130
|
end
|
115
131
|
|
116
132
|
should "work for natural order ascending" do
|
117
|
-
FinderOptions.
|
133
|
+
FinderOptions.new(Room, :sort => {'$natural' => 1}).options[:sort]['$natural'].should == 1
|
118
134
|
end
|
119
135
|
|
120
136
|
should "work for natural order descending" do
|
121
|
-
FinderOptions.
|
137
|
+
FinderOptions.new(Room, :sort => {'$natural' => -1}).options[:sort]['$natural'].should == -1
|
122
138
|
end
|
123
139
|
end
|
124
140
|
|
125
141
|
context "skip" do
|
126
142
|
should "default to 0" do
|
127
|
-
FinderOptions.
|
143
|
+
FinderOptions.new(Room, {}).options[:skip].should == 0
|
128
144
|
end
|
129
145
|
|
130
146
|
should "use skip provided" do
|
131
|
-
FinderOptions.
|
147
|
+
FinderOptions.new(Room, :skip => 2).options[:skip].should == 2
|
132
148
|
end
|
133
149
|
|
134
150
|
should "covert string to integer" do
|
135
|
-
FinderOptions.
|
151
|
+
FinderOptions.new(Room, :skip => '2').options[:skip].should == 2
|
136
152
|
end
|
137
153
|
|
138
154
|
should "convert offset to skip" do
|
139
|
-
FinderOptions.
|
155
|
+
FinderOptions.new(Room, :offset => 1).options[:skip].should == 1
|
140
156
|
end
|
141
157
|
end
|
142
158
|
|
143
159
|
context "limit" do
|
144
160
|
should "default to 0" do
|
145
|
-
FinderOptions.
|
161
|
+
FinderOptions.new(Room, {}).options[:limit].should == 0
|
146
162
|
end
|
147
163
|
|
148
164
|
should "use limit provided" do
|
149
|
-
FinderOptions.
|
165
|
+
FinderOptions.new(Room, :limit => 2).options[:limit].should == 2
|
150
166
|
end
|
151
167
|
|
152
168
|
should "covert string to integer" do
|
153
|
-
FinderOptions.
|
169
|
+
FinderOptions.new(Room, :limit => '2').options[:limit].should == 2
|
154
170
|
end
|
155
171
|
end
|
156
172
|
|
157
173
|
context "fields" do
|
158
174
|
should "default to nil" do
|
159
|
-
FinderOptions.
|
175
|
+
FinderOptions.new(Room, {}).options[:fields].should be(nil)
|
160
176
|
end
|
161
177
|
|
162
178
|
should "be converted to nil if empty string" do
|
163
|
-
FinderOptions.
|
179
|
+
FinderOptions.new(Room, :fields => '').options[:fields].should be(nil)
|
164
180
|
end
|
165
181
|
|
166
182
|
should "be converted to nil if []" do
|
167
|
-
FinderOptions.
|
183
|
+
FinderOptions.new(Room, :fields => []).options[:fields].should be(nil)
|
168
184
|
end
|
169
185
|
|
170
186
|
should "should work with array" do
|
171
|
-
FinderOptions.
|
187
|
+
FinderOptions.new(Room, {:fields => %w(a b)}).options[:fields].should == %w(a b)
|
172
188
|
end
|
173
189
|
|
174
190
|
should "convert comma separated list to array" do
|
175
|
-
FinderOptions.
|
191
|
+
FinderOptions.new(Room, {:fields => 'a, b'}).options[:fields].should == %w(a b)
|
176
192
|
end
|
177
193
|
|
178
194
|
should "also work as select" do
|
179
|
-
FinderOptions.new(:select => %w(a b)).options[:fields].should == %w(a b)
|
195
|
+
FinderOptions.new(Room, :select => %w(a b)).options[:fields].should == %w(a b)
|
180
196
|
end
|
181
197
|
end
|
182
198
|
|
183
199
|
context "Condition auto-detection" do
|
184
200
|
should "know :conditions are criteria" do
|
185
|
-
finder = FinderOptions.new(:conditions => {:foo => 'bar'})
|
201
|
+
finder = FinderOptions.new(Room, :conditions => {:foo => 'bar'})
|
186
202
|
finder.criteria.should == {:foo => 'bar'}
|
187
203
|
finder.options.keys.should_not include(:conditions)
|
188
204
|
end
|
189
205
|
|
190
206
|
should "know fields is an option" do
|
191
|
-
finder = FinderOptions.new(:fields => ['foo'])
|
207
|
+
finder = FinderOptions.new(Room, :fields => ['foo'])
|
192
208
|
finder.options[:fields].should == ['foo']
|
193
209
|
finder.criteria.keys.should_not include(:fields)
|
194
210
|
end
|
195
211
|
|
196
212
|
# select gets converted to fields so just checking keys
|
197
213
|
should "know select is an option" do
|
198
|
-
finder = FinderOptions.new(:select => 'foo')
|
214
|
+
finder = FinderOptions.new(Room, :select => 'foo')
|
199
215
|
finder.options.keys.should include(:sort)
|
200
216
|
finder.criteria.keys.should_not include(:select)
|
201
217
|
finder.criteria.keys.should_not include(:fields)
|
202
218
|
end
|
203
219
|
|
204
220
|
should "know skip is an option" do
|
205
|
-
finder = FinderOptions.new(:skip => 10)
|
221
|
+
finder = FinderOptions.new(Room, :skip => 10)
|
206
222
|
finder.options[:skip].should == 10
|
207
223
|
finder.criteria.keys.should_not include(:skip)
|
208
224
|
end
|
209
225
|
|
210
226
|
# offset gets converted to skip so just checking keys
|
211
227
|
should "know offset is an option" do
|
212
|
-
finder = FinderOptions.new(:offset => 10)
|
228
|
+
finder = FinderOptions.new(Room, :offset => 10)
|
213
229
|
finder.options.keys.should include(:skip)
|
214
230
|
finder.criteria.keys.should_not include(:skip)
|
215
231
|
finder.criteria.keys.should_not include(:offset)
|
216
232
|
end
|
217
233
|
|
218
234
|
should "know limit is an option" do
|
219
|
-
finder = FinderOptions.new(:limit => 10)
|
235
|
+
finder = FinderOptions.new(Room, :limit => 10)
|
220
236
|
finder.options[:limit].should == 10
|
221
237
|
finder.criteria.keys.should_not include(:limit)
|
222
238
|
end
|
223
239
|
|
224
240
|
should "know sort is an option" do
|
225
|
-
finder = FinderOptions.new(:sort => [['foo', 1]])
|
241
|
+
finder = FinderOptions.new(Room, :sort => [['foo', 1]])
|
226
242
|
finder.options[:sort].should == [['foo', 1]]
|
227
243
|
finder.criteria.keys.should_not include(:sort)
|
228
244
|
end
|
229
245
|
|
230
246
|
# order gets converted to sort so just checking keys
|
231
247
|
should "know order is an option" do
|
232
|
-
finder = FinderOptions.new(:order => 'foo')
|
248
|
+
finder = FinderOptions.new(Room, :order => 'foo')
|
233
249
|
finder.options.keys.should include(:sort)
|
234
250
|
finder.criteria.keys.should_not include(:sort)
|
235
251
|
end
|
236
252
|
|
237
253
|
should "work with full range of things" do
|
238
|
-
finder_options = FinderOptions.new({
|
254
|
+
finder_options = FinderOptions.new(Room, {
|
239
255
|
:foo => 'bar',
|
240
256
|
:baz => true,
|
241
257
|
:sort => [['foo', 1]],
|
@@ -15,9 +15,9 @@ class MongoMapperTest < Test::Unit::TestCase
|
|
15
15
|
end
|
16
16
|
|
17
17
|
should "be able to write and read default database" do
|
18
|
-
MongoMapper.database =
|
18
|
+
MongoMapper.database = 'test'
|
19
19
|
MongoMapper.database.should be_instance_of(Mongo::DB)
|
20
|
-
MongoMapper.database.name.should ==
|
20
|
+
MongoMapper.database.name.should == 'test'
|
21
21
|
end
|
22
22
|
|
23
23
|
should "have document not found error" do
|
@@ -58,6 +58,10 @@ class PaginationTest < Test::Unit::TestCase
|
|
58
58
|
should "know how many records to skip" do
|
59
59
|
PaginationProxy.new(25, 2, 10).skip.should == 10
|
60
60
|
end
|
61
|
+
|
62
|
+
should "alias offset to skip" do
|
63
|
+
PaginationProxy.new(25, 2, 10).offset.should == 10
|
64
|
+
end
|
61
65
|
|
62
66
|
context "previous_page" do
|
63
67
|
should "be nil if current page 1" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_mapper-unstable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2009.10.
|
4
|
+
version: 2009.10.31
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-31 00:00:00 -05:00
|
13
13
|
default_executable: mmconsole
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: "0.16"
|
34
34
|
version:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: jnunemaker-validatable
|
@@ -40,7 +40,7 @@ dependencies:
|
|
40
40
|
requirements:
|
41
41
|
- - "="
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 1.
|
43
|
+
version: 1.8.1
|
44
44
|
version:
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
46
|
name: jnunemaker-matchy
|
@@ -147,7 +147,7 @@ files:
|
|
147
147
|
- test/functional/test_validations.rb
|
148
148
|
- test/models.rb
|
149
149
|
- test/support/custom_matchers.rb
|
150
|
-
- test/support/
|
150
|
+
- test/support/timing.rb
|
151
151
|
- test/test_helper.rb
|
152
152
|
- test/unit/serializers/test_json_serializer.rb
|
153
153
|
- test/unit/test_association_base.rb
|
@@ -187,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
187
187
|
version:
|
188
188
|
requirements: []
|
189
189
|
|
190
|
-
rubyforge_project:
|
190
|
+
rubyforge_project:
|
191
191
|
rubygems_version: 1.3.5
|
192
192
|
signing_key:
|
193
193
|
specification_version: 3
|
@@ -210,7 +210,7 @@ test_files:
|
|
210
210
|
- test/unit/test_pagination.rb
|
211
211
|
- test/test_helper.rb
|
212
212
|
- test/support/custom_matchers.rb
|
213
|
-
- test/support/
|
213
|
+
- test/support/timing.rb
|
214
214
|
- test/functional/test_embedded_document.rb
|
215
215
|
- test/functional/test_associations.rb
|
216
216
|
- test/functional/associations/test_belongs_to_proxy.rb
|