mrkurt-mongo_mapper 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +38 -0
  4. data/Rakefile +55 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/lib/mongo_mapper.rb +139 -0
  8. data/lib/mongo_mapper/associations.rb +72 -0
  9. data/lib/mongo_mapper/associations/base.rb +113 -0
  10. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +26 -0
  11. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +21 -0
  12. data/lib/mongo_mapper/associations/collection.rb +19 -0
  13. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +26 -0
  14. data/lib/mongo_mapper/associations/many_documents_proxy.rb +115 -0
  15. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +31 -0
  16. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +54 -0
  17. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  18. data/lib/mongo_mapper/associations/one_proxy.rb +61 -0
  19. data/lib/mongo_mapper/associations/proxy.rb +111 -0
  20. data/lib/mongo_mapper/callbacks.rb +61 -0
  21. data/lib/mongo_mapper/dirty.rb +117 -0
  22. data/lib/mongo_mapper/document.rb +496 -0
  23. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  24. data/lib/mongo_mapper/embedded_document.rb +380 -0
  25. data/lib/mongo_mapper/finder_options.rb +145 -0
  26. data/lib/mongo_mapper/key.rb +36 -0
  27. data/lib/mongo_mapper/mongo_mapper.rb +125 -0
  28. data/lib/mongo_mapper/pagination.rb +66 -0
  29. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  30. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +28 -0
  31. data/lib/mongo_mapper/serialization.rb +54 -0
  32. data/lib/mongo_mapper/serializers/json_serializer.rb +48 -0
  33. data/lib/mongo_mapper/support.rb +192 -0
  34. data/lib/mongo_mapper/validations.rb +39 -0
  35. data/mongo_mapper.gemspec +173 -0
  36. data/specs.watchr +30 -0
  37. data/test/NOTE_ON_TESTING +1 -0
  38. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  39. data/test/functional/associations/test_belongs_to_proxy.rb +91 -0
  40. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  41. data/test/functional/associations/test_many_documents_proxy.rb +477 -0
  42. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
  43. data/test/functional/associations/test_many_embedded_proxy.rb +192 -0
  44. data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
  45. data/test/functional/associations/test_one_proxy.rb +131 -0
  46. data/test/functional/test_associations.rb +44 -0
  47. data/test/functional/test_binary.rb +33 -0
  48. data/test/functional/test_callbacks.rb +85 -0
  49. data/test/functional/test_dirty.rb +159 -0
  50. data/test/functional/test_document.rb +1198 -0
  51. data/test/functional/test_embedded_document.rb +135 -0
  52. data/test/functional/test_logger.rb +20 -0
  53. data/test/functional/test_modifiers.rb +242 -0
  54. data/test/functional/test_pagination.rb +95 -0
  55. data/test/functional/test_rails_compatibility.rb +25 -0
  56. data/test/functional/test_string_id_compatibility.rb +72 -0
  57. data/test/functional/test_validations.rb +361 -0
  58. data/test/models.rb +271 -0
  59. data/test/support/custom_matchers.rb +55 -0
  60. data/test/support/timing.rb +16 -0
  61. data/test/test_helper.rb +27 -0
  62. data/test/unit/associations/test_base.rb +182 -0
  63. data/test/unit/associations/test_proxy.rb +91 -0
  64. data/test/unit/serializers/test_json_serializer.rb +189 -0
  65. data/test/unit/test_document.rb +236 -0
  66. data/test/unit/test_dynamic_finder.rb +125 -0
  67. data/test/unit/test_embedded_document.rb +709 -0
  68. data/test/unit/test_finder_options.rb +325 -0
  69. data/test/unit/test_key.rb +172 -0
  70. data/test/unit/test_mongo_mapper.rb +65 -0
  71. data/test/unit/test_pagination.rb +119 -0
  72. data/test/unit/test_rails_compatibility.rb +52 -0
  73. data/test/unit/test_serializations.rb +52 -0
  74. data/test/unit/test_support.rb +346 -0
  75. data/test/unit/test_time_zones.rb +40 -0
  76. data/test/unit/test_validations.rb +503 -0
  77. metadata +239 -0
