lookout-mongo_mapper 0.11.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +33 -0
  3. data/UPGRADES +26 -0
  4. data/bin/mmconsole +59 -0
  5. data/examples/attr_accessible.rb +22 -0
  6. data/examples/attr_protected.rb +22 -0
  7. data/examples/cache_key.rb +24 -0
  8. data/examples/custom_types.rb +24 -0
  9. data/examples/identity_map.rb +33 -0
  10. data/examples/identity_map/automatic.rb +2 -0
  11. data/examples/keys.rb +40 -0
  12. data/examples/modifiers/set.rb +25 -0
  13. data/examples/plugins.rb +38 -0
  14. data/examples/querying.rb +35 -0
  15. data/examples/safe.rb +43 -0
  16. data/examples/scopes.rb +52 -0
  17. data/examples/validating/embedded_docs.rb +29 -0
  18. data/lib/mongo_mapper.rb +94 -0
  19. data/lib/mongo_mapper/connection.rb +96 -0
  20. data/lib/mongo_mapper/document.rb +42 -0
  21. data/lib/mongo_mapper/embedded_document.rb +32 -0
  22. data/lib/mongo_mapper/exceptions.rb +30 -0
  23. data/lib/mongo_mapper/extensions/array.rb +19 -0
  24. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  25. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  26. data/lib/mongo_mapper/extensions/date.rb +25 -0
  27. data/lib/mongo_mapper/extensions/float.rb +14 -0
  28. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  29. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  30. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  31. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  32. data/lib/mongo_mapper/extensions/object.rb +26 -0
  33. data/lib/mongo_mapper/extensions/object_id.rb +32 -0
  34. data/lib/mongo_mapper/extensions/set.rb +20 -0
  35. data/lib/mongo_mapper/extensions/string.rb +18 -0
  36. data/lib/mongo_mapper/extensions/time.rb +28 -0
  37. data/lib/mongo_mapper/locale/en.yml +5 -0
  38. data/lib/mongo_mapper/middleware/identity_map.rb +16 -0
  39. data/lib/mongo_mapper/plugins.rb +22 -0
  40. data/lib/mongo_mapper/plugins/accessible.rb +52 -0
  41. data/lib/mongo_mapper/plugins/active_model.rb +18 -0
  42. data/lib/mongo_mapper/plugins/associations.rb +90 -0
  43. data/lib/mongo_mapper/plugins/associations/base.rb +92 -0
  44. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +54 -0
  45. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +34 -0
  46. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
  47. data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
  48. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +44 -0
  49. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +133 -0
  50. data/lib/mongo_mapper/plugins/associations/many_association.rb +63 -0
  51. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  52. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +118 -0
  53. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  54. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  55. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  56. data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +22 -0
  57. data/lib/mongo_mapper/plugins/associations/one_association.rb +48 -0
  58. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
  59. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
  60. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +95 -0
  61. data/lib/mongo_mapper/plugins/associations/proxy.rb +134 -0
  62. data/lib/mongo_mapper/plugins/associations/single_association.rb +46 -0
  63. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  64. data/lib/mongo_mapper/plugins/callbacks.rb +29 -0
  65. data/lib/mongo_mapper/plugins/clone.rb +22 -0
  66. data/lib/mongo_mapper/plugins/dirty.rb +60 -0
  67. data/lib/mongo_mapper/plugins/document.rb +41 -0
  68. data/lib/mongo_mapper/plugins/dynamic_querying.rb +45 -0
  69. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  70. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +56 -0
  71. data/lib/mongo_mapper/plugins/embedded_document.rb +53 -0
  72. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  73. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  74. data/lib/mongo_mapper/plugins/indexes.rb +13 -0
  75. data/lib/mongo_mapper/plugins/inspect.rb +16 -0
  76. data/lib/mongo_mapper/plugins/keys.rb +313 -0
  77. data/lib/mongo_mapper/plugins/keys/key.rb +61 -0
  78. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  79. data/lib/mongo_mapper/plugins/modifiers.rb +134 -0
  80. data/lib/mongo_mapper/plugins/pagination.rb +16 -0
  81. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  82. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  83. data/lib/mongo_mapper/plugins/querying.rb +165 -0
  84. data/lib/mongo_mapper/plugins/querying/decorator.rb +36 -0
  85. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  86. data/lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
  87. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  88. data/lib/mongo_mapper/plugins/sci.rb +36 -0
  89. data/lib/mongo_mapper/plugins/scopes.rb +27 -0
  90. data/lib/mongo_mapper/plugins/serialization.rb +109 -0
  91. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  92. data/lib/mongo_mapper/plugins/touch.rb +18 -0
  93. data/lib/mongo_mapper/plugins/userstamps.rb +18 -0
  94. data/lib/mongo_mapper/plugins/validations.rb +86 -0
  95. data/lib/mongo_mapper/railtie.rb +48 -0
  96. data/lib/mongo_mapper/railtie/database.rake +65 -0
  97. data/lib/mongo_mapper/translation.rb +10 -0
  98. data/lib/mongo_mapper/version.rb +4 -0
  99. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +24 -0
  100. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +18 -0
  101. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +23 -0
  102. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +13 -0
  103. data/test/_NOTE_ON_TESTING +1 -0
  104. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +64 -0
  105. data/test/functional/associations/test_belongs_to_proxy.rb +238 -0
  106. data/test/functional/associations/test_in_array_proxy.rb +349 -0
  107. data/test/functional/associations/test_many_documents_as_proxy.rb +231 -0
  108. data/test/functional/associations/test_many_documents_proxy.rb +866 -0
  109. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +239 -0
  110. data/test/functional/associations/test_many_embedded_proxy.rb +289 -0
  111. data/test/functional/associations/test_many_polymorphic_proxy.rb +303 -0
  112. data/test/functional/associations/test_one_as_proxy.rb +491 -0
  113. data/test/functional/associations/test_one_embedded_polymorphic_proxy.rb +208 -0
  114. data/test/functional/associations/test_one_embedded_proxy.rb +100 -0
  115. data/test/functional/associations/test_one_proxy.rb +383 -0
  116. data/test/functional/test_accessible.rb +198 -0
  117. data/test/functional/test_associations.rb +46 -0
  118. data/test/functional/test_binary.rb +27 -0
  119. data/test/functional/test_caching.rb +77 -0
  120. data/test/functional/test_callbacks.rb +232 -0
  121. data/test/functional/test_dirty.rb +301 -0
  122. data/test/functional/test_document.rb +282 -0
  123. data/test/functional/test_dynamic_querying.rb +75 -0
  124. data/test/functional/test_embedded_document.rb +288 -0
  125. data/test/functional/test_equality.rb +20 -0
  126. data/test/functional/test_identity_map.rb +513 -0
  127. data/test/functional/test_indexes.rb +50 -0
  128. data/test/functional/test_logger.rb +20 -0
  129. data/test/functional/test_modifiers.rb +537 -0
  130. data/test/functional/test_pagination.rb +91 -0
  131. data/test/functional/test_protected.rb +201 -0
  132. data/test/functional/test_querying.rb +935 -0
  133. data/test/functional/test_safe.rb +76 -0
  134. data/test/functional/test_sci.rb +240 -0
  135. data/test/functional/test_scopes.rb +171 -0
  136. data/test/functional/test_timestamps.rb +62 -0
  137. data/test/functional/test_touch.rb +125 -0
  138. data/test/functional/test_userstamps.rb +44 -0
  139. data/test/functional/test_validations.rb +414 -0
  140. data/test/models.rb +261 -0
  141. data/test/support/railtie.rb +4 -0
  142. data/test/support/railtie/autoloaded.rb +2 -0
  143. data/test/support/railtie/not_autoloaded.rb +3 -0
  144. data/test/support/railtie/parent.rb +3 -0
  145. data/test/test_active_model_lint.rb +18 -0
  146. data/test/test_helper.rb +93 -0
  147. data/test/unit/associations/test_base.rb +146 -0
  148. data/test/unit/associations/test_belongs_to_association.rb +29 -0
  149. data/test/unit/associations/test_many_association.rb +63 -0
  150. data/test/unit/associations/test_one_association.rb +47 -0
  151. data/test/unit/associations/test_proxy.rb +100 -0
  152. data/test/unit/serializers/test_json_serializer.rb +216 -0
  153. data/test/unit/serializers/test_xml_serializer.rb +196 -0
  154. data/test/unit/test_clone.rb +69 -0
  155. data/test/unit/test_document.rb +249 -0
  156. data/test/unit/test_dynamic_finder.rb +125 -0
  157. data/test/unit/test_embedded_document.rb +682 -0
  158. data/test/unit/test_equality.rb +38 -0
  159. data/test/unit/test_exceptions.rb +12 -0
  160. data/test/unit/test_extensions.rb +380 -0
  161. data/test/unit/test_identity_map_middleware.rb +34 -0
  162. data/test/unit/test_inspect.rb +47 -0
  163. data/test/unit/test_key.rb +205 -0
  164. data/test/unit/test_keys.rb +65 -0
  165. data/test/unit/test_mongo_mapper.rb +143 -0
  166. data/test/unit/test_pagination.rb +11 -0
  167. data/test/unit/test_plugins.rb +89 -0
  168. data/test/unit/test_rails.rb +183 -0
  169. data/test/unit/test_rails_compatibility.rb +38 -0
  170. data/test/unit/test_rails_reflect_on_association.rb +118 -0
  171. data/test/unit/test_railtie.rb +66 -0
  172. data/test/unit/test_serialization.rb +166 -0
  173. data/test/unit/test_time_zones.rb +44 -0
  174. data/test/unit/test_translation.rb +27 -0
  175. data/test/unit/test_validations.rb +562 -0
  176. metadata +285 -0
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class EqualityTest < Test::Unit::TestCase
4
+ context "Case equality" do
5
+ setup do
6
+ @klass = Class.new do
7
+ include MongoMapper::Plugins::Equality
8
+ end
9
+ @subklass = Class.new(@klass)
10
+
11
+ @faker = Class.new do
12
+ def initialize(faked)
13
+ @faked = faked
14
+ end
15
+
16
+ def is_a?(klass)
17
+ @faked.is_a? klass
18
+ end
19
+ end
20
+ end
21
+
22
+ should "work with regular instance" do
23
+ @klass.should === @klass.new
24
+ end
25
+
26
+ should "work with instances of subclasses" do
27
+ @klass.should === @subklass.new
28
+ end
29
+
30
+ should "work with a faker class" do
31
+ @klass.should === @faker.new(@klass.new)
32
+ end
33
+
34
+ should "not work with other instances" do
35
+ @klass.should_not === 1
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class ExtensionsTest < Test::Unit::TestCase
4
+ context "DocumentNotValid" do
5
+ should "have document reader method" do
6
+ doc_class = Doc()
7
+ instance = doc_class.new
8
+ exception = MongoMapper::DocumentNotValid.new(instance)
9
+ exception.document.should == instance
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,380 @@
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 include(['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 binary if not binary" do
24
+ Binary.to_mongo('asdfsadasdfs').is_a?(BSON::Binary).should be_true
25
+ end
26
+
27
+ should "be binary if binary" do
28
+ Binary.to_mongo(BSON::Binary.new('asdfsadasdfs')).is_a?(BSON::Binary).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
+ binary = BSON::Binary.new('asdfasdfasdf')
39
+ Binary.from_mongo(binary).to_s.should == binary.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.from_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
+
137
+ should "leave nil values nil" do
138
+ Float.to_mongo(nil).should == nil
139
+ end
140
+
141
+ should "leave blank values nil" do
142
+ Float.to_mongo('').should == nil
143
+ end
144
+ end
145
+
146
+ context "Hash.from_mongo" do
147
+ should "convert hash to hash with indifferent access" do
148
+ hash = Hash.from_mongo(:foo => 'bar')
149
+ hash[:foo].should == 'bar'
150
+ hash['foo'].should == 'bar'
151
+ end
152
+
153
+ should "be hash if nil" do
154
+ hash = Hash.from_mongo(nil)
155
+ hash.should == {}
156
+ hash.is_a?(HashWithIndifferentAccess).should be_true
157
+ end
158
+ end
159
+
160
+ context "Hash.to_mongo instance method" do
161
+ should "have instance method that returns self" do
162
+ hash = HashWithIndifferentAccess.new('foo' => 'bar')
163
+ hash.to_mongo.should == {'foo' => 'bar'}
164
+ end
165
+ end
166
+
167
+ context "Integer.to_mongo" do
168
+ should "convert value to integer" do
169
+ [21, 21.0, '21'].each do |value|
170
+ Integer.to_mongo(value).should == 21
171
+ end
172
+ end
173
+
174
+ should "work fine with big integers" do
175
+ [9223372036854775807, '9223372036854775807'].each do |value|
176
+ Integer.to_mongo(value).should == 9223372036854775807
177
+ end
178
+ end
179
+ end
180
+
181
+ context "NilClass#from_mongo" do
182
+ should "return nil" do
183
+ nil.from_mongo(nil).should be_nil
184
+ end
185
+ end
186
+
187
+ context "NilClass#to_mongo" do
188
+ should "return nil" do
189
+ nil.to_mongo(nil).should be_nil
190
+ end
191
+ end
192
+
193
+ context "ObjectId#to_mongo" do
194
+ should "call class to_mongo with self" do
195
+ object = Object.new
196
+ object.class.expects(:to_mongo).with(object)
197
+ object.to_mongo
198
+ end
199
+ end
200
+
201
+ context "ObjectId.to_mongo" do
202
+ should "return nil for nil" do
203
+ ObjectId.to_mongo(nil).should be_nil
204
+ end
205
+
206
+ should "return nil if blank string" do
207
+ ObjectId.to_mongo('').should be_nil
208
+ end
209
+
210
+ should "return value if object id" do
211
+ id = BSON::ObjectId.new
212
+ ObjectId.to_mongo(id).should be(id)
213
+ end
214
+
215
+ should "return value" do
216
+ Object.to_mongo(21).should == 21
217
+ Object.to_mongo('21').should == '21'
218
+ Object.to_mongo(9223372036854775807).should == 9223372036854775807
219
+ end
220
+ end
221
+
222
+ context "ObjectId.from_mongo" do
223
+ should "return value" do
224
+ Object.from_mongo(21).should == 21
225
+ Object.from_mongo('21').should == '21'
226
+ Object.from_mongo(9223372036854775807).should == 9223372036854775807
227
+
228
+ id = BSON::ObjectId.new
229
+ ObjectId.from_mongo(id).should == id
230
+ end
231
+ end
232
+
233
+ context "Set.to_mongo" do
234
+ should "convert value to_a" do
235
+ Set.to_mongo(Set.new([1,2,3])).should == [1,2,3]
236
+ end
237
+
238
+ should "convert to empty array if nil" do
239
+ Set.to_mongo(nil).should == []
240
+ end
241
+ end
242
+
243
+ context "Set.from_mongo" do
244
+ should "be a set if array" do
245
+ Set.from_mongo([1,2,3]).should == Set.new([1,2,3])
246
+ end
247
+
248
+ should "be empty set if nil" do
249
+ Set.from_mongo(nil).should == Set.new([])
250
+ end
251
+ end
252
+
253
+ context "String.to_mongo" do
254
+ should "convert value to_s" do
255
+ [21, '21'].each do |value|
256
+ String.to_mongo(value).should == '21'
257
+ end
258
+ end
259
+
260
+ should "be nil if nil" do
261
+ String.to_mongo(nil).should be_nil
262
+ end
263
+ end
264
+
265
+ context "String.from_mongo" do
266
+ should "be string if value present" do
267
+ String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
268
+ end
269
+
270
+ should "return nil if nil" do
271
+ String.from_mongo(nil).should be_nil
272
+ end
273
+
274
+ should "return empty string if blank" do
275
+ String.from_mongo('').should == ''
276
+ end
277
+ end
278
+
279
+ context "Time.to_mongo without Time.zone" do
280
+ setup do
281
+ Time.zone = nil
282
+ end
283
+
284
+ should "be time to milliseconds if string" do
285
+ Time.to_mongo('2000-01-01 01:01:01.123456').to_f.should be_close(Time.local(2000, 1, 1, 1, 1, 1, 123456).utc.to_f, 0.0000001)
286
+ end
287
+
288
+ should "be time in utc if time" do
289
+ Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
290
+ end
291
+
292
+ should "be nil if blank string" do
293
+ Time.to_mongo('').should be_nil
294
+ end
295
+
296
+ should "not be nil if nil" do
297
+ Time.to_mongo(nil).should be_nil
298
+ end
299
+ end
300
+
301
+ context "Time.to_mongo with Time.zone" do
302
+ should "be time to milliseconds if time" do
303
+ Time.zone = 'Hawaii'
304
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).to_f.should be_close(Time.utc(2009, 8, 16, 0, 0, 0, 123456).to_f, 0.0000001)
305
+ Time.zone = nil
306
+ end
307
+
308
+ should "be time to milliseconds if string" do
309
+ Time.zone = 'Hawaii'
310
+ Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should be_close(Time.utc(2009, 8, 16, 0, 0, 0, 123456).to_f, 0.0000001)
311
+ Time.zone = nil
312
+ end
313
+
314
+ should "not round up times at the end of the month" do
315
+ Time.to_mongo(Time.now.end_of_month).to_f.should be_close(Time.now.end_of_month.utc.to_f, 0.0000001)
316
+ end
317
+
318
+ should "be nil if blank string" do
319
+ Time.zone = 'Hawaii'
320
+ Time.to_mongo('').should be_nil
321
+ Time.zone = nil
322
+ end
323
+
324
+ should "be nil if nil" do
325
+ Time.zone = 'Hawaii'
326
+ Time.to_mongo(nil).should be_nil
327
+ Time.zone = nil
328
+ end
329
+ end
330
+
331
+ context "Time.from_mongo without Time.zone" do
332
+ should "be time" do
333
+ time = Time.now
334
+ Time.from_mongo(time).should == time
335
+ end
336
+
337
+ should "be nil if nil" do
338
+ Time.from_mongo(nil).should be_nil
339
+ end
340
+ end
341
+
342
+ context "Time.from_mongo with Time.zone" do
343
+ should "be time in Time.zone" do
344
+ Time.zone = 'Hawaii'
345
+
346
+ time = Time.from_mongo(Time.utc(2009, 10, 1))
347
+ time.should == Time.zone.local(2009, 9, 30, 14)
348
+ time.is_a?(ActiveSupport::TimeWithZone).should be_true
349
+
350
+ Time.zone = nil
351
+ end
352
+
353
+ should "be nil if nil" do
354
+ Time.zone = 'Hawaii'
355
+ Time.from_mongo(nil).should be_nil
356
+ Time.zone = nil
357
+ end
358
+ end
359
+
360
+ context "BSON::ObjectId" do
361
+ context "#as_json" do
362
+ should "convert object id to string" do
363
+ id = BSON::ObjectId.new
364
+ id.as_json.should == id.to_s
365
+ end
366
+ end
367
+
368
+ context "#to_json" do
369
+ should "convert object id to string" do
370
+ id = BSON::ObjectId.new
371
+ id.to_json.should == %Q("#{id}")
372
+ end
373
+
374
+ should "support ruby driver syntax also" do
375
+ id = BSON::ObjectId.new
376
+ id.original_to_json.should == %Q({"$oid": "#{id}"})
377
+ end
378
+ end
379
+ end
380
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+ require 'rack/test'
3
+
4
+ class IdentityMapMiddlewareTest < Test::Unit::TestCase
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ @app ||= Rack::Builder.new do
9
+ use MongoMapper::Middleware::IdentityMap
10
+ map "/" do
11
+ run lambda {|env| [200, {}, []] }
12
+ end
13
+ map "/fail" do
14
+ run lambda {|env| raise "FAIL!" }
15
+ end
16
+ end.to_app
17
+ end
18
+
19
+ context "with a successful request" do
20
+ should "clear the identity map" do
21
+ MongoMapper::Plugins::IdentityMap.expects(:clear).twice
22
+ get '/'
23
+ end
24
+ end
25
+
26
+ context "when the request raises an error" do
27
+ should "clear the identity map" do
28
+ MongoMapper::Plugins::IdentityMap.expects(:clear).twice
29
+ get '/fail' rescue nil
30
+ end
31
+ end
32
+
33
+
34
+ end