mongo_mapper-unstable 2010.3.8 → 2010.06.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/README.rdoc +4 -8
  2. data/bin/mmconsole +1 -1
  3. data/examples/keys.rb +37 -0
  4. data/examples/plugins.rb +41 -0
  5. data/examples/querying.rb +35 -0
  6. data/examples/scopes.rb +52 -0
  7. data/lib/mongo_mapper/connection.rb +83 -0
  8. data/lib/mongo_mapper/document.rb +11 -329
  9. data/lib/mongo_mapper/embedded_document.rb +9 -38
  10. data/lib/mongo_mapper/exceptions.rb +30 -0
  11. data/lib/mongo_mapper/extensions/array.rb +19 -0
  12. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  13. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  14. data/lib/mongo_mapper/extensions/date.rb +25 -0
  15. data/lib/mongo_mapper/extensions/float.rb +14 -0
  16. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  17. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  18. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  19. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  20. data/lib/mongo_mapper/extensions/object.rb +27 -0
  21. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  22. data/lib/mongo_mapper/extensions/set.rb +20 -0
  23. data/lib/mongo_mapper/extensions/string.rb +18 -0
  24. data/lib/mongo_mapper/extensions/time.rb +29 -0
  25. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  26. data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
  27. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
  28. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
  29. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  30. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
  31. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
  32. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
  33. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
  34. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  35. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
  36. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  37. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  38. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
  39. data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
  40. data/lib/mongo_mapper/plugins/associations.rb +14 -22
  41. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  42. data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
  43. data/lib/mongo_mapper/plugins/clone.rb +10 -4
  44. data/lib/mongo_mapper/plugins/descendants.rb +3 -2
  45. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  46. data/lib/mongo_mapper/plugins/document.rb +41 -0
  47. data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
  48. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  49. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  50. data/lib/mongo_mapper/plugins/equality.rb +4 -10
  51. data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
  52. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  53. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  54. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  55. data/lib/mongo_mapper/plugins/keys.rb +85 -110
  56. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  57. data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
  58. data/lib/mongo_mapper/plugins/pagination.rb +5 -15
  59. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  60. data/lib/mongo_mapper/plugins/protected.rb +9 -1
  61. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  62. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  63. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  64. data/lib/mongo_mapper/plugins/rails.rb +6 -1
  65. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  66. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  67. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  68. data/lib/mongo_mapper/plugins/serialization.rb +5 -4
  69. data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
  70. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  71. data/lib/mongo_mapper/plugins/validations.rb +9 -5
  72. data/lib/mongo_mapper/plugins.rb +1 -20
  73. data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
  74. data/lib/mongo_mapper/version.rb +4 -0
  75. data/lib/mongo_mapper.rb +71 -128
  76. data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
  77. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
  78. data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
  79. data/test/functional/associations/test_in_array_proxy.rb +7 -9
  80. data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
  81. data/test/functional/associations/test_many_documents_proxy.rb +186 -64
  82. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
  83. data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
  84. data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
  85. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  86. data/test/functional/associations/test_one_proxy.rb +70 -49
  87. data/test/functional/test_accessible.rb +168 -0
  88. data/test/functional/test_associations.rb +11 -11
  89. data/test/functional/test_binary.rb +5 -5
  90. data/test/functional/test_caching.rb +76 -0
  91. data/test/functional/test_callbacks.rb +104 -34
  92. data/test/functional/test_dirty.rb +16 -16
  93. data/test/functional/test_document.rb +12 -924
  94. data/test/functional/test_dynamic_querying.rb +75 -0
  95. data/test/functional/test_embedded_document.rb +88 -8
  96. data/test/functional/test_identity_map.rb +41 -43
  97. data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
  98. data/test/functional/test_logger.rb +1 -1
  99. data/test/functional/test_modifiers.rb +275 -181
  100. data/test/functional/test_pagination.rb +13 -15
  101. data/test/functional/test_protected.rb +25 -11
  102. data/test/functional/test_querying.rb +873 -0
  103. data/test/functional/test_safe.rb +76 -0
  104. data/test/functional/test_sci.rb +230 -0
  105. data/test/functional/test_scopes.rb +171 -0
  106. data/test/functional/test_string_id_compatibility.rb +11 -11
  107. data/test/functional/test_timestamps.rb +0 -2
  108. data/test/functional/test_userstamps.rb +0 -1
  109. data/test/functional/test_validations.rb +44 -31
  110. data/test/models.rb +18 -17
  111. data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
  112. data/test/test_helper.rb +59 -16
  113. data/test/unit/associations/test_base.rb +47 -42
  114. data/test/unit/associations/test_proxy.rb +15 -15
  115. data/test/unit/serializers/test_json_serializer.rb +29 -29
  116. data/test/unit/test_clone.rb +69 -0
  117. data/test/unit/test_descendant_appends.rb +3 -3
  118. data/test/unit/test_document.rb +49 -67
  119. data/test/unit/test_dynamic_finder.rb +53 -51
  120. data/test/unit/test_embedded_document.rb +19 -38
  121. data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
  122. data/test/unit/test_key.rb +185 -0
  123. data/test/unit/test_keys.rb +29 -147
  124. data/test/unit/test_mongo_mapper.rb +3 -48
  125. data/test/unit/test_pagination.rb +1 -150
  126. data/test/unit/test_rails.rb +77 -19
  127. data/test/unit/test_rails_compatibility.rb +12 -12
  128. data/test/unit/test_serialization.rb +5 -5
  129. data/test/unit/test_time_zones.rb +9 -9
  130. data/test/unit/test_validations.rb +46 -46
  131. metadata +157 -155
  132. data/.gitignore +0 -10
  133. data/Rakefile +0 -55
  134. data/VERSION +0 -1
  135. data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
  136. data/lib/mongo_mapper/query.rb +0 -130
  137. data/lib/mongo_mapper/support.rb +0 -215
  138. data/mongo_mapper.gemspec +0 -196
  139. data/performance/read_write.rb +0 -52
  140. data/specs.watchr +0 -51
  141. data/test/support/custom_matchers.rb +0 -55
  142. data/test/support/timing.rb +0 -16
  143. data/test/unit/test_query.rb +0 -340
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class SupportTest < Test::Unit::TestCase
4
- context "Array#to_mongo" do
4
+ context "Array.to_mongo" do
5
5
  should "convert value to_a" do
