mongo_mapper_ign 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +35 -0
  4. data/Rakefile +37 -0
  5. data/bin/mmconsole +60 -0
  6. data/lib/mongo_mapper.rb +116 -0
  7. data/lib/mongo_mapper/document.rb +313 -0
  8. data/lib/mongo_mapper/embedded_document.rb +70 -0
  9. data/lib/mongo_mapper/plugins.rb +35 -0
  10. data/lib/mongo_mapper/plugins/associations.rb +114 -0
  11. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  13. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  14. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  15. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -0
  16. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +144 -0
  17. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +129 -0
  19. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  20. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  21. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  22. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
  23. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +69 -0
  24. data/lib/mongo_mapper/plugins/associations/proxy.rb +124 -0
  25. data/lib/mongo_mapper/plugins/callbacks.rb +240 -0
  26. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  27. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  28. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  29. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  30. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  31. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  32. data/lib/mongo_mapper/plugins/keys.rb +345 -0
  33. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  34. data/lib/mongo_mapper/plugins/modifiers.rb +107 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +24 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/persistence.rb +68 -0
  38. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  39. data/lib/mongo_mapper/plugins/rails.rb +57 -0
  40. data/lib/mongo_mapper/plugins/serialization.rb +91 -0
  41. data/lib/mongo_mapper/plugins/serialization/array.rb +56 -0
  42. data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +240 -0
  43. data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
  44. data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
  45. data/lib/mongo_mapper/plugins/validations.rb +46 -0
  46. data/lib/mongo_mapper/query.rb +143 -0
  47. data/lib/mongo_mapper/support.rb +218 -0
  48. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  49. data/lib/mongo_mapper/support/find.rb +77 -0
  50. data/lib/mongo_mapper/version.rb +3 -0
  51. data/mongo_mapper.gemspec +214 -0
  52. data/mongo_mapper_ign.gemspec +217 -0
  53. data/performance/read_write.rb +52 -0
  54. data/specs.watchr +51 -0
  55. data/test/NOTE_ON_TESTING +1 -0
  56. data/test/active_model_lint_test.rb +13 -0
  57. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  58. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  59. data/test/functional/associations/test_in_array_proxy.rb +325 -0
  60. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  61. data/test/functional/associations/test_many_documents_proxy.rb +536 -0
  62. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  63. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  64. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  65. data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
  66. data/test/functional/associations/test_one_proxy.rb +196 -0
  67. data/test/functional/test_associations.rb +44 -0
  68. data/test/functional/test_binary.rb +27 -0
  69. data/test/functional/test_callbacks.rb +151 -0
  70. data/test/functional/test_dirty.rb +163 -0
  71. data/test/functional/test_document.rb +1219 -0
  72. data/test/functional/test_embedded_document.rb +210 -0
  73. data/test/functional/test_identity_map.rb +507 -0
  74. data/test/functional/test_indexing.rb +44 -0
  75. data/test/functional/test_logger.rb +20 -0
  76. data/test/functional/test_modifiers.rb +394 -0
  77. data/test/functional/test_pagination.rb +93 -0
  78. data/test/functional/test_protected.rb +163 -0
  79. data/test/functional/test_string_id_compatibility.rb +67 -0
  80. data/test/functional/test_timestamps.rb +64 -0
  81. data/test/functional/test_userstamps.rb +28 -0
  82. data/test/functional/test_validations.rb +342 -0
  83. data/test/models.rb +227 -0
  84. data/test/support/custom_matchers.rb +37 -0
  85. data/test/support/timing.rb +16 -0
  86. data/test/test_helper.rb +64 -0
  87. data/test/unit/associations/test_base.rb +212 -0
  88. data/test/unit/associations/test_proxy.rb +105 -0
  89. data/test/unit/serializers/test_json_serializer.rb +202 -0
  90. data/test/unit/test_descendant_appends.rb +71 -0
  91. data/test/unit/test_document.rb +225 -0
  92. data/test/unit/test_dynamic_finder.rb +123 -0
  93. data/test/unit/test_embedded_document.rb +657 -0
  94. data/test/unit/test_keys.rb +185 -0
  95. data/test/unit/test_mongo_mapper.rb +118 -0
  96. data/test/unit/test_pagination.rb +160 -0
  97. data/test/unit/test_plugins.rb +50 -0
  98. data/test/unit/test_query.rb +374 -0
  99. data/test/unit/test_rails.rb +181 -0
  100. data/test/unit/test_rails_compatibility.rb +52 -0
  101. data/test/unit/test_serialization.rb +51 -0
  102. data/test/unit/test_support.rb +382 -0
  103. data/test/unit/test_time_zones.rb +39 -0
  104. data/test/unit/test_validations.rb +544 -0
  105. metadata +327 -0
