bson 1.2.0-jruby

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,190 @@
1
+ # encoding: binary
2
+ require './test/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_nil_get_returns_one_byte
19
+ @buf.put_array([1, 2, 3, 4])
20
+ @buf.rewind
21
+ assert_equal 1, @buf.get
22
+ end
23
+
24
+ def test_one_get_returns_array_length_one
25
+ @buf.put_array([1, 2, 3, 4])
26
+ @buf.rewind
27
+ assert_equal [1], @buf.get(1)
28
+ end
29
+
30
+ def test_zero_get_returns_empty_array
31
+ @buf.put_array([1, 2, 3, 4])
32
+ @buf.rewind
33
+ assert_equal [], @buf.get(0)
34
+ end
35
+
36
+ def test_length
37
+ @buf.put_int 3
38
+ assert_equal 4, @buf.length
39
+ end
40
+
41
+ def test_default_order
42
+ assert_equal :little_endian, @buf.order
43
+ end
44
+
45
+ def test_long_length
46
+ @buf.put_long 1027
47
+ assert_equal 8, @buf.length
48
+ end
49
+
50
+ def test_get_long
51
+ @buf.put_long 1027
52
+ @buf.rewind
53
+ assert_equal 1027, @buf.get_long
54
+ end
55
+
56
+ def test_get_double
57
+ @buf.put_double 41.2
58
+ @buf.rewind
59
+ assert_equal 41.2, @buf.get_double
60
+ end
61
+
62
+ if defined?(Encoding)
63
+ def test_serialize_cstr_converts_encoding_to_utf8
64
+ theta = "hello \xC8".force_encoding("ISO-8859-7")
65
+ ByteBuffer.serialize_cstr(@buf, theta)
66
+ assert_equal "hello \xCE\x98\0", @buf.to_s
67
+ assert_equal Encoding.find('binary'), @buf.to_s.encoding
68
+ end
69
+
70
+ def test_serialize_cstr_validates_data_as_utf8
71
+ assert_raises(Encoding::UndefinedConversionError) do
72
+ ByteBuffer.serialize_cstr(@buf, "hello \xFF")
73
+ end
74
+ end
75
+ else
76
+ def test_serialize_cstr_forces_encoding_to_utf8
77
+ # Unicode snowman (\u2603)
78
+ ByteBuffer.serialize_cstr(@buf, "hello \342\230\203")
79
+ assert_equal "hello \342\230\203\0", @buf.to_s
80
+ end
81
+
82
+ def test_serialize_cstr_validates_data_as_utf8
83
+ assert_raises(BSON::InvalidStringEncoding) do
84
+ ByteBuffer.serialize_cstr(@buf, "hello \xFF")
85
+ end
86
+ end
87
+ end
88
+
89
+ def test_put_negative_byte
90
+ @buf.put(-1)
91
+ @buf.rewind
92
+ assert_equal 255, @buf.get
93
+ assert_equal "\xFF", @buf.to_s
94
+ end
95
+
96
+ def test_put_with_offset
97
+ @buf.put(1)
98
+ @buf.put(2, 0)
99
+ @buf.put(3, 3)
100
+ assert_equal "\x02\x00\x00\x03", @buf.to_s
101
+ end
102
+
103
+ def test_put_array_with_offset
104
+ @buf.put(1)
105
+ @buf.put_array([2, 3], 0)
106
+ @buf.put_array([4, 5], 4)
107
+ assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
108
+ end
109
+
110
+ def test_put_int_with_offset
111
+ @buf.put(1)
112
+ @buf.put_int(2, 0)
113
+ @buf.put_int(3, 5)
114
+ assert_equal "\x02\x00\x00\x00\x00\x03\x00\x00\x00", @buf.to_s
115
+ end
116
+
117
+ def test_put_long_with_offset
118
+ @buf.put(1)
119
+ @buf.put_long(2, 0)
120
+ @buf.put_long(3, 9)
121
+ assert_equal(
122
+ "\x02\x00\x00\x00\x00\x00\x00\x00" +
123
+ "\x00" +
124
+ "\x03\x00\x00\x00\x00\x00\x00\x00",
125
+ @buf.to_s)
126
+ end
127
+
128
+ def test_put_binary
129
+ @buf.put(1)
130
+ @buf.put_binary("\x02\x03", 0)
131
+ @buf.put_binary("\x04\x05", 4)
132
+ assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
133
+ end
134
+
135
+ def test_rewrite
136
+ @buf.put_int(0)
137
+ @buf.rewind
138
+ @buf.put_int(1027)
139
+ assert_equal 4, @buf.length
140
+ @buf.rewind
141
+ assert_equal 1027, @buf.get_int
142
+ assert_equal 4, @buf.position
143
+ end
144
+
145
+ def test_prepend_byte_buffer
146
+ @buf.put_int(4)
147
+ new_buf = ByteBuffer.new([5, 0, 0, 0])
148
+ @buf.prepend!(new_buf)
149
+ assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
150
+ end
151
+
152
+ def test_append_byte_buffer
153
+ @buf.put_int(4)
154
+ new_buf = ByteBuffer.new([5, 0, 0, 0])
155
+ @buf.append!(new_buf)
156
+ assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
157
+ end
158
+
159
+ def test_array_as_initial_input
160
+ @buf = ByteBuffer.new([5, 0, 0, 0])
161
+ assert_equal 4, @buf.size
162
+ assert_equal "\x05\x00\x00\x00", @buf.to_s
163
+ assert_equal [5, 0, 0, 0], @buf.to_a
164
+ @buf.put_int(32)
165
+ @buf.rewind
166
+ assert_equal 5, @buf.get_int
167
+ assert_equal 32, @buf.get_int
168
+ end
169
+
170
+ def test_binary_string_as_initial_input
171
+ str = "abcd"
172
+ str.force_encoding('binary') if str.respond_to?(:force_encoding)
173
+ @buf = ByteBuffer.new(str)
174
+ assert_equal "abcd", @buf.to_s
175
+ assert_equal [97, 98, 99, 100], @buf.to_a
176
+ @buf.put_int(0)
177
+ assert_equal [97, 98, 99, 100, 0, 0, 0, 0], @buf.to_a
178
+ end
179
+
180
+ def test_more
181
+ assert !@buf.more?
182
+ @buf.put_int(5)
183
+ assert !@buf.more?
184
+ @buf.rewind
185
+ assert @buf.more?
186
+ @buf.get_int
187
+ assert !@buf.more?
188
+ end
189
+
190
+ end
@@ -0,0 +1,38 @@
1
+ # encoding:utf-8
2
+ require './test/test_helper'
3
+ require './test/support/hash_with_indifferent_access'
4
+
5
+ class HashWithIndifferentAccessTest < Test::Unit::TestCase
6
+ include BSON
7
+
8
+ def setup
9
+ @encoder = BSON::BSON_CODER
10
+ end
11
+
12
+ def test_document
13
+ doc = HashWithIndifferentAccess.new
14
+ doc['foo'] = 1
15
+ doc['bar'] = 'baz'
16
+
17
+ bson = @encoder.serialize(doc)
18
+ assert_equal doc, @encoder.deserialize(bson.to_s)
19
+ end
20
+
21
+ def test_embedded_document
22
+ jimmy = HashWithIndifferentAccess.new
23
+ jimmy['name'] = 'Jimmy'
24
+ jimmy['species'] = 'Siberian Husky'
25
+
26
+ stats = HashWithIndifferentAccess.new
27
+ stats['eyes'] = 'blue'
28
+
29
+ person = HashWithIndifferentAccess.new
30
+ person['_id'] = BSON::ObjectId.new
31
+ person['name'] = 'Mr. Pet Lover'
32
+ person['pets'] = [jimmy, {'name' => 'Sasha'}]
33
+ person['stats'] = stats
34
+
35
+ bson = @encoder.serialize(person)
36
+ assert_equal person, @encoder.deserialize(bson.to_s)
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ require './test/test_helper'
2
+ require 'rubygems'
3
+ require 'json'
4
+
5
+ class JSONTest < Test::Unit::TestCase
6
+
7
+ # This test passes when run by itself but fails
8
+ # when run as part of the whole test suite.
9
+ def test_object_id_as_json
10
+ warn "Pending test object id as json"
11
+ #id = BSON::ObjectId.new
12
+
13
+ #obj = {'_id' => id}
14
+ #assert_equal "{\"_id\":#{id.to_json}}", obj.to_json
15
+ end
16
+
17
+ end
@@ -0,0 +1,141 @@
1
+ require './test/test_helper'
2
+ require 'rubygems'
3
+ require 'json'
4
+
5
+ class ObjectIdTest < Test::Unit::TestCase
6
+
7
+ include Mongo
8
+ include BSON
9
+
10
+ def setup
11
+ @o = ObjectId.new
12
+ end
13
+
14
+ def test_hashcode
15
+ assert_equal @o.instance_variable_get(:@data).hash, @o.hash
16
+ end
17
+
18
+ def test_array_uniq_for_equilavent_ids
19
+ a = ObjectId.new('123')
20
+ b = ObjectId.new('123')
21
+ assert_equal a, b
22
+ assert_equal 1, [a, b].uniq.size
23
+ end
24
+
25
+ def test_create_pk_method
26
+ doc = {:name => 'Mongo'}
27
+ doc = ObjectId.create_pk(doc)
28
+ assert doc[:_id]
29
+
30
+ doc = {:name => 'Mongo', :_id => '12345'}
31
+ doc = ObjectId.create_pk(doc)
32
+ assert_equal '12345', doc[:_id]
33
+ end
34
+
35
+ def test_different
36
+ a = ObjectId.new
37
+ b = ObjectId.new
38
+ assert_not_equal a.to_a, b.to_a
39
+ assert_not_equal a, b
40
+ end
41
+
42
+ def test_eql?
43
+ o2 = ObjectId.new(@o.to_a)
44
+ assert_equal @o, o2
45
+ end
46
+
47
+ def test_to_s
48
+ s = @o.to_s
49
+ assert_equal 24, s.length
50
+ s =~ /^([0-9a-f]+)$/
51
+ assert_equal 24, $1.length
52
+ end
53
+
54
+ def test_to_s2
55
+ @o = ObjectId.new([76, 244, 52, 174, 44, 84, 121, 76, 88, 0, 0, 3])
56
+ s = '4cf434ae2c54794c58000003'
57
+ assert_equal @o.to_s, s
58
+ end
59
+
60
+ def test_method
61
+ assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s)
62
+ end
63
+
64
+ def test_inspect
65
+ assert_equal "BSON::ObjectId('#{@o.to_s}')", @o.inspect
66
+ end
67
+
68
+ def test_save_and_restore
69
+ db = standard_connection.db(MONGO_TEST_DB)
70
+ coll = db.collection('test')
71
+
72
+ coll.remove
73
+ coll << {'a' => 1, '_id' => @o}
74
+
75
+ row = coll.find().collect.first
76
+ assert_equal 1, row['a']
77
+ assert_equal @o, row['_id']
78
+ end
79
+
80
+ def test_from_string
81
+ hex_str = @o.to_s
82
+ o2 = ObjectId.from_string(hex_str)
83
+ assert_equal hex_str, o2.to_s
84
+ assert_equal @o, o2
85
+ assert_equal @o.to_s, o2.to_s
86
+ end
87
+
88
+ def test_illegal_from_string
89
+ assert_raise InvalidObjectId do
90
+ ObjectId.from_string("")
91
+ end
92
+ end
93
+
94
+ def test_legal
95
+ assert !ObjectId.legal?(nil)
96
+ assert !ObjectId.legal?("fred")
97
+ assert !ObjectId.legal?("0000")
98
+ assert !ObjectId.legal?('000102030405060708090A0')
99
+ assert ObjectId.legal?('000102030405060708090A0B')
100
+ assert ObjectId.legal?('abcdefABCDEF123456789012')
101
+ assert !ObjectId.legal?('abcdefABCDEF12345678901x')
102
+ end
103
+
104
+ def test_from_string_leading_zeroes
105
+ hex_str = '000000000000000000000000'
106
+ o = ObjectId.from_string(hex_str)
107
+ assert_equal hex_str, o.to_s
108
+ end
109
+
110
+ def test_byte_order
111
+ hex_str = '000102030405060708090A0B'
112
+ o = ObjectId.from_string(hex_str)
113
+ assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
114
+ end
115
+
116
+ def test_generation_time
117
+ time = Time.now
118
+ id = ObjectId.new
119
+ generated_time = id.generation_time
120
+
121
+ assert_in_delta time.to_i, generated_time.to_i, 2
122
+ assert_equal "UTC", generated_time.zone
123
+ end
124
+
125
+ def test_from_time
126
+ time = Time.now.utc
127
+ id = ObjectId.from_time(time)
128
+
129
+ assert_equal time.to_i, id.generation_time.to_i
130
+ end
131
+
132
+ def test_json
133
+ id = ObjectId.new
134
+ assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
135
+ end
136
+
137
+ def test_as_json
138
+ id = ObjectId.new
139
+ assert_equal({"$oid" => id.to_s}, id.as_json)
140
+ end
141
+ end
@@ -0,0 +1,197 @@
1
+ require './test/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
+ 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 = BSON::OrderedHash.new
50
+ a['x'] = 1
51
+ a['y'] = 2
52
+
53
+ b = BSON::OrderedHash.new
54
+ b['y'] = 2
55
+ b['x'] = 1
56
+
57
+ c = BSON::OrderedHash.new
58
+ c['x'] = 1
59
+ c['y'] = 2
60
+
61
+ d = BSON::OrderedHash.new
62
+ d['x'] = 2
63
+ d['y'] = 3
64
+
65
+ e = BSON::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 = BSON::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 = BSON::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 = BSON::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_equality_with_hash
144
+ o = BSON::OrderedHash.new
145
+ o[:a] = 1
146
+ o[:b] = 2
147
+ o[:c] = 3
148
+ r = {:a => 1, :b => 2, :c => 3}
149
+ assert r == o
150
+ assert o == r
151
+ end
152
+
153
+ def test_update
154
+ other = BSON::OrderedHash.new
155
+ other['f'] = 'foo'
156
+ noob = @oh.update(other)
157
+ assert_equal @ordered_keys + ['f'], noob.keys
158
+ assert_equal [1, 2, 3, 'foo'], noob.values
159
+ end
160
+
161
+ def test_inspect_retains_order
162
+ assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
163
+ end
164
+
165
+ def test_clear
166
+ @oh.clear
167
+ assert @oh.keys.empty?
168
+ end
169
+
170
+ def test_delete
171
+ assert @oh.keys.include?('z')
172
+ @oh.delete('z')
173
+ assert !@oh.keys.include?('z')
174
+ end
175
+
176
+ def test_delete_if
177
+ assert @oh.keys.include?('z')
178
+ @oh.delete_if { |k,v| k == 'z' }
179
+ assert !@oh.keys.include?('z')
180
+ end
181
+
182
+ def test_reject
183
+ new = @oh.reject { |k, v| k == 'foo' }
184
+ assert new.keys == @oh.keys
185
+
186
+ new = @oh.reject { |k, v| k == 'z' }
187
+ assert !new.keys.include?('z')
188
+ end
189
+
190
+ def test_clone
191
+ copy = @oh.clone
192
+ assert copy.keys == @oh.keys
193
+
194
+ copy[:foo] = 1
195
+ assert copy.keys != @oh.keys
196
+ end
197
+ end