@@ -0,0 +1,325 @@
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
+ end
246
+
247
+ context "Condition auto-detection" do
248
+ should "know :conditions are criteria" do
249
+ finder = FinderOptions.new(Room, :conditions => {:foo => 'bar'})
250
+ finder.criteria.should == {:foo => 'bar'}
251
+ finder.options.keys.should_not include(:conditions)
252
+ end
253
+
254
+ should "know fields is an option" do
255
+ finder = FinderOptions.new(Room, :fields => ['foo'])
256
+ finder.options[:fields].should == ['foo']
257
+ finder.criteria.keys.should_not include(:fields)
258
+ end
259
+
260
+ # select gets converted to fields so just checking keys
261
+ should "know select is an option" do
262
+ finder = FinderOptions.new(Room, :select => 'foo')
263
+ finder.options.keys.should include(:sort)
264
+ finder.criteria.keys.should_not include(:select)
265
+ finder.criteria.keys.should_not include(:fields)
266
+ end
267
+
268
+ should "know skip is an option" do
269
+ finder = FinderOptions.new(Room, :skip => 10)
270
+ finder.options[:skip].should == 10
271
+ finder.criteria.keys.should_not include(:skip)
272
+ end
273
+
274
+ # offset gets converted to skip so just checking keys
275
+ should "know offset is an option" do
276
+ finder = FinderOptions.new(Room, :offset => 10)
277
+ finder.options.keys.should include(:skip)
278
+ finder.criteria.keys.should_not include(:skip)
279
+ finder.criteria.keys.should_not include(:offset)
280
+ end
281
+
282
+ should "know limit is an option" do
283
+ finder = FinderOptions.new(Room, :limit => 10)
284
+ finder.options[:limit].should == 10
285
+ finder.criteria.keys.should_not include(:limit)
286
+ end
287
+
288
+ should "know sort is an option" do
289
+ finder = FinderOptions.new(Room, :sort => [['foo', 1]])
290
+ finder.options[:sort].should == [['foo', 1]]
291
+ finder.criteria.keys.should_not include(:sort)
292
+ end
293
+
294
+ # order gets converted to sort so just checking keys
295
+ should "know order is an option" do
296
+ finder = FinderOptions.new(Room, :order => 'foo')
297
+ finder.options.keys.should include(:sort)
298
+ finder.criteria.keys.should_not include(:sort)
299
+ end
300
+
301
+ should "work with full range of things" do
302
+ finder_options = FinderOptions.new(Room, {
303
+ :foo => 'bar',
304
+ :baz => true,
305
+ :sort => [['foo', 1]],
306
+ :fields => ['foo', 'baz'],
307
+ :limit => 10,
308
+ :skip => 10,
309
+ })
310
+
311
+ finder_options.criteria.should == {
312
+ :foo => 'bar',
313
+ :baz => true,
314
+ }
315
+
316
+ finder_options.options.should == {
317
+ :sort => [['foo', 1]],
318
+ :fields => ['foo', 'baz'],
319
+ :limit => 10,
320
+ :skip => 10,
321
+ }
322
+ end
323
+ end
324
+
325
+ end # FinderOptionsTest
@@ -0,0 +1,172 @@
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
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
+ klass = Class.new do
86
+ include MongoMapper::EmbeddedDocument
87
+ end
88
+ Key.new(:name, klass).embeddable?.should be_true
89
+ end
90
+
91
+ should "know if it is not a embedded_document" do
92
+ Key.new(:name, String).embeddable?.should be_false
93
+ end
94
+
95
+ should "know if it is a number" do
96
+ Key.new(:age, Integer).number?.should be_true
97
+ Key.new(:age, Float).number?.should be_true
98
+ end
99
+
100
+ should "know if it is not a number" do
101
+ Key.new(:age, String).number?.should be_false
102
+ end
103
+ end
104
+
105
+ context "setting a value with a custom type" do
106
+ should "correctly typecast" do
107
+ key = Key.new(:foo, FooType)
108
+ key.set("something").should == 'to_mongo'
109
+ end
110
+
111
+ should "correctly typecast if object of that type is given" do
112
+ key = Key.new(:foo, FooType)
113
+ key.set(FooType.new('something')).should == 'to_mongo'
114
+ end
115
+ end
116
+
117
+ context "getting a value with a custom type" do
118
+ should "use #from_mongo to convert back to custom type" do
119
+ key = Key.new(:foo, FooType)
120
+ key.get('something').should == 'from_mongo'
121
+ end
122
+ end
123
+
124
+ context "getting a value" do
125
+ should "work with a type" do
126
+ key = Key.new(:foo, String)
127
+ key.get('bar').should == 'bar'
128
+ end
129
+
130
+ should "work without type" do
131
+ key = Key.new(:foo)
132
+ key.get([1, '2']).should == [1, '2']
133
+ key.get(false).should == false
134
+ key.get({}).should == {}
135
+ end
136
+
137
+ context "for a embedded_document" do
138
+ should "default to nil" do
139
+ key = Key.new(:foo, Address)
140
+ key.get(nil).should be_nil
141
+ end
142
+
143
+ should "return instance if instance" do
144
+ address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
145
+ key = Key.new(:foo, Address)
146
+ key.get(address).should == address
147
+ end
148
+ end
149
+ end
150
+
151
+ context "getting a value with a default set" do
152
+ setup do
153
+ @key = Key.new(:foo, String, :default => 'baz')
154
+ end
155
+
156
+ should "return default value if value nil" do
157
+ @key.get(nil).should == 'baz'
158
+ end
159
+
160
+ should "return value if not blank" do
161
+ @key.get('foobar').should == 'foobar'
162
+ end
163
+
164
+ should "work with Boolean type and false value" do
165
+ Key.new(:active, Boolean, :default => false).get(nil).should be_false
166
+ end
167
+
168
+ should "work with Boolean type and true value" do
169
+ Key.new(:active, Boolean, :default => true).get(nil).should be_true
170
+ end
171
+ end
172
+ end # KeyTest