jonbell-mongo 1.3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +190 -0
- data/README.md +333 -0
- data/Rakefile +215 -0
- data/bin/mongo_console +21 -0
- data/docs/CREDITS.md +123 -0
- data/docs/FAQ.md +116 -0
- data/docs/GridFS.md +158 -0
- data/docs/HISTORY.md +263 -0
- data/docs/RELEASES.md +33 -0
- data/docs/REPLICA_SETS.md +72 -0
- data/docs/TUTORIAL.md +247 -0
- data/docs/WRITE_CONCERN.md +28 -0
- data/lib/mongo.rb +97 -0
- data/lib/mongo/collection.rb +895 -0
- data/lib/mongo/connection.rb +926 -0
- data/lib/mongo/cursor.rb +474 -0
- data/lib/mongo/db.rb +617 -0
- data/lib/mongo/exceptions.rb +71 -0
- data/lib/mongo/gridfs/grid.rb +107 -0
- data/lib/mongo/gridfs/grid_ext.rb +57 -0
- data/lib/mongo/gridfs/grid_file_system.rb +146 -0
- data/lib/mongo/gridfs/grid_io.rb +485 -0
- data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
- data/lib/mongo/repl_set_connection.rb +356 -0
- data/lib/mongo/util/conversions.rb +89 -0
- data/lib/mongo/util/core_ext.rb +60 -0
- data/lib/mongo/util/pool.rb +177 -0
- data/lib/mongo/util/server_version.rb +71 -0
- data/lib/mongo/util/support.rb +82 -0
- data/lib/mongo/util/uri_parser.rb +185 -0
- data/mongo.gemspec +34 -0
- data/test/auxillary/1.4_features.rb +166 -0
- data/test/auxillary/authentication_test.rb +68 -0
- data/test/auxillary/autoreconnect_test.rb +41 -0
- data/test/auxillary/fork_test.rb +30 -0
- data/test/auxillary/repl_set_auth_test.rb +58 -0
- data/test/auxillary/slave_connection_test.rb +36 -0
- data/test/auxillary/threaded_authentication_test.rb +101 -0
- data/test/bson/binary_test.rb +15 -0
- data/test/bson/bson_test.rb +654 -0
- data/test/bson/byte_buffer_test.rb +208 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +154 -0
- data/test/bson/ordered_hash_test.rb +210 -0
- data/test/bson/timestamp_test.rb +24 -0
- data/test/collection_test.rb +910 -0
- data/test/connection_test.rb +324 -0
- data/test/conversions_test.rb +119 -0
- data/test/cursor_fail_test.rb +75 -0
- data/test/cursor_message_test.rb +43 -0
- data/test/cursor_test.rb +483 -0
- data/test/db_api_test.rb +738 -0
- data/test/db_connection_test.rb +15 -0
- data/test/db_test.rb +315 -0
- data/test/grid_file_system_test.rb +259 -0
- data/test/grid_io_test.rb +209 -0
- data/test/grid_test.rb +258 -0
- data/test/load/thin/load.rb +24 -0
- data/test/load/unicorn/load.rb +23 -0
- data/test/replica_sets/connect_test.rb +112 -0
- data/test/replica_sets/connection_string_test.rb +32 -0
- data/test/replica_sets/count_test.rb +35 -0
- data/test/replica_sets/insert_test.rb +53 -0
- data/test/replica_sets/pooled_insert_test.rb +55 -0
- data/test/replica_sets/query_secondaries.rb +108 -0
- data/test/replica_sets/query_test.rb +51 -0
- data/test/replica_sets/replication_ack_test.rb +66 -0
- data/test/replica_sets/rs_test_helper.rb +27 -0
- data/test/safe_test.rb +68 -0
- data/test/support/hash_with_indifferent_access.rb +186 -0
- data/test/support/keys.rb +45 -0
- data/test/support_test.rb +18 -0
- data/test/test_helper.rb +102 -0
- data/test/threading/threading_with_large_pool_test.rb +90 -0
- data/test/threading_test.rb +87 -0
- data/test/tools/auth_repl_set_manager.rb +14 -0
- data/test/tools/repl_set_manager.rb +266 -0
- data/test/unit/collection_test.rb +130 -0
- data/test/unit/connection_test.rb +85 -0
- data/test/unit/cursor_test.rb +109 -0
- data/test/unit/db_test.rb +94 -0
- data/test/unit/grid_test.rb +49 -0
- data/test/unit/pool_test.rb +9 -0
- data/test/unit/repl_set_connection_test.rb +59 -0
- data/test/unit/safe_test.rb +125 -0
- data/test/uri_test.rb +91 -0
- metadata +224 -0
@@ -0,0 +1,208 @@
|
|
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_unpack
|
25
|
+
@buf.put_array([17, 2, 3, 4])
|
26
|
+
assert_equal [17, 2, 3, 4], @buf.to_a
|
27
|
+
assert_equal ["11020304"], @buf.unpack("H*")
|
28
|
+
assert_equal ["11020304"], @buf.to_a("H*")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_one_get_returns_array_length_one
|
32
|
+
@buf.put_array([1, 2, 3, 4])
|
33
|
+
@buf.rewind
|
34
|
+
assert_equal [1], @buf.get(1)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_zero_get_returns_empty_array
|
38
|
+
@buf.put_array([1, 2, 3, 4])
|
39
|
+
@buf.rewind
|
40
|
+
assert_equal [], @buf.get(0)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_length
|
44
|
+
@buf.put_int 3
|
45
|
+
assert_equal 4, @buf.length
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_default_order
|
49
|
+
assert_equal :little_endian, @buf.order
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_long_length
|
53
|
+
@buf.put_long 1027
|
54
|
+
assert_equal 8, @buf.length
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_get_long
|
58
|
+
@buf.put_long 1027
|
59
|
+
@buf.rewind
|
60
|
+
assert_equal 1027, @buf.get_long
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_get_double
|
64
|
+
@buf.put_double 41.2
|
65
|
+
@buf.rewind
|
66
|
+
assert_equal 41.2, @buf.get_double
|
67
|
+
end
|
68
|
+
|
69
|
+
if defined?(Encoding)
|
70
|
+
def test_serialize_cstr_converts_encoding_to_utf8
|
71
|
+
theta = "hello \xC8".force_encoding("ISO-8859-7")
|
72
|
+
ByteBuffer.serialize_cstr(@buf, theta)
|
73
|
+
assert_equal "hello \xCE\x98\0", @buf.to_s
|
74
|
+
assert_equal Encoding.find('binary'), @buf.to_s.encoding
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_serialize_cstr_validates_data_as_utf8
|
78
|
+
assert_raises(Encoding::UndefinedConversionError) do
|
79
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
else
|
83
|
+
def test_serialize_cstr_forces_encoding_to_utf8
|
84
|
+
# Unicode snowman (\u2603)
|
85
|
+
ByteBuffer.serialize_cstr(@buf, "hello \342\230\203")
|
86
|
+
assert_equal "hello \342\230\203\0", @buf.to_s
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_serialize_cstr_validates_data_as_utf8
|
90
|
+
assert_raises(BSON::InvalidStringEncoding) do
|
91
|
+
ByteBuffer.serialize_cstr(@buf, "hello \xFF")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_put_negative_byte
|
97
|
+
@buf.put(-1)
|
98
|
+
@buf.rewind
|
99
|
+
assert_equal 255, @buf.get
|
100
|
+
assert_equal "\xFF", @buf.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_put_with_offset
|
104
|
+
@buf.put(1)
|
105
|
+
@buf.put(2, 0)
|
106
|
+
@buf.put(3, 3)
|
107
|
+
assert_equal "\x02\x00\x00\x03", @buf.to_s
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_put_array_with_offset
|
111
|
+
@buf.put(1)
|
112
|
+
@buf.put_array([2, 3], 0)
|
113
|
+
@buf.put_array([4, 5], 4)
|
114
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_put_int_with_offset
|
118
|
+
@buf.put(1)
|
119
|
+
@buf.put_int(2, 0)
|
120
|
+
@buf.put_int(3, 5)
|
121
|
+
assert_equal "\x02\x00\x00\x00\x00\x03\x00\x00\x00", @buf.to_s
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_put_long_with_offset
|
125
|
+
@buf.put(1)
|
126
|
+
@buf.put_long(2, 0)
|
127
|
+
@buf.put_long(3, 9)
|
128
|
+
assert_equal(
|
129
|
+
"\x02\x00\x00\x00\x00\x00\x00\x00" +
|
130
|
+
"\x00" +
|
131
|
+
"\x03\x00\x00\x00\x00\x00\x00\x00",
|
132
|
+
@buf.to_s)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_put_binary
|
136
|
+
@buf.put(1)
|
137
|
+
@buf.put_binary("\x02\x03", 0)
|
138
|
+
@buf.put_binary("\x04\x05", 4)
|
139
|
+
assert_equal "\x02\x03\x00\x00\x04\x05", @buf.to_s
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_rewrite
|
143
|
+
@buf.put_int(0)
|
144
|
+
@buf.rewind
|
145
|
+
@buf.put_int(1027)
|
146
|
+
assert_equal 4, @buf.length
|
147
|
+
@buf.rewind
|
148
|
+
assert_equal 1027, @buf.get_int
|
149
|
+
assert_equal 4, @buf.position
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_prepend_byte_buffer
|
153
|
+
@buf.put_int(4)
|
154
|
+
new_buf = ByteBuffer.new([5, 0, 0, 0])
|
155
|
+
@buf.prepend!(new_buf)
|
156
|
+
assert_equal [5, 0, 0, 0, 4, 0, 0, 0], @buf.to_a
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_append_byte_buffer
|
160
|
+
@buf.put_int(4)
|
161
|
+
new_buf = ByteBuffer.new([5, 0, 0, 0])
|
162
|
+
@buf.append!(new_buf)
|
163
|
+
assert_equal [4, 0, 0, 0, 5, 0, 0, 0], @buf.to_a
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_array_as_initial_input
|
167
|
+
@buf = ByteBuffer.new([5, 0, 0, 0])
|
168
|
+
assert_equal 4, @buf.size
|
169
|
+
assert_equal "\x05\x00\x00\x00", @buf.to_s
|
170
|
+
assert_equal [5, 0, 0, 0], @buf.to_a
|
171
|
+
@buf.put_int(32)
|
172
|
+
@buf.rewind
|
173
|
+
assert_equal 5, @buf.get_int
|
174
|
+
assert_equal 32, @buf.get_int
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_binary_string_as_initial_input
|
178
|
+
str = "abcd"
|
179
|
+
str.force_encoding('binary') if str.respond_to?(:force_encoding)
|
180
|
+
@buf = ByteBuffer.new(str)
|
181
|
+
assert_equal "abcd", @buf.to_s
|
182
|
+
assert_equal [97, 98, 99, 100], @buf.to_a
|
183
|
+
@buf.put_int(0)
|
184
|
+
assert_equal [97, 98, 99, 100, 0, 0, 0, 0], @buf.to_a
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_more
|
188
|
+
assert !@buf.more?
|
189
|
+
@buf.put_int(5)
|
190
|
+
assert !@buf.more?
|
191
|
+
@buf.rewind
|
192
|
+
assert @buf.more?
|
193
|
+
@buf.get_int
|
194
|
+
assert !@buf.more?
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_equality
|
198
|
+
@buf = ByteBuffer.new("foo")
|
199
|
+
assert_equal @buf, @buf
|
200
|
+
assert_equal ByteBuffer.new(""), ByteBuffer.new("")
|
201
|
+
assert_equal ByteBuffer.new("123"), ByteBuffer.new("123")
|
202
|
+
assert_not_equal ByteBuffer.new("123"), ByteBuffer.new("1234")
|
203
|
+
assert_equal @buf, "foo"
|
204
|
+
assert_not_equal @buf, 123
|
205
|
+
assert_not_equal @buf, nil
|
206
|
+
end
|
207
|
+
|
208
|
+
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,154 @@
|
|
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 id.to_a[4, 8].all? {|byte| byte == 0 }
|
130
|
+
assert_equal time.to_i, id.generation_time.to_i
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_from_time_unique
|
134
|
+
time = Time.now.utc
|
135
|
+
id = ObjectId.from_time(time, :unique => true)
|
136
|
+
|
137
|
+
mac_id = Digest::MD5.digest(Socket.gethostname)[0, 3].unpack("C3")
|
138
|
+
assert_equal id.to_a[4, 3], mac_id
|
139
|
+
assert_equal time.to_i, id.generation_time.to_i
|
140
|
+
|
141
|
+
id2 = ObjectId.new(nil, time)
|
142
|
+
assert_equal time.to_i, id2.generation_time.to_i
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_json
|
146
|
+
id = ObjectId.new
|
147
|
+
assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_as_json
|
151
|
+
id = ObjectId.new
|
152
|
+
assert_equal({"$oid" => id.to_s}, id.as_json)
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,210 @@
|
|
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_reject_bang
|
191
|
+
@oh.reject! { |k, v| k == 'z' }
|
192
|
+
assert !@oh.keys.include?('z')
|
193
|
+
assert_nil @oh.reject! { |k, v| k == 'z' }
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_clone
|
197
|
+
copy = @oh.clone
|
198
|
+
assert copy.keys == @oh.keys
|
199
|
+
|
200
|
+
copy[:foo] = 1
|
201
|
+
assert copy.keys != @oh.keys
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_dup
|
205
|
+
oh2 = @oh.dup
|
206
|
+
oh2['f'] = 9
|
207
|
+
assert_nil @oh['f']
|
208
|
+
assert_equal ['c', 'a', 'z'], @oh.keys
|
209
|
+
end
|
210
|
+
end
|