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,57 @@
1
+ require 'test_helper'
2
+
3
+ class TestRailsCompatibility < Test::Unit::TestCase
4
+ class BigStuff
5
+ include MongoMapper::Document
6
+ end
7
+
8
+ class Item
9
+ include MongoMapper::EmbeddedDocument
10
+ key :for_all, String
11
+ end
12
+
13
+ class FirstItem < Item
14
+ key :first_only, String
15
+ many :second_items
16
+ end
17
+
18
+ class SecondItem < Item
19
+ key :second_only, String
20
+ end
21
+
22
+ context "EmbeddedDocument" do
23
+ should "alias many to has_many" do
24
+ FirstItem.should respond_to(:has_many)
25
+ end
26
+
27
+ should "alias one to has_one" do
28
+ FirstItem.should respond_to(:has_one)
29
+ end
30
+
31
+ should "have column names" do
32
+ Item.column_names.sort.should == ['_id', 'for_all']
33
+ FirstItem.column_names.sort.should == ['_id', 'first_only', 'for_all']
34
+ SecondItem.column_names.sort.should == ['_id', 'for_all', 'second_only']
35
+ end
36
+
37
+ should "alias new to new_record?" do
38
+ instance = Item.new
39
+ instance.new_record?.should == instance.new?
40
+ end
41
+
42
+ should "implement human_name" do
43
+ Item.human.should == 'Item'
44
+ end
45
+ end
46
+
47
+ context "Document" do
48
+ should "implement human_name" do
49
+ BigStuff.human.should == 'Big Stuff'
50
+ end
51
+
52
+ should "respond to to_model" do
53
+ big_stuff = BigStuff.new
54
+ big_stuff.to_model.should == big_stuff
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ class SerializationTest < Test::Unit::TestCase
4
+ def setup
5
+ @document = EDoc do
6
+ key :name, String
7
+ key :age, Integer
8
+ key :awesome, Boolean
9
+ key :preferences, Hash
10
+ key :created_at, Time
11
+ end
12
+
13
+ @instance = @document.new(
14
+ :name => 'John Doe',
15
+ :age => 25,
16
+ :awesome => true,
17
+ :preferences => {:language => 'Ruby'},
18
+ :created_at => Time.now.change(:usec => 0)
19
+ )
20
+ end
21
+
22
+ [:json].each do |format|
23
+ context format do
24
+ should "be reversable" do
25
+ serialized = @instance.send("to_#{format}")
26
+ unserialized = @document.new.send("from_#{format}", serialized)
27
+
28
+ assert_equal @instance, unserialized
29
+ end
30
+
31
+ should "allow attribute only filtering" do
32
+ serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
33
+ unserialized = @document.new.send("from_#{format}", serialized)
34
+
35
+ assert_equal @instance.name, unserialized.name
36
+ assert_equal @instance.age, unserialized.age
37
+ assert ! unserialized.awesome
38
+ assert_nil unserialized.created_at
39
+ end
40
+
41
+ should "allow attribute except filtering" do
42
+ serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
43
+ unserialized = @document.new.send("from_#{format}", serialized)
44
+
45
+ assert_nil unserialized.name
46
+ assert_nil unserialized.age
47
+ assert_equal @instance.awesome, unserialized.awesome
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,362 @@
1
+ require 'test_helper'
2
+
3
+ class SupportTest < Test::Unit::TestCase
4
+ context "Array#to_mongo" do
5
+ should "convert value to_a" do
6
+ Array.to_mongo([1, 2, 3, 4]).should == [1, 2, 3, 4]
7
+ Array.to_mongo('1').should == ['1']
8
+ Array.to_mongo({'1' => '2', '3' => '4'}).should == [['1', '2'], ['3', '4']]
9
+ end
10
+ end
11
+
12
+ context "Array#from_mongo" do
13
+ should "be array if array" do
14
+ Array.from_mongo([1, 2]).should == [1, 2]
15
+ end
16
+
17
+ should "be empty array if nil" do
18
+ Array.from_mongo(nil).should == []
19
+ end
20
+ end
21
+
22
+ context "Binary#to_mongo" do
23
+ should "convert to byte buffer if not byte buffer" do
24
+ Binary.to_mongo('asdfsadasdfs').is_a?(ByteBuffer).should be_true
25
+ end
26
+
27
+ should "be byte buffer if byte buffer" do
28
+ Binary.to_mongo(ByteBuffer.new('asdfsadasdfs')).is_a?(ByteBuffer).should be_true
29
+ end
30
+
31
+ should "be nil if nil" do
32
+ Binary.to_mongo(nil).should be_nil
33
+ end
34
+ end
35
+
36
+ context "Binary#from_mongo" do
37
+ should "return value" do
38
+ buffer = ByteBuffer.new('asdfasdfasdf')
39
+ Binary.from_mongo(buffer).to_s.should == buffer.to_s
40
+ end
41
+ end
42
+
43
+ context "Boolean#to_mongo" do
44
+ should "be true for true" do
45
+ Boolean.to_mongo(true).should be_true
46
+ end
47
+
48
+ should "be false for false" do
49
+ Boolean.to_mongo(false).should be_false
50
+ end
51
+
52
+ should "handle odd assortment of other values" do
53
+ Boolean.to_mongo('true').should be_true
54
+ Boolean.to_mongo('t').should be_true
55
+ Boolean.to_mongo('1').should be_true
56
+ Boolean.to_mongo(1).should be_true
57
+
58
+ Boolean.to_mongo('false').should be_false
59
+ Boolean.to_mongo('f').should be_false
60
+ Boolean.to_mongo('0').should be_false
61
+ Boolean.to_mongo(0).should be_false
62
+ end
63
+ end
64
+
65
+ context "Boolean#from_mongo" do
66
+ should "be true for true" do
67
+ Boolean.from_mongo(true).should be_true
68
+ end
69
+
70
+ should "be false for false" do
71
+ Boolean.from_mongo(false).should be_false
72
+ end
73
+
74
+ should "be false for nil" do
75
+ Boolean.from_mongo(nil).should be_false
76
+ end
77
+ end
78
+
79
+ context "Date#to_mongo" do
80
+ should "be time if string" do
81
+ date = Date.to_mongo('10/1/2009')
82
+ date.should == Time.utc(2009, 10, 1)
83
+ date.should == date
84
+ date.month.should == 10
85
+ date.day.should == 1
86
+ date.year.should == 2009
87
+ end
88
+
89
+ should "be time if date" do
90
+ Date.to_mongo(Date.new(2009, 10, 1)).should == Time.utc(2009, 10, 1)
91
+ end
92
+
93
+ should "be date if time" do
94
+ Date.to_mongo(Time.parse("2009-10-1T12:30:00")).should == Time.utc(2009, 10, 1)
95
+ end
96
+
97
+ should "be nil if bogus string" do
98
+ Date.to_mongo('jdsafop874').should be_nil
99
+ end
100
+
101
+ should "be nil if empty string" do
102
+ Date.to_mongo('').should be_nil
103
+ end
104
+ end
105
+
106
+ context "Date#from_mongo" do
107
+ should "be date if date" do
108
+ date = Date.new(2009, 10, 1)
109
+ from_date = Date.from_mongo(date)
110
+ from_date.should == date
111
+ from_date.month.should == 10
112
+ from_date.day.should == 1
113
+ from_date.year.should == 2009
114
+ end
115
+
116
+ should "be date if time" do
117
+ time = Time.now
118
+ Date.from_mongo(time).should == time.to_date
119
+ end
120
+
121
+ should "be nil if nil" do
122
+ Date.from_mongo(nil).should be_nil
123
+ end
124
+ end
125
+
126
+ context "Float#to_mongo" do
127
+ should "convert value to_f" do
128
+ [21, 21.0, '21'].each do |value|
129
+ Float.to_mongo(value).should == 21.0
130
+ end
131
+ end
132
+ end
133
+
134
+ context "Hash#from_mongo" do
135
+ should "convert hash to hash with indifferent access" do
136
+ hash = Hash.from_mongo(:foo => 'bar')
137
+ hash[:foo].should == 'bar'
138
+ hash['foo'].should == 'bar'
139
+ end
140
+
141
+ should "be hash if nil" do
142
+ hash = Hash.from_mongo(nil)
143
+ hash.should == {}
144
+ hash.is_a?(HashWithIndifferentAccess).should be_true
145
+ end
146
+ end
147
+
148
+ context "Hash#to_mongo instance method" do
149
+ should "have instance method that returns self" do
150
+ hash = HashWithIndifferentAccess.new('foo' => 'bar')
151
+ hash.to_mongo.should == {'foo' => 'bar'}
152
+ end
153
+ end
154
+
155
+ context "Integer#to_mongo" do
156
+ should "convert value to integer" do
157
+ [21, 21.0, '21'].each do |value|
158
+ Integer.to_mongo(value).should == 21
159
+ end
160
+ end
161
+
162
+ should "work fine with big integers" do
163
+ [9223372036854775807, '9223372036854775807'].each do |value|
164
+ Integer.to_mongo(value).should == 9223372036854775807
165
+ end
166
+ end
167
+ end
168
+
169
+ context "ObjectId#to_mongo" do
170
+ should "return nil for nil" do
171
+ ObjectId.to_mongo(nil).should be_nil
172
+ end
173
+
174
+ should "return nil if blank string" do
175
+ ObjectId.to_mongo('').should be_nil
176
+ end
177
+
178
+ should "return value if object id" do
179
+ id = Mongo::ObjectID.new
180
+ ObjectId.to_mongo(id).should be(id)
181
+ end
182
+
183
+ should "return object id if string" do
184
+ id = Mongo::ObjectID.new
185
+ ObjectId.to_mongo(id.to_s).should be(id)
186
+ end
187
+ end
188
+
189
+ context "ObjectId#from_mongo" do
190
+ should "return value" do
191
+ id = Mongo::ObjectID.new
192
+ ObjectId.from_mongo(id).should == id
193
+ end
194
+ end
195
+
196
+ context "NilClass#to_mongo" do
197
+ should "return nil" do
198
+ nil.to_mongo(nil).should be_nil
199
+ end
200
+ end
201
+
202
+ context "NilClass#from_mongo" do
203
+ should "return nil" do
204
+ nil.from_mongo(nil).should be_nil
205
+ end
206
+ end
207
+
208
+ context "Object#to_mongo" do
209
+ should "return value" do
210
+ Object.to_mongo(21).should == 21
211
+ Object.to_mongo('21').should == '21'
212
+ Object.to_mongo(9223372036854775807).should == 9223372036854775807
213
+ end
214
+ end
215
+
216
+ context "Object#from_mongo" do
217
+ should "return value" do
218
+ Object.from_mongo(21).should == 21
219
+ Object.from_mongo('21').should == '21'
220
+ Object.from_mongo(9223372036854775807).should == 9223372036854775807
221
+ end
222
+ end
223
+
224
+ context "Set#to_mongo" do
225
+ should "convert value to_a" do
226
+ Set.to_mongo(Set.new([1,2,3])).should == [1,2,3]
227
+ end
228
+
229
+ should "convert to empty array if nil" do
230
+ Set.to_mongo(nil).should == []
231
+ end
232
+ end
233
+
234
+ context "Set#from_mongo" do
235
+ should "be a set if array" do
236
+ Set.from_mongo([1,2,3]).should == Set.new([1,2,3])
237
+ end
238
+
239
+ should "be empty set if nil" do
240
+ Set.from_mongo(nil).should == Set.new([])
241
+ end
242
+ end
243
+
244
+ context "String#to_mongo" do
245
+ should "convert value to_s" do
246
+ [21, '21'].each do |value|
247
+ String.to_mongo(value).should == '21'
248
+ end
249
+ end
250
+
251
+ should "be nil if nil" do
252
+ String.to_mongo(nil).should be_nil
253
+ end
254
+ end
255
+
256
+ context "String#from_mongo" do
257
+ should "be string if value present" do
258
+ String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
259
+ end
260
+
261
+ should "return nil if nil" do
262
+ String.from_mongo(nil).should be_nil
263
+ end
264
+
265
+ should "return empty string if blank" do
266
+ String.from_mongo('').should == ''
267
+ end
268
+ end
269
+
270
+ context "Symbol" do
271
+ %w(gt lt gte lte ne in nin mod all size where exists asc desc).each do |operator|
272
+ should "have $#{operator} operator" do
273
+ :foo.respond_to?(operator)
274
+ end
275
+ end
276
+ end
277
+
278
+ context "Time#to_mongo without Time.zone" do
279
+ should "be time to milliseconds if string" do
280
+ Time.to_mongo('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123000).utc
281
+ end
282
+
283
+ should "be time in utc if time" do
284
+ Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
285
+ end
286
+
287
+ should "be nil if blank string" do
288
+ Time.to_mongo('').should be_nil
289
+ end
290
+
291
+ should "not be nil if nil" do
292
+ Time.to_mongo(nil).should be_nil
293
+ end
294
+ end
295
+
296
+ context "Time#to_mongo with Time.zone" do
297
+ should "be time to milliseconds if time" do
298
+ Time.zone = 'Hawaii'
299
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).should == Time.utc(2009, 8, 16, 0, 0, 0, 123000)
300
+ Time.zone = nil
301
+ end
302
+
303
+ should "be time to milliseconds if string" do
304
+ Time.zone = 'Hawaii'
305
+ Time.to_mongo('2009-08-15 14:00:00.123456').should == Time.utc(2009, 8, 16, 0, 0, 0, 123000)
306
+ Time.zone = nil
307
+ end
308
+
309
+ should "be nil if blank string" do
310
+ Time.zone = 'Hawaii'
311
+ Time.to_mongo('').should be_nil
312
+ Time.zone = nil
313
+ end
314
+
315
+ should "be nil if nil" do
316
+ Time.zone = 'Hawaii'
317
+ Time.to_mongo(nil).should be_nil
318
+ Time.zone = nil
319
+ end
320
+ end
321
+
322
+ context "Time#from_mongo without Time.zone" do
323
+ should "be time" do
324
+ time = Time.now
325
+ Time.from_mongo(time).should == time
326
+ end
327
+
328
+ should "be nil if nil" do
329
+ Time.from_mongo(nil).should be_nil
330
+ end
331
+ end
332
+
333
+ context "Time#from_mongo with Time.zone" do
334
+ should "be time in Time.zone" do
335
+ Time.zone = 'Hawaii'
336
+
337
+ time = Time.from_mongo(Time.utc(2009, 10, 1))
338
+ time.should == Time.zone.local(2009, 9, 30, 14)
339
+ time.is_a?(ActiveSupport::TimeWithZone).should be_true
340
+
341
+ Time.zone = nil
342
+ end
343
+
344
+ should "be nil if nil" do
345
+ Time.zone = 'Hawaii'
346
+ Time.from_mongo(nil).should be_nil
347
+ Time.zone = nil
348
+ end
349
+ end
350
+
351
+ context "Mongo::ObjectID.to_json" do
352
+ should "convert object id to string" do
353
+ id = Mongo::ObjectID.new
354
+ id.to_json.should == %Q("#{id}")
355
+ end
356
+
357
+ should "support ruby driver syntax also" do
358
+ id = Mongo::ObjectID.new
359
+ id.original_to_json.should == %Q({"$oid": "#{id}"})
360
+ end
361
+ end
362
+ end