mongo-find_replace 0.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. data/LICENSE.txt +202 -0
  2. data/README.rdoc +358 -0
  3. data/Rakefile +133 -0
  4. data/bin/bson_benchmark.rb +59 -0
  5. data/bin/fail_if_no_c.rb +11 -0
  6. data/examples/admin.rb +42 -0
  7. data/examples/capped.rb +22 -0
  8. data/examples/cursor.rb +48 -0
  9. data/examples/gridfs.rb +88 -0
  10. data/examples/index_test.rb +126 -0
  11. data/examples/info.rb +31 -0
  12. data/examples/queries.rb +70 -0
  13. data/examples/simple.rb +24 -0
  14. data/examples/strict.rb +35 -0
  15. data/examples/types.rb +36 -0
  16. data/lib/mongo.rb +61 -0
  17. data/lib/mongo/admin.rb +95 -0
  18. data/lib/mongo/collection.rb +664 -0
  19. data/lib/mongo/connection.rb +555 -0
  20. data/lib/mongo/cursor.rb +393 -0
  21. data/lib/mongo/db.rb +527 -0
  22. data/lib/mongo/exceptions.rb +60 -0
  23. data/lib/mongo/gridfs.rb +22 -0
  24. data/lib/mongo/gridfs/chunk.rb +90 -0
  25. data/lib/mongo/gridfs/grid_store.rb +555 -0
  26. data/lib/mongo/types/binary.rb +48 -0
  27. data/lib/mongo/types/code.rb +36 -0
  28. data/lib/mongo/types/dbref.rb +38 -0
  29. data/lib/mongo/types/min_max_keys.rb +58 -0
  30. data/lib/mongo/types/objectid.rb +219 -0
  31. data/lib/mongo/types/regexp_of_holding.rb +45 -0
  32. data/lib/mongo/util/bson_c.rb +18 -0
  33. data/lib/mongo/util/bson_ruby.rb +595 -0
  34. data/lib/mongo/util/byte_buffer.rb +222 -0
  35. data/lib/mongo/util/conversions.rb +97 -0
  36. data/lib/mongo/util/ordered_hash.rb +135 -0
  37. data/lib/mongo/util/server_version.rb +69 -0
  38. data/lib/mongo/util/support.rb +26 -0
  39. data/lib/mongo/util/xml_to_ruby.rb +112 -0
  40. data/mongo-ruby-driver.gemspec +28 -0
  41. data/test/replica/count_test.rb +34 -0
  42. data/test/replica/insert_test.rb +50 -0
  43. data/test/replica/pooled_insert_test.rb +54 -0
  44. data/test/replica/query_test.rb +39 -0
  45. data/test/test_admin.rb +67 -0
  46. data/test/test_bson.rb +397 -0
  47. data/test/test_byte_buffer.rb +81 -0
  48. data/test/test_chunk.rb +82 -0
  49. data/test/test_collection.rb +534 -0
  50. data/test/test_connection.rb +160 -0
  51. data/test/test_conversions.rb +120 -0
  52. data/test/test_cursor.rb +386 -0
  53. data/test/test_db.rb +254 -0
  54. data/test/test_db_api.rb +783 -0
  55. data/test/test_db_connection.rb +16 -0
  56. data/test/test_grid_store.rb +306 -0
  57. data/test/test_helper.rb +42 -0
  58. data/test/test_objectid.rb +156 -0
  59. data/test/test_ordered_hash.rb +168 -0
  60. data/test/test_round_trip.rb +114 -0
  61. data/test/test_slave_connection.rb +36 -0
  62. data/test/test_threading.rb +87 -0
  63. data/test/threading/test_threading_large_pool.rb +90 -0
  64. data/test/unit/collection_test.rb +52 -0
  65. data/test/unit/connection_test.rb +59 -0
  66. data/test/unit/cursor_test.rb +94 -0
  67. data/test/unit/db_test.rb +97 -0
  68. metadata +123 -0
