github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jnunemaker / mongomapper

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 713
    • 142
  • Source
  • Commits
  • Network (142)
  • Downloads (33)
  • Wiki (4)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (33)
    • v0.6.10
    • v0.6.9
    • v0.6.8
    • v0.6.7
    • v0.6.6
    • v0.6.5
    • v0.6.4
    • v0.6.3
    • v0.6.2
    • v0.6.1
    • v0.6.0
    • v0.5.8
    • v0.5.7
    • v0.5.6
    • v0.5.5
    • v0.5.4
    • v0.5.3
    • v0.5.2
    • v0.5.1
    • v0.5.0
    • v0.4.2
    • v0.4.1
    • v0.4.0
    • v0.3.5
    • v0.3.4
    • v0.3.3
    • v0.3.2
    • v0.3.1
    • v0.3.0
    • v0.2.0
    • v0.1.2
    • v0.1.1
    • v0.1.0
Sending Request…
Click here to lend your support to: mongomapper and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Awesome gem for modeling your domain and storing it in mongo — Read more

  cancel

http://mongomapper.com

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Moved some things around and autoloaded some stuff. 
jnunemaker (author)
Tue Feb 09 17:35:17 -0800 2010
commit  67590db2835efc1110cdd3057e07a01072123d0e
tree    aa6b6ccf9856fc6234e793cbb8e5f71f3b961111
parent  64c24faa470e10cbd330afdaa08493b593441479
mongomapper / test / unit / test_support.rb test/unit/test_support.rb
100644 362 lines (299 sloc) 9.411 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require 'test_helper'
 
