mongo_mapper-rails3 0.7.0.1

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