danielharan-mongo_mapper 0.6.5

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 (74) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +53 -0
  4. data/Rakefile +55 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/lib/mongo_mapper.rb +134 -0
  8. data/lib/mongo_mapper/associations.rb +183 -0
  9. data/lib/mongo_mapper/associations/base.rb +110 -0
  10. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
  11. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +22 -0
  12. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +25 -0
  13. data/lib/mongo_mapper/associations/many_documents_proxy.rb +127 -0
  14. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
  15. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +53 -0
  16. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  17. data/lib/mongo_mapper/associations/many_proxy.rb +6 -0
  18. data/lib/mongo_mapper/associations/proxy.rb +80 -0
  19. data/lib/mongo_mapper/callbacks.rb +109 -0
  20. data/lib/mongo_mapper/dirty.rb +136 -0
  21. data/lib/mongo_mapper/document.rb +481 -0
  22. data/lib/mongo_mapper/dynamic_finder.rb +35 -0
  23. data/lib/mongo_mapper/embedded_document.rb +386 -0
  24. data/lib/mongo_mapper/finder_options.rb +133 -0
  25. data/lib/mongo_mapper/key.rb +36 -0
  26. data/lib/mongo_mapper/observing.rb +50 -0
  27. data/lib/mongo_mapper/pagination.rb +53 -0
  28. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  29. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +27 -0
  30. data/lib/mongo_mapper/serialization.rb +54 -0
  31. data/lib/mongo_mapper/serializers/json_serializer.rb +92 -0
  32. data/lib/mongo_mapper/support.rb +193 -0
  33. data/lib/mongo_mapper/validations.rb +41 -0
  34. data/mongo_mapper.gemspec +171 -0
  35. data/specs.watchr +32 -0
  36. data/test/NOTE_ON_TESTING +1 -0
  37. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  38. data/test/functional/associations/test_belongs_to_proxy.rb +48 -0
  39. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  40. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
  41. data/test/functional/associations/test_many_embedded_proxy.rb +196 -0
  42. data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
  43. data/test/functional/associations/test_many_proxy.rb +384 -0
  44. data/test/functional/test_associations.rb +44 -0
  45. data/test/functional/test_binary.rb +18 -0
  46. data/test/functional/test_callbacks.rb +85 -0
  47. data/test/functional/test_dirty.rb +159 -0
  48. data/test/functional/test_document.rb +1180 -0
  49. data/test/functional/test_embedded_document.rb +125 -0
  50. data/test/functional/test_logger.rb +20 -0
  51. data/test/functional/test_pagination.rb +95 -0
  52. data/test/functional/test_rails_compatibility.rb +25 -0
  53. data/test/functional/test_string_id_compatibility.rb +72 -0
  54. data/test/functional/test_validations.rb +369 -0
  55. data/test/models.rb +271 -0
  56. data/test/support/custom_matchers.rb +55 -0
  57. data/test/support/timing.rb +16 -0
  58. data/test/test_helper.rb +27 -0
  59. data/test/unit/serializers/test_json_serializer.rb +189 -0
  60. data/test/unit/test_association_base.rb +166 -0
  61. data/test/unit/test_document.rb +204 -0
  62. data/test/unit/test_dynamic_finder.rb +125 -0
  63. data/test/unit/test_embedded_document.rb +718 -0
  64. data/test/unit/test_finder_options.rb +296 -0
  65. data/test/unit/test_key.rb +172 -0
  66. data/test/unit/test_mongo_mapper.rb +65 -0
  67. data/test/unit/test_observing.rb +101 -0
  68. data/test/unit/test_pagination.rb +113 -0
  69. data/test/unit/test_rails_compatibility.rb +49 -0
  70. data/test/unit/test_serializations.rb +52 -0
  71. data/test/unit/test_support.rb +342 -0
  72. data/test/unit/test_time_zones.rb +40 -0
  73. data/test/unit/test_validations.rb +503 -0
  74. metadata +233 -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,101 @@
