bson 0.20

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

@@ -0,0 +1,82 @@
1
+ require 'test/test_helper'
2
+
3
+ class ByteBufferTest < Test::Unit::TestCase
4
+ include BSON
5
+
6
+ def setup
7
+ @buf = ByteBuffer.new
8
+ end
9
+
10
+ def test_nil_get_returns_one_byte
11
+ @buf.put_array([1, 2, 3, 4])
12
+ @buf.rewind
13
+ assert_equal 1, @buf.get
14
+ end
15
+
16
+ def test_one_get_returns_array_length_one
17
+ @buf.put_array([1, 2, 3, 4])
18
+ @buf.rewind
19
+ assert_equal [1], @buf.get(1)
20
+ end
21
+
22
+ def test_zero_get_returns_empty_array
23
+ @buf.put_array([1, 2, 3, 4])
24
+ @buf.rewind
25
+ assert_equal [], @buf.get(0)
26
+ end
27
+
28
+ def test_empty
29
+ assert_equal 0, @buf.length
30
+ end
31
+
32
+ def test_length
33
+ @buf.put_int 3
34
+ assert_equal 4, @buf.length
35
+ end
36
+
37
+ def test_default_order
38
+ assert_equal :little_endian, @buf.order
39
+ end
40
+
41
+ def test_long_length
42
+ @buf.put_long 1027
43
+ assert_equal 8, @buf.length
44
+ end
45
+
46
+ def test_get_long
47
+ @buf.put_long 1027
48
+ @buf.rewind
49
+ assert_equal 1027, @buf.get_long
50
+ end
51
+
52
+ def test_get_double
53
+ @buf.put_double 41.2
54
+ @buf.rewind
55
+ assert_equal 41.2, @buf.get_double
56
+ end
57
+
58
+ def test_rewrite
59
+ @buf.put_int(0)
60
+ @buf.rewind
61
+ @buf.put_int(1027)
62
+ assert_equal 4, @buf.length
63
+ @buf.rewind
64
+ assert_equal 1027, @buf.get_int
65
+ assert_equal 4, @buf.position
66
+ end
67
+
68
+ def test_prepend_byte_buffer
69
+ @buf.put_int(4)
70
+ new_buf = ByteBuffer.new([5, 0, 0, 0])
71
+ @buf.prepend!(new_buf)
72
+ assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
73
+ end
74
+
75
+ def test_append_byte_buffer
76
+ @buf.put_int(4)
77
+ new_buf = ByteBuffer.new([5, 0, 0, 0])
78
+ @buf.append!(new_buf)
79
+ assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
80
+ end
81
+
82
+ end
@@ -0,0 +1,126 @@
1
+ require 'test/test_helper'
2
+
3
+ class ObjectIDTest < Test::Unit::TestCase
4
+
5
+ include Mongo
6
+ include BSON
7
+
8
+ def setup
9
+ @o = ObjectID.new
10
+ end
11
+
12
+ def test_hashcode
13
+ assert_equal @o.instance_variable_get(:@data).hash, @o.hash
14
+ end
15
+
16
+ def test_array_uniq_for_equilavent_ids
17
+ a = ObjectID.new('123')
18
+ b = ObjectID.new('123')
19
+ assert_equal a, b
20
+ assert_equal 1, [a, b].uniq.size
21
+ end
22
+
23
+ def test_create_pk_method
24
+ doc = {:name => 'Mongo'}
25
+ doc = ObjectID.create_pk(doc)
26
+ assert doc[:_id]
27
+
28
+ doc = {:name => 'Mongo', :_id => '12345'}
29
+ doc = ObjectID.create_pk(doc)
30
+ assert_equal '12345', doc[:_id]
31
+ end
32
+
33
+ def test_different
34
+ a = ObjectID.new
35
+ b = ObjectID.new
36
+ assert_not_equal a.to_a, b.to_a
37
+ assert_not_equal a, b
38
+ end
39
+
40
+ def test_eql?
41
+ o2 = ObjectID.new(@o.to_a)
42
+ assert_equal @o, o2
43
+ end
44
+
45
+ def test_to_s
46
+ s = @o.to_s
47
+ assert_equal 24, s.length
48
+ s =~ /^([0-9a-f]+)$/
49
+ assert_equal 24, $1.length
50
+ end
51
+
52
+ def test_inspect
53
+ assert_equal "ObjectID('#{@o.to_s}')", @o.inspect
54
+ end
55
+
56
+ def test_save_and_restore
57
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
58
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
59
+ db = Connection.new(host, port).db(MONGO_TEST_DB)
60
+ coll = db.collection('test')
61
+
62
+ coll.remove
63
+ coll << {'a' => 1, '_id' => @o}
64
+
65
+ row = coll.find().collect.first
66
+ assert_equal 1, row['a']
67
+ assert_equal @o, row['_id']
68
+ end
69
+
70
+ def test_from_string
71
+ hex_str = @o.to_s
72
+ o2 = ObjectID.from_string(hex_str)
73
+ assert_equal hex_str, o2.to_s
74
+ assert_equal @o, o2
75
+ assert_equal @o.to_s, o2.to_s
76
+ end
77
+
78
+ def test_illegal_from_string
79
+ assert_raise InvalidObjectID do
80
+ ObjectID.from_string("")
81
+ end
82
+ end
83
+
84
+ def test_legal
85
+ assert !ObjectID.legal?(nil)
86
+ assert !ObjectID.legal?("fred")
87
+ assert !ObjectID.legal?("0000")
88
+ assert !ObjectID.legal?('000102030405060708090A0')
89
+ assert ObjectID.legal?('000102030405060708090A0B')
90
+ assert ObjectID.legal?('abcdefABCDEF123456789012')
91
+ assert !ObjectID.legal?('abcdefABCDEF12345678901x')
92
+ end
93
+
94
+ def test_from_string_leading_zeroes
95
+ hex_str = '000000000000000000000000'
96
+ o = ObjectID.from_string(hex_str)
97
+ assert_equal hex_str, o.to_s
98
+ end
99
+
100
+ def test_byte_order
101
+ hex_str = '000102030405060708090A0B'
102
+ o = ObjectID.from_string(hex_str)
103
+ assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
104
+ end
105
+
106
+ def test_generation_time
107
+ time = Time.now
108
+ id = ObjectID.new
109
+ generated_time = id.generation_time
110
+
111
+ assert_in_delta time.to_i, generated_time.to_i, 2
112
+ assert_equal "UTC", generated_time.zone
113
+ end
114
+
115
+ def test_from_time
116
+ time = Time.now.utc
117
+ id = ObjectID.from_time(time)
118
+
119
+ assert_equal time.to_i, id.generation_time.to_i
120
+ end
121
+
122
+ def test_json
123
+ id = ObjectID.new
124
+ assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
125
+ end
126
+ end
@@ -0,0 +1,172 @@
1
+ require 'test/test_helper'
2
+
3
+ class OrderedHashTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @oh = OrderedHash.new
7
+ @oh['c'] = 1
8
+ @oh['a'] = 2
9
+ @oh['z'] = 3
10
+ @ordered_keys = %w(c a z)
11
+ end
12
+
13
+ def test_initialize
14
+ a = OrderedHash.new
15
+ a['x'] = 1
16
+ a['y'] = 2
17
+
18
+ b = OrderedHash['x' => 1, 'y' => 2]
19
+ assert_equal a, b
20
+ end
21
+
22
+ def test_hash_code
23
+ o = OrderedHash.new
24
+ o['number'] = 50
25
+ assert o.hash
26
+ end
27
+
28
+ def test_empty
29
+ assert_equal [], OrderedHash.new.keys
30
+ end
31
+
32
+ def test_uniq
33
+ list = []
34
+ doc = OrderedHash.new
35
+ doc['_id'] = 'ab12'
36
+ doc['name'] = 'test'
37
+
38
+ same_doc = OrderedHash.new
39
+ same_doc['_id'] = 'ab12'
40
+ same_doc['name'] = 'test'
41
+ list << doc
42
+ list << same_doc
43
+
44
+ assert_equal 2, list.size
45
+ assert_equal 1, list.uniq.size
46
+ end
47
+
48
+ def test_equality
49
+ a = OrderedHash.new
50
+ a['x'] = 1
51
+ a['y'] = 2
52
+
53
+ b = OrderedHash.new
54
+ b['y'] = 2
55
+ b['x'] = 1
56
+
57
+ c = OrderedHash.new
58
+ c['x'] = 1
59
+ c['y'] = 2
60
+
61
+ d = OrderedHash.new
62
+ d['x'] = 2
63
+ d['y'] = 3
64
+
65
+ e = OrderedHash.new
66
+ e['z'] = 1
67
+ e['y'] = 2
68
+
69
+ assert_equal a, c
70
+ assert_not_equal a, b
71
+ assert_not_equal a, d
72
+ assert_not_equal a, e
73
+ end
74
+
75
+ def test_order_preserved
76
+ assert_equal @ordered_keys, @oh.keys
77
+ end
78
+
79
+ def test_to_a_order_preserved
80
+ assert_equal @ordered_keys, @oh.to_a.map {|m| m.first}
81
+ end
82
+
83
+ def test_order_preserved_after_replace
84
+ @oh['a'] = 42
85
+ assert_equal @ordered_keys, @oh.keys
86
+ @oh['c'] = 'foobar'
87
+ assert_equal @ordered_keys, @oh.keys
88
+ @oh['z'] = /huh?/
89
+ assert_equal @ordered_keys, @oh.keys
90
+ end
91
+
92
+ def test_each
93
+ keys = []
94
+ @oh.each { |k, v| keys << k }
95
+ assert_equal keys, @oh.keys
96
+
97
+ @oh['z'] = 42
98
+ assert_equal keys, @oh.keys
99
+
100
+ assert_equal @oh, @oh.each {|k,v|}
101
+ end
102
+
103
+ def test_values
104
+ assert_equal [1, 2, 3], @oh.values
105
+ end
106
+
107
+ def test_merge
108
+ other = OrderedHash.new
109
+ other['f'] = 'foo'
110
+ noob = @oh.merge(other)
111
+ assert_equal @ordered_keys + ['f'], noob.keys
112
+ assert_equal [1, 2, 3, 'foo'], noob.values
113
+ end
114
+
115
+ def test_merge_bang
116
+ other = OrderedHash.new
117
+ other['f'] = 'foo'
118
+ @oh.merge!(other)
119
+ assert_equal @ordered_keys + ['f'], @oh.keys
120
+ assert_equal [1, 2, 3, 'foo'], @oh.values
121
+ end
122
+
123
+ def test_merge_bang_with_overlap
124
+ other = OrderedHash.new
125
+ other['a'] = 'apple'
126
+ other['c'] = 'crab'
127
+ other['f'] = 'foo'
128
+ @oh.merge!(other)
129
+ assert_equal @ordered_keys + ['f'], @oh.keys
130
+ assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
131
+ end
132
+
133
+ def test_merge_bang_with_hash_with_overlap
134
+ other = Hash.new
135
+ other['a'] = 'apple'
136
+ other['c'] = 'crab'
137
+ other['f'] = 'foo'
138
+ @oh.merge!(other)
139
+ assert_equal @ordered_keys + ['f'], @oh.keys
140
+ assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
141
+ end
142
+
143
+ def test_update
144
+ other = OrderedHash.new
145
+ other['f'] = 'foo'
146
+ noob = @oh.update(other)
147
+ assert_equal @ordered_keys + ['f'], noob.keys
148
+ assert_equal [1, 2, 3, 'foo'], noob.values
149
+ end
150
+
151
+ def test_inspect_retains_order
152
+ assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
153
+ end
154
+
155
+ def test_clear
156
+ @oh.clear
157
+ assert @oh.keys.empty?
158
+ end
159
+
160
+ def test_delete
161
+ assert @oh.keys.include?('z')
162
+ @oh.delete('z')
163
+ assert !@oh.keys.include?('z')
164
+ end
165
+
166
+ def test_delete_if
167
+ assert @oh.keys.include?('z')
168
+ @oh.delete_if { |k,v| k == 'z' }
169
+ assert !@oh.keys.include?('z')
170
+ end
171
+
172
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bson
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.20"
5
+ platform: ruby
6
+ authors:
7
+ - Jim Menard
8
+ - Mike Dirolf
9
+ - Kyle Banker
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2010-04-07 00:00:00 -04:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: A Ruby BSON implementation for MongoDB. For more information about Mongo, see http://www.mongodb.org. For more information on BSON, see http://www.bsonspec.org.
19
+ email: mongodb-dev@googlegroups.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - Rakefile
28
+ - bson.gemspec
29
+ - LICENSE.txt
30
+ - lib/bson.rb
31
+ - lib/bson/bson_c.rb
32
+ - lib/bson/bson_ruby.rb
33
+ - lib/bson/byte_buffer.rb
34
+ - lib/bson/exceptions.rb
35
+ - lib/bson/ordered_hash.rb
36
+ - lib/bson/types/binary.rb
37
+ - lib/bson/types/code.rb
38
+ - lib/bson/types/dbref.rb
39
+ - lib/bson/types/min_max_keys.rb
40
+ - lib/bson/types/objectid.rb
41
+ has_rdoc: true
42
+ homepage: http://www.mongodb.org
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Ruby implementation of BSON
69
+ test_files:
70
+ - test/mongo_bson/binary_test.rb
71
+ - test/mongo_bson/bson_test.rb
72
+ - test/mongo_bson/byte_buffer_test.rb
73
+ - test/mongo_bson/objectid_test.rb
74
+ - test/mongo_bson/ordered_hash_test.rb