6
6
  Array.to_mongo([1, 2, 3, 4]).should == [1, 2, 3, 4]
7
7
  Array.to_mongo('1').should == ['1']
@@ -9,74 +9,78 @@ class SupportTest < Test::Unit::TestCase
9
9
  end
10
10
  end
11
11
 
12
- context "Array#from_mongo" do
12
+ context "Array.from_mongo" do
13
13
  should "be array if array" do
14
14
  Array.from_mongo([1, 2]).should == [1, 2]
15
15
  end
16
-
16
+
17
17
  should "be empty array if nil" do
18
18
  Array.from_mongo(nil).should == []
19
19
  end
20
20
  end
21
-
22
- context "Binary#to_mongo" do
21
+
22
+ context "Binary.to_mongo" do
23
23
  should "convert to byte buffer if not byte buffer" do
24
- Binary.to_mongo('asdfsadasdfs').is_a?(ByteBuffer).should be_true
24
+ Binary.to_mongo('asdfsadasdfs').is_a?(BSON::ByteBuffer).should be_true
25
25
  end
26
-
26
+
27
27
  should "be byte buffer if byte buffer" do
28
- Binary.to_mongo(ByteBuffer.new('asdfsadasdfs')).is_a?(ByteBuffer).should be_true
28
+ Binary.to_mongo(BSON::ByteBuffer.new('asdfsadasdfs')).is_a?(BSON::ByteBuffer).should be_true
29
29
  end
30
-
30
+
31
31
  should "be nil if nil" do
32
32
  Binary.to_mongo(nil).should be_nil
33
33
  end
34
34
  end