@@ -0,0 +1,16 @@
1
+ require 'test/test_helper'
2
+
3
+ # NOTE: assumes Mongo is running
4
+ class DBConnectionTest < Test::Unit::TestCase
5
+
6
+ include Mongo
7
+
8
+ def test_no_exceptions
9
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
10
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
11
+ db = Connection.new(host, port).db('ruby-mongo-demo')
12
+ coll = db.collection('test')
13
+ coll.remove
14
+ db.error
15
+ end
16
+ end
@@ -0,0 +1,306 @@
1
+ require 'test/test_helper'
2
+ require 'mongo/gridfs'
3
+
4
+ class GridStoreTest < Test::Unit::TestCase
5
+
6
+ include Mongo
7
+ include GridFS
8
+
9
+ @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
10
+ ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
11
+ @@files = @@db.collection('fs.files')
12
+ @@chunks = @@db.collection('fs.chunks')
13
+
14
+ def setup
15
+ @@chunks.remove
16
+ @@files.remove
17
+ GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
18
+ end
19
+
20
+ def teardown
21
+ @@chunks.remove
22
+ @@files.remove
23
+ @@db.error
24
+ end
25
+
26
+ def test_exist
27
+ assert GridStore.exist?(@@db, 'foobar')
28
+ assert !GridStore.exist?(@@db, 'does_not_exist')
29
+ assert !GridStore.exist?(@@db, 'foobar', 'another_root')
30
+ end
31
+
32
+ def test_list
33
+ assert_equal ['foobar'], GridStore.list(@@db)
34
+ assert_equal ['foobar'], GridStore.list(@@db, 'fs')
35
+ assert_equal [], GridStore.list(@@db, 'my_fs')
36
+
37
+ GridStore.open(@@db, 'test', 'w') { |f| f.write("my file") }
38
+
39
+ assert_equal ['foobar', 'test'], GridStore.list(@@db)
40
+ end
41
+
42
+ def test_small_write
43
+ rows = @@files.find({'filename' => 'foobar'}).to_a
44
+ assert_not_nil rows
45
+ assert_equal 1, rows.length
46
+ row = rows[0]
47
+ assert_not_nil row
48
+
49
+ file_id = row['_id']
50
+ assert_kind_of ObjectID, file_id
51
+ rows = @@chunks.find({'files_id' => file_id}).to_a
52
+ assert_not_nil rows
53
+ assert_equal 1, rows.length
54
+ end
55
+
56
+ def test_small_file
57
+ rows = @@files.find({'filename' => 'foobar'}).to_a
58
+ assert_not_nil rows
59
+ assert_equal 1, rows.length
60
+ row = rows[0]
61
+ assert_not_nil row
62
+ assert_equal "hello, world!", GridStore.read(@@db, 'foobar')
63
+ end
64
+
65
+ def test_overwrite
66
+ GridStore.open(@@db, 'foobar', 'w') { |f| f.write("overwrite") }
67
+ assert_equal "overwrite", GridStore.read(@@db, 'foobar')
68
+ end
69
+
70
+ def test_read_length
71
+ assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
72
+ end
73
+
74
+ def test_read_with_and_without_length
75
+ GridStore.open(@@db, 'read-types', 'w') do |f|
76
+ f.write('hello, there')
77
+ end
78
+
79
+ GridStore.open(@@db, 'read-types', 'r') do |f|
80
+ assert_equal 'hello, ', f.read(7)
81
+ assert_equal 'there', f.read
82
+ end
83
+ end
84
+
85
+ def test_access_length
86
+ assert_equal 13, GridStore.new(@@db, 'foobar').length
87
+ end
88
+
89
+ # Also tests seek
90
+ def test_read_with_offset
91
+ assert_equal "world!", GridStore.read(@@db, 'foobar', nil, 7)
92
+ end
93
+
94
+ def test_seek
95
+ GridStore.open(@@db, 'foobar', 'r') { |f|
96
+ f.seek(0)
97
+ assert_equal 'h', f.getc.chr
98
+ f.seek(7)
99
+ assert_equal 'w', f.getc.chr
100
+ f.seek(4)
101
+ assert_equal 'o', f.getc.chr
102
+
103
+ f.seek(-1, IO::SEEK_END)
104
+ assert_equal '!', f.getc.chr
105
+ f.seek(-6, IO::SEEK_END)
106
+ assert_equal 'w', f.getc.chr
107
+
108
+ f.seek(0)
109
+ f.seek(7, IO::SEEK_CUR)
110
+ assert_equal 'w', f.getc.chr
111
+ f.seek(-1, IO::SEEK_CUR)
112
+ assert_equal 'w', f.getc.chr
113
+ f.seek(-4, IO::SEEK_CUR)
114
+ assert_equal 'o', f.getc.chr
115
+ f.seek(3, IO::SEEK_CUR)
116
+ assert_equal 'o', f.getc.chr
117
+ }
118
+ end
119
+
120
+ def test_multi_chunk
121
+ @@chunks.remove
122
+ @@files.remove
123
+
124
+ size = 512
125
+ GridStore.open(@@db, 'biggie', 'w') { |f|
126
+ f.chunk_size = size
127
+ f.write('x' * size)
128
+ f.write('y' * size)
129
+ f.write('z' * size)
130
+ }
131
+
132
+ assert_equal 3, @@chunks.count
133
+ #assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
134
+ end
135
+
136
+ def test_puts_and_readlines
137
+ GridStore.open(@@db, 'multiline', 'w') { |f|
138
+ f.puts "line one"
139
+ f.puts "line two\n"
140
+ f.puts "line three"
141
+ }
142
+
143
+ lines = GridStore.readlines(@@db, 'multiline')
144
+ assert_equal ["line one\n", "line two\n", "line three\n"], lines
145
+ end
146
+
147
+ def test_unlink
148
+ assert_equal 1, @@files.count
149
+ assert_equal 1, @@chunks.count
150
+ GridStore.unlink(@@db, 'foobar')
151
+ assert_equal 0, @@files.count
152
+ assert_equal 0, @@chunks.count
153
+ end
154
+
155
+ def test_mv
156
+ assert_equal 1, @@files.count
157
+ assert_equal 1, @@chunks.count
158
+ GridStore.mv(@@db, 'foobar', 'bazqux')
159
+ assert_equal 1, @@files.count
160
+ assert_equal 1, @@chunks.count
161
+ assert !GridStore.exist?(@@db, 'foobar')
162
+ assert GridStore.exist?(@@db, 'bazqux')
163
+ end
164
+
165
+ def test_append
166
+ GridStore.open(@@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
167
+ assert_equal 1, @@chunks.count
168
+ assert_equal "hello, world! how are you?", GridStore.read(@@db, 'foobar')
169
+ end
170
+
171
+ def test_rewind_and_truncate_on_write
172
+ GridStore.open(@@db, 'foobar', 'w') { |f|
173
+ f.write("some text is inserted here")
174
+ f.rewind
175
+ f.write("abc")
176
+ }
177
+ assert_equal "abc", GridStore.read(@@db, 'foobar')
178
+ end
179
+
180
+ def test_tell
181
+ GridStore.open(@@db, 'foobar', 'r') { |f|
182
+ f.read(5)
183
+ assert_equal 5, f.tell
184
+ }
185
+ end
186
+
187
+ def test_empty_block_ok
188
+ GridStore.open(@@db, 'empty', 'w')
189
+ end
190
+
191
+ def test_save_empty_file
192
+ @@chunks.remove
193
+ @@files.remove
194
+ GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
195
+ assert_equal 1, @@files.count
196
+ assert_equal 0, @@chunks.count
197
+ end
198
+
199
+ def test_empty_file_eof
200
+ GridStore.open(@@db, 'empty', 'w')
201
+ GridStore.open(@@db, 'empty', 'r') { |f|
202
+ assert f.eof?
203
+ }
204
+ end
205
+
206
+ def test_cannot_change_chunk_size_on_read
207
+ begin
208
+ GridStore.open(@@db, 'foobar', 'r') { |f| f.chunk_size = 42 }
209
+ fail "should have seen error"
210
+ rescue => ex
211
+ assert_match /error: can only change chunk size/, ex.to_s
212
+ end
213
+ end
214
+
215
+ def test_cannot_change_chunk_size_after_data_written
216
+ begin
217
+ GridStore.open(@@db, 'foobar', 'w') { |f|
218
+ f.write("some text")
219
+ f.chunk_size = 42
220
+ }
221
+ fail "should have seen error"
222
+ rescue => ex
223
+ assert_match /error: can only change chunk size/, ex.to_s
224
+ end
225
+ end
226
+
227
+ def test_change_chunk_size
228
+ GridStore.open(@@db, 'new-file', 'w') { |f|
229
+ f.chunk_size = 42
230
+ f.write("foo")
231
+ }
232
+ GridStore.open(@@db, 'new-file', 'r') { |f|
233
+ assert f.chunk_size == 42
234
+ }
235
+ end
236
+
237
+ def test_chunk_size_in_option
238
+ GridStore.open(@@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
239
+ GridStore.open(@@db, 'new-file', 'r') { |f|
240
+ assert f.chunk_size == 42
241
+ }
242
+ end
243
+
244
+ def test_md5
245
+ GridStore.open(@@db, 'new-file', 'w') { |f| f.write("hello world\n")}
246
+ GridStore.open(@@db, 'new-file', 'r') { |f|
247
+ assert f.md5 == '6f5902ac237024bdd0c176cb93063dc4'
248
+ begin
249
+ f.md5 = 'cant do this'
250
+ fail "should have seen error"
251
+ rescue => ex
252
+ true
253
+ end
254
+ }
255
+ GridStore.open(@@db, 'new-file', 'w') {}
256
+ GridStore.open(@@db, 'new-file', 'r') { |f|
257
+ assert f.md5 == 'd41d8cd98f00b204e9800998ecf8427e'
258
+ }
259
+ end
260
+
261
+ def test_upload_date
262
+ now = Time.now
263
+ orig_file_upload_date = nil
264
+ GridStore.open(@@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
265
+ assert_not_nil orig_file_upload_date
266
+ assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
267
+
268
+ sleep(2)
269
+ GridStore.open(@@db, 'foobar', 'w') { |f| f.write "new data" }
270
+ file_upload_date = nil
271
+ GridStore.open(@@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
272
+ assert_equal orig_file_upload_date, file_upload_date
273
+ end
274
+
275
+ def test_content_type
276
+ ct = nil
277
+ GridStore.open(@@db, 'foobar', 'r') { |f| ct = f.content_type }
278
+ assert_equal GridStore::DEFAULT_CONTENT_TYPE, ct
279
+
280
+ GridStore.open(@@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
281
+ ct2 = nil
282
+ GridStore.open(@@db, 'foobar', 'r') { |f| ct2 = f.content_type }
283
+ assert_equal 'text/html', ct2
284
+ end
285
+
286
+ def test_content_type_option
287
+ GridStore.open(@@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
288
+ ct = nil
289
+ GridStore.open(@@db, 'new-file', 'r') { |f| ct = f.content_type }
290
+ assert_equal 'image/jpg', ct
291
+ end
292
+
293
+ def test_unknown_mode
294
+ GridStore.open(@@db, 'foobar', 'x')
295
+ fail 'should have seen "illegal mode" error raised'
296
+ rescue => ex
297
+ assert_equal "error: illegal mode x", ex.to_s
298
+ end
299
+
300
+ def test_metadata
301
+ GridStore.open(@@db, 'foobar', 'r') { |f| assert_nil f.metadata }
302
+ GridStore.open(@@db, 'foobar', 'w+') { |f| f.metadata = {'a' => 1} }
303
+ GridStore.open(@@db, 'foobar', 'r') { |f| assert_equal({'a' => 1}, f.metadata) }
304
+ end
305
+
306
+ end
@@ -0,0 +1,42 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'rubygems' if ENV['C_EXT']
3
+ require 'mongo'
4
+ require 'test/unit'
5
+
6
+ begin
7
+ require 'rubygems'
8
+ require 'shoulda'
9
+ require 'mocha'
10
+ rescue LoadError
11
+ puts <<MSG
12
+
13
+ This test suite now requires shoulda and mocha.
14
+ You can install these gems as follows:
15
+ gem install shoulda
16
+ gem install mocha
17
+
18
+ MSG
19
+ exit
20
+ end
21
+
22
+ require 'mongo_ext/cbson' if ENV['C_EXT']
23
+
24
+ # NOTE: most tests assume that MongoDB is running.
25
+ class Test::Unit::TestCase
26
+ include Mongo
27
+
28
+ # Generic code for rescuing connection failures and retrying operations.
29
+ # This could be combined with some timeout functionality.
30
+ def rescue_connection_failure
31
+ success = false
32
+ while !success
33
+ begin
34
+ yield
35
+ success = true
36
+ rescue Mongo::ConnectionFailure
37
+ puts "Rescuing"
38
+ sleep(1)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,156 @@
1
+ require 'test/test_helper'
2
+
3
+ class ObjectIDTest < Test::Unit::TestCase
4
+
5
+ include Mongo
6
+
7
+ def setup
8
+ @o = ObjectID.new
9
+ end
10
+
11
+ def test_hashcode
12
+ assert_equal @o.instance_variable_get(:@data).hash, @o.hash
13
+ end
14
+
15
+ def test_array_uniq_for_equilavent_ids
16
+ a = ObjectID.new('123')
17
+ b = ObjectID.new('123')
18
+ assert_equal a, b
19
+ assert_equal 1, [a, b].uniq.size
20
+ end
21
+
22
+ def test_create_pk_method
23
+ doc = {:name => 'Mongo'}
24
+ doc = ObjectID.create_pk(doc)
25
+ assert doc[:_id]
26
+
27
+ doc = {:name => 'Mongo', :_id => '12345'}
28
+ doc = ObjectID.create_pk(doc)
29
+ assert_equal '12345', doc[:_id]
30
+ end
31
+
32
+ def test_different
33
+ a = ObjectID.new
34
+ b = ObjectID.new
35
+ assert_not_equal a.to_a, b.to_a
36
+ assert_not_equal a, b
37
+ end
38
+
39
+ def test_eql?
40
+ o2 = ObjectID.new(@o.to_a)
41
+ assert_equal @o, o2
42
+ end
43
+
44
+ def test_to_s
45
+ s = @o.to_s
46
+ assert_equal 24, s.length
47
+ s =~ /^([0-9a-f]+)$/
48
+ assert_equal 24, $1.length
49
+ end
50
+
51
+ def test_to_s_legacy
52
+ s = @o.to_s_legacy
53
+ assert_equal 24, s.length
54
+ s =~ /^([0-9a-f]+)$/
55
+ assert_equal 24, $1.length
56
+
57
+ assert_not_equal s, @o.to_s
58
+ end
59
+
60
+ def test_save_and_restore
61
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
62
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
63
+ db = Connection.new(host, port).db('ruby-mongo-test')
64
+ coll = db.collection('test')
65
+
66
+ coll.remove
67
+ coll << {'a' => 1, '_id' => @o}
68
+
69
+ row = coll.find().collect.first
70
+ assert_equal 1, row['a']
71
+ assert_equal @o, row['_id']
72
+ end
73
+
74
+ def test_from_string
75
+ hex_str = @o.to_s
76
+ o2 = ObjectID.from_string(hex_str)
77
+ assert_equal hex_str, o2.to_s
78
+ assert_equal @o, o2
79
+ assert_equal @o.to_s, o2.to_s
80
+ end
81
+
82
+ def test_illegal_from_string
83
+ assert_raise InvalidObjectID do
84
+ ObjectID.from_string("")
85
+ end
86
+ end
87
+
88
+ def test_from_string_legacy
89
+ hex_str = @o.to_s_legacy
90
+ o2 = ObjectID.from_string_legacy(hex_str)
91
+ assert_equal hex_str, o2.to_s_legacy
92
+ assert_equal @o, o2
93
+ assert_equal @o.to_s, o2.to_s
94
+ end
95
+
96
+ def test_illegal_from_string_legacy
97
+ assert_raise InvalidObjectID do
98
+ ObjectID.from_string_legacy("")
99
+ end
100
+ end
101
+
102
+ def test_legal
103
+ assert !ObjectID.legal?(nil)
104
+ assert !ObjectID.legal?("fred")
105
+ assert !ObjectID.legal?("0000")
106
+ assert !ObjectID.legal?('000102030405060708090A0')
107
+ assert ObjectID.legal?('000102030405060708090A0B')
108
+ assert ObjectID.legal?('abcdefABCDEF123456789012')
109
+ assert !ObjectID.legal?('abcdefABCDEF12345678901x')
110
+ end
111
+
112
+ def test_from_string_leading_zeroes
113
+ hex_str = '000000000000000000000000'
114
+ o = ObjectID.from_string(hex_str)
115
+ assert_equal hex_str, o.to_s
116
+ end
117
+
118
+ def test_byte_order
119
+ hex_str = '000102030405060708090A0B'
120
+ o = ObjectID.from_string(hex_str)
121
+ assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
122
+ end
123
+
124
+ def test_legacy_byte_order
125
+ hex_str = '000102030405060708090A0B'
126
+ o = ObjectID.from_string_legacy(hex_str)
127
+ assert_equal [0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x0b, 0x0a, 0x09, 0x08], o.to_a
128
+ end
129
+
130
+ def test_legacy_string_convert
131
+ l = @o.to_s_legacy
132
+ s = @o.to_s
133
+ assert_equal s, ObjectID.legacy_string_convert(l)
134
+ end
135
+
136
+ def test_generation_time
137
+ time = Time.now
138
+ id = ObjectID.new
139
+ generated_time = id.generation_time
140
+
141
+ assert_in_delta time.to_i, generated_time.to_i, 2
142
+ assert_equal "UTC", generated_time.zone
143
+ end
144
+
145
+ def test_from_time
146
+ time = Time.now.utc
147
+ id = ObjectID.from_time(time)
148
+
149
+ assert_equal time.to_i, id.generation_time.to_i
150
+ end
151
+
152
+ def test_json
153
+ id = ObjectID.new
154
+ assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
155
+ end
156
+ end