class SupportTest < Test::Unit::TestCase
  context "Array#to_mongo" do
    should "convert value to_a" do
      Array.to_mongo([1, 2, 3, 4]).should == [1, 2, 3, 4]
      Array.to_mongo('1').should == ['1']
      Array.to_mongo({'1' => '2', '3' => '4'}).should == [['1', '2'], ['3', '4']]
    end
  end
 
  context "Array#from_mongo" do
    should "be array if array" do
      Array.from_mongo([1, 2]).should == [1, 2]
    end
    
    should "be empty array if nil" do
      Array.from_mongo(nil).should == []
    end
  end
  
  context "Binary#to_mongo" do
    should "convert to byte buffer if not byte buffer" do
      Binary.to_mongo('asdfsadasdfs').is_a?(ByteBuffer).should be_true
    end
    
    should "be byte buffer if byte buffer" do
      Binary.to_mongo(ByteBuffer.new('asdfsadasdfs')).is_a?(ByteBuffer).should be_true
    end
    
    should "be nil if nil" do
      Binary.to_mongo(nil).should be_nil
    end
  end
  
  context "Binary#from_mongo" do
    should "return value" do
      buffer = ByteBuffer.new('asdfasdfasdf')
      Binary.from_mongo(buffer).to_s.should == buffer.to_s
    end
  end
  
  context "Boolean#to_mongo" do
    should "be true for true" do
      Boolean.to_mongo(true).should be_true
    end
    
    should "be false for false" do
      Boolean.to_mongo(false).should be_false
    end
    
    should "handle odd assortment of other values" do
      Boolean.to_mongo('true').should be_true
      Boolean.to_mongo('t').should be_true
      Boolean.to_mongo('1').should be_true
      Boolean.to_mongo(1).should be_true
      
      Boolean.to_mongo('false').should be_false
      Boolean.to_mongo('f').should be_false
      Boolean.to_mongo('0').should be_false
      Boolean.to_mongo(0).should be_false
    end
  end
  
  context "Boolean#from_mongo" do
    should "be true for true" do
      Boolean.from_mongo(true).should be_true
    end
    
    should "be false for false" do
      Boolean.from_mongo(false).should be_false
    end
    
    should "be false for nil" do
      Boolean.from_mongo(nil).should be_false
    end
  end
  
  context "Date#to_mongo" do
    should "be time if string" do
      date = Date.to_mongo('10/1/2009')
      date.should == Time.utc(2009, 10, 1)
      date.should == date
      date.month.should == 10
      date.day.should == 1
      date.year.should == 2009
    end
    
    should "be time if date" do
      Date.to_mongo(Date.new(2009, 10, 1)).should == Time.utc(2009, 10, 1)
    end
    
    should "be date if time" do
      Date.to_mongo(Time.parse("2009-10-1T12:30:00")).should == Time.utc(2009, 10, 1)
    end
    
    should "be nil if bogus string" do
      Date.to_mongo('jdsafop874').should be_nil
    end
    
    should "be nil if empty string" do
      Date.to_mongo('').should be_nil
    end
  end
  
  context "Date#from_mongo" do
    should "be date if date" do
      date = Date.new(2009, 10, 1)
      from_date = Date.from_mongo(date)
      from_date.should == date
      from_date.month.should == 10
      from_date.day.should == 1
      from_date.year.should == 2009
    end
    
    should "be date if time" do
      time = Time.now
      Date.from_mongo(time).should == time.to_date
    end
    
    should "be nil if nil" do
      Date.from_mongo(nil).should be_nil
    end
  end
  
  context "Float#to_mongo" do
    should "convert value to_f" do
      [21, 21.0, '21'].each do |value|
        Float.to_mongo(value).should == 21.0
      end
    end
  end
  
  context "Hash#from_mongo" do
    should "convert hash to hash with indifferent access" do
      hash = Hash.from_mongo(:foo => 'bar')
      hash[:foo].should == 'bar'
      hash['foo'].should == 'bar'
    end
    
    should "be hash if nil" do
      hash = Hash.from_mongo(nil)
      hash.should == {}
      hash.is_a?(HashWithIndifferentAccess).should be_true
    end
  end
  
  context "Hash#to_mongo instance method" do
    should "have instance method that returns self" do
      hash = HashWithIndifferentAccess.new('foo' => 'bar')
      hash.to_mongo.should == {'foo' => 'bar'}
    end
  end
  
  context "Integer#to_mongo" do
    should "convert value to integer" do
      [21, 21.0, '21'].each do |value|
        Integer.to_mongo(value).should == 21
      end
    end
    
    should "work fine with big integers" do
      [9223372036854775807, '9223372036854775807'].each do |value|
        Integer.to_mongo(value).should == 9223372036854775807
      end
    end
  end
  
  context "ObjectId#to_mongo" do
    should "return nil for nil" do
      ObjectId.to_mongo(nil).should be_nil
    end
    
    should "return nil if blank string" do
      ObjectId.to_mongo('').should be_nil
    end
    
    should "return value if object id" do
      id = Mongo::ObjectID.new
      ObjectId.to_mongo(id).should be(id)
    end
    
    should "return object id if string" do
      id = Mongo::ObjectID.new
      ObjectId.to_mongo(id.to_s).should be(id)
    end
  end
  
  context "ObjectId#from_mongo" do
    should "return value" do
      id = Mongo::ObjectID.new
      ObjectId.from_mongo(id).should == id
    end
  end
  
  context "NilClass#to_mongo" do
    should "return nil" do
      nil.to_mongo(nil).should be_nil
    end
  end
  
  context "NilClass#from_mongo" do
    should "return nil" do
      nil.from_mongo(nil).should be_nil
    end
  end
  
  context "Object#to_mongo" do
    should "return value" do
      Object.to_mongo(21).should == 21
      Object.to_mongo('21').should == '21'
      Object.to_mongo(9223372036854775807).should == 9223372036854775807
    end
  end
  
  context "Object#from_mongo" do
    should "return value" do
      Object.from_mongo(21).should == 21
      Object.from_mongo('21').should == '21'
      Object.from_mongo(9223372036854775807).should == 9223372036854775807
    end
  end
  
  context "Set#to_mongo" do
    should "convert value to_a" do
      Set.to_mongo(Set.new([1,2,3])).should == [1,2,3]
    end
 
    should "convert to empty array if nil" do
      Set.to_mongo(nil).should == []
    end
  end
 
  context "Set#from_mongo" do
    should "be a set if array" do
      Set.from_mongo([1,2,3]).should == Set.new([1,2,3])
    end
 
    should "be empty set if nil" do
      Set.from_mongo(nil).should == Set.new([])
    end
  end
  
  context "String#to_mongo" do
    should "convert value to_s" do
      [21, '21'].each do |value|
        String.to_mongo(value).should == '21'
      end
    end
    
    should "be nil if nil" do
      String.to_mongo(nil).should be_nil
    end
  end
 
  context "String#from_mongo" do
    should "be string if value present" do
      String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
    end
    
    should "return nil if nil" do
      String.from_mongo(nil).should be_nil
    end
    
    should "return empty string if blank" do
      String.from_mongo('').should == ''
    end
  end
  
  context "Symbol" do
    %w(gt lt gte lte ne in nin mod all size where exists asc desc).each do |operator|
      should "have $#{operator} operator" do
        :foo.respond_to?(operator)
      end
    end
  end
  
  context "Time#to_mongo without Time.zone" do
    should "be time to milliseconds if string" do
      Time.to_mongo('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123000).utc
    end
    
    should "be time in utc if time" do
      Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
    end
    
    should "be nil if blank string" do
      Time.to_mongo('').should be_nil
    end
    
    should "not be nil if nil" do
      Time.to_mongo(nil).should be_nil
    end
  end
  
  context "Time#to_mongo with Time.zone" do
    should "be time to milliseconds if time" do
      Time.zone = 'Hawaii'
      Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).should == Time.utc(2009, 8, 16, 0, 0, 0, 123000)
      Time.zone = nil
    end
 
    should "be time to milliseconds if string" do
      Time.zone = 'Hawaii'
      Time.to_mongo('2009-08-15 14:00:00.123456').should == Time.utc(2009, 8, 16, 0, 0, 0, 123000)
      Time.zone = nil
    end
    
    should "be nil if blank string" do
      Time.zone = 'Hawaii'
      Time.to_mongo('').should be_nil
      Time.zone = nil
    end
    
    should "be nil if nil" do
      Time.zone = 'Hawaii'
      Time.to_mongo(nil).should be_nil
      Time.zone = nil
    end
  end
  
  context "Time#from_mongo without Time.zone" do
    should "be time" do
      time = Time.now
      Time.from_mongo(time).should == time
    end
    
    should "be nil if nil" do
      Time.from_mongo(nil).should be_nil
    end
  end
  
  context "Time#from_mongo with Time.zone" do
    should "be time in Time.zone" do
      Time.zone = 'Hawaii'
      
      time = Time.from_mongo(Time.utc(2009, 10, 1))
      time.should == Time.zone.local(2009, 9, 30, 14)
      time.is_a?(ActiveSupport::TimeWithZone).should be_true
      
      Time.zone = nil
    end
    
    should "be nil if nil" do
      Time.zone = 'Hawaii'
      Time.from_mongo(nil).should be_nil
      Time.zone = nil
    end
  end
  
  context "Mongo::ObjectID.to_json" do
    should "convert object id to string" do
      id = Mongo::ObjectID.new
      id.to_json.should == %Q("#{id}")
    end
    
    should "support ruby driver syntax also" do
      id = Mongo::ObjectID.new
      id.original_to_json.should == %Q({"$oid": "#{id}"})
    end
  end
end
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server