35
-
36
- context "Binary#from_mongo" do
35
+
36
+ context "Binary.from_mongo" do
37
37
  should "return value" do
38
- buffer = ByteBuffer.new('asdfasdfasdf')
38
+ buffer = BSON::ByteBuffer.new('asdfasdfasdf')
39
39
  Binary.from_mongo(buffer).to_s.should == buffer.to_s
40
40
  end
41
41
  end
42
-
43
- context "Boolean#to_mongo" do
42
+
43
+ context "Boolean.to_mongo" do
44
44
  should "be true for true" do
45
45
  Boolean.to_mongo(true).should be_true
46
46
  end
47
-
47
+
48
48
  should "be false for false" do
49
49
  Boolean.to_mongo(false).should be_false
50
50
  end
51
-
51
+
52
52
  should "handle odd assortment of other values" do
53
53
  Boolean.to_mongo('true').should be_true
54
54
  Boolean.to_mongo('t').should be_true
55
55
  Boolean.to_mongo('1').should be_true
56
56
  Boolean.to_mongo(1).should be_true
57
-
57
+
58
58
  Boolean.to_mongo('false').should be_false
59
59
  Boolean.to_mongo('f').should be_false
60
60
  Boolean.to_mongo('0').should be_false
61
61
  Boolean.to_mongo(0).should be_false
62
62
  end
63
+
64
+ should "be nil for nil" do
65
+ Boolean.to_mongo(nil).should be_nil
66
+ end
63
67
  end
64
-
65
- context "Boolean#from_mongo" do
68
+
69
+ context "Boolean.from_mongo" do
66
70
  should "be true for true" do
67
71
  Boolean.from_mongo(true).should be_true
68
72
  end
69
-
73
+
70
74
  should "be false for false" do
71
75
  Boolean.from_mongo(false).should be_false
72
76
  end
73
-
74
- should "be false for nil" do
75
- Boolean.from_mongo(nil).should be_false
77
+
78
+ should "be nil for nil" do
79
+ Boolean.from_mongo(nil).should be_nil
76
80
  end
77
81
  end
78
-
79
- context "Date#to_mongo" do
82
+
83
+ context "Date.to_mongo" do
80
84
  should "be time if string" do
81
85
  date = Date.to_mongo('2009-10-01')
82
86
  date.should == Time.utc(2009, 10, 1)
@@ -85,25 +89,25 @@ class SupportTest < Test::Unit::TestCase
85
89
  date.day.should == 1
86
90
  date.year.should == 2009
87
91
  end
88
-
92
+
89
93
  should "be time if date" do
90
94
  Date.to_mongo(Date.new(2009, 10, 1)).should == Time.utc(2009, 10, 1)
91
95
  end
92
-
96
+
93
97
  should "be date if time" do
94
98
  Date.to_mongo(Time.parse("2009-10-1T12:30:00")).should == Time.utc(2009, 10, 1)
95
99
  end
96
-
100
+
97
101
  should "be nil if bogus string" do
98
102
  Date.to_mongo('jdsafop874').should be_nil
99
103
  end
100
-
104
+
101
105
  should "be nil if empty string" do
102
106
  Date.to_mongo('').should be_nil
103
107
  end
104
108
  end
105
-
106
- context "Date#from_mongo" do
109
+
110
+ context "Date.from_mongo" do
107
111
  should "be date if date" do
108
112
  date = Date.new(2009, 10, 1)
109
113
  from_date = Date.from_mongo(date)
@@ -112,116 +116,117 @@ class SupportTest < Test::Unit::TestCase
112
116
  from_date.day.should == 1
113
117
  from_date.year.should == 2009
114
118
  end
115
-
119
+
116
120
  should "be date if time" do
117
121
  time = Time.now
118
122
  Date.from_mongo(time).should == time.to_date
119
123
  end
120
-
124
+
121
125
  should "be nil if nil" do
122
126
  Date.from_mongo(nil).should be_nil
123
127
  end
124
128
  end
125
-
126
- context "Float#to_mongo" do
129
+
130
+ context "Float.to_mongo" do
127
131
  should "convert value to_f" do