1
+ require 'test_helper'
2
+
3
+ class Comment
4
+ include MongoMapper::Document
5
+
6
+ key :name, String
7
+ key :body, String
8
+
9
+ attr_accessor :callers
10
+ before_validation :record_callers
11
+
12
+ def after_validation
13
+ record_callers
14
+ end
15
+
16
+ def record_callers
17
+ callers << self.class if callers
18
+ end
19
+ end
20
+
21
+ class Article
22
+ include MongoMapper::Document
23
+
24
+ key :title, String
25
+ key :body, String
26
+ end
27
+
28
+ class CommentObserver < MongoMapper::Observer
29
+ attr_accessor :callers
30
+
31
+ def after_validation(model)
32
+ callers << self.class if callers
33
+ end
34
+ end
35
+
36
+ class AuditObserver < MongoMapper::Observer
37
+ observe Article, Comment
38
+ attr_reader :document
39
+
40
+ def after_validation(document)
41
+ @document = document
42
+ end
43
+ end
44
+
45
+ class GlobalObserver < MongoMapper::Observer
46
+ observe Article, Comment
47
+ attr_reader :document
48
+
49
+ def before_save(document)
50
+ @document = document
51
+ end
52
+ end
53
+
54
+ class NonAutomaticObserver < MongoMapper::Observer
55
+ observe Comment
56
+ attr_reader :comment
57
+
58
+ def after_validation(comment)
59
+ @comment = comment
60
+ end
61
+ end
62
+
63
+ class ObserverTest < Test::Unit::TestCase
64
+ should "fire model callbacks before observer" do
65
+ callers = []
66
+ comment = Comment.new
67
+ comment.callers = callers
68
+
69
+ CommentObserver.instance.callers = callers
70
+
71
+ comment.valid?
72
+ callers.should == [Comment, Comment, CommentObserver]
73
+ end
74
+
75
+ should "automatically observe model based on name when possible" do
76
+ CommentObserver.observed_class.should == Comment
77
+ end
78
+
79
+ should "be able to observe other models using observe" do
80
+ obs = NonAutomaticObserver.instance
81
+ comment = Comment.new(:name => 'John Nunemaker', :body => 'is awesome')
82
+ comment.valid?
83
+ obs.comment.name.should == 'John Nunemaker'
84
+ obs.comment.body.should == 'is awesome'
85
+ end
86
+
87
+ should "be able to observe multiple models" do
88
+ obs = AuditObserver.instance
89
+ comment = Comment.new(:name => 'Steve Smith', :body => 'is awesome')
90
+ comment.valid?
91
+
92
+ obs.document.name.should == 'Steve Smith'
93
+ obs.document.body.should == 'is awesome'
94
+
95
+ article = Article.new(:title => 'Ordered List Is Awesome', :body => 'Learn to accept it!')
96
+ article.valid?
97
+
98
+ obs.document.title.should == 'Ordered List Is Awesome'
99
+ obs.document.body.should == 'Learn to accept it!'
100
+ end
101
+ end
@@ -0,0 +1,113 @@
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
+ context "previous_page" do
67
+ should "be nil if current page 1" do
68
+ PaginationProxy.new(25, 1, 10).previous_page.should be_nil
69
+ end
70
+
71
+ should "be one less than current page if current is > 1" do
72
+ PaginationProxy.new(25, 2, 10).previous_page.should == 1
73
+ end
74
+ end
75
+
76
+ context "next_page" do
77
+ should "be nil if current page is last page" do
78
+ PaginationProxy.new(25, 3, 10).next_page.should be_nil
79
+ end
80
+
81
+ should "work for any page that is not last" do
82
+ PaginationProxy.new(25, 1, 10).next_page.should == 2
83
+ PaginationProxy.new(25, 2, 10).next_page.should == 3
84
+ end
85
+ end
86
+
87
+ context "previous_page" do
88
+ should "be nil if current page is first page" do
89
+ PaginationProxy.new(25, 1, 10).previous_page.should be_nil
90
+ end
91
+
92
+ should "work for any page other than first" do
93
+ PaginationProxy.new(25, 2, 10).previous_page.should == 1
94
+ PaginationProxy.new(25, 3, 10).previous_page.should == 2
95
+ end
96
+ end
97
+
98
+ context "out_of_bounds?" do
99
+ should "be true if current_page is greater than total_pages" do
100
+ PaginationProxy.new(25, 10, 4).out_of_bounds?.should be_true
101
+ end
102
+
103
+ should "be false if current_page is less than total_pages" do
104
+ PaginationProxy.new(25, 10, 1).out_of_bounds?.should be_false
105
+ PaginationProxy.new(25, 2, 10).out_of_bounds?.should be_false
106
+ end
107
+
108
+ should "be false if current_page is equal to total_pages" do
109
+ PaginationProxy.new(25, 3, 10).out_of_bounds?.should be_false
110
+ end
111
+ end
112
+ end # end of pagination proxy
113
+ end # end of test case
@@ -0,0 +1,49 @@
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
+ FirstItem.method(:has_many).should == FirstItem.method(:many)
26
+ end
27
+
28
+ should "have column names" do
29
+ Item.column_names.sort.should == ['_id', 'for_all']
30
+ FirstItem.column_names.sort.should == ['_id', 'first_only', 'for_all']
31
+ SecondItem.column_names.sort.should == ['_id', 'for_all', 'second_only']
32
+ end
33
+
34
+ should "alias new to new_record?" do
35
+ instance = Item.new
36
+ instance.new_record?.should == instance.new?
37
+ end
38
+
39
+ should "implement human_name" do
40
+ Item.human_name.should == 'Item'
41
+ end
42
+ end
43
+
44
+ context "Document" do
45
+ should "implement human_name" do
46
+ BigStuff.human_name.should == 'Big Stuff'
47
+ end
48
+ end
49
+ 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,342 @@
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 value if object id" do
171
+ id = Mongo::ObjectID.new
172
+ ObjectId.to_mongo(id).should be(id)
173
+ end
174
+
175
+ should "return object id if string" do
176
+ id = Mongo::ObjectID.new
177
+ ObjectId.to_mongo(id.to_s).should be(id)
178
+ end
179
+ end
180
+
181
+ context "ObjectId#from_mongo" do
182
+ should "return value" do
183
+ id = Mongo::ObjectID.new
184
+ ObjectId.from_mongo(id).should == id
185
+ end
186
+ end
187
+
188
+ context "NilClass#to_mongo" do
189
+ should "return nil" do
190
+ nil.to_mongo(nil).should be_nil
191
+ end
192
+ end
193
+
194
+ context "NilClass#from_mongo" do
195
+ should "return nil" do
196
+ nil.from_mongo(nil).should be_nil
197
+ end
198
+ end
199
+
200
+ context "Object#to_mongo" do
201
+ should "return value" do
202
+ Object.to_mongo(21).should == 21
203
+ Object.to_mongo('21').should == '21'
204
+ Object.to_mongo(9223372036854775807).should == 9223372036854775807
205
+ end
206
+ end
207
+
208
+ context "Object#from_mongo" do
209
+ should "return value" do
210
+ Object.from_mongo(21).should == 21
211
+ Object.from_mongo('21').should == '21'
212
+ Object.from_mongo(9223372036854775807).should == 9223372036854775807
213
+ end
214
+ end
215
+
216
+ context "Set#to_mongo" do
217
+ should "convert value to_a" do
218
+ Set.to_mongo(Set.new([1,2,3])).should == [1,2,3]
219
+ end
220
+
221
+ should "convert to empty array if nil" do
222
+ Set.to_mongo(nil).should == []
223
+ end
224
+ end
225
+
226
+ context "Set#from_mongo" do
227
+ should "be a set if array" do
228
+ Set.from_mongo([1,2,3]).should == Set.new([1,2,3])
229
+ end
230
+
231
+ should "be empty set if nil" do
232
+ Set.from_mongo(nil).should == Set.new([])
233
+ end
234
+ end
235
+
236
+ context "String#to_mongo" do
237
+ should "convert value to_s" do
238
+ [21, '21'].each do |value|
239
+ String.to_mongo(value).should == '21'
240
+ end
241
+ end
242
+
243
+ should "be nil if nil" do
244
+ String.to_mongo(nil).should be_nil
245
+ end
246
+ end
247
+
248
+ context "String#from_mongo" do
249
+ should "be string if value present" do
250
+ String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
251
+ end
252
+
253
+ should "return nil if nil" do
254
+ String.from_mongo(nil).should be_nil
255
+ end
256
+
257
+ should "return empty string if blank" do
258
+ String.from_mongo('').should == ''
259
+ end
260
+ end
261
+
262
+ context "Time#to_mongo without Time.zone" do
263
+ should "be time if string" do
264
+ Time.to_mongo('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123456).utc
265
+ end
266
+
267
+ should "be time in utc if time" do
268
+ Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
269
+ end
270
+
271
+ should "be nil if blank string" do
272
+ Time.to_mongo('').should be_nil
273
+ end
274
+
275
+ should "not be nil if nil" do
276
+ Time.to_mongo(nil).should be_nil
277
+ end
278
+ end
279
+
280
+ context "Time#to_mongo with Time.zone" do
281
+ should "be time if time" do
282
+ Time.zone = 'Hawaii'
283
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0)).should == Time.utc(2009, 8, 16, 0, 0, 0)
284
+ Time.zone = nil
285
+ end
286
+
287
+ should "be time if string" do
288
+ Time.zone = 'Hawaii'
289
+ Time.to_mongo('2009-08-15 14:00:00').should == Time.utc(2009, 8, 16, 0, 0, 0)
290
+ Time.zone = nil
291
+ end
292
+
293
+ should "be nil if blank string" do
294
+ Time.zone = 'Hawaii'
295
+ Time.to_mongo('').should be_nil
296
+ Time.zone = nil
297
+ end
298
+
299
+ should "be nil if nil" do
300
+ Time.zone = 'Hawaii'
301
+ Time.to_mongo(nil).should be_nil
302
+ Time.zone = nil
303
+ end
304
+ end
305
+
306
+ context "Time#from_mongo without Time.zone" do
307
+ should "be time" do
308
+ time = Time.now
309
+ Time.from_mongo(time).should == time
310
+ end
311
+
312
+ should "be nil if nil" do
313
+ Time.from_mongo(nil).should be_nil
314
+ end
315
+ end
316
+
317
+ context "Time#from_mongo with Time.zone" do
318
+ should "be time in Time.zone" do
319
+ Time.zone = 'Hawaii'
320
+
321
+ time = Time.from_mongo(Time.utc(2009, 10, 1))
322
+ time.should == Time.zone.local(2009, 9, 30, 14)
323
+ time.is_a?(ActiveSupport::TimeWithZone).should be_true
324
+
325
+ Time.zone = nil
326
+ end
327
+
328
+ should "be nil if nil" do
329
+ Time.zone = 'Hawaii'
330
+ Time.from_mongo(nil).should be_nil
331
+ Time.zone = nil
332
+ end
333
+ end
334
+
335
+ context "Mongo::ObjectID.to_json" do
336
+ should "convert object id to string" do
337
+ id = Mongo::ObjectID.new
338
+ id.to_json.should == id.to_s
339
+ end
340
+ end
341
+
342
+ end