bson 1.8.4 → 1.8.5

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.

@@ -1,215 +0,0 @@
1
- # encoding: binary
2
- require 'test_helper'
3
-
4
- class ByteBufferTest < Test::Unit::TestCase
5
- include BSON
6
-
7
- def setup
8
- @buf = ByteBuffer.new
9
- end
10
-
11
- def test_initial_state
12
- assert_equal 0, @buf.position
13
- assert_equal [], @buf.to_a
14
- assert_equal "", @buf.to_s
15
- assert_equal 0, @buf.length
16
- end
17
-
18
- def test_initialize_with_string_and_clear
19
- @buf = ByteBuffer.new("a")
20
- assert_equal 1, @buf.size
21
- assert_equal 1, @buf.position
22
- @buf.clear
23
- assert_equal 0, @buf.position
24
- assert_equal "", @buf.to_s
25
- assert_equal 0, @buf.size
26
- end
27
-
28
- def test_nil_get_returns_one_byte
29
- @buf.put_array([1, 2, 3, 4])
30
- @buf.rewind
31
- assert_equal 1, @buf.get
32
- end
33
-
34
- def test_unpack
35
- @buf.put_array([17, 2, 3, 4])
36
- assert_equal [17, 2, 3, 4], @buf.to_a
37
- assert_equal ["11020304"], @buf.unpack("H*")
38
- assert_equal ["11020304"], @buf.to_a("H*")
39
- end
40
-
41
- def test_one_get_returns_array_length_one
42
- @buf.put_array([1, 2, 3, 4])
43
- @buf.rewind
44
- assert_equal [1], @buf.get(1)
45
- end
46
-
47
- def test_zero_get_returns_empty_array
48
- @buf.put_array([1, 2, 3, 4])
49
- @buf.rewind
50
- assert_equal [], @buf.get(0)
51
- end
52
-
53
- def test_length
54
- @buf.put_int 3
55
- assert_equal 4, @buf.length
56
- end
57
-
58
- def test_long_length
59
- @buf.put_long 1027
60
- assert_equal 8, @buf.length
61
- end
62
-
63
- def test_get_long
64
- @buf.put_long 1027
65
- @buf.rewind
66
- assert_equal 1027, @buf.get_long
67
- end
68
-
69
- def test_get_double
70
- @buf.put_double 41.2
71
- @buf.rewind
72
- assert_equal 41.2, @buf.get_double
73
- end
74
-
75
- if BSON_CODER == BSON::BSON_RUBY
76
- if defined?(Encoding)
77
- def test_serialize_cstr_throws_error_for_bad_utf8
78
- bad = "hello \xC8".force_encoding("ISO-8859-7")
79
- assert_raises(BSON::InvalidStringEncoding) do
80
- BSON_CODER::serialize_cstr(@buf, bad)
81
- end
82
- end
83
-
84
- def test_serialize_cstr_does_not_validate_data_as_utf8
85
- assert_raises(BSON::InvalidStringEncoding) do
86
- BSON_CODER::serialize_cstr(@buf, "hello \xFF")
87
- end
88
- end
89
- else
90
- def test_serialize_cstr_forces_encoding_to_utf8
91
- # Unicode snowman (\u2603)
92
- BSON_CODER::serialize_cstr(@buf, "hello \342\230\203")
93
- assert_equal "hello \342\230\203\0", @buf.to_s
94
- end
95
-
96
- def test_serialize_cstr_validates_data_as_utf8
97
- assert_raises(BSON::InvalidStringEncoding) do
98
- BSON_CODER::serialize_cstr(@buf, "hello \xFF")
99
- end
100
- end
101
- end
102
- end
103
-
104
- def test_put_negative_byte
105
- @buf.put(-1)
106
- @buf.rewind
107
- assert_equal 255, @buf.get
108
- assert_equal "\xFF", @buf.to_s
109
- end
110
-
111
- def test_put_with_offset
112
- @buf.put(1)
113
- @buf.put(2, 0)
114
- @buf.put(3, 3)
115
- assert_equal "\x02\x00\x00\x03", @buf.to_s
116
- end
117
-
118
- def test_put_array_with_offset
119
- @buf.put(1)
120
- @buf.put_array([2, 3], 0)
121
- @buf.put_array([4, 5], 4)
122
- assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
123
- end
124
-
125
- def test_put_int_with_offset
126
- @buf.put(1)
127
- @buf.put_int(2, 0)
128
- @buf.put_int(3, 5)
129
- assert_equal "\x02\x00\x00\x00\x00\x03\x00\x00\x00", @buf.to_s
130
- end
131
-
132
- def test_put_long_with_offset
133
- @buf.put(1)
134
- @buf.put_long(2, 0)
135
- @buf.put_long(3, 9)
136
- assert_equal(
137
- "\x02\x00\x00\x00\x00\x00\x00\x00" +
138
- "\x00" +
139
- "\x03\x00\x00\x00\x00\x00\x00\x00",
140
- @buf.to_s)
141
- end
142
-
143
- def test_put_binary
144
- @buf.put(1)
145
- @buf.put_binary("\x02\x03", 0)
146
- @buf.put_binary("\x04\x05", 4)
147
- assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
148
- end
149
-
150
- def test_rewrite
151
- @buf.put_int(0)
152
- @buf.rewind
153
- @buf.put_int(1027)
154
- assert_equal 4, @buf.length
155
- @buf.rewind
156
- assert_equal 1027, @buf.get_int
157
- assert_equal 4, @buf.position
158
- end
159
-
160
- def test_prepend_byte_buffer
161
- @buf.put_int(4)
162
- new_buf = ByteBuffer.new([5, 0, 0, 0])
163
- @buf.prepend!(new_buf)
164
- assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
165
- end
166
-
167
- def test_append_byte_buffer
168
- @buf.put_int(4)
169
- new_buf = ByteBuffer.new([5, 0, 0, 0])
170
- @buf.append!(new_buf)
171
- assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
172
- end
173
-
174
- def test_array_as_initial_input
175
- @buf = ByteBuffer.new([5, 0, 0, 0])
176
- assert_equal 4, @buf.size
177
- assert_equal "\x05\x00\x00\x00", @buf.to_s
178
- assert_equal [5, 0, 0, 0], @buf.to_a
179
- @buf.put_int(32)
180
- @buf.rewind
181
- assert_equal 5, @buf.get_int
182
- assert_equal 32, @buf.get_int
183
- end
184
-
185
- def test_binary_string_as_initial_input
186
- str = "abcd"
187
- str.force_encoding('binary') if str.respond_to?(:force_encoding)
188
- @buf = ByteBuffer.new(str)
189
- assert_equal "abcd", @buf.to_s
190
- assert_equal [97, 98, 99, 100], @buf.to_a
191
- @buf.put_int(0)
192
- assert_equal [97, 98, 99, 100, 0, 0, 0, 0], @buf.to_a
193
- end
194
-
195
- def test_more
196
- assert !@buf.more?
197
- @buf.put_int(5)
198
- assert !@buf.more?
199
- @buf.rewind
200
- assert @buf.more?
201
- @buf.get_int
202
- assert !@buf.more?
203
- end
204
-
205
- def test_equality
206
- @buf = ByteBuffer.new("foo")
207
- assert_equal @buf, @buf
208
- assert_equal ByteBuffer.new(""), ByteBuffer.new("")
209
- assert_equal ByteBuffer.new("123"), ByteBuffer.new("123")
210
- assert_not_equal ByteBuffer.new("123"), ByteBuffer.new("1234")
211
- assert_equal @buf, "foo"
212
- assert_not_equal @buf, 123
213
- assert_not_equal @buf, nil
214
- end
215
- end
@@ -1,48 +0,0 @@
1
- # encoding:utf-8
2
- require 'test_helper'
3
-
4
- # Note: HashWithIndifferentAccess is so commonly used
5
- # that we always need to make sure that the driver works
6
- # with it. However, the bson gem should never need to
7
- # depend on it.
8
-
9
- # As a result, ActiveSupport is no longer a gem dependency and it should remain
10
- # that way. It must be required by the application code or
11
- # via bundler for developmet.
12
-
13
- require 'bson/support/hash_with_indifferent_access'
14
-
15
- class HashWithIndifferentAccessTest < Test::Unit::TestCase
16
- include BSON
17
-
18
- def setup
19
- @encoder = BSON::BSON_CODER
20
- end
21
-
22
- def test_document
23
- doc = HashWithIndifferentAccess.new
24
- doc['foo'] = 1
25
- doc['bar'] = 'baz'
26
-
27
- bson = @encoder.serialize(doc)
28
- assert_equal doc, @encoder.deserialize(bson.to_s)
29
- end
30
-
31
- def test_embedded_document
32
- jimmy = HashWithIndifferentAccess.new
33
- jimmy['name'] = 'Jimmy'
34
- jimmy['species'] = 'Siberian Husky'
35
-
36
- stats = HashWithIndifferentAccess.new
37
- stats['eyes'] = 'blue'
38
-
39
- person = HashWithIndifferentAccess.new
40
- person['_id'] = BSON::ObjectId.new
41
- person['name'] = 'Mr. Pet Lover'
42
- person['pets'] = [jimmy, {'name' => 'Sasha'}]
43
- person['stats'] = stats
44
-
45
- bson = @encoder.serialize(person)
46
- assert_equal person, @encoder.deserialize(bson.to_s)
47
- end
48
- end
@@ -1,16 +0,0 @@
1
- require 'test_helper'
2
- require 'json'
3
-
4
- class JSONTest < Test::Unit::TestCase
5
-
6
- # This test passes when run by itself but fails
7
- # when run as part of the whole test suite.
8
- def test_object_id_as_json
9
- #warn "Pending test object id as json"
10
- #id = BSON::ObjectId.new
11
-
12
- #obj = {'_id' => id}
13
- #assert_equal "{\"_id\":#{id.to_json}}", obj.to_json
14
- end
15
-
16
- end
@@ -1,153 +0,0 @@
1
- require 'test_helper'
2
- require 'json'
3
-
4
- class ObjectIdTest < Test::Unit::TestCase
5
-
6
- def setup
7
- @o = ObjectId.new
8
- end
9
-
10
- def test_hashcode
11
- assert_equal @o.instance_variable_get(:@data).hash, @o.hash
12
- end
13
-
14
- def test_array_uniq_for_equilavent_ids
15
- a = ObjectId.new('123')
16
- b = ObjectId.new('123')
17
- assert_equal a, b
18
- assert_equal 1, [a, b].uniq.size
19
- end
20
-
21
- def test_create_pk_method
22
- doc = {:name => 'Mongo'}
23
- doc = ObjectId.create_pk(doc)
24
- assert doc[:_id]
25
-
26
- doc = {:name => 'Mongo', :_id => '12345'}
27
- doc = ObjectId.create_pk(doc)
28
- assert_equal '12345', doc[:_id]
29
- end
30
-
31
- def test_different
32
- a = ObjectId.new
33
- b = ObjectId.new
34
- assert_not_equal a.to_a, b.to_a
35
- assert_not_equal a, b
36
- end
37
-
38
- def test_eql?
39
- o2 = ObjectId.new(@o.to_a)
40
- assert_equal @o, o2
41
- end
42
-
43
- def test_to_s
44
- s = @o.to_s
45
- assert_equal 24, s.length
46
- s =~ /^([0-9a-f]+)$/
47
- assert_equal 24, $1.length
48
- end
49
-
50
- def test_to_s2
51
- @o = ObjectId.new([76, 244, 52, 174, 44, 84, 121, 76, 88, 0, 0, 3])
52
- s = '4cf434ae2c54794c58000003'
53
- assert_equal @o.to_s, s
54
- end
55
-
56
- def test_method
57
- assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s)
58
- end
59
-
60
- def test_inspect
61
- assert_equal "BSON::ObjectId('#{@o.to_s}')", @o.inspect
62
- end
63
-
64
- def test_from_string
65
- hex_str = @o.to_s
66
- o2 = ObjectId.from_string(hex_str)
67
- assert_equal hex_str, o2.to_s
68
- assert_equal @o, o2
69
- assert_equal @o.to_s, o2.to_s
70
- end
71
-
72
- def test_illegal_from_string
73
- assert_raise InvalidObjectId do
74
- ObjectId.from_string("")
75
- end
76
- end
77
-
78
- def test_from_string_with_object_id
79
- assert_raise InvalidObjectId do
80
- ObjectId.from_string(@o)
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 id.to_a[4, 8].all? {|byte| byte == 0 }
120
- assert_equal time.to_i, id.generation_time.to_i
121
- end
122
-
123
- def test_from_time_unique
124
- time = Time.now.utc
125
- id = ObjectId.from_time(time, :unique => true)
126
-
127
- assert_equal id.to_a[4, 3], ObjectId.machine_id.unpack("C3")
128
- assert_equal time.to_i, id.generation_time.to_i
129
-
130
- id2 = ObjectId.new(nil, time)
131
- assert_equal time.to_i, id2.generation_time.to_i
132
- end
133
-
134
- def test_json
135
- id = ObjectId.new
136
- assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
137
- end
138
-
139
- def test_as_json
140
- id = ObjectId.new
141
- assert_equal({"$oid" => id.to_s}, id.as_json)
142
- end
143
-
144
- def test_object_id_array_flatten
145
- id = ObjectId.new
146
- assert_equal [ id ], [[ id ]].flatten
147
- end
148
-
149
- def test_object_id_array_flatten_bang
150
- id = ObjectId.new
151
- assert_equal [ id ], [[ id ]].flatten!
152
- end
153
- end
@@ -1,247 +0,0 @@
1
- require 'test_helper'
2
-
3
- class OrderedHashTest < Test::Unit::TestCase
4
-
5
- def setup
6
- @oh = BSON::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 = BSON::OrderedHash.new
15
- a['x'] = 1
16
- a['y'] = 2
17
-
18
- b = BSON::OrderedHash['x', 1, 'y', 2]
19
- assert_equal a, b
20
- end
21
-
22
- def test_hash_code
23
- o = BSON::OrderedHash.new
24
- o['number'] = 50
25
- assert o.hash
26
- end
27
-
28
- def test_empty
29
- assert_equal [], BSON::OrderedHash.new.keys
30
- end
31
-
32
- def test_uniq
33
- list = []
34
- doc = BSON::OrderedHash.new
35
- doc['_id'] = 'ab12'
36
- doc['name'] = 'test'
37
-
38
- same_doc = BSON::OrderedHash.new
39
- same_doc['_id'] = 'ab12'
40
- same_doc['name'] = 'test'
41
-
42
- list << doc
43
- list << same_doc
44
-
45
- assert_equal 2, list.size
46
- assert_equal 1, list.uniq.size
47
- end
48
-
49
- if !(RUBY_VERSION =~ /1.8.6/)
50
- def test_compatibility_with_hash
51
- list = []
52
- doc = BSON::OrderedHash.new
53
- doc['_id'] = 'ab12'
54
- doc['name'] = 'test'
55
-
56
- doc2 = {}
57
- doc2['_id'] = 'ab12'
58
- doc2['name'] = 'test'
59
- list << doc
60
- list << doc2
61
-
62
- assert_equal 1, list.uniq.size
63
- end
64
- end
65
-
66
- def test_equality
67
- a = BSON::OrderedHash.new
68
- a['x'] = 1
69
- a['y'] = 2
70
-
71
- b = BSON::OrderedHash.new
72
- b['y'] = 2
73
- b['x'] = 1
74
-
75
- c = BSON::OrderedHash.new
76
- c['x'] = 1
77
- c['y'] = 2
78
-
79
- d = BSON::OrderedHash.new
80
- d['x'] = 2
81
- d['y'] = 3
82
-
83
- e = BSON::OrderedHash.new
84
- e['z'] = 1
85
- e['y'] = 2
86
-
87
- assert_equal a, c
88
- assert_not_equal a, b
89
- assert_not_equal a, d
90
- assert_not_equal a, e
91
- end
92
-
93
- def test_order_preserved
94
- assert_equal @ordered_keys, @oh.keys
95
- end
96
-
97
- def test_replace
98
- h1 = BSON::OrderedHash.new
99
- h1[:a] = 1
100
- h1[:b] = 2
101
-
102
- h2 = BSON::OrderedHash.new
103
- h2[:c] = 3
104
- h2[:d] = 4
105
- h1.replace(h2)
106
-
107
- assert_equal [:c, :d], h1.keys
108
- assert_equal [3, 4], h1.values
109
- assert h1.keys.object_id != h2.keys.object_id
110
- end
111
-
112
- def test_to_a_order_preserved
113
- assert_equal @ordered_keys, @oh.to_a.map {|m| m.first}
114
- end
115
-
116
- def test_order_preserved_after_replace
117
- @oh['a'] = 42
118
- assert_equal @ordered_keys, @oh.keys
119
- @oh['c'] = 'foobar'
120
- assert_equal @ordered_keys, @oh.keys
121
- @oh['z'] = /huh?/
122
- assert_equal @ordered_keys, @oh.keys
123
- end
124
-
125
- def test_each
126
- keys = []
127
- @oh.each { |k, v| keys << k }
128
- assert_equal keys, @oh.keys
129
-
130
- @oh['z'] = 42
131
- assert_equal keys, @oh.keys
132
-
133
- assert_equal @oh, @oh.each {|k,v|}
134
- end
135
-
136
- def test_values
137
- assert_equal [1, 2, 3], @oh.values
138
- end
139
-
140
- def test_merge
141
- other = BSON::OrderedHash.new
142
- other['f'] = 'foo'
143
- noob = @oh.merge(other)
144
- assert_equal @ordered_keys + ['f'], noob.keys
145
- assert_equal [1, 2, 3, 'foo'], noob.values
146
- end
147
-
148
- def test_merge_bang
149
- other = BSON::OrderedHash.new
150
- other['f'] = 'foo'
151
- @oh.merge!(other)
152
- assert_equal @ordered_keys + ['f'], @oh.keys
153
- assert_equal [1, 2, 3, 'foo'], @oh.values
154
- end
155
-
156
- def test_merge_bang_with_overlap
157
- other = BSON::OrderedHash.new
158
- other['a'] = 'apple'
159
- other['c'] = 'crab'
160
- other['f'] = 'foo'
161
- @oh.merge!(other)
162
- assert_equal @ordered_keys + ['f'], @oh.keys
163
- assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
164
- end
165
-
166
- def test_merge_bang_with_hash_with_overlap
167
- other = Hash.new
168
- other['a'] = 'apple'
169
- other['c'] = 'crab'
170
- other['f'] = 'foo'
171
- @oh.merge!(other)
172
- assert_equal @ordered_keys + ['f'], @oh.keys
173
- assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
174
- end
175
-
176
- def test_equality_with_hash
177
- o = BSON::OrderedHash.new
178
- o[:a] = 1
179
- o[:b] = 2
180
- o[:c] = 3
181
- r = {:a => 1, :b => 2, :c => 3}
182
- assert r == o
183
- assert o == r
184
- end
185
-
186
- def test_update
187
- other = BSON::OrderedHash.new
188
- other['f'] = 'foo'
189
- noob = @oh.update(other)
190
- assert_equal @ordered_keys + ['f'], noob.keys
191
- assert_equal [1, 2, 3, 'foo'], noob.values
192
- end
193
-
194
- if RUBY_VERSION < "1.9.2"
195
- def test_inspect_retains_order
196
- assert_equal "#<BSON::OrderedHash:0x#{@oh.object_id.to_s(16)} {\"c\"=>1, \"a\"=>2, \"z\"=>3}>", @oh.inspect
197
- end
198
- end
199
-
200
- def test_clear
201
- @oh.clear
202
- assert @oh.keys.empty?
203
- end
204
-
205
- def test_delete
206
- assert @oh.keys.include?('z')
207
- @oh.delete('z')
208
- assert !@oh.keys.include?('z')
209
- end
210
-
211
- def test_delete_if
212
- assert @oh.keys.include?('z')
213
- @oh.delete_if { |k,v| k == 'z' }
214
- assert !@oh.keys.include?('z')
215
- @oh.delete_if { |k, v| v > 0 }
216
- assert @oh.keys.empty?
217
- end
218
-
219
- def test_reject
220
- new = @oh.reject { |k, v| k == 'foo' }
221
- assert new.keys == @oh.keys
222
-
223
- new = @oh.reject { |k, v| k == 'z' }
224
- assert !new.keys.include?('z')
225
- end
226
-
227
- def test_reject_bang
228
- @oh.reject! { |k, v| k == 'z' }
229
- assert !@oh.keys.include?('z')
230
- assert_nil @oh.reject! { |k, v| k == 'z' }
231
- end
232
-
233
- def test_clone
234
- copy = @oh.clone
235
- assert copy.keys == @oh.keys
236
-
237
- copy[:foo] = 1
238
- assert copy.keys != @oh.keys
239
- end
240
-
241
- def test_dup
242
- oh2 = @oh.dup
243
- oh2['f'] = 9
244
- assert_nil @oh['f']
245
- assert_equal ['c', 'a', 'z'], @oh.keys
246
- end
247
- end