128
132
  [21, 21.0, '21'].each do |value|
129
133
  Float.to_mongo(value).should == 21.0
130
134
  end
131
135
  end
136
+
137
+ should "leave nil values nil" do
138
+ Float.to_mongo(nil).should == nil
139
+ end
132
140
  end
133
-
134
- context "Hash#from_mongo" do
141
+
142
+ context "Hash.from_mongo" do
135
143
  should "convert hash to hash with indifferent access" do
136
144
  hash = Hash.from_mongo(:foo => 'bar')
137
145
  hash[:foo].should == 'bar'
138
146
  hash['foo'].should == 'bar'
139
147
  end
140
-
148
+
141
149
  should "be hash if nil" do
142
150
  hash = Hash.from_mongo(nil)
143
151
  hash.should == {}
144
152
  hash.is_a?(HashWithIndifferentAccess).should be_true
145
153
  end
146
154
  end
147
-
148
- context "Hash#to_mongo instance method" do
155
+
156
+ context "Hash.to_mongo instance method" do
149
157
  should "have instance method that returns self" do
150
158
  hash = HashWithIndifferentAccess.new('foo' => 'bar')
151
159
  hash.to_mongo.should == {'foo' => 'bar'}
152
160
  end
153
161
  end
154
-
155
- context "Integer#to_mongo" do
162
+
163
+ context "Integer.to_mongo" do
156
164
  should "convert value to integer" do
157
165
  [21, 21.0, '21'].each do |value|
158
166
  Integer.to_mongo(value).should == 21
159
167
  end
160
168
  end
161
-
169
+
162
170
  should "work fine with big integers" do
163
171
  [9223372036854775807, '9223372036854775807'].each do |value|
164
172
  Integer.to_mongo(value).should == 9223372036854775807
165
173
  end
166
174
  end
167
175
  end
168
-
176
+
177
+ context "NilClass#from_mongo" do
178
+ should "return nil" do
179
+ nil.from_mongo(nil).should be_nil
180
+ end
181
+ end
182
+
183
+ context "NilClass#to_mongo" do
184
+ should "return nil" do
185
+ nil.to_mongo(nil).should be_nil
186
+ end
187
+ end
188
+
169
189
  context "ObjectId#to_mongo" do
190
+ should "call class to_mongo with self" do
191
+ object = Object.new
192
+ object.class.expects(:to_mongo).with(object)
193
+ object.to_mongo
194
+ end
195
+ end
196
+
197
+ context "ObjectId.to_mongo" do
170
198
  should "return nil for nil" do
171
199
  ObjectId.to_mongo(nil).should be_nil
172
200
  end
173
-
201
+
174
202
  should "return nil if blank string" do
175
203
  ObjectId.to_mongo('').should be_nil
176
204
  end
177
-
205
+
178
206
  should "return value if object id" do
179
- id = Mongo::ObjectID.new
207
+ id = BSON::ObjectID.new
180
208
  ObjectId.to_mongo(id).should be(id)
181
209
  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
210
+
209
211
  should "return value" do
210
212
  Object.to_mongo(21).should == 21
211
213
  Object.to_mongo('21').should == '21'
212
214
  Object.to_mongo(9223372036854775807).should == 9223372036854775807
213
215
  end
214
216
  end
215
-
216
- context "Object#from_mongo" do
217
+
218
+ context "ObjectId.from_mongo" do
217
219
  should "return value" do
218
220
  Object.from_mongo(21).should == 21
219
221
  Object.from_mongo('21').should == '21'
220
222
  Object.from_mongo(9223372036854775807).should == 9223372036854775807
223
+
224
+ id = BSON::ObjectID.new
225
+ ObjectId.from_mongo(id).should == id
221
226
  end
222
227
  end
223
-
224
- context "Set#to_mongo" do
228
+
229
+ context "Set.to_mongo" do
225
230
  should "convert value to_a" do
226
231
  Set.to_mongo(Set.new([1,2,3])).should == [1,2,3]
227
232
  end
