mrkurt-mongo_mapper 0.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) 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/lib/mongo_mapper.rb +139 -0
  8. data/lib/mongo_mapper/associations.rb +72 -0
  9. data/lib/mongo_mapper/associations/base.rb +113 -0
  10. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +26 -0
  11. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +21 -0
  12. data/lib/mongo_mapper/associations/collection.rb +19 -0
  13. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +26 -0
  14. data/lib/mongo_mapper/associations/many_documents_proxy.rb +115 -0
  15. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +31 -0
  16. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +54 -0
  17. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  18. data/lib/mongo_mapper/associations/one_proxy.rb +61 -0
  19. data/lib/mongo_mapper/associations/proxy.rb +111 -0
  20. data/lib/mongo_mapper/callbacks.rb +61 -0
  21. data/lib/mongo_mapper/dirty.rb +117 -0
  22. data/lib/mongo_mapper/document.rb +496 -0
  23. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  24. data/lib/mongo_mapper/embedded_document.rb +380 -0
  25. data/lib/mongo_mapper/finder_options.rb +145 -0
  26. data/lib/mongo_mapper/key.rb +36 -0
  27. data/lib/mongo_mapper/mongo_mapper.rb +125 -0
  28. data/lib/mongo_mapper/pagination.rb +66 -0
  29. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  30. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +28 -0
  31. data/lib/mongo_mapper/serialization.rb +54 -0
  32. data/lib/mongo_mapper/serializers/json_serializer.rb +48 -0
  33. data/lib/mongo_mapper/support.rb +192 -0
  34. data/lib/mongo_mapper/validations.rb +39 -0
  35. data/mongo_mapper.gemspec +173 -0
  36. data/specs.watchr +30 -0
  37. data/test/NOTE_ON_TESTING +1 -0
  38. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  39. data/test/functional/associations/test_belongs_to_proxy.rb +91 -0
  40. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  41. data/test/functional/associations/test_many_documents_proxy.rb +477 -0
  42. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
  43. data/test/functional/associations/test_many_embedded_proxy.rb +192 -0
  44. data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
  45. data/test/functional/associations/test_one_proxy.rb +131 -0
  46. data/test/functional/test_associations.rb +44 -0
  47. data/test/functional/test_binary.rb +33 -0
  48. data/test/functional/test_callbacks.rb +85 -0
  49. data/test/functional/test_dirty.rb +159 -0
  50. data/test/functional/test_document.rb +1198 -0
  51. data/test/functional/test_embedded_document.rb +135 -0
  52. data/test/functional/test_logger.rb +20 -0
  53. data/test/functional/test_modifiers.rb +242 -0
  54. data/test/functional/test_pagination.rb +95 -0
  55. data/test/functional/test_rails_compatibility.rb +25 -0
  56. data/test/functional/test_string_id_compatibility.rb +72 -0
  57. data/test/functional/test_validations.rb +361 -0
  58. data/test/models.rb +271 -0
  59. data/test/support/custom_matchers.rb +55 -0
  60. data/test/support/timing.rb +16 -0
  61. data/test/test_helper.rb +27 -0
  62. data/test/unit/associations/test_base.rb +182 -0
  63. data/test/unit/associations/test_proxy.rb +91 -0
  64. data/test/unit/serializers/test_json_serializer.rb +189 -0
  65. data/test/unit/test_document.rb +236 -0
  66. data/test/unit/test_dynamic_finder.rb +125 -0
  67. data/test/unit/test_embedded_document.rb +709 -0
  68. data/test/unit/test_finder_options.rb +325 -0
  69. data/test/unit/test_key.rb +172 -0
  70. data/test/unit/test_mongo_mapper.rb +65 -0
  71. data/test/unit/test_pagination.rb +119 -0
  72. data/test/unit/test_rails_compatibility.rb +52 -0
  73. data/test/unit/test_serializations.rb +52 -0
  74. data/test/unit/test_support.rb +346 -0
  75. data/test/unit/test_time_zones.rb +40 -0
  76. data/test/unit/test_validations.rb +503 -0
  77. metadata +239 -0
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class Address; end
4
+
5
+ class MongoMapperTest < Test::Unit::TestCase
6
+ should "be able to write and read connection" do
7
+ conn = Mongo::Connection.new
8
+ MongoMapper.connection = conn
9
+ MongoMapper.connection.should == conn
10
+ end
11
+
12
+ should "default connection to new mongo ruby driver" do
13
+ MongoMapper.connection = nil
14
+ MongoMapper.connection.should be_instance_of(Mongo::Connection)
15
+ end
16
+
17
+ should "be able to write and read default database" do
18
+ MongoMapper.database = 'test'
19
+ MongoMapper.database.should be_instance_of(Mongo::DB)
20
+ MongoMapper.database.name.should == 'test'
21
+ end
22
+
23
+ should "have document not found error" do
24
+ lambda {
25
+ MongoMapper::DocumentNotFound
26
+ }.should_not raise_error
27
+ end
28
+
29
+ context "use_time_zone?" do
30
+ should "be true if Time.zone set" do
31
+ Time.zone = 'Hawaii'
32
+ MongoMapper.use_time_zone?.should be_true
33
+ Time.zone = nil
34
+ end
35
+
36
+ should "be false if Time.zone not set" do
37
+ MongoMapper.use_time_zone?.should be_false
38
+ end
39
+ end
40
+
41
+ context "time_class" do
42
+ should "be Time.zone if using time zones" do
43
+ Time.zone = 'Hawaii'
44
+ MongoMapper.time_class.should == Time.zone
45
+ Time.zone = nil
46
+ end
47
+
48
+ should "be Time if not using time zones" do
49
+ MongoMapper.time_class.should == Time
50
+ end
51
+ end
52
+
53
+ context "normalize_object_id" do
54
+ should "turn string into object id" do
55
+ id = Mongo::ObjectID.new
56
+ MongoMapper.normalize_object_id(id.to_s).should == id
57
+ end
58
+
59
+ should "leave object id alone" do
60
+ id = Mongo::ObjectID.new
61
+ MongoMapper.normalize_object_id(id).should == id
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,119 @@
1
+ require 'test_helper'
2
+
3
+ class PaginationTest < Test::Unit::TestCase
4
+ context "Pagination proxy" do
5
+ include MongoMapper::Pagination
6
+
7
+ should "should have accessors for subject" do
8
+ subject = [1,2,3,4,5]
9
+ collection = PaginationProxy.new(25, 2)
10
+ collection.subject = subject
11
+ collection.subject.should == subject
12
+ end
13
+
14
+ should "delegate any methods not defined to the subject" do
15
+ subject = [1,2,3,4,5]
16
+ collection = PaginationProxy.new(25, 2, 10)
17
+ collection.subject = subject
18
+ collection.size.should == 5
19
+ collection.each_with_index do |value, i|
20
+ value.should == subject[i]
21
+ end
22
+ collection[0..2].should == [1,2,3]
23
+ collection.class.should == Array
24
+ end
25
+
26
+ should "return correct value for total_entries" do
27
+ PaginationProxy.new(25, 2, 10).total_entries.should == 25
28
+ PaginationProxy.new('25', 2, 10).total_entries.should == 25
29
+ end
30
+
31
+ should "return correct value for per_page" do
32
+ PaginationProxy.new(25, 2, 10).per_page.should == 10
33
+ PaginationProxy.new(25, 2, '10').per_page.should == 10
34
+ end
35
+
36
+ should "alias limit to per_page" do
37
+ PaginationProxy.new(100, 1, 300).limit.should == 300
38
+ end
39
+
40
+ should "set per_page to 25 if nil or blank" do
41
+ PaginationProxy.new(25, 2).per_page.should == 25
42
+ PaginationProxy.new(25, 2, '').per_page.should == 25
43
+ end
44
+
45
+ should "return correct value for current_page" do
46
+ PaginationProxy.new(25, 2, 10).current_page.should == 2
47
+ PaginationProxy.new(25, '2', 10).current_page.should == 2
48
+ end
49
+
50
+ should "not allow value less than 1 for current page" do
51
+ PaginationProxy.new(25, -1).current_page.should == 1
52
+ end
53
+
54
+ should "automatically calculate total_pages from total_entries and per page" do
55
+ PaginationProxy.new(25, 2, 10).total_pages.should == 3
56
+ end
57
+
58
+ should "know how many records to skip" do
59
+ PaginationProxy.new(25, 2, 10).skip.should == 10
60
+ end
61
+
62
+ should "alias offset to skip" do
63
+ PaginationProxy.new(25, 2, 10).offset.should == 10
64
+ end
65
+
66
+ should "return true for === Array" do
67
+ collection = PaginationProxy.new(25, 2, 10)
68
+ collection.subject = [1, 2]
69
+ collection.should === Array
70
+ end
71
+
72
+ context "previous_page" do
73
+ should "be nil if current page 1" do
74
+ PaginationProxy.new(25, 1, 10).previous_page.should be_nil
75
+ end
76
+
77
+ should "be one less than current page if current is > 1" do
78
+ PaginationProxy.new(25, 2, 10).previous_page.should == 1
79
+ end
80
+ end
81
+
82
+ context "next_page" do
83
+ should "be nil if current page is last page" do
84
+ PaginationProxy.new(25, 3, 10).next_page.should be_nil
85
+ end
86
+
87
+ should "work for any page that is not last" do
88
+ PaginationProxy.new(25, 1, 10).next_page.should == 2
89
+ PaginationProxy.new(25, 2, 10).next_page.should == 3
90
+ end
91
+ end
92
+
93
+ context "previous_page" do
94
+ should "be nil if current page is first page" do
95
+ PaginationProxy.new(25, 1, 10).previous_page.should be_nil
96
+ end
97
+
98
+ should "work for any page other than first" do
99
+ PaginationProxy.new(25, 2, 10).previous_page.should == 1
100
+ PaginationProxy.new(25, 3, 10).previous_page.should == 2
101
+ end
102
+ end
103
+
104
+ context "out_of_bounds?" do
105
+ should "be true if current_page is greater than total_pages" do
106
+ PaginationProxy.new(25, 10, 4).out_of_bounds?.should be_true
107
+ end
108
+
109
+ should "be false if current_page is less than total_pages" do
110
+ PaginationProxy.new(25, 10, 1).out_of_bounds?.should be_false
111
+ PaginationProxy.new(25, 2, 10).out_of_bounds?.should be_false
112
+ end
113
+
114
+ should "be false if current_page is equal to total_pages" do
115
+ PaginationProxy.new(25, 3, 10).out_of_bounds?.should be_false
116
+ end
117
+ end
118
+ end # end of pagination proxy
119
+ end # end of test case
@@ -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', '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_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,52 @@
1
+ require 'test_helper'
2
+
3
+ class SerializationTest < Test::Unit::TestCase
4
+ def setup
5
+ @document = Class.new do
6
+ include MongoMapper::EmbeddedDocument
7
+ key :name, String
8
+ key :age, Integer
9
+ key :awesome, Boolean
10
+ key :preferences, Hash
11
+ key :created_at, Time
12
+ end
13
+
14
+ @instance = @document.new(
15
+ :name => 'John Doe',
16
+ :age => 25,
17
+ :awesome => true,
18
+ :preferences => {:language => 'Ruby'},
19
+ :created_at => Time.now.change(:usec => 0)
20
+ )
21
+ end
22
+
23
+ [:json].each do |format|
24
+ context format do
25
+ should "be reversable" do
26
+ serialized = @instance.send("to_#{format}")
27
+ unserialized = @document.new.send("from_#{format}", serialized)
28
+
29
+ assert_equal @instance, unserialized
30
+ end
31
+
32
+ should "allow attribute only filtering" do
33
+ serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
34
+ unserialized = @document.new.send("from_#{format}", serialized)
35
+
36
+ assert_equal @instance.name, unserialized.name
37
+ assert_equal @instance.age, unserialized.age
38
+ assert ! unserialized.awesome
39
+ assert_nil unserialized.created_at
40
+ end
41
+
42
+ should "allow attribute except filtering" do
43
+ serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
44
+ unserialized = @document.new.send("from_#{format}", serialized)
45
+
46
+ assert_nil unserialized.name
47
+ assert_nil unserialized.age
48
+ assert_equal @instance.awesome, unserialized.awesome
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,346 @@
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 nil if bogus string" do
94
+ Date.to_mongo('jdsafop874').should be_nil
95
+ end
96
+
97
+ should "be nil if empty string" do
98
+ Date.to_mongo('').should be_nil
99
+ end
100
+ end
101
+
102
+ context "Date#from_mongo" do
103
+ should "be date if date" do
104
+ date = Date.new(2009, 10, 1)
105
+ from_date = Date.from_mongo(date)
106
+ from_date.should == date
107
+ from_date.month.should == 10
108
+ from_date.day.should == 1
109
+ from_date.year.should == 2009
110
+ end
111
+
112
+ should "be date if time" do
113
+ time = Time.now
114
+ Date.from_mongo(time).should == time.to_date
115
+ end
116
+
117
+ should "be nil if nil" do
118
+ Date.from_mongo(nil).should be_nil
119
+ end
120
+ end
121
+
122
+ context "Float#to_mongo" do
123
+ should "convert value to_f" do
124
+ [21, 21.0, '21'].each do |value|
125
+ Float.to_mongo(value).should == 21.0
126
+ end
127
+ end
128
+ end
129
+
130
+ context "Hash#from_mongo" do
131
+ should "convert hash to hash with indifferent access" do
132
+ hash = Hash.from_mongo(:foo => 'bar')
133
+ hash[:foo].should == 'bar'
134
+ hash['foo'].should == 'bar'
135
+ end
136
+
137
+ should "be hash if nil" do
138
+ hash = Hash.from_mongo(nil)
139
+ hash.should == {}
140
+ hash.is_a?(HashWithIndifferentAccess).should be_true
141
+ end
142
+ end
143
+
144
+ context "Hash#to_mongo instance method" do
145
+ should "have instance method that returns self" do
146
+ hash = HashWithIndifferentAccess.new('foo' => 'bar')
147
+ hash.to_mongo.should == {'foo' => 'bar'}
148
+ end
149
+ end
150
+
151
+ context "Integer#to_mongo" do
152
+ should "convert value to integer" do
153
+ [21, 21.0, '21'].each do |value|
154
+ Integer.to_mongo(value).should == 21
155
+ end
156
+ end
157
+
158
+ should "work fine with big integers" do
159
+ [9223372036854775807, '9223372036854775807'].each do |value|
160
+ Integer.to_mongo(value).should == 9223372036854775807
161
+ end
162
+ end
163
+ end
164
+
165
+ context "ObjectId#to_mongo" do
166
+ should "return nil for nil" do
167
+ ObjectId.to_mongo(nil).should be_nil
168
+ end
169
+
170
+ should "return nil if blank string" do
171
+ ObjectId.to_mongo('').should be_nil
172
+ end
173
+
174
+ should "return value if object id" do
175
+ id = Mongo::ObjectID.new
176
+ ObjectId.to_mongo(id).should be(id)
177
+ end
178
+
179
+ should "return object id if string" do
180
+ id = Mongo::ObjectID.new
181
+ ObjectId.to_mongo(id.to_s).should be(id)
182
+ end
183
+ end
184
+
185
+ context "ObjectId#from_mongo" do
186
+ should "return value" do
187
+ id = Mongo::ObjectID.new
188
+ ObjectId.from_mongo(id).should == id
189
+ end
190
+ end
191
+
192
+ context "NilClass#to_mongo" do
193
+ should "return nil" do
194
+ nil.to_mongo(nil).should be_nil
195
+ end
196
+ end
197
+
198
+ context "NilClass#from_mongo" do
199
+ should "return nil" do
200
+ nil.from_mongo(nil).should be_nil
201
+ end
202
+ end
203
+
204
+ context "Object#to_mongo" do
205
+ should "return value" do
206
+ Object.to_mongo(21).should == 21
207
+ Object.to_mongo('21').should == '21'
208
+ Object.to_mongo(9223372036854775807).should == 9223372036854775807
209
+ end
210
+ end
211
+
212
+ context "Object#from_mongo" do
213
+ should "return value" do
214
+ Object.from_mongo(21).should == 21
215
+ Object.from_mongo('21').should == '21'
216
+ Object.from_mongo(9223372036854775807).should == 9223372036854775807
217
+ end
218
+ end
219
+
220
+ context "Set#to_mongo" do
221
+ should "convert value to_a" do
222
+ Set.to_mongo(Set.new([1,2,3])).should == [1,2,3]
223
+ end
224
+
225
+ should "convert to empty array if nil" do
226
+ Set.to_mongo(nil).should == []
227
+ end
228
+ end
229
+
230
+ context "Set#from_mongo" do
231
+ should "be a set if array" do
232
+ Set.from_mongo([1,2,3]).should == Set.new([1,2,3])
233
+ end
234
+
235
+ should "be empty set if nil" do
236
+ Set.from_mongo(nil).should == Set.new([])
237
+ end
238
+ end
239
+
240
+ context "String#to_mongo" do
241
+ should "convert value to_s" do
242
+ [21, '21'].each do |value|
243
+ String.to_mongo(value).should == '21'
244
+ end
245
+ end
246
+
247
+ should "be nil if nil" do
248
+ String.to_mongo(nil).should be_nil
249
+ end
250
+ end
251
+
252
+ context "String#from_mongo" do
253
+ should "be string if value present" do
254
+ String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
255
+ end
256
+
257
+ should "return nil if nil" do
258
+ String.from_mongo(nil).should be_nil
259
+ end
260
+
261
+ should "return empty string if blank" do
262
+ String.from_mongo('').should == ''
263
+ end
264
+ end
265
+
266
+ context "Time#to_mongo without Time.zone" do
267
+ should "be time if string" do
268
+ Time.to_mongo('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123456).utc
269
+ end
270
+
271
+ should "be time in utc if time" do
272
+ Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
273
+ end
274
+
275
+ should "be nil if blank string" do
276
+ Time.to_mongo('').should be_nil
277
+ end
278
+
279
+ should "not be nil if nil" do
280
+ Time.to_mongo(nil).should be_nil
281
+ end
282
+ end
283
+
284
+ context "Time#to_mongo with Time.zone" do
285
+ should "be time if time" do
286
+ Time.zone = 'Hawaii'
287
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0)).should == Time.utc(2009, 8, 16, 0, 0, 0)
288
+ Time.zone = nil
289
+ end
290
+
291
+ should "be time if string" do
292
+ Time.zone = 'Hawaii'
293
+ Time.to_mongo('2009-08-15 14:00:00').should == Time.utc(2009, 8, 16, 0, 0, 0)
294
+ Time.zone = nil
295
+ end
296
+
297
+ should "be nil if blank string" do
298
+ Time.zone = 'Hawaii'
299
+ Time.to_mongo('').should be_nil
300
+ Time.zone = nil
301
+ end
302
+
303
+ should "be nil if nil" do
304
+ Time.zone = 'Hawaii'
305
+ Time.to_mongo(nil).should be_nil
306
+ Time.zone = nil
307
+ end
308
+ end
309
+
310
+ context "Time#from_mongo without Time.zone" do
311
+ should "be time" do
312
+ time = Time.now
313
+ Time.from_mongo(time).should == time
314
+ end
315
+
316
+ should "be nil if nil" do
317
+ Time.from_mongo(nil).should be_nil
318
+ end
319
+ end
320
+
321
+ context "Time#from_mongo with Time.zone" do
322
+ should "be time in Time.zone" do
323
+ Time.zone = 'Hawaii'
324
+
325
+ time = Time.from_mongo(Time.utc(2009, 10, 1))
326
+ time.should == Time.zone.local(2009, 9, 30, 14)
327
+ time.is_a?(ActiveSupport::TimeWithZone).should be_true
328
+
329
+ Time.zone = nil
330
+ end
331
+
332
+ should "be nil if nil" do
333
+ Time.zone = 'Hawaii'
334
+ Time.from_mongo(nil).should be_nil
335
+ Time.zone = nil
336
+ end
337
+ end
338
+
339
+ context "Mongo::ObjectID.to_json" do
340
+ should "convert object id to string" do
341
+ id = Mongo::ObjectID.new
342
+ id.to_json.should == %Q("#{id}")
343
+ end
344
+ end
345
+
346
+ end