pahagon-mongo-abd 0.14.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +353 -0
- data/Rakefile +62 -0
- data/bin/bson_benchmark.rb +59 -0
- data/bin/mongo_console +21 -0
- data/bin/run_test_script +19 -0
- data/bin/standard_benchmark +109 -0
- data/examples/admin.rb +41 -0
- data/examples/benchmarks.rb +42 -0
- data/examples/blog.rb +76 -0
- data/examples/capped.rb +23 -0
- data/examples/cursor.rb +47 -0
- data/examples/gridfs.rb +87 -0
- data/examples/index_test.rb +125 -0
- data/examples/info.rb +30 -0
- data/examples/queries.rb +69 -0
- data/examples/simple.rb +23 -0
- data/examples/strict.rb +34 -0
- data/examples/types.rb +35 -0
- data/lib/mongo.rb +19 -0
- data/lib/mongo/admin.rb +83 -0
- data/lib/mongo/collection.rb +415 -0
- data/lib/mongo/connection.rb +151 -0
- data/lib/mongo/cursor.rb +279 -0
- data/lib/mongo/db.rb +560 -0
- data/lib/mongo/errors.rb +26 -0
- data/lib/mongo/gridfs.rb +16 -0
- data/lib/mongo/gridfs/chunk.rb +92 -0
- data/lib/mongo/gridfs/grid_store.rb +464 -0
- data/lib/mongo/message.rb +20 -0
- data/lib/mongo/message/get_more_message.rb +32 -0
- data/lib/mongo/message/insert_message.rb +37 -0
- data/lib/mongo/message/kill_cursors_message.rb +31 -0
- data/lib/mongo/message/message.rb +80 -0
- data/lib/mongo/message/message_header.rb +45 -0
- data/lib/mongo/message/msg_message.rb +29 -0
- data/lib/mongo/message/opcodes.rb +27 -0
- data/lib/mongo/message/query_message.rb +78 -0
- data/lib/mongo/message/remove_message.rb +37 -0
- data/lib/mongo/message/update_message.rb +38 -0
- data/lib/mongo/query.rb +118 -0
- data/lib/mongo/types/binary.rb +38 -0
- data/lib/mongo/types/code.rb +30 -0
- data/lib/mongo/types/dbref.rb +33 -0
- data/lib/mongo/types/objectid.rb +143 -0
- data/lib/mongo/types/regexp_of_holding.rb +40 -0
- data/lib/mongo/util/bson.rb +546 -0
- data/lib/mongo/util/byte_buffer.rb +167 -0
- data/lib/mongo/util/ordered_hash.rb +113 -0
- data/lib/mongo/util/xml_to_ruby.rb +105 -0
- data/mongo-ruby-driver.gemspec +103 -0
- data/test/mongo-qa/_common.rb +8 -0
- data/test/mongo-qa/admin +26 -0
- data/test/mongo-qa/capped +22 -0
- data/test/mongo-qa/count1 +18 -0
- data/test/mongo-qa/dbs +22 -0
- data/test/mongo-qa/find +10 -0
- data/test/mongo-qa/find1 +15 -0
- data/test/mongo-qa/gridfs_in +16 -0
- data/test/mongo-qa/gridfs_out +17 -0
- data/test/mongo-qa/indices +49 -0
- data/test/mongo-qa/remove +25 -0
- data/test/mongo-qa/stress1 +35 -0
- data/test/mongo-qa/test1 +11 -0
- data/test/mongo-qa/update +18 -0
- data/test/test_admin.rb +69 -0
- data/test/test_bson.rb +268 -0
- data/test/test_byte_buffer.rb +69 -0
- data/test/test_chunk.rb +84 -0
- data/test/test_collection.rb +249 -0
- data/test/test_connection.rb +101 -0
- data/test/test_cursor.rb +331 -0
- data/test/test_db.rb +185 -0
- data/test/test_db_api.rb +798 -0
- data/test/test_db_connection.rb +18 -0
- data/test/test_grid_store.rb +284 -0
- data/test/test_message.rb +35 -0
- data/test/test_objectid.rb +105 -0
- data/test/test_ordered_hash.rb +138 -0
- data/test/test_round_trip.rb +120 -0
- data/test/test_threading.rb +37 -0
- metadata +135 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
# NOTE: assumes Mongo is running
|
6
|
+
class DBConnectionTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include Mongo
|
9
|
+
|
10
|
+
def test_no_exceptions
|
11
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
12
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
13
|
+
db = Connection.new(host, port).db('ruby-mongo-demo')
|
14
|
+
coll = db.collection('test')
|
15
|
+
coll.clear
|
16
|
+
db.error
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,284 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'test/unit'
|
3
|
+
require 'mongo'
|
4
|
+
require 'mongo/gridfs'
|
5
|
+
|
6
|
+
class GridStoreTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include Mongo
|
9
|
+
include GridFS
|
10
|
+
|
11
|
+
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
12
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
|
13
|
+
@@files = @@db.collection('fs.files')
|
14
|
+
@@chunks = @@db.collection('fs.chunks')
|
15
|
+
|
16
|
+
def setup
|
17
|
+
@@chunks.clear
|
18
|
+
@@files.clear
|
19
|
+
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
20
|
+
end
|
21
|
+
|
22
|
+
def teardown
|
23
|
+
@@chunks.clear
|
24
|
+
@@files.clear
|
25
|
+
@@db.error
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_exist
|
29
|
+
assert GridStore.exist?(@@db, 'foobar')
|
30
|
+
assert !GridStore.exist?(@@db, 'does_not_exist')
|
31
|
+
assert !GridStore.exist?(@@db, 'foobar', 'another_root')
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_list
|
35
|
+
assert_equal ['foobar'], GridStore.list(@@db)
|
36
|
+
assert_equal ['foobar'], GridStore.list(@@db, 'fs')
|
37
|
+
assert_equal [], GridStore.list(@@db, 'my_fs')
|
38
|
+
|
39
|
+
GridStore.open(@@db, 'test', 'w') { |f| f.write("my file") }
|
40
|
+
|
41
|
+
assert_equal ['foobar', 'test'], GridStore.list(@@db)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_small_write
|
45
|
+
rows = @@files.find({'filename' => 'foobar'}).to_a
|
46
|
+
assert_not_nil rows
|
47
|
+
assert_equal 1, rows.length
|
48
|
+
row = rows[0]
|
49
|
+
assert_not_nil row
|
50
|
+
|
51
|
+
file_id = row['_id']
|
52
|
+
assert_kind_of ObjectID, file_id
|
53
|
+
rows = @@chunks.find({'files_id' => file_id}).to_a
|
54
|
+
assert_not_nil rows
|
55
|
+
assert_equal 1, rows.length
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_small_file
|
59
|
+
rows = @@files.find({'filename' => 'foobar'}).to_a
|
60
|
+
assert_not_nil rows
|
61
|
+
assert_equal 1, rows.length
|
62
|
+
row = rows[0]
|
63
|
+
assert_not_nil row
|
64
|
+
assert_equal "hello, world!", GridStore.read(@@db, 'foobar')
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_overwrite
|
68
|
+
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("overwrite") }
|
69
|
+
assert_equal "overwrite", GridStore.read(@@db, 'foobar')
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_read_length
|
73
|
+
assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Also tests seek
|
77
|
+
def test_read_with_offset
|
78
|
+
assert_equal "world", GridStore.read(@@db, 'foobar', 5, 7)
|
79
|
+
assert_equal "world!", GridStore.read(@@db, 'foobar', nil, 7)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_seek
|
83
|
+
GridStore.open(@@db, 'foobar', 'r') { |f|
|
84
|
+
f.seek(0)
|
85
|
+
assert_equal 'h', f.getc.chr
|
86
|
+
f.seek(7)
|
87
|
+
assert_equal 'w', f.getc.chr
|
88
|
+
f.seek(4)
|
89
|
+
assert_equal 'o', f.getc.chr
|
90
|
+
|
91
|
+
f.seek(-1, IO::SEEK_END)
|
92
|
+
assert_equal '!', f.getc.chr
|
93
|
+
f.seek(-6, IO::SEEK_END)
|
94
|
+
assert_equal 'w', f.getc.chr
|
95
|
+
|
96
|
+
f.seek(0)
|
97
|
+
f.seek(7, IO::SEEK_CUR)
|
98
|
+
assert_equal 'w', f.getc.chr
|
99
|
+
f.seek(-1, IO::SEEK_CUR)
|
100
|
+
assert_equal 'w', f.getc.chr
|
101
|
+
f.seek(-4, IO::SEEK_CUR)
|
102
|
+
assert_equal 'o', f.getc.chr
|
103
|
+
f.seek(3, IO::SEEK_CUR)
|
104
|
+
assert_equal 'o', f.getc.chr
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_multi_chunk
|
109
|
+
@@chunks.clear
|
110
|
+
@@files.clear
|
111
|
+
|
112
|
+
size = 512
|
113
|
+
GridStore.open(@@db, 'biggie', 'w') { |f|
|
114
|
+
f.chunk_size = size
|
115
|
+
f.write('x' * size)
|
116
|
+
f.write('y' * size)
|
117
|
+
f.write('z' * size)
|
118
|
+
}
|
119
|
+
|
120
|
+
assert_equal 3, @@chunks.count
|
121
|
+
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_puts_and_readlines
|
125
|
+
GridStore.open(@@db, 'multiline', 'w') { |f|
|
126
|
+
f.puts "line one"
|
127
|
+
f.puts "line two\n"
|
128
|
+
f.puts "line three"
|
129
|
+
}
|
130
|
+
|
131
|
+
lines = GridStore.readlines(@@db, 'multiline')
|
132
|
+
assert_equal ["line one\n", "line two\n", "line three\n"], lines
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_unlink
|
136
|
+
assert_equal 1, @@files.count
|
137
|
+
assert_equal 1, @@chunks.count
|
138
|
+
GridStore.unlink(@@db, 'foobar')
|
139
|
+
assert_equal 0, @@files.count
|
140
|
+
assert_equal 0, @@chunks.count
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_append
|
144
|
+
GridStore.open(@@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
|
145
|
+
assert_equal 1, @@chunks.count
|
146
|
+
assert_equal "hello, world! how are you?", GridStore.read(@@db, 'foobar')
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_rewind_and_truncate_on_write
|
150
|
+
GridStore.open(@@db, 'foobar', 'w') { |f|
|
151
|
+
f.write("some text is inserted here")
|
152
|
+
f.rewind
|
153
|
+
f.write("abc")
|
154
|
+
}
|
155
|
+
assert_equal "abc", GridStore.read(@@db, 'foobar')
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_tell
|
159
|
+
GridStore.open(@@db, 'foobar', 'r') { |f|
|
160
|
+
f.read(5)
|
161
|
+
assert_equal 5, f.tell
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_empty_block_ok
|
166
|
+
GridStore.open(@@db, 'empty', 'w')
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_save_empty_file
|
170
|
+
@@chunks.clear
|
171
|
+
@@files.clear
|
172
|
+
GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
|
173
|
+
assert_equal 1, @@files.count
|
174
|
+
assert_equal 0, @@chunks.count
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_empty_file_eof
|
178
|
+
GridStore.open(@@db, 'empty', 'w')
|
179
|
+
GridStore.open(@@db, 'empty', 'r') { |f|
|
180
|
+
assert f.eof?
|
181
|
+
}
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_cannot_change_chunk_size_on_read
|
185
|
+
begin
|
186
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| f.chunk_size = 42 }
|
187
|
+
fail "should have seen error"
|
188
|
+
rescue => ex
|
189
|
+
assert_match /error: can only change chunk size/, ex.to_s
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_cannot_change_chunk_size_after_data_written
|
194
|
+
begin
|
195
|
+
GridStore.open(@@db, 'foobar', 'w') { |f|
|
196
|
+
f.write("some text")
|
197
|
+
f.chunk_size = 42
|
198
|
+
}
|
199
|
+
fail "should have seen error"
|
200
|
+
rescue => ex
|
201
|
+
assert_match /error: can only change chunk size/, ex.to_s
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_change_chunk_size
|
206
|
+
GridStore.open(@@db, 'new-file', 'w') { |f|
|
207
|
+
f.chunk_size = 42
|
208
|
+
f.write("foo")
|
209
|
+
}
|
210
|
+
GridStore.open(@@db, 'new-file', 'r') { |f|
|
211
|
+
assert f.chunk_size == 42
|
212
|
+
}
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_chunk_size_in_option
|
216
|
+
GridStore.open(@@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
|
217
|
+
GridStore.open(@@db, 'new-file', 'r') { |f|
|
218
|
+
assert f.chunk_size == 42
|
219
|
+
}
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_md5
|
223
|
+
GridStore.open(@@db, 'new-file', 'w') { |f| f.write("hello world\n")}
|
224
|
+
GridStore.open(@@db, 'new-file', 'r') { |f|
|
225
|
+
assert f.md5 == '6f5902ac237024bdd0c176cb93063dc4'
|
226
|
+
begin
|
227
|
+
f.md5 = 'cant do this'
|
228
|
+
fail "should have seen error"
|
229
|
+
rescue => ex
|
230
|
+
true
|
231
|
+
end
|
232
|
+
}
|
233
|
+
GridStore.open(@@db, 'new-file', 'w') {}
|
234
|
+
GridStore.open(@@db, 'new-file', 'r') { |f|
|
235
|
+
assert f.md5 == 'd41d8cd98f00b204e9800998ecf8427e'
|
236
|
+
}
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_upload_date
|
240
|
+
now = Time.now
|
241
|
+
orig_file_upload_date = nil
|
242
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
|
243
|
+
assert_not_nil orig_file_upload_date
|
244
|
+
assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
|
245
|
+
|
246
|
+
sleep(2)
|
247
|
+
GridStore.open(@@db, 'foobar', 'w') { |f| f.write "new data" }
|
248
|
+
file_upload_date = nil
|
249
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
|
250
|
+
assert_equal orig_file_upload_date, file_upload_date
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_content_type
|
254
|
+
ct = nil
|
255
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| ct = f.content_type }
|
256
|
+
assert_equal GridStore::DEFAULT_CONTENT_TYPE, ct
|
257
|
+
|
258
|
+
GridStore.open(@@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
|
259
|
+
ct2 = nil
|
260
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| ct2 = f.content_type }
|
261
|
+
assert_equal 'text/html', ct2
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_content_type_option
|
265
|
+
GridStore.open(@@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
|
266
|
+
ct = nil
|
267
|
+
GridStore.open(@@db, 'new-file', 'r') { |f| ct = f.content_type }
|
268
|
+
assert_equal 'image/jpg', ct
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_unknown_mode
|
272
|
+
GridStore.open(@@db, 'foobar', 'x')
|
273
|
+
fail 'should have seen "illegal mode" error raised'
|
274
|
+
rescue => ex
|
275
|
+
assert_equal "error: illegal mode x", ex.to_s
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_metadata
|
279
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| assert_nil f.metadata }
|
280
|
+
GridStore.open(@@db, 'foobar', 'w+') { |f| f.metadata = {'a' => 1} }
|
281
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| assert_equal({'a' => 1}, f.metadata) }
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class MessageTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Mongo
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@msg = Message.new(42)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_initial_info
|
14
|
+
assert_equal Message::HEADER_SIZE, @msg.buf.length
|
15
|
+
@msg.write_long(1029)
|
16
|
+
@msg.buf.rewind
|
17
|
+
assert_equal Message::HEADER_SIZE + 8, @msg.buf.get_int
|
18
|
+
@msg.buf.get_int # skip message id
|
19
|
+
assert_equal 0, @msg.buf.get_int
|
20
|
+
assert_equal 42, @msg.buf.get_int
|
21
|
+
assert_equal 1029, @msg.buf.get_long
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_update_length
|
25
|
+
@msg.update_message_length
|
26
|
+
@msg.buf.rewind
|
27
|
+
assert_equal Message::HEADER_SIZE, @msg.buf.get_int
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_long_length
|
31
|
+
@msg.write_long(1027)
|
32
|
+
assert_equal Message::HEADER_SIZE + 8, @msg.buf.length
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class ObjectIDTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Mongo
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@o = ObjectID.new()
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_different
|
14
|
+
a = ObjectID.new
|
15
|
+
b = ObjectID.new
|
16
|
+
assert_not_equal a.to_a, b.to_a
|
17
|
+
assert_not_equal a, b
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_eql?
|
21
|
+
o2 = ObjectID.new(@o.to_a)
|
22
|
+
assert_equal @o, o2
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_to_s
|
26
|
+
s = @o.to_s
|
27
|
+
assert_equal 24, s.length
|
28
|
+
s =~ /^([0-9a-f]+)$/
|
29
|
+
assert_equal 24, $1.length
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_to_s_legacy
|
33
|
+
s = @o.to_s_legacy
|
34
|
+
assert_equal 24, s.length
|
35
|
+
s =~ /^([0-9a-f]+)$/
|
36
|
+
assert_equal 24, $1.length
|
37
|
+
|
38
|
+
assert_not_equal s, @o.to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_save_and_restore
|
42
|
+
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
43
|
+
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
44
|
+
db = Connection.new(host, port).db('ruby-mongo-test')
|
45
|
+
coll = db.collection('test')
|
46
|
+
|
47
|
+
coll.clear
|
48
|
+
coll << {'a' => 1, '_id' => @o}
|
49
|
+
|
50
|
+
row = coll.find().collect.first
|
51
|
+
assert_equal 1, row['a']
|
52
|
+
assert_equal @o, row['_id']
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_from_string
|
56
|
+
hex_str = @o.to_s
|
57
|
+
o2 = ObjectID.from_string(hex_str)
|
58
|
+
assert_equal hex_str, o2.to_s
|
59
|
+
assert_equal @o, o2
|
60
|
+
assert_equal @o.to_s, o2.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_from_string_legacy
|
64
|
+
hex_str = @o.to_s_legacy
|
65
|
+
o2 = ObjectID.from_string_legacy(hex_str)
|
66
|
+
assert_equal hex_str, o2.to_s_legacy
|
67
|
+
assert_equal @o, o2
|
68
|
+
assert_equal @o.to_s, o2.to_s
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_legal
|
72
|
+
assert !ObjectID.legal?(nil)
|
73
|
+
assert !ObjectID.legal?("fred")
|
74
|
+
assert !ObjectID.legal?("0000")
|
75
|
+
assert !ObjectID.legal?('000102030405060708090A0')
|
76
|
+
assert ObjectID.legal?('000102030405060708090A0B')
|
77
|
+
assert ObjectID.legal?('abcdefABCDEF123456789012')
|
78
|
+
assert !ObjectID.legal?('abcdefABCDEF12345678901x')
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_from_string_leading_zeroes
|
82
|
+
hex_str = '000000000000000000000000'
|
83
|
+
o = ObjectID.from_string(hex_str)
|
84
|
+
assert_equal hex_str, o.to_s
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_byte_order
|
88
|
+
hex_str = '000102030405060708090A0B'
|
89
|
+
o = ObjectID.from_string(hex_str)
|
90
|
+
assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_legacy_byte_order
|
94
|
+
hex_str = '000102030405060708090A0B'
|
95
|
+
o = ObjectID.from_string_legacy(hex_str)
|
96
|
+
assert_equal [0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x0b, 0x0a, 0x09, 0x08], o.to_a
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_legacy_string_convert
|
100
|
+
l = @o.to_s_legacy
|
101
|
+
s = @o.to_s
|
102
|
+
assert_equal s, ObjectID.legacy_string_convert(l)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'mongo/util/ordered_hash'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class OrderedHashTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@oh = OrderedHash.new
|
9
|
+
@oh['c'] = 1
|
10
|
+
@oh['a'] = 2
|
11
|
+
@oh['z'] = 3
|
12
|
+
@ordered_keys = %w(c a z)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_initialize
|
16
|
+
a = OrderedHash.new
|
17
|
+
a['x'] = 1
|
18
|
+
a['y'] = 2
|
19
|
+
|
20
|
+
b = OrderedHash['x' => 1, 'y' => 2]
|
21
|
+
assert_equal a, b
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_empty
|
25
|
+
assert_equal [], OrderedHash.new.keys
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_equality
|
29
|
+
a = OrderedHash.new
|
30
|
+
a['x'] = 1
|
31
|
+
a['y'] = 2
|
32
|
+
|
33
|
+
b = OrderedHash.new
|
34
|
+
b['y'] = 2
|
35
|
+
b['x'] = 1
|
36
|
+
|
37
|
+
c = OrderedHash.new
|
38
|
+
c['x'] = 1
|
39
|
+
c['y'] = 2
|
40
|
+
|
41
|
+
d = OrderedHash.new
|
42
|
+
d['x'] = 2
|
43
|
+
d['y'] = 3
|
44
|
+
|
45
|
+
e = OrderedHash.new
|
46
|
+
e['z'] = 1
|
47
|
+
e['y'] = 2
|
48
|
+
|
49
|
+
assert_equal a, c
|
50
|
+
assert_not_equal a, b
|
51
|
+
assert_not_equal a, d
|
52
|
+
assert_not_equal a, e
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_order_preserved
|
56
|
+
assert_equal @ordered_keys, @oh.keys
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_order_preserved_after_replace
|
60
|
+
@oh['a'] = 42
|
61
|
+
assert_equal @ordered_keys, @oh.keys
|
62
|
+
@oh['c'] = 'foobar'
|
63
|
+
assert_equal @ordered_keys, @oh.keys
|
64
|
+
@oh['z'] = /huh?/
|
65
|
+
assert_equal @ordered_keys, @oh.keys
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_each
|
69
|
+
keys = []
|
70
|
+
@oh.each { |k, v| keys << k }
|
71
|
+
assert_equal keys, @oh.keys
|
72
|
+
|
73
|
+
@oh['z'] = 42
|
74
|
+
assert_equal keys, @oh.keys
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_values
|
78
|
+
assert_equal [1, 2, 3], @oh.values
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_merge
|
82
|
+
other = OrderedHash.new
|
83
|
+
other['f'] = 'foo'
|
84
|
+
noob = @oh.merge(other)
|
85
|
+
assert_equal @ordered_keys + ['f'], noob.keys
|
86
|
+
assert_equal [1, 2, 3, 'foo'], noob.values
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_merge_bang
|
90
|
+
other = OrderedHash.new
|
91
|
+
other['f'] = 'foo'
|
92
|
+
@oh.merge!(other)
|
93
|
+
assert_equal @ordered_keys + ['f'], @oh.keys
|
94
|
+
assert_equal [1, 2, 3, 'foo'], @oh.values
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_merge_bang_with_overlap
|
98
|
+
other = OrderedHash.new
|
99
|
+
other['a'] = 'apple'
|
100
|
+
other['c'] = 'crab'
|
101
|
+
other['f'] = 'foo'
|
102
|
+
@oh.merge!(other)
|
103
|
+
assert_equal @ordered_keys + ['f'], @oh.keys
|
104
|
+
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_merge_bang_with_hash_with_overlap
|
108
|
+
other = Hash.new
|
109
|
+
other['a'] = 'apple'
|
110
|
+
other['c'] = 'crab'
|
111
|
+
other['f'] = 'foo'
|
112
|
+
@oh.merge!(other)
|
113
|
+
assert_equal @ordered_keys + ['f'], @oh.keys
|
114
|
+
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_inspect_retains_order
|
118
|
+
assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_clear
|
122
|
+
@oh.clear
|
123
|
+
assert @oh.keys.empty?
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_delete
|
127
|
+
assert @oh.keys.include?('z')
|
128
|
+
@oh.delete('z')
|
129
|
+
assert !@oh.keys.include?('z')
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_delete_if
|
133
|
+
assert @oh.keys.include?('z')
|
134
|
+
@oh.delete_if { |k,v| k == 'z' }
|
135
|
+
assert !@oh.keys.include?('z')
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|