@@ -231,7 +236,7 @@ class SupportTest < Test::Unit::TestCase
231
236
  end
232
237
  end
233
238
 
234
- context "Set#from_mongo" do
239
+ context "Set.from_mongo" do
235
240
  should "be a set if array" do
236
241
  Set.from_mongo([1,2,3]).should == Set.new([1,2,3])
237
242
  end
@@ -240,127 +245,136 @@ class SupportTest < Test::Unit::TestCase
240
245
  Set.from_mongo(nil).should == Set.new([])
241
246
  end
242
247
  end
243
-
244
- context "String#to_mongo" do
248
+
249
+ context "String.to_mongo" do
245
250
  should "convert value to_s" do
246
251
  [21, '21'].each do |value|
247
252
  String.to_mongo(value).should == '21'
248
253
  end
249
254
  end
250
-
255
+
251
256
  should "be nil if nil" do
252
257
  String.to_mongo(nil).should be_nil
253
258
  end
254
259
  end
255
260
 
256
- context "String#from_mongo" do
261
+ context "String.from_mongo" do
257
262
  should "be string if value present" do
258
263
  String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
259
264
  end
260
-
265
+
261
266
  should "return nil if nil" do
262
267
  String.from_mongo(nil).should be_nil
263
268
  end
264
-
269
+
265
270
  should "return empty string if blank" do
266
271
  String.from_mongo('').should == ''
267
272
  end
268
273
  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
274
+
275
+ context "Time.to_mongo without Time.zone" do
279
276
  setup do
280
277
  Time.zone = nil
281
278
  end
282
279
 
283
280
  should "be time to milliseconds if string" do
284
- 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
281
+ Time.to_mongo('2000-01-01 01:01:01.123456').to_f.should == Time.local(2000, 1, 1, 1, 1, 1, 0).utc.to_f
285
282
  end
286
-
283
+
287
284
  should "be time in utc if time" do
288
285
  Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
289
286
  end
290
-
287
+
291
288
  should "be nil if blank string" do
292
289
  Time.to_mongo('').should be_nil
293
290
  end
294
-
291
+
295
292
  should "not be nil if nil" do
296
293
  Time.to_mongo(nil).should be_nil
297
294
  end
298
295
  end
299
-
300
- context "Time#to_mongo with Time.zone" do
296
+
297
+ context "Time.to_mongo with Time.zone" do
301
298
  should "be time to milliseconds if time" do
302
299
  Time.zone = 'Hawaii'
303
- 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
300
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 0).to_f
304
301
  Time.zone = nil
305
302
  end
306
303
 
307
304
  should "be time to milliseconds if string" do
308
305
  Time.zone = 'Hawaii'
309
- Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 123000).to_f
306
+ Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 0).to_f
310
307
  Time.zone = nil
311
308
  end
312
-
309
+
310
+ should "not round up times at the end of the month" do
311
+ Time.to_mongo(Time.now.end_of_month).to_i.should == Time.now.end_of_month.utc.to_i
312
+ end
313
+
313
314
  should "be nil if blank string" do
314
315
  Time.zone = 'Hawaii'
315
316
  Time.to_mongo('').should be_nil
316
317
  Time.zone = nil
317
318
  end
318
-
319
+
319
320
  should "be nil if nil" do
320
321
  Time.zone = 'Hawaii'
321
322
  Time.to_mongo(nil).should be_nil
322
323
  Time.zone = nil
323
324
  end
324
325
  end
325
-
326
- context "Time#from_mongo without Time.zone" do
326
+
327
+ context "Time.from_mongo without Time.zone" do
327
328
  should "be time" do
328
329
  time = Time.now
329
330
  Time.from_mongo(time).should == time
330
331
  end
331
-
332
+
332
333
  should "be nil if nil" do
333
334
  Time.from_mongo(nil).should be_nil
334
335
  end
335
336
  end
336
-
337
- context "Time#from_mongo with Time.zone" do
337
+
338
+ context "Time.from_mongo with Time.zone" do
338
339
  should "be time in Time.zone" do
