novelys_mongo_mapper 0.6.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/LICENSE +20 -0
- data/README.rdoc +38 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/bin/mmconsole +60 -0
- data/lib/mongo_mapper.rb +124 -0
- data/lib/mongo_mapper/descendant_appends.rb +44 -0
- data/lib/mongo_mapper/document.rb +423 -0
- data/lib/mongo_mapper/dynamic_finder.rb +74 -0
- data/lib/mongo_mapper/embedded_document.rb +67 -0
- data/lib/mongo_mapper/finder_options.rb +127 -0
- data/lib/mongo_mapper/plugins.rb +14 -0
- data/lib/mongo_mapper/plugins/associations.rb +104 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +121 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +139 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +118 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +76 -0
- data/lib/mongo_mapper/plugins/clone.rb +13 -0
- data/lib/mongo_mapper/plugins/descendants.rb +16 -0
- data/lib/mongo_mapper/plugins/dirty.rb +119 -0
- data/lib/mongo_mapper/plugins/equality.rb +23 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
- data/lib/mongo_mapper/plugins/inspect.rb +14 -0
- data/lib/mongo_mapper/plugins/keys.rb +300 -0
- data/lib/mongo_mapper/plugins/logger.rb +17 -0
- data/lib/mongo_mapper/plugins/pagination.rb +85 -0
- data/lib/mongo_mapper/plugins/protected.rb +41 -0
- data/lib/mongo_mapper/plugins/rails.rb +45 -0
- data/lib/mongo_mapper/plugins/serialization.rb +105 -0
- data/lib/mongo_mapper/plugins/validations.rb +62 -0
- data/lib/mongo_mapper/support.rb +214 -0
- data/mongo_mapper.gemspec +179 -0
- data/performance/read_write.rb +52 -0
- data/specs.watchr +51 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
- data/test/functional/associations/test_in_array_proxy.rb +309 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +431 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_proxy.rb +161 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_callbacks.rb +81 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +1245 -0
- data/test/functional/test_embedded_document.rb +125 -0
- data/test/functional/test_identity_map.rb +508 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +252 -0
- data/test/functional/test_pagination.rb +93 -0
- data/test/functional/test_protected.rb +139 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_validations.rb +329 -0
- data/test/models.rb +232 -0
- data/test/support/custom_matchers.rb +55 -0
- data/test/support/timing.rb +16 -0
- data/test/test_helper.rb +55 -0
- data/test/unit/associations/test_base.rb +207 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +231 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +663 -0
- data/test/unit/test_finder_options.rb +329 -0
- data/test/unit/test_keys.rb +169 -0
- data/test/unit/test_mongo_mapper.rb +65 -0
- data/test/unit/test_pagination.rb +127 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +123 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_support.rb +354 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +544 -0
- metadata +248 -0
@@ -0,0 +1,329 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class FinderOptionsTest < Test::Unit::TestCase
|
5
|
+
include MongoMapper
|
6
|
+
|
7
|
+
should "raise error if provided something other than a hash" do
|
8
|
+
lambda { FinderOptions.new(Room) }.should raise_error(ArgumentError)
|
9
|
+
lambda { FinderOptions.new(Room, 1) }.should raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
|
12
|
+
should "symbolize the keys of the hash provided" do
|
13
|
+
FinderOptions.new(Room, 'offset' => 1).options.keys.map do |key|
|
14
|
+
key.should be_instance_of(Symbol)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
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
|
+
}
|
23
|
+
end
|
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
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
should "automatically add _type to query if model is single collection inherited" do
|
34
|
+
FinderOptions.new(Enter, :foo => 'bar').criteria.should == {
|
35
|
+
:foo => 'bar',
|
36
|
+
:_type => 'Enter'
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
%w{gt lt gte lte ne in nin mod size where exists}.each do |operator|
|
41
|
+
should "convert #{operator} conditions" do
|
42
|
+
FinderOptions.new(Room, :age.send(operator) => 21).criteria.should == {
|
43
|
+
:age => {"$#{operator}" => 21}
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
should "work with simple criteria" do
|
49
|
+
FinderOptions.new(Room, :foo => 'bar').criteria.should == {
|
50
|
+
:foo => 'bar'
|
51
|
+
}
|
52
|
+
|
53
|
+
FinderOptions.new(Room, :foo => 'bar', :baz => 'wick').criteria.should == {
|
54
|
+
:foo => 'bar',
|
55
|
+
:baz => 'wick'
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
should "convert id to _id" do
|
60
|
+
id = Mongo::ObjectID.new
|
61
|
+
FinderOptions.new(Room, :id => id).criteria.should == {:_id => id}
|
62
|
+
end
|
63
|
+
|
64
|
+
should "convert id with symbol operator to _id with modifier" do
|
65
|
+
id = Mongo::ObjectID.new
|
66
|
+
FinderOptions.new(Room, :id.ne => id).criteria.should == {
|
67
|
+
:_id => {'$ne' => id}
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
should "make sure that _id's are object ids" do
|
72
|
+
id = Mongo::ObjectID.new
|
73
|
+
FinderOptions.new(Room, :_id => id.to_s).criteria.should == {:_id => id}
|
74
|
+
end
|
75
|
+
|
76
|
+
should "work fine with _id's that are object ids" do
|
77
|
+
id = Mongo::ObjectID.new
|
78
|
+
FinderOptions.new(Room, :_id => id).criteria.should == {:_id => id}
|
79
|
+
end
|
80
|
+
|
81
|
+
should "make sure other object id typed keys get converted" do
|
82
|
+
id = Mongo::ObjectID.new
|
83
|
+
FinderOptions.new(Message, :room_id => id.to_s).criteria.should == {:room_id => id}
|
84
|
+
end
|
85
|
+
|
86
|
+
should "work fine with object ids for object id typed keys" do
|
87
|
+
id = Mongo::ObjectID.new
|
88
|
+
FinderOptions.new(Message, :room_id => id).criteria.should == {:room_id => id}
|
89
|
+
end
|
90
|
+
|
91
|
+
should "convert times to utc if they aren't already" do
|
92
|
+
time = Time.now.in_time_zone('Indiana (East)')
|
93
|
+
criteria = FinderOptions.new(Room, :created_at => time).criteria
|
94
|
+
criteria[:created_at].utc?.should be_true
|
95
|
+
end
|
96
|
+
|
97
|
+
should "not funk with times already in utc" do
|
98
|
+
time = Time.now.utc
|
99
|
+
criteria = FinderOptions.new(Room, :created_at => time).criteria
|
100
|
+
criteria[:created_at].utc?.should be_true
|
101
|
+
criteria[:created_at].should == time
|
102
|
+
end
|
103
|
+
|
104
|
+
should "use $in for arrays" do
|
105
|
+
FinderOptions.new(Room, :foo => [1,2,3]).criteria.should == {
|
106
|
+
:foo => {'$in' => [1,2,3]}
|
107
|
+
}
|
108
|
+
end
|
109
|
+
|
110
|
+
should "not use $in for arrays if already using array operator" do
|
111
|
+
FinderOptions.new(Room, :foo => {'$all' => [1,2,3]}).criteria.should == {
|
112
|
+
:foo => {'$all' => [1,2,3]}
|
113
|
+
}
|
114
|
+
|
115
|
+
FinderOptions.new(Room, :foo => {'$any' => [1,2,3]}).criteria.should == {
|
116
|
+
:foo => {'$any' => [1,2,3]}
|
117
|
+
}
|
118
|
+
end
|
119
|
+
|
120
|
+
should "work arbitrarily deep" do
|
121
|
+
FinderOptions.new(Room, :foo => {:bar => [1,2,3]}).criteria.should == {
|
122
|
+
:foo => {:bar => {'$in' => [1,2,3]}}
|
123
|
+
}
|
124
|
+
|
125
|
+
FinderOptions.new(Room, :foo => {:bar => {'$any' => [1,2,3]}}).criteria.should == {
|
126
|
+
:foo => {:bar => {'$any' => [1,2,3]}}
|
127
|
+
}
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "ordering" do
|
132
|
+
should "single field with ascending direction" do
|
133
|
+
sort = [['foo', 1]]
|
134
|
+
FinderOptions.new(Room, :order => 'foo asc').options[:sort].should == sort
|
135
|
+
FinderOptions.new(Room, :order => 'foo ASC').options[:sort].should == sort
|
136
|
+
end
|
137
|
+
|
138
|
+
should "single field with descending direction" do
|
139
|
+
sort = [['foo', -1]]
|
140
|
+
FinderOptions.new(Room, :order => 'foo desc').options[:sort].should == sort
|
141
|
+
FinderOptions.new(Room, :order => 'foo DESC').options[:sort].should == sort
|
142
|
+
end
|
143
|
+
|
144
|
+
should "convert order operators to mongo sort" do
|
145
|
+
FinderOptions.new(Room, :order => :foo.asc).options[:sort].should == [['foo', 1]]
|
146
|
+
FinderOptions.new(Room, :order => :foo.desc).options[:sort].should == [['foo', -1]]
|
147
|
+
end
|
148
|
+
|
149
|
+
should "convert array of order operators to mongo sort" do
|
150
|
+
FinderOptions.new(Room, :order => [:foo.asc, :bar.desc]).options[:sort].should == [['foo', 1], ['bar', -1]]
|
151
|
+
end
|
152
|
+
|
153
|
+
should "convert field without direction to ascending" do
|
154
|
+
sort = [['foo', 1]]
|
155
|
+
FinderOptions.new(Room, :order => 'foo').options[:sort].should == sort
|
156
|
+
end
|
157
|
+
|
158
|
+
should "convert multiple fields with directions" do
|
159
|
+
sort = [['foo', -1], ['bar', 1], ['baz', -1]]
|
160
|
+
FinderOptions.new(Room, :order => 'foo desc, bar asc, baz desc').options[:sort].should == sort
|
161
|
+
end
|
162
|
+
|
163
|
+
should "convert multiple fields with some missing directions" do
|
164
|
+
sort = [['foo', -1], ['bar', 1], ['baz', 1]]
|
165
|
+
FinderOptions.new(Room, :order => 'foo desc, bar, baz').options[:sort].should == sort
|
166
|
+
end
|
167
|
+
|
168
|
+
should "just use sort if sort and order are present" do
|
169
|
+
sort = [['$natural', 1]]
|
170
|
+
FinderOptions.new(Room, :sort => sort, :order => 'foo asc').options[:sort].should == sort
|
171
|
+
end
|
172
|
+
|
173
|
+
should "convert natural in order to proper" do
|
174
|
+
sort = [['$natural', 1]]
|
175
|
+
FinderOptions.new(Room, :order => '$natural asc').options[:sort].should == sort
|
176
|
+
sort = [['$natural', -1]]
|
177
|
+
FinderOptions.new(Room, :order => '$natural desc').options[:sort].should == sort
|
178
|
+
end
|
179
|
+
|
180
|
+
should "work for natural order ascending" do
|
181
|
+
FinderOptions.new(Room, :sort => {'$natural' => 1}).options[:sort]['$natural'].should == 1
|
182
|
+
end
|
183
|
+
|
184
|
+
should "work for natural order descending" do
|
185
|
+
FinderOptions.new(Room, :sort => {'$natural' => -1}).options[:sort]['$natural'].should == -1
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context "skip" do
|
190
|
+
should "default to 0" do
|
191
|
+
FinderOptions.new(Room, {}).options[:skip].should == 0
|
192
|
+
end
|
193
|
+
|
194
|
+
should "use skip provided" do
|
195
|
+
FinderOptions.new(Room, :skip => 2).options[:skip].should == 2
|
196
|
+
end
|
197
|
+
|
198
|
+
should "covert string to integer" do
|
199
|
+
FinderOptions.new(Room, :skip => '2').options[:skip].should == 2
|
200
|
+
end
|
201
|
+
|
202
|
+
should "convert offset to skip" do
|
203
|
+
FinderOptions.new(Room, :offset => 1).options[:skip].should == 1
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context "limit" do
|
208
|
+
should "default to 0" do
|
209
|
+
FinderOptions.new(Room, {}).options[:limit].should == 0
|
210
|
+
end
|
211
|
+
|
212
|
+
should "use limit provided" do
|
213
|
+
FinderOptions.new(Room, :limit => 2).options[:limit].should == 2
|
214
|
+
end
|
215
|
+
|
216
|
+
should "covert string to integer" do
|
217
|
+
FinderOptions.new(Room, :limit => '2').options[:limit].should == 2
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "fields" do
|
222
|
+
should "default to nil" do
|
223
|
+
FinderOptions.new(Room, {}).options[:fields].should be(nil)
|
224
|
+
end
|
225
|
+
|
226
|
+
should "be converted to nil if empty string" do
|
227
|
+
FinderOptions.new(Room, :fields => '').options[:fields].should be(nil)
|
228
|
+
end
|
229
|
+
|
230
|
+
should "be converted to nil if []" do
|
231
|
+
FinderOptions.new(Room, :fields => []).options[:fields].should be(nil)
|
232
|
+
end
|
233
|
+
|
234
|
+
should "should work with array" do
|
235
|
+
FinderOptions.new(Room, {:fields => %w(a b)}).options[:fields].should == %w(a b)
|
236
|
+
end
|
237
|
+
|
238
|
+
should "convert comma separated list to array" do
|
239
|
+
FinderOptions.new(Room, {:fields => 'a, b'}).options[:fields].should == %w(a b)
|
240
|
+
end
|
241
|
+
|
242
|
+
should "also work as select" do
|
243
|
+
FinderOptions.new(Room, :select => %w(a b)).options[:fields].should == %w(a b)
|
244
|
+
end
|
245
|
+
|
246
|
+
should "also work with select as array of symbols" do
|
247
|
+
FinderOptions.new(Room, :select => [:a, :b]).options[:fields].should == [:a, :b]
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
context "Condition auto-detection" do
|
252
|
+
should "know :conditions are criteria" do
|
253
|
+
finder = FinderOptions.new(Room, :conditions => {:foo => 'bar'})
|
254
|
+
finder.criteria.should == {:foo => 'bar'}
|
255
|
+
finder.options.keys.should_not include(:conditions)
|
256
|
+
end
|
257
|
+
|
258
|
+
should "know fields is an option" do
|
259
|
+
finder = FinderOptions.new(Room, :fields => ['foo'])
|
260
|
+
finder.options[:fields].should == ['foo']
|
261
|
+
finder.criteria.keys.should_not include(:fields)
|
262
|
+
end
|
263
|
+
|
264
|
+
# select gets converted to fields so just checking keys
|
265
|
+
should "know select is an option" do
|
266
|
+
finder = FinderOptions.new(Room, :select => 'foo')
|
267
|
+
finder.options.keys.should include(:sort)
|
268
|
+
finder.criteria.keys.should_not include(:select)
|
269
|
+
finder.criteria.keys.should_not include(:fields)
|
270
|
+
end
|
271
|
+
|
272
|
+
should "know skip is an option" do
|
273
|
+
finder = FinderOptions.new(Room, :skip => 10)
|
274
|
+
finder.options[:skip].should == 10
|
275
|
+
finder.criteria.keys.should_not include(:skip)
|
276
|
+
end
|
277
|
+
|
278
|
+
# offset gets converted to skip so just checking keys
|
279
|
+
should "know offset is an option" do
|
280
|
+
finder = FinderOptions.new(Room, :offset => 10)
|
281
|
+
finder.options.keys.should include(:skip)
|
282
|
+
finder.criteria.keys.should_not include(:skip)
|
283
|
+
finder.criteria.keys.should_not include(:offset)
|
284
|
+
end
|
285
|
+
|
286
|
+
should "know limit is an option" do
|
287
|
+
finder = FinderOptions.new(Room, :limit => 10)
|
288
|
+
finder.options[:limit].should == 10
|
289
|
+
finder.criteria.keys.should_not include(:limit)
|
290
|
+
end
|
291
|
+
|
292
|
+
should "know sort is an option" do
|
293
|
+
finder = FinderOptions.new(Room, :sort => [['foo', 1]])
|
294
|
+
finder.options[:sort].should == [['foo', 1]]
|
295
|
+
finder.criteria.keys.should_not include(:sort)
|
296
|
+
end
|
297
|
+
|
298
|
+
# order gets converted to sort so just checking keys
|
299
|
+
should "know order is an option" do
|
300
|
+
finder = FinderOptions.new(Room, :order => 'foo')
|
301
|
+
finder.options.keys.should include(:sort)
|
302
|
+
finder.criteria.keys.should_not include(:sort)
|
303
|
+
end
|
304
|
+
|
305
|
+
should "work with full range of things" do
|
306
|
+
finder_options = FinderOptions.new(Room, {
|
307
|
+
:foo => 'bar',
|
308
|
+
:baz => true,
|
309
|
+
:sort => [['foo', 1]],
|
310
|
+
:fields => ['foo', 'baz'],
|
311
|
+
:limit => 10,
|
312
|
+
:skip => 10,
|
313
|
+
})
|
314
|
+
|
315
|
+
finder_options.criteria.should == {
|
316
|
+
:foo => 'bar',
|
317
|
+
:baz => true,
|
318
|
+
}
|
319
|
+
|
320
|
+
finder_options.options.should == {
|
321
|
+
:sort => [['foo', 1]],
|
322
|
+
:fields => ['foo', 'baz'],
|
323
|
+
:limit => 10,
|
324
|
+
:skip => 10,
|
325
|
+
}
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
end # FinderOptionsTest
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Address
|
4
|
+
include MongoMapper::EmbeddedDocument
|
5
|
+
|
6
|
+
key :address, String
|
7
|
+
key :city, String
|
8
|
+
key :state, String
|
9
|
+
key :zip, Integer
|
10
|
+
end
|
11
|
+
|
12
|
+
class FooType < Struct.new(:bar)
|
13
|
+
def self.to_mongo(value)
|
14
|
+
'to_mongo'
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_mongo(value)
|
18
|
+
'from_mongo'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class KeyTest < Test::Unit::TestCase
|
23
|
+
include MongoMapper::Plugins::Keys
|
24
|
+
|
25
|
+
context "Initializing a new key" do
|
26
|
+
should "allow setting the name" do
|
27
|
+
Key.new(:foo, String).name.should == 'foo'
|
28
|
+
end
|
29
|
+
|
30
|
+
should "allow setting the type" do
|
31
|
+
Key.new(:foo, Integer).type.should be(Integer)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "allow setting options" do
|
35
|
+
Key.new(:foo, Integer, :required => true).options[:required].should be(true)
|
36
|
+
end
|
37
|
+
|
38
|
+
should "default options to {}" do
|
39
|
+
Key.new(:foo, Integer, nil).options.should == {}
|
40
|
+
end
|
41
|
+
|
42
|
+
should "symbolize option keys" do
|
43
|
+
Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
should "work with just name" do
|
47
|
+
key = Key.new(:foo)
|
48
|
+
key.name.should == 'foo'
|
49
|
+
end
|
50
|
+
|
51
|
+
should "work with name and type" do
|
52
|
+
key = Key.new(:foo, String)
|
53
|
+
key.name.should == 'foo'
|
54
|
+
key.type.should == String
|
55
|
+
end
|
56
|
+
|
57
|
+
should "work with name, type, and options" do
|
58
|
+
key = Key.new(:foo, String, :required => true)
|
59
|
+
key.name.should == 'foo'
|
60
|
+
key.type.should == String
|
61
|
+
key.options[:required].should be_true
|
62
|
+
end
|
63
|
+
|
64
|
+
should "work with name and options" do
|
65
|
+
key = Key.new(:foo, :required => true)
|
66
|
+
key.name.should == 'foo'
|
67
|
+
key.options[:required].should be_true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "A key" do
|
72
|
+
should "be equal to another key with same name and type" do
|
73
|
+
Key.new(:name, String).should == Key.new(:name, String)
|
74
|
+
end
|
75
|
+
|
76
|
+
should "not be equal to another key with different name" do
|
77
|
+
Key.new(:name, String).should_not == Key.new(:foo, String)
|
78
|
+
end
|
79
|
+
|
80
|
+
should "not be equal to another key with different type" do
|
81
|
+
Key.new(:name, String).should_not == Key.new(:name, Integer)
|
82
|
+
end
|
83
|
+
|
84
|
+
should "know if it is a embedded_document" do
|
85
|
+
Key.new(:name, EDoc()).embeddable?.should be_true
|
86
|
+
end
|
87
|
+
|
88
|
+
should "know if it is not a embedded_document" do
|
89
|
+
Key.new(:name, String).embeddable?.should be_false
|
90
|
+
end
|
91
|
+
|
92
|
+
should "know if it is a number" do
|
93
|
+
Key.new(:age, Integer).number?.should be_true
|
94
|
+
Key.new(:age, Float).number?.should be_true
|
95
|
+
end
|
96
|
+
|
97
|
+
should "know if it is not a number" do
|
98
|
+
Key.new(:age, String).number?.should be_false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "setting a value with a custom type" do
|
103
|
+
should "correctly typecast" do
|
104
|
+
key = Key.new(:foo, FooType)
|
105
|
+
key.set("something").should == 'to_mongo'
|
106
|
+
end
|
107
|
+
|
108
|
+
should "correctly typecast if object of that type is given" do
|
109
|
+
key = Key.new(:foo, FooType)
|
110
|
+
key.set(FooType.new('something')).should == 'to_mongo'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "getting a value with a custom type" do
|
115
|
+
should "use #from_mongo to convert back to custom type" do
|
116
|
+
key = Key.new(:foo, FooType)
|
117
|
+
key.get('something').should == 'from_mongo'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context "getting a value" do
|
122
|
+
should "work with a type" do
|
123
|
+
key = Key.new(:foo, String)
|
124
|
+
key.get('bar').should == 'bar'
|
125
|
+
end
|
126
|
+
|
127
|
+
should "work without type" do
|
128
|
+
key = Key.new(:foo)
|
129
|
+
key.get([1, '2']).should == [1, '2']
|
130
|
+
key.get(false).should == false
|
131
|
+
key.get({}).should == {}
|
132
|
+
end
|
133
|
+
|
134
|
+
context "for a embedded_document" do
|
135
|
+
should "default to nil" do
|
136
|
+
key = Key.new(:foo, Address)
|
137
|
+
key.get(nil).should be_nil
|
138
|
+
end
|
139
|
+
|
140
|
+
should "return instance if instance" do
|
141
|
+
address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
|
142
|
+
key = Key.new(:foo, Address)
|
143
|
+
key.get(address).should == address
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "getting a value with a default set" do
|
149
|
+
setup do
|
150
|
+
@key = Key.new(:foo, String, :default => 'baz')
|
151
|
+
end
|
152
|
+
|
153
|
+
should "return default value if value nil" do
|
154
|
+
@key.get(nil).should == 'baz'
|
155
|
+
end
|
156
|
+
|
157
|
+
should "return value if not blank" do
|
158
|
+
@key.get('foobar').should == 'foobar'
|
159
|
+
end
|
160
|
+
|
161
|
+
should "work with Boolean type and false value" do
|
162
|
+
Key.new(:active, Boolean, :default => false).get(nil).should be_false
|
163
|
+
end
|
164
|
+
|
165
|
+
should "work with Boolean type and true value" do
|
166
|
+
Key.new(:active, Boolean, :default => true).get(nil).should be_true
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end # KeyTest
|