jmonteiro-mongo_mapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) 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/jmonteiro-mongo_mapper.gemspec +195 -0
  8. data/lib/mongo_mapper.rb +128 -0
  9. data/lib/mongo_mapper/descendant_appends.rb +44 -0
  10. data/lib/mongo_mapper/document.rb +402 -0
  11. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  12. data/lib/mongo_mapper/embedded_document.rb +61 -0
  13. data/lib/mongo_mapper/finder_options.rb +127 -0
  14. data/lib/mongo_mapper/plugins.rb +19 -0
  15. data/lib/mongo_mapper/plugins/associations.rb +104 -0
  16. data/lib/mongo_mapper/plugins/associations/base.rb +121 -0
  17. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +23 -0
  19. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  20. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +49 -0
  21. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +139 -0
  22. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  25. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  26. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  27. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +66 -0
  28. data/lib/mongo_mapper/plugins/associations/proxy.rb +118 -0
  29. data/lib/mongo_mapper/plugins/callbacks.rb +65 -0
  30. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  31. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  32. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  33. data/lib/mongo_mapper/plugins/equality.rb +11 -0
  34. data/lib/mongo_mapper/plugins/identity_map.rb +66 -0
  35. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  36. data/lib/mongo_mapper/plugins/keys.rb +295 -0
  37. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  38. data/lib/mongo_mapper/plugins/pagination.rb +85 -0
  39. data/lib/mongo_mapper/plugins/protected.rb +31 -0
  40. data/lib/mongo_mapper/plugins/rails.rb +80 -0
  41. data/lib/mongo_mapper/plugins/serialization.rb +109 -0
  42. data/lib/mongo_mapper/plugins/validations.rb +48 -0
  43. data/lib/mongo_mapper/support.rb +213 -0
  44. data/performance/read_write.rb +52 -0
  45. data/specs.watchr +51 -0
  46. data/test/NOTE_ON_TESTING +1 -0
  47. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  48. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  49. data/test/functional/associations/test_in_array_proxy.rb +309 -0
  50. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  51. data/test/functional/associations/test_many_documents_proxy.rb +437 -0
  52. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +175 -0
  53. data/test/functional/associations/test_many_embedded_proxy.rb +216 -0
  54. data/test/functional/associations/test_many_polymorphic_proxy.rb +340 -0
  55. data/test/functional/associations/test_one_proxy.rb +149 -0
  56. data/test/functional/test_associations.rb +44 -0
  57. data/test/functional/test_binary.rb +27 -0
  58. data/test/functional/test_callbacks.rb +81 -0
  59. data/test/functional/test_dirty.rb +156 -0
  60. data/test/functional/test_document.rb +1171 -0
  61. data/test/functional/test_embedded_document.rb +125 -0
  62. data/test/functional/test_identity_map.rb +233 -0
  63. data/test/functional/test_logger.rb +20 -0
  64. data/test/functional/test_modifiers.rb +252 -0
  65. data/test/functional/test_pagination.rb +93 -0
  66. data/test/functional/test_protected.rb +41 -0
  67. data/test/functional/test_string_id_compatibility.rb +67 -0
  68. data/test/functional/test_validations.rb +329 -0
  69. data/test/models.rb +232 -0
  70. data/test/support/custom_matchers.rb +55 -0
  71. data/test/support/timing.rb +16 -0
  72. data/test/test_helper.rb +60 -0
  73. data/test/unit/associations/test_base.rb +207 -0
  74. data/test/unit/associations/test_proxy.rb +103 -0
  75. data/test/unit/serializers/test_json_serializer.rb +189 -0
  76. data/test/unit/test_descendant_appends.rb +71 -0
  77. data/test/unit/test_document.rb +203 -0
  78. data/test/unit/test_dynamic_finder.rb +125 -0
  79. data/test/unit/test_embedded_document.rb +628 -0
  80. data/test/unit/test_finder_options.rb +325 -0
  81. data/test/unit/test_keys.rb +169 -0
  82. data/test/unit/test_mongo_mapper.rb +65 -0
  83. data/test/unit/test_pagination.rb +127 -0
  84. data/test/unit/test_plugins.rb +42 -0
  85. data/test/unit/test_rails.rb +139 -0
  86. data/test/unit/test_rails_compatibility.rb +42 -0
  87. data/test/unit/test_serialization.rb +51 -0
  88. data/test/unit/test_support.rb +350 -0
  89. data/test/unit/test_time_zones.rb +39 -0
  90. data/test/unit/test_validations.rb +492 -0
  91. metadata +260 -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,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
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