339
340
  Time.zone = 'Hawaii'
340
-
341
+
341
342
  time = Time.from_mongo(Time.utc(2009, 10, 1))
342
343
  time.should == Time.zone.local(2009, 9, 30, 14)
343
344
  time.is_a?(ActiveSupport::TimeWithZone).should be_true
344
-
345
+
345
346
  Time.zone = nil
346
347
  end
347
-
348
+
348
349
  should "be nil if nil" do
349
350
  Time.zone = 'Hawaii'
350
351
  Time.from_mongo(nil).should be_nil
351
352
  Time.zone = nil
352
353
  end
353
354
  end
354
-
355
- context "Mongo::ObjectID.to_json" do
356
- should "convert object id to string" do
357
- id = Mongo::ObjectID.new
358
- id.to_json.should == %Q("#{id}")
355
+
356
+ context "BSON::ObjectID" do
357
+ context "#as_json" do
358
+ should "convert object id to string" do
359
+ id = BSON::ObjectID.new
360
+ id.as_json.should == id.to_s
361
+ end
359
362
  end
360
-
361
- should "support ruby driver syntax also" do
362
- id = Mongo::ObjectID.new
363
- id.original_to_json.should == %Q({"$oid": "#{id}"})
363
+
364
+ context "#to_json" do
365
+ should "convert object id to string" do
366
+ id = BSON::ObjectID.new
367
+ id.to_json.should == %Q("#{id}")
368
+ end
369
+
370
+ should "support ruby driver syntax also" do
371
+ id = BSON::ObjectID.new
372
+ id.original_to_json.should == %Q({"$oid": "#{id}"})
373
+ end
364
374
  end
365
375
  end
376
+
377
+ context "BSON::ObjectID.to_json" do
378
+
379
+ end
366
380
  end