@@ -0,0 +1,52 @@
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', '_type', 'for_all']
33
+ FirstItem.column_names.sort.should == ['_id', '_type', 'first_only', 'for_all']
34
+ SecondItem.column_names.sort.should == ['_id', '_type', '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_name.should == 'Item'
44
+ end
45
+ end
46
+
47
+ context "Document" do
48
+ should "implement human_name" do
49
+ BigStuff.human_name.should == 'Big Stuff'
50
+ end
51
+ end
52
+ 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.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.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.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,382 @@
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?(BSON::ByteBuffer).should be_true
25
+ end
26
+
27
+ should "be byte buffer if byte buffer" do
28
+ Binary.to_mongo(BSON::ByteBuffer.new('asdfsadasdfs')).is_a?(BSON::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 = BSON::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
+
64
+ should "be nil for nil" do
65
+ Boolean.to_mongo(nil).should be_nil
66
+ end
67
+ end
68
+
69
+ context "Boolean#from_mongo" do
70
+ should "be true for true" do
71
+ Boolean.from_mongo(true).should be_true
72
+ end
73
+
74
+ should "be false for false" do
75
+ Boolean.from_mongo(false).should be_false
76
+ end
77
+
78
+ should "be nil for nil" do
79
+ Boolean.to_mongo(nil).should be_nil
80
+ end
81
+ end
82
+
83
+ context "Date#to_mongo" do
84
+ should "be time if string" do
85
+ date = Date.to_mongo('2009-10-01')
86
+ date.should == Time.utc(2009, 10, 1)
87
+ date.should == date
88
+ date.month.should == 10
89
+ date.day.should == 1
90
+ date.year.should == 2009
91
+ end
92
+
93
+ should "be time if date" do
94
+ Date.to_mongo(Date.new(2009, 10, 1)).should == Time.utc(2009, 10, 1)
95
+ end
96
+
97
+ should "be date if time" do
98
+ Date.to_mongo(Time.parse("2009-10-1T12:30:00")).should == Time.utc(2009, 10, 1)
99
+ end
100
+
101
+ should "be nil if bogus string" do
102
+ Date.to_mongo('jdsafop874').should be_nil
103
+ end
104
+
105
+ should "be nil if empty string" do
106
+ Date.to_mongo('').should be_nil
107
+ end
108
+ end
109
+
110
+ context "Date#from_mongo" do
111
+ should "be date if date" do
112
+ date = Date.new(2009, 10, 1)
113
+ from_date = Date.from_mongo(date)
114
+ from_date.should == date
115
+ from_date.month.should == 10
116
+ from_date.day.should == 1
117
+ from_date.year.should == 2009
118
+ end
119
+
120
+ should "be date if time" do
121
+ time = Time.now
122
+ Date.from_mongo(time).should == time.to_date
123
+ end
124
+
125
+ should "be nil if nil" do
126
+ Date.from_mongo(nil).should be_nil
127
+ end
128
+ end
129
+
130
+ context "Float#to_mongo" do
131
+ should "convert value to_f" do
132
+ [21, 21.0, '21'].each do |value|
133
+ Float.to_mongo(value).should == 21.0
134
+ end
135
+ end
136
+ end
137
+
138
+ context "Hash#from_mongo" do
139
+ should "convert hash to hash with indifferent access" do
140
+ hash = Hash.from_mongo(:foo => 'bar')
141
+ hash[:foo].should == 'bar'
142
+ hash['foo'].should == 'bar'
143
+ end
144
+
145
+ should "be hash if nil" do
146
+ hash = Hash.from_mongo(nil)
147
+ hash.should == {}
148
+ hash.is_a?(HashWithIndifferentAccess).should be_true
149
+ end
150
+ end
151
+
152
+ context "Hash#to_mongo instance method" do
153
+ should "have instance method that returns self" do
154
+ hash = HashWithIndifferentAccess.new('foo' => 'bar')
155
+ hash.to_mongo.should == {'foo' => 'bar'}
156
+ end
157
+ end
158
+
159
+ context "Integer#to_mongo" do
160
+ should "convert value to integer" do
161
+ [21, 21.0, '21'].each do |value|
162
+ Integer.to_mongo(value).should == 21
163
+ end
164
+ end
165
+
166
+ should "work fine with big integers" do
167
+ [9223372036854775807, '9223372036854775807'].each do |value|
168
+ Integer.to_mongo(value).should == 9223372036854775807
169
+ end
170
+ end
171
+ end
172
+
173
+ context "ObjectId#to_mongo" do
174
+ should "return nil for nil" do
175
+ ObjectId.to_mongo(nil).should be_nil
176
+ end
177
+
178
+ should "return nil if blank string" do
179
+ ObjectId.to_mongo('').should be_nil
180
+ end
181
+
182
+ should "return value if object id" do
183
+ id = BSON::ObjectID.new
184
+ ObjectId.to_mongo(id).should be(id)
185
+ end
186
+
187
+ should "return object id if string" do
188
+ id = BSON::ObjectID.new
189
+ ObjectId.to_mongo(id.to_s).should be(id)
190
+ end
191
+ end
192
+
193
+ context "ObjectId#from_mongo" do
194
+ should "return value" do
195
+ id = BSON::ObjectID.new
196
+ ObjectId.from_mongo(id).should == id
197
+ end
198
+ end
199
+
200
+ context "NilClass#to_mongo" do
201
+ should "return nil" do
202
+ nil.to_mongo(nil).should be_nil
203
+ end
204
+ end
205
+
206
+ context "NilClass#from_mongo" do
207
+ should "return nil" do
208
+ nil.from_mongo(nil).should be_nil
209
+ end
210
+ end
211
+
212
+ context "Object#to_mongo" do
213
+ should "return value" do
214
+ Object.to_mongo(21).should == 21
215
+ Object.to_mongo('21').should == '21'
216
+ Object.to_mongo(9223372036854775807).should == 9223372036854775807
217
+ end
218
+ end
219
+
220
+ context "Object#from_mongo" do
221
+ should "return value" do
222
+ Object.from_mongo(21).should == 21
223
+ Object.from_mongo('21').should == '21'
224
+ Object.from_mongo(9223372036854775807).should == 9223372036854775807
225
+ end
226
+ end
227
+
228
+ context "Set#to_mongo" do
229
+ should "convert value to_a" do
230
+ Set.to_mongo(Set.new([1,2,3])).should == [1,2,3]
231
+ end
232
+
233
+ should "convert to empty array if nil" do
234
+ Set.to_mongo(nil).should == []
235
+ end
236
+ end
237
+
238
+ context "Set#from_mongo" do
239
+ should "be a set if array" do
240
+ Set.from_mongo([1,2,3]).should == Set.new([1,2,3])
241
+ end
242
+
243
+ should "be empty set if nil" do
244
+ Set.from_mongo(nil).should == Set.new([])
245
+ end
246
+ end
247
+
248
+ context "String#to_mongo" do
249
+ should "convert value to_s" do
250
+ [21, '21'].each do |value|
251
+ String.to_mongo(value).should == '21'
252
+ end
253
+ end
254
+
255
+ should "be nil if nil" do
256
+ String.to_mongo(nil).should be_nil
257
+ end
258
+ end
259
+
260
+ context "String#from_mongo" do
261
+ should "be string if value present" do
262
+ String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
263
+ end
264
+
265
+ should "return nil if nil" do
266
+ String.from_mongo(nil).should be_nil
267
+ end
268
+
269
+ should "return empty string if blank" do
270
+ String.from_mongo('').should == ''
271
+ end
272
+ end
273
+
274
+ context "Symbol" do
275
+ %w(gt lt gte lte ne in nin mod all size where exists asc desc).each do |operator|
276
+ should "have $#{operator} operator" do
277
+ :foo.respond_to?(operator)
278
+ end
279
+ end
280
+ end
281
+
282
+ context "Time#to_mongo without Time.zone" do
283
+ setup do
284
+ Time.zone = nil
285
+ end
286
+
287
+ should "be time to milliseconds if string" do
288
+ Time.to_mongo('2000-01-01 01:01:01.123456').to_f.should == Time.local(2000, 1, 1, 1, 1, 1, 123000).utc.to_f
289
+ end
290
+
291
+ should "be time in utc if time" do
292
+ Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
293
+ end
294
+
295
+ should "be nil if blank string" do
296
+ Time.to_mongo('').should be_nil
297
+ end
298
+
299
+ should "not be nil if nil" do
300
+ Time.to_mongo(nil).should be_nil
301
+ end
302
+ end
303
+
304
+ context "Time#to_mongo with Time.zone" do
305
+ should "be time to milliseconds if time" do
306
+ Time.zone = 'Hawaii'
307
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 123000).to_f
308
+ Time.zone = nil
309
+ end
310
+
311
+ should "be time to milliseconds if string" do
312
+ Time.zone = 'Hawaii'
313
+ Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 123000).to_f
314
+ Time.zone = nil
315
+ end
316
+
317
+ should "be nil if blank string" do
318
+ Time.zone = 'Hawaii'
319
+ Time.to_mongo('').should be_nil
320
+ Time.zone = nil
321
+ end
322
+
323
+ should "be nil if nil" do
324
+ Time.zone = 'Hawaii'
325
+ Time.to_mongo(nil).should be_nil
326
+ Time.zone = nil
327
+ end
328
+ end
329
+
330
+ context "Time#from_mongo without Time.zone" do
331
+ should "be time" do
332
+ time = Time.now
333
+ Time.from_mongo(time).should == time
334
+ end
335
+
336
+ should "be nil if nil" do
337
+ Time.from_mongo(nil).should be_nil
338
+ end
339
+ end
340
+
341
+ context "Time#from_mongo with Time.zone" do
342
+ should "be time in Time.zone" do
343
+ Time.zone = 'Hawaii'
344
+
345
+ time = Time.from_mongo(Time.utc(2009, 10, 1))
346
+ time.should == Time.zone.local(2009, 9, 30, 14)
347
+ time.is_a?(ActiveSupport::TimeWithZone).should be_true
348
+
349
+ Time.zone = nil
350
+ end
351
+
352
+ should "be nil if nil" do
353
+ Time.zone = 'Hawaii'
354
+ Time.from_mongo(nil).should be_nil
355
+ Time.zone = nil
356
+ end
357
+ end
358
+
359
+ context "BSON::ObjectID" do
360
+ context "#as_json" do
361
+ should "convert object id to string" do
362
+ id = BSON::ObjectID.new
363
+ id.as_json.should == id.to_s
364
+ end
365
+ end
366
+ context "#to_json" do
367
+ should "convert object id to string" do
368
+ id = BSON::ObjectID.new
369
+ id.to_json.should == %Q("#{id}")
370
+ end
371
+
372
+ should "support ruby driver syntax also" do
373
+ id = BSON::ObjectID.new
374
+ id.original_to_json.should == %Q({"$oid": "#{id}"})
375
+ end
376
+ end
377
+ end
378
+
379
+ context "BSON::ObjectID.to_json" do
380
+
381
+ end
382
+ end