@@ -0,0 +1,185 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class FooType < Struct.new(:bar)
5
+ def self.to_mongo(value)
6
+ 'to_mongo'
7
+ end
8
+
9
+ def self.from_mongo(value)
10
+ 'from_mongo'
11
+ end
12
+ end
13
+
14
+ class KeyTest < Test::Unit::TestCase
15
+ include MongoMapper::Plugins::Keys
16
+
17
+ context "Initializing a new key" do
18
+ should "allow setting the name" do
19
+ Key.new(:foo, String).name.should == 'foo'
20
+ end
21
+
22
+ should "allow setting the type" do
23
+ Key.new(:foo, Integer).type.should be(Integer)
24
+ end
25
+
26
+ should "allow setting options" do
27
+ Key.new(:foo, Integer, :required => true).options[:required].should be(true)
28
+ end
29
+
30
+ should "default options to {}" do
31
+ Key.new(:foo, Integer, nil).options.should == {}
32
+ end
33
+
34
+ should "symbolize option keys" do
35
+ Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
36
+ end
37
+
38
+ should "work with just name" do
39
+ key = Key.new(:foo)
40
+ key.name.should == 'foo'
41
+ end
42
+
43
+ should "work with name and type" do
44
+ key = Key.new(:foo, String)
45
+ key.name.should == 'foo'
46
+ key.type.should == String
47
+ end
48
+
49
+ should "work with name, type, and options" do
50
+ key = Key.new(:foo, String, :required => true)
51
+ key.name.should == 'foo'
52
+ key.type.should == String
53
+ key.options[:required].should be_true
54
+ end
55
+
56
+ should "work with name and options" do
57
+ key = Key.new(:foo, :required => true)
58
+ key.name.should == 'foo'
59
+ key.options[:required].should be_true
60
+ end
61
+ end
62
+
63
+ context "A key" do
64
+ should "be equal to another key with same name and type" do
65
+ Key.new(:name, String).should == Key.new(:name, String)
66
+ end
67
+
68
+ should "not be equal to another key with different name" do
69
+ Key.new(:name, String).should_not == Key.new(:foo, String)
70
+ end
71
+
72
+ should "not be equal to another key with different type" do
73
+ Key.new(:name, String).should_not == Key.new(:name, Integer)
74
+ end
75
+
76
+ should "know if it is a embedded_document" do
77
+ Key.new(:name, EDoc()).embeddable?.should be_true
78
+ end
79
+
80
+ should "know if it is not a embedded_document" do
81
+ Key.new(:name, String).embeddable?.should be_false
82
+ end
83
+
84
+ should "know if it is a number" do
85
+ Key.new(:age, Integer).number?.should be_true
86
+ Key.new(:age, Float).number?.should be_true
87
+ end
88
+
89
+ should "know if it is not a number" do
90
+ Key.new(:age, String).number?.should be_false
91
+ end
92
+ end
93
+
94
+ context "for an array with :typecast option" do
95
+ setup { @key = Key.new(:user_ids, Array, :typecast => 'ObjectId') }
96
+ subject { @key }
97
+
98
+ should "cast each element correctly" do
99
+ ids = [BSON::ObjectID.new, BSON::ObjectID.new, BSON::ObjectID.new.to_s, BSON::ObjectID.new.to_s]
100
+ subject.set(ids).should == ids.map { |id| ObjectId.to_mongo(id) }
101
+ end
102
+ end
103
+
104
+ context "for a set with :typecast option" do
105
+ setup { @key = Key.new(:user_ids, Set, :typecast => 'ObjectId') }
106
+ subject { @key }
107
+
108
+ should "cast each element correctly" do
109
+ ids = [BSON::ObjectID.new, BSON::ObjectID.new, BSON::ObjectID.new.to_s, BSON::ObjectID.new.to_s]
110
+ subject.set(ids).should == ids.map { |id| ObjectId.to_mongo(id) }
111
+ end
112
+ end
113
+
114
+ context "setting a value with a custom type" do
115
+ should "correctly typecast" do
116
+ key = Key.new(:foo, FooType)
117
+ key.set("something").should == 'to_mongo'
118
+ end
119
+
120
+ should "correctly typecast if object of that type is given" do
121
+ key = Key.new(:foo, FooType)
122
+ key.set(FooType.new('something')).should == 'to_mongo'
123
+ end
124
+ end
125
+
126
+ context "getting a value with a custom type" do
127
+ should "use #from_mongo to convert back to custom type" do
128
+ key = Key.new(:foo, FooType)
129
+ key.get('something').should == 'from_mongo'
130
+ end
131
+ end
132
+
133
+ context "getting a value" do
134
+ should "work with a type" do
135
+ key = Key.new(:foo, String)
136
+ key.get('bar').should == 'bar'
137
+ end
138
+
139
+ should "work without type" do
140
+ key = Key.new(:foo)
141
+ key.get([1, '2']).should == [1, '2']
142
+ key.get(false).should == false
143
+ key.get({}).should == {}
144
+ end
145
+
146
+ context "for a embedded_document" do
147
+ should "default to nil" do
148
+ key = Key.new(:foo, Address)
149
+ key.get(nil).should be_nil
150
+ end
151
+
152
+ should "return instance if instance" do
153
+ address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
154
+ key = Key.new(:foo, Address)
155
+ key.get(address).should == address
156
+ end
157
+ end
158
+ end
159
+
160
+ context "getting a value with a default set" do
161
+ setup do
162
+ @key = Key.new(:foo, String, :default => 'baz')
163
+ end
164
+
165
+ should "return default value if value nil" do
166
+ @key.get(nil).should == 'baz'
167
+ end
168
+
169
+ should "return value if not blank" do
170
+ @key.get('foobar').should == 'foobar'
171
+ end
172
+
173
+ should "work with Boolean type and false value" do
174
+ Key.new(:active, Boolean, :default => false).get(nil).should be_false
175
+ end
176
+
177
+ should "work with Boolean type and true value" do
178
+ Key.new(:active, Boolean, :default => true).get(nil).should be_true
179
+ end
180
+
181
+ should "work with procs" do
182
+ Key.new(:foo, String, :default => lambda { return 'hello world' }).get(nil).should == "hello world"
183
+ end
184
+ end
185
+ end # KeyTest