mongodb-mongo 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/run_test_script +1 -1
- data/lib/mongo/collection.rb +1 -0
- data/mongo-ruby-driver.gemspec +1 -1
- data/tests/test_admin.rb +12 -15
- data/tests/test_chunk.rb +10 -14
- data/tests/test_cursor.rb +11 -14
- data/tests/test_db.rb +44 -35
- data/tests/test_db_api.rb +142 -127
- data/tests/test_grid_store.rb +67 -72
- metadata +1 -1
data/bin/run_test_script
CHANGED
data/lib/mongo/collection.rb
CHANGED
@@ -108,6 +108,7 @@ module XGen
|
|
108
108
|
# Create a new index named +index_name+. +fields+ should be an array
|
109
109
|
# of field names.
|
110
110
|
def create_index(name, *fields)
|
111
|
+
fields = *fields if fields.kind_of?(Array) && fields.length == 1
|
111
112
|
@db.create_index(@name, name, fields)
|
112
113
|
end
|
113
114
|
|
data/mongo-ruby-driver.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mongo'
|
3
|
-
s.version = '0.5.
|
3
|
+
s.version = '0.5.3'
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.summary = 'Simple pure-Ruby driver for the 10gen Mongo DB'
|
6
6
|
s.description = 'A pure-Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
|
data/tests/test_admin.rb
CHANGED
@@ -7,24 +7,21 @@ class AdminTest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
include XGen::Mongo::Driver
|
9
9
|
|
10
|
+
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
11
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
|
12
|
+
@@coll = @@db.collection('test')
|
13
|
+
|
10
14
|
def setup
|
11
|
-
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
12
|
-
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
13
|
-
@db = Mongo.new(host, port).db('ruby-mongo-test')
|
14
15
|
# Insert some data to make sure the database itself exists.
|
15
|
-
|
16
|
-
@coll.
|
17
|
-
|
18
|
-
@
|
19
|
-
@admin = @db.admin
|
16
|
+
@@coll.clear
|
17
|
+
@r1 = @@coll.insert('a' => 1) # collection not created until it's used
|
18
|
+
@@coll_full_name = 'ruby-mongo-test.test'
|
19
|
+
@admin = @@db.admin
|
20
20
|
end
|
21
21
|
|
22
22
|
def teardown
|
23
|
-
|
24
|
-
|
25
|
-
@coll.clear if @coll
|
26
|
-
@db.close
|
27
|
-
end
|
23
|
+
@admin.profiling_level = :off
|
24
|
+
@@coll.clear if @@coll
|
28
25
|
end
|
29
26
|
|
30
27
|
def test_default_profiling_level
|
@@ -41,7 +38,7 @@ class AdminTest < Test::Unit::TestCase
|
|
41
38
|
def test_profiling_info
|
42
39
|
# Perform at least one query while profiling so we have something to see.
|
43
40
|
@admin.profiling_level = :all
|
44
|
-
|
41
|
+
@@coll.find()
|
45
42
|
@admin.profiling_level = :off
|
46
43
|
|
47
44
|
info = @admin.profiling_info
|
@@ -54,7 +51,7 @@ class AdminTest < Test::Unit::TestCase
|
|
54
51
|
end
|
55
52
|
|
56
53
|
def test_validate_collection
|
57
|
-
doc = @admin.validate_collection(
|
54
|
+
doc = @admin.validate_collection(@@coll.name)
|
58
55
|
assert_not_nil doc
|
59
56
|
result = doc['result']
|
60
57
|
assert_not_nil result
|
data/tests/test_chunk.rb
CHANGED
@@ -8,26 +8,22 @@ class ChunkTest < Test::Unit::TestCase
|
|
8
8
|
include XGen::Mongo::Driver
|
9
9
|
include XGen::Mongo::GridFS
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
12
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-utils-test')
|
13
|
+
@@files = @@db.collection('gridfs.files')
|
14
|
+
@@chunks = @@db.collection('gridfs.chunks')
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@files.clear
|
16
|
+
def setup
|
17
|
+
@@chunks.clear
|
18
|
+
@@files.clear
|
20
19
|
|
21
|
-
@f = GridStore.new(
|
20
|
+
@f = GridStore.new(@@db, 'foobar', 'w')
|
22
21
|
@c = @f.instance_variable_get('@curr_chunk')
|
23
22
|
end
|
24
23
|
|
25
24
|
def teardown
|
26
|
-
|
27
|
-
|
28
|
-
@files.clear
|
29
|
-
@db.close
|
30
|
-
end
|
25
|
+
@@chunks.clear
|
26
|
+
@@files.clear
|
31
27
|
end
|
32
28
|
|
33
29
|
def test_pos
|
data/tests/test_cursor.rb
CHANGED
@@ -7,25 +7,22 @@ class CursorTest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
include XGen::Mongo::Driver
|
9
9
|
|
10
|
+
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
11
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
|
12
|
+
@@coll = @@db.collection('test')
|
13
|
+
|
10
14
|
def setup
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@coll = @db.collection('test')
|
15
|
-
@coll.clear
|
16
|
-
@r1 = @coll.insert('a' => 1) # collection not created until it's used
|
17
|
-
@coll_full_name = 'ruby-mongo-test.test'
|
15
|
+
@@coll.clear
|
16
|
+
@@coll.insert('a' => 1) # collection not created until it's used
|
17
|
+
@@coll_full_name = 'ruby-mongo-test.test'
|
18
18
|
end
|
19
19
|
|
20
20
|
def teardown
|
21
|
-
|
22
|
-
@coll.clear if @coll
|
23
|
-
@db.close
|
24
|
-
end
|
21
|
+
@@coll.clear
|
25
22
|
end
|
26
23
|
|
27
24
|
def test_explain
|
28
|
-
cursor =
|
25
|
+
cursor = @@coll.find('a' => 1)
|
29
26
|
explaination = cursor.explain
|
30
27
|
assert_not_nil explaination['cursor']
|
31
28
|
assert_kind_of Numeric, explaination['n']
|
@@ -35,7 +32,7 @@ class CursorTest < Test::Unit::TestCase
|
|
35
32
|
|
36
33
|
def test_close_no_query_sent
|
37
34
|
begin
|
38
|
-
cursor =
|
35
|
+
cursor = @@coll.find('a' => 1)
|
39
36
|
cursor.close
|
40
37
|
assert cursor.closed?
|
41
38
|
rescue => ex
|
@@ -45,7 +42,7 @@ class CursorTest < Test::Unit::TestCase
|
|
45
42
|
|
46
43
|
def test_close_after_query_sent
|
47
44
|
begin
|
48
|
-
cursor =
|
45
|
+
cursor = @@coll.find('a' => 1)
|
49
46
|
cursor.next_object
|
50
47
|
cursor.close
|
51
48
|
assert cursor.closed?
|
data/tests/test_db.rb
CHANGED
@@ -15,50 +15,53 @@ class DBTest < Test::Unit::TestCase
|
|
15
15
|
|
16
16
|
include XGen::Mongo::Driver
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
@@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
19
|
+
@@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT
|
20
|
+
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
|
21
|
+
@@users = @@db.collection('system.users')
|
22
22
|
|
23
|
+
def setup
|
23
24
|
@spongebob = 'spongebob'
|
24
25
|
@spongebob_password = 'squarepants'
|
25
|
-
|
26
|
-
|
27
|
-
@users.insert(:user => @spongebob, :pwd => @db.send(:hash_password, @spongebob, @spongebob_password))
|
26
|
+
@@users.clear
|
27
|
+
@@users.insert(:user => @spongebob, :pwd => @@db.send(:hash_password, @spongebob, @spongebob_password))
|
28
28
|
end
|
29
29
|
|
30
30
|
def teardown
|
31
|
-
if
|
32
|
-
@users.clear if @users
|
33
|
-
@db.close
|
34
|
-
end
|
31
|
+
@@users.clear if @@users
|
35
32
|
end
|
36
33
|
|
37
34
|
def test_close
|
38
|
-
|
39
|
-
assert
|
35
|
+
@@db.close
|
36
|
+
assert !@@db.connected?
|
40
37
|
begin
|
41
|
-
|
38
|
+
@@db.collection('test').insert('a' => 1)
|
42
39
|
fail "expected 'NilClass' exception"
|
43
40
|
rescue => ex
|
44
41
|
assert_match /NilClass/, ex.to_s
|
42
|
+
ensure
|
43
|
+
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
|
44
|
+
@@users = @@db.collection('system.users')
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
def test_full_coll_name
|
49
|
-
coll =
|
50
|
-
assert_equal 'ruby-mongo-test.test',
|
49
|
+
coll = @@db.collection('test')
|
50
|
+
assert_equal 'ruby-mongo-test.test', @@db.full_coll_name(coll.name)
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_pair
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
assert
|
54
|
+
@@db.close
|
55
|
+
@@users = nil
|
56
|
+
@@db = Mongo.new({:left => "this-should-fail", :right => [@@host, @@port]}).db('ruby-mongo-test')
|
57
|
+
assert @@db.connected?
|
58
|
+
ensure
|
59
|
+
@@db = Mongo.new(@@host, @@port) unless @@db.connected?
|
60
|
+
@@users = @@db.collection('system.users')
|
58
61
|
end
|
59
62
|
|
60
63
|
def test_pk_factory
|
61
|
-
db = Mongo.new(
|
64
|
+
db = Mongo.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new)
|
62
65
|
coll = db.collection('test')
|
63
66
|
coll.clear
|
64
67
|
|
@@ -80,28 +83,31 @@ class DBTest < Test::Unit::TestCase
|
|
80
83
|
end
|
81
84
|
|
82
85
|
def test_pk_factory_reset
|
83
|
-
|
86
|
+
db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
|
87
|
+
db.pk_factory = Object.new # first time
|
84
88
|
begin
|
85
|
-
|
89
|
+
db.pk_factory = Object.new
|
86
90
|
fail "error: expected exception"
|
87
91
|
rescue => ex
|
88
92
|
assert_match /can not change PK factory/, ex.to_s
|
93
|
+
ensure
|
94
|
+
db.close
|
89
95
|
end
|
90
96
|
end
|
91
97
|
|
92
98
|
def test_authenticate
|
93
|
-
assert
|
94
|
-
assert
|
95
|
-
assert
|
99
|
+
assert !@@db.authenticate('nobody', 'nopassword')
|
100
|
+
assert !@@db.authenticate(@spongebob, 'squareliederhosen')
|
101
|
+
assert @@db.authenticate(@spongebob, @spongebob_password)
|
96
102
|
end
|
97
103
|
|
98
104
|
def test_logout
|
99
|
-
|
105
|
+
@@db.logout # only testing that we don't throw exception
|
100
106
|
end
|
101
107
|
|
102
108
|
def test_auto_connect
|
103
|
-
|
104
|
-
db = Mongo.new(
|
109
|
+
@@db.close
|
110
|
+
db = Mongo.new(@@host, @@port, :auto_reconnect => true).db('ruby-mongo-test')
|
105
111
|
assert db.connected?
|
106
112
|
assert db.auto_reconnect?
|
107
113
|
db.close
|
@@ -109,22 +115,25 @@ class DBTest < Test::Unit::TestCase
|
|
109
115
|
assert db.auto_reconnect?
|
110
116
|
db.collection('test').insert('a' => 1)
|
111
117
|
assert db.connected?
|
118
|
+
ensure
|
119
|
+
@@db = Mongo.new(@@host, @@port).db('ruby-mongo-test')
|
120
|
+
@@users = @@db.collection('system.users')
|
112
121
|
end
|
113
122
|
|
114
123
|
def test_error
|
115
|
-
doc =
|
116
|
-
assert
|
117
|
-
err =
|
124
|
+
doc = @@db.send(:db_command, :forceerror => 1)
|
125
|
+
assert @@db.error?
|
126
|
+
err = @@db.error
|
118
127
|
assert_match /forced error/, err
|
119
128
|
|
120
129
|
# ask again
|
121
|
-
assert
|
122
|
-
err2 =
|
130
|
+
assert @@db.error?
|
131
|
+
err2 = @@db.error
|
123
132
|
assert_equal err, err2
|
124
133
|
end
|
125
134
|
|
126
135
|
def test_text_port_number
|
127
|
-
db = DB.new('ruby-mongo-test', [[
|
136
|
+
db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]])
|
128
137
|
# If there is no error, all is well
|
129
138
|
db.collection('users').clear
|
130
139
|
end
|
data/tests/test_db_api.rb
CHANGED
@@ -7,51 +7,48 @@ class DBAPITest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
include XGen::Mongo::Driver
|
9
9
|
|
10
|
+
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
11
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
|
12
|
+
@@coll = @@db.collection('test')
|
13
|
+
|
10
14
|
def setup
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@coll = @db.collection('test')
|
15
|
-
@coll.clear
|
16
|
-
@r1 = @coll.insert('a' => 1) # collection not created until it's used
|
17
|
-
@coll_full_name = 'ruby-mongo-test.test'
|
15
|
+
@@coll.clear
|
16
|
+
@r1 = @@coll.insert('a' => 1) # collection not created until it's used
|
17
|
+
@@coll_full_name = 'ruby-mongo-test.test'
|
18
18
|
end
|
19
19
|
|
20
20
|
def teardown
|
21
|
-
|
22
|
-
@coll.clear unless @coll == nil
|
23
|
-
@db.close
|
24
|
-
end
|
21
|
+
@@coll.clear
|
25
22
|
end
|
26
23
|
|
27
24
|
def test_clear
|
28
|
-
assert_equal 1,
|
29
|
-
|
30
|
-
assert_equal 0,
|
25
|
+
assert_equal 1, @@coll.count
|
26
|
+
@@coll.clear
|
27
|
+
assert_equal 0, @@coll.count
|
31
28
|
end
|
32
29
|
|
33
30
|
def test_insert
|
34
|
-
|
35
|
-
|
31
|
+
@@coll.insert('a' => 2)
|
32
|
+
@@coll.insert('b' => 3)
|
36
33
|
|
37
|
-
assert_equal 3,
|
38
|
-
docs =
|
34
|
+
assert_equal 3, @@coll.count
|
35
|
+
docs = @@coll.find().to_a
|
39
36
|
assert_equal 3, docs.length
|
40
37
|
assert docs.detect { |row| row['a'] == 1 }
|
41
38
|
assert docs.detect { |row| row['a'] == 2 }
|
42
39
|
assert docs.detect { |row| row['b'] == 3 }
|
43
40
|
|
44
|
-
|
45
|
-
docs =
|
41
|
+
@@coll << {'b' => 4}
|
42
|
+
docs = @@coll.find().to_a
|
46
43
|
assert_equal 4, docs.length
|
47
44
|
assert docs.detect { |row| row['b'] == 4 }
|
48
45
|
end
|
49
46
|
|
50
47
|
def test_insert_multiple
|
51
|
-
|
48
|
+
@@coll.insert({'a' => 2}, {'b' => 3})
|
52
49
|
|
53
|
-
assert_equal 3,
|
54
|
-
docs =
|
50
|
+
assert_equal 3, @@coll.count
|
51
|
+
docs = @@coll.find().to_a
|
55
52
|
assert_equal 3, docs.length
|
56
53
|
assert docs.detect { |row| row['a'] == 1 }
|
57
54
|
assert docs.detect { |row| row['a'] == 2 }
|
@@ -59,15 +56,15 @@ class DBAPITest < Test::Unit::TestCase
|
|
59
56
|
end
|
60
57
|
|
61
58
|
def test_find_simple
|
62
|
-
@r2 =
|
63
|
-
@r3 =
|
59
|
+
@r2 = @@coll.insert('a' => 2)
|
60
|
+
@r3 = @@coll.insert('b' => 3)
|
64
61
|
# Check sizes
|
65
|
-
docs =
|
62
|
+
docs = @@coll.find().to_a
|
66
63
|
assert_equal 3, docs.size
|
67
|
-
assert_equal 3,
|
64
|
+
assert_equal 3, @@coll.count
|
68
65
|
|
69
66
|
# Find by other value
|
70
|
-
docs =
|
67
|
+
docs = @@coll.find('a' => @r1['a']).to_a
|
71
68
|
assert_equal 1, docs.size
|
72
69
|
doc = docs.first
|
73
70
|
# Can't compare _id values because at insert, an _id was added to @r1 by
|
@@ -78,58 +75,58 @@ class DBAPITest < Test::Unit::TestCase
|
|
78
75
|
end
|
79
76
|
|
80
77
|
def test_find_advanced
|
81
|
-
|
82
|
-
|
78
|
+
@@coll.insert('a' => 2)
|
79
|
+
@@coll.insert('b' => 3)
|
83
80
|
|
84
81
|
# Find by advanced query (less than)
|
85
|
-
docs =
|
82
|
+
docs = @@coll.find('a' => { '$lt' => 10 }).to_a
|
86
83
|
assert_equal 2, docs.size
|
87
84
|
assert docs.detect { |row| row['a'] == 1 }
|
88
85
|
assert docs.detect { |row| row['a'] == 2 }
|
89
86
|
|
90
87
|
# Find by advanced query (greater than)
|
91
|
-
docs =
|
88
|
+
docs = @@coll.find('a' => { '$gt' => 1 }).to_a
|
92
89
|
assert_equal 1, docs.size
|
93
90
|
assert docs.detect { |row| row['a'] == 2 }
|
94
91
|
|
95
92
|
# Find by advanced query (less than or equal to)
|
96
|
-
docs =
|
93
|
+
docs = @@coll.find('a' => { '$lte' => 1 }).to_a
|
97
94
|
assert_equal 1, docs.size
|
98
95
|
assert docs.detect { |row| row['a'] == 1 }
|
99
96
|
|
100
97
|
# Find by advanced query (greater than or equal to)
|
101
|
-
docs =
|
98
|
+
docs = @@coll.find('a' => { '$gte' => 1 }).to_a
|
102
99
|
assert_equal 2, docs.size
|
103
100
|
assert docs.detect { |row| row['a'] == 1 }
|
104
101
|
assert docs.detect { |row| row['a'] == 2 }
|
105
102
|
|
106
103
|
# Find by advanced query (between)
|
107
|
-
docs =
|
104
|
+
docs = @@coll.find('a' => { '$gt' => 1, '$lt' => 3 }).to_a
|
108
105
|
assert_equal 1, docs.size
|
109
106
|
assert docs.detect { |row| row['a'] == 2 }
|
110
107
|
|
111
108
|
# Find by advanced query (in clause)
|
112
|
-
docs =
|
109
|
+
docs = @@coll.find('a' => {'$in' => [1,2]}).to_a
|
113
110
|
assert_equal 2, docs.size
|
114
111
|
assert docs.detect { |row| row['a'] == 1 }
|
115
112
|
assert docs.detect { |row| row['a'] == 2 }
|
116
113
|
|
117
114
|
# Find by advanced query (regexp)
|
118
|
-
docs =
|
115
|
+
docs = @@coll.find('a' => /[1|2]/).to_a
|
119
116
|
assert_equal 2, docs.size
|
120
117
|
assert docs.detect { |row| row['a'] == 1 }
|
121
118
|
assert docs.detect { |row| row['a'] == 2 }
|
122
119
|
end
|
123
120
|
|
124
121
|
def test_find_sorting
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
122
|
+
@@coll.clear
|
123
|
+
@@coll.insert('a' => 1, 'b' => 2)
|
124
|
+
@@coll.insert('a' => 2, 'b' => 1)
|
125
|
+
@@coll.insert('a' => 3, 'b' => 2)
|
126
|
+
@@coll.insert('a' => 4, 'b' => 1)
|
130
127
|
|
131
128
|
# Sorting (ascending)
|
132
|
-
docs =
|
129
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => 1}).to_a
|
133
130
|
assert_equal 4, docs.size
|
134
131
|
assert_equal 1, docs[0]['a']
|
135
132
|
assert_equal 2, docs[1]['a']
|
@@ -137,7 +134,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
137
134
|
assert_equal 4, docs[3]['a']
|
138
135
|
|
139
136
|
# Sorting (descending)
|
140
|
-
docs =
|
137
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => {'a' => -1}).to_a
|
141
138
|
assert_equal 4, docs.size
|
142
139
|
assert_equal 4, docs[0]['a']
|
143
140
|
assert_equal 3, docs[1]['a']
|
@@ -145,7 +142,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
145
142
|
assert_equal 1, docs[3]['a']
|
146
143
|
|
147
144
|
# Sorting using array of names; assumes ascending order.
|
148
|
-
docs =
|
145
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => ['a']).to_a
|
149
146
|
assert_equal 4, docs.size
|
150
147
|
assert_equal 1, docs[0]['a']
|
151
148
|
assert_equal 2, docs[1]['a']
|
@@ -153,14 +150,14 @@ class DBAPITest < Test::Unit::TestCase
|
|
153
150
|
assert_equal 4, docs[3]['a']
|
154
151
|
|
155
152
|
# Sorting using single name; assumes ascending order.
|
156
|
-
docs =
|
153
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => 'a').to_a
|
157
154
|
assert_equal 4, docs.size
|
158
155
|
assert_equal 1, docs[0]['a']
|
159
156
|
assert_equal 2, docs[1]['a']
|
160
157
|
assert_equal 3, docs[2]['a']
|
161
158
|
assert_equal 4, docs[3]['a']
|
162
159
|
|
163
|
-
docs =
|
160
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => ['b', 'a']).to_a
|
164
161
|
assert_equal 4, docs.size
|
165
162
|
assert_equal 2, docs[0]['a']
|
166
163
|
assert_equal 4, docs[1]['a']
|
@@ -169,19 +166,19 @@ class DBAPITest < Test::Unit::TestCase
|
|
169
166
|
|
170
167
|
# Sorting using empty array; no order guarantee (Mongo bug #898) but
|
171
168
|
# should not blow up.
|
172
|
-
docs =
|
169
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => []).to_a
|
173
170
|
assert_equal 4, docs.size
|
174
171
|
|
175
172
|
# Sorting using array of hashes; no order guarantee (Mongo bug #898) but
|
176
173
|
# should not blow up.
|
177
|
-
docs =
|
174
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => [{'b' => 1}, {'a' => -1}]).to_a
|
178
175
|
assert_equal 4, docs.size
|
179
176
|
|
180
177
|
# Sorting using ordered hash. You can use an unordered one, but then the
|
181
178
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
182
179
|
oh = OrderedHash.new
|
183
180
|
oh['a'] = -1
|
184
|
-
docs =
|
181
|
+
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
185
182
|
assert_equal 4, docs.size
|
186
183
|
assert_equal 4, docs[0]['a']
|
187
184
|
assert_equal 3, docs[1]['a']
|
@@ -192,7 +189,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
192
189
|
# oh = OrderedHash.new
|
193
190
|
# oh['b'] = -1
|
194
191
|
# oh['a'] = 1
|
195
|
-
# docs =
|
192
|
+
# docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
196
193
|
# assert_equal 4, docs.size
|
197
194
|
# assert_equal 1, docs[0]['a']
|
198
195
|
# assert_equal 3, docs[1]['a']
|
@@ -201,90 +198,106 @@ class DBAPITest < Test::Unit::TestCase
|
|
201
198
|
end
|
202
199
|
|
203
200
|
def test_find_limits
|
204
|
-
|
205
|
-
|
206
|
-
|
201
|
+
@@coll.insert('b' => 2)
|
202
|
+
@@coll.insert('c' => 3)
|
203
|
+
@@coll.insert('d' => 4)
|
207
204
|
|
208
|
-
docs =
|
205
|
+
docs = @@coll.find({}, :limit => 1).to_a
|
209
206
|
assert_equal 1, docs.size
|
210
|
-
docs =
|
207
|
+
docs = @@coll.find({}, :limit => 2).to_a
|
211
208
|
assert_equal 2, docs.size
|
212
|
-
docs =
|
209
|
+
docs = @@coll.find({}, :limit => 3).to_a
|
213
210
|
assert_equal 3, docs.size
|
214
|
-
docs =
|
211
|
+
docs = @@coll.find({}, :limit => 4).to_a
|
215
212
|
assert_equal 4, docs.size
|
216
|
-
docs =
|
213
|
+
docs = @@coll.find({}).to_a
|
217
214
|
assert_equal 4, docs.size
|
218
|
-
docs =
|
215
|
+
docs = @@coll.find({}, :limit => 99).to_a
|
219
216
|
assert_equal 4, docs.size
|
220
217
|
end
|
221
218
|
|
222
219
|
def test_drop_collection
|
223
|
-
assert
|
224
|
-
assert
|
225
|
-
@coll = nil
|
220
|
+
assert @@db.drop_collection(@@coll.name), "drop of collection #{@@coll.name} failed"
|
221
|
+
assert !@@db.collection_names.include?(@@coll_full_name)
|
226
222
|
end
|
227
223
|
|
228
224
|
def test_collection_names
|
229
|
-
names =
|
225
|
+
names = @@db.collection_names
|
230
226
|
assert names.length >= 1
|
231
|
-
assert names.include?(
|
227
|
+
assert names.include?(@@coll_full_name)
|
232
228
|
|
233
|
-
coll2 =
|
229
|
+
coll2 = @@db.collection('test2')
|
234
230
|
coll2.insert('a' => 1) # collection not created until it's used
|
235
|
-
names =
|
231
|
+
names = @@db.collection_names
|
236
232
|
assert names.length >= 2
|
237
|
-
assert names.include?(
|
233
|
+
assert names.include?(@@coll_full_name)
|
238
234
|
assert names.include?('ruby-mongo-test.test2')
|
239
235
|
ensure
|
240
|
-
|
236
|
+
@@db.drop_collection('test2')
|
241
237
|
end
|
242
238
|
|
243
239
|
def test_collections_info
|
244
|
-
cursor =
|
240
|
+
cursor = @@db.collections_info
|
245
241
|
rows = cursor.to_a
|
246
242
|
assert rows.length >= 1
|
247
|
-
row = rows.detect { |r| r['name'] ==
|
243
|
+
row = rows.detect { |r| r['name'] == @@coll_full_name }
|
248
244
|
assert_not_nil row
|
249
245
|
end
|
250
246
|
|
251
247
|
def test_collection_options
|
252
|
-
|
253
|
-
|
248
|
+
@@db.drop_collection('foobar')
|
249
|
+
@@db.strict = true
|
254
250
|
|
255
251
|
begin
|
256
|
-
coll =
|
252
|
+
coll = @@db.create_collection('foobar', :capped => true, :size => 1024)
|
257
253
|
options = coll.options()
|
258
254
|
assert_equal 'foobar', options['create']
|
259
255
|
assert_equal true, options['capped']
|
260
256
|
assert_equal 1024, options['size']
|
261
257
|
rescue => ex
|
262
|
-
|
258
|
+
@@db.drop_collection('foobar')
|
263
259
|
fail "did not expect exception \"#{ex}\""
|
260
|
+
ensure
|
261
|
+
@@db.strict = false
|
264
262
|
end
|
265
263
|
end
|
266
264
|
|
267
265
|
def test_index_information
|
268
|
-
|
269
|
-
list =
|
266
|
+
@@db.create_index(@@coll.name, 'index_name', ['a'])
|
267
|
+
list = @@db.index_information(@@coll.name)
|
270
268
|
assert_equal 1, list.length
|
271
269
|
|
272
270
|
info = list[0]
|
273
271
|
assert_equal 'index_name', info[:name]
|
274
272
|
assert_equal 1, info[:keys]['a']
|
273
|
+
ensure
|
274
|
+
@@db.drop_index(@@coll.name, 'index_name')
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_multiple_index_cols
|
278
|
+
@@db.create_index(@@coll.name, 'index_name', ['a', 'b', 'c'])
|
279
|
+
list = @@db.index_information(@@coll.name)
|
280
|
+
assert_equal 1, list.length
|
281
|
+
|
282
|
+
info = list[0]
|
283
|
+
assert_equal 'index_name', info[:name]
|
284
|
+
keys = info[:keys].keys
|
285
|
+
assert_equal ['a', 'b', 'c'], keys.sort
|
286
|
+
ensure
|
287
|
+
@@db.drop_index(@@coll.name, 'index_name')
|
275
288
|
end
|
276
289
|
|
277
290
|
def test_array
|
278
|
-
|
279
|
-
rows =
|
291
|
+
@@coll << {'b' => [1, 2, 3]}
|
292
|
+
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
280
293
|
assert_equal 1, rows.length
|
281
294
|
assert_equal [1, 2, 3], rows[0]['b']
|
282
295
|
end
|
283
296
|
|
284
297
|
def test_regex
|
285
298
|
regex = /foobar/i
|
286
|
-
|
287
|
-
rows =
|
299
|
+
@@coll << {'b' => regex}
|
300
|
+
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
288
301
|
assert_equal 1, rows.length
|
289
302
|
assert_equal regex, rows[0]['b']
|
290
303
|
end
|
@@ -293,37 +306,39 @@ class DBAPITest < Test::Unit::TestCase
|
|
293
306
|
# Note: can't use Time.new because that will include fractional seconds,
|
294
307
|
# which Mongo does not store.
|
295
308
|
t = Time.at(1234567890)
|
296
|
-
|
297
|
-
rows =
|
309
|
+
@@coll << {'_id' => t}
|
310
|
+
rows = @@coll.find({'_id' => t}).to_a
|
298
311
|
assert_equal 1, rows.length
|
299
312
|
assert_equal t, rows[0]['_id']
|
300
313
|
end
|
301
314
|
|
302
315
|
def test_strict
|
303
|
-
assert
|
304
|
-
|
305
|
-
assert
|
316
|
+
assert !@@db.strict?
|
317
|
+
@@db.strict = true
|
318
|
+
assert @@db.strict?
|
319
|
+
ensure
|
320
|
+
@@db.strict = false
|
306
321
|
end
|
307
322
|
|
308
323
|
def test_strict_access_collection
|
309
|
-
|
324
|
+
@@db.strict = true
|
310
325
|
begin
|
311
|
-
|
326
|
+
@@db.collection('does-not-exist')
|
312
327
|
fail "expected exception"
|
313
328
|
rescue => ex
|
314
329
|
assert_equal "Collection does-not-exist doesn't exist. Currently in strict mode.", ex.to_s
|
315
330
|
ensure
|
316
|
-
|
317
|
-
|
331
|
+
@@db.strict = false
|
332
|
+
@@db.drop_collection('does-not-exist')
|
318
333
|
end
|
319
334
|
end
|
320
335
|
|
321
336
|
def test_strict_create_collection
|
322
|
-
|
323
|
-
|
337
|
+
@@db.drop_collection('foobar')
|
338
|
+
@@db.strict = true
|
324
339
|
|
325
340
|
begin
|
326
|
-
|
341
|
+
@@db.create_collection('foobar')
|
327
342
|
assert true
|
328
343
|
rescue => ex
|
329
344
|
fail "did not expect exception \"#{ex}\""
|
@@ -331,18 +346,18 @@ class DBAPITest < Test::Unit::TestCase
|
|
331
346
|
|
332
347
|
# Now the collection exists. This time we should see an exception.
|
333
348
|
begin
|
334
|
-
|
349
|
+
@@db.create_collection('foobar')
|
335
350
|
fail "expected exception"
|
336
351
|
rescue => ex
|
337
352
|
assert_equal "Collection foobar already exists. Currently in strict mode.", ex.to_s
|
338
353
|
ensure
|
339
|
-
|
340
|
-
|
354
|
+
@@db.strict = false
|
355
|
+
@@db.drop_collection('foobar')
|
341
356
|
end
|
342
357
|
end
|
343
358
|
|
344
359
|
def test_to_a
|
345
|
-
cursor =
|
360
|
+
cursor = @@coll.find()
|
346
361
|
rows = cursor.to_a
|
347
362
|
|
348
363
|
# Make sure we get back exactly the same array the next time we ask
|
@@ -357,7 +372,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
357
372
|
end
|
358
373
|
|
359
374
|
def test_to_a_after_each
|
360
|
-
cursor =
|
375
|
+
cursor = @@coll.find
|
361
376
|
cursor.each { |row| row }
|
362
377
|
begin
|
363
378
|
cursor.to_a
|
@@ -368,40 +383,40 @@ class DBAPITest < Test::Unit::TestCase
|
|
368
383
|
end
|
369
384
|
|
370
385
|
def test_ismaster
|
371
|
-
assert
|
386
|
+
assert @@db.master?
|
372
387
|
end
|
373
388
|
|
374
389
|
def test_master
|
375
|
-
assert_equal "#{
|
390
|
+
assert_equal "#{@@db.host}:#{@@db.port}", @@db.master
|
376
391
|
end
|
377
392
|
|
378
393
|
def test_hint
|
379
|
-
|
394
|
+
@@coll.create_index('test_a_index', 'a')
|
380
395
|
begin
|
381
|
-
assert_nil
|
382
|
-
assert_equal 1,
|
383
|
-
assert_equal 1,
|
384
|
-
assert_equal 1,
|
385
|
-
|
386
|
-
|
387
|
-
assert_equal({'a' => 1},
|
388
|
-
assert_equal 1,
|
389
|
-
|
390
|
-
|
391
|
-
assert_equal({'a' => 1},
|
392
|
-
assert_equal 1,
|
393
|
-
|
394
|
-
|
395
|
-
assert_equal({'a' => 1},
|
396
|
-
assert_equal 1,
|
397
|
-
|
398
|
-
|
399
|
-
assert_nil
|
400
|
-
assert_equal 1,
|
396
|
+
assert_nil @@coll.hint
|
397
|
+
assert_equal 1, @@coll.find({'a' => 1}, :hint => 'a').to_a.size
|
398
|
+
assert_equal 1, @@coll.find({'a' => 1}, :hint => ['a']).to_a.size
|
399
|
+
assert_equal 1, @@coll.find({'a' => 1}, :hint => {'a' => 1}).to_a.size
|
400
|
+
|
401
|
+
@@coll.hint = 'a'
|
402
|
+
assert_equal({'a' => 1}, @@coll.hint)
|
403
|
+
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
404
|
+
|
405
|
+
@@coll.hint = ['a']
|
406
|
+
assert_equal({'a' => 1}, @@coll.hint)
|
407
|
+
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
408
|
+
|
409
|
+
@@coll.hint = {'a' => 1}
|
410
|
+
assert_equal({'a' => 1}, @@coll.hint)
|
411
|
+
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
412
|
+
|
413
|
+
@@coll.hint = nil
|
414
|
+
assert_nil @@coll.hint
|
415
|
+
assert_equal 1, @@coll.find('a' => 1).to_a.size
|
401
416
|
rescue => ex
|
402
417
|
fail ex.to_s
|
403
418
|
ensure
|
404
|
-
|
419
|
+
@@coll.drop_index('test_a_index')
|
405
420
|
end
|
406
421
|
end
|
407
422
|
|
@@ -410,11 +425,11 @@ class DBAPITest < Test::Unit::TestCase
|
|
410
425
|
|
411
426
|
# def test_insert_undefined
|
412
427
|
# doc = {'undef' => Undefined.new}
|
413
|
-
#
|
414
|
-
#
|
415
|
-
# p
|
416
|
-
# assert_equal 1,
|
417
|
-
# row =
|
428
|
+
# @@coll.clear
|
429
|
+
# @@coll.insert(doc)
|
430
|
+
# p @@db.error # DEBUG
|
431
|
+
# assert_equal 1, @@coll.count
|
432
|
+
# row = @@coll.find().next_object
|
418
433
|
# assert_not_nil row
|
419
434
|
# end
|
420
435
|
|
data/tests/test_grid_store.rb
CHANGED
@@ -8,35 +8,30 @@ class GridStoreTest < Test::Unit::TestCase
|
|
8
8
|
include XGen::Mongo::Driver
|
9
9
|
include XGen::Mongo::GridFS
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
@files = @db.collection('gridfs.files')
|
17
|
-
@chunks = @db.collection('gridfs.chunks')
|
18
|
-
@chunks.clear
|
19
|
-
@files.clear
|
11
|
+
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
12
|
+
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
|
13
|
+
@@files = @@db.collection('gridfs.files')
|
14
|
+
@@chunks = @@db.collection('gridfs.chunks')
|
20
15
|
|
21
|
-
|
16
|
+
def setup
|
17
|
+
@@chunks.clear
|
18
|
+
@@files.clear
|
19
|
+
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
|
22
20
|
end
|
23
21
|
|
24
22
|
def teardown
|
25
|
-
|
26
|
-
|
27
|
-
@files.clear
|
28
|
-
@db.close
|
29
|
-
end
|
23
|
+
@@chunks.clear
|
24
|
+
@@files.clear
|
30
25
|
end
|
31
26
|
|
32
27
|
def test_exist
|
33
|
-
assert GridStore.exist?(
|
34
|
-
assert !GridStore.exist?(
|
35
|
-
assert !GridStore.exist?(
|
28
|
+
assert GridStore.exist?(@@db, 'foobar')
|
29
|
+
assert !GridStore.exist?(@@db, 'does_not_exist')
|
30
|
+
assert !GridStore.exist?(@@db, 'foobar', 'another_root')
|
36
31
|
end
|
37
32
|
|
38
33
|
def test_small_write
|
39
|
-
rows =
|
34
|
+
rows = @@files.find({'filename' => 'foobar'}).to_a
|
40
35
|
assert_not_nil rows
|
41
36
|
assert_equal 1, rows.length
|
42
37
|
row = rows[0]
|
@@ -44,37 +39,37 @@ class GridStoreTest < Test::Unit::TestCase
|
|
44
39
|
|
45
40
|
file_id = row['_id']
|
46
41
|
assert_kind_of ObjectID, file_id
|
47
|
-
rows =
|
42
|
+
rows = @@chunks.find({'files_id' => file_id}).to_a
|
48
43
|
assert_not_nil rows
|
49
44
|
assert_equal 1, rows.length
|
50
45
|
end
|
51
46
|
|
52
47
|
def test_small_file
|
53
|
-
rows =
|
48
|
+
rows = @@files.find({'filename' => 'foobar'}).to_a
|
54
49
|
assert_not_nil rows
|
55
50
|
assert_equal 1, rows.length
|
56
51
|
row = rows[0]
|
57
52
|
assert_not_nil row
|
58
|
-
assert_equal "hello, world!", GridStore.read(
|
53
|
+
assert_equal "hello, world!", GridStore.read(@@db, 'foobar')
|
59
54
|
end
|
60
55
|
|
61
56
|
def test_overwrite
|
62
|
-
GridStore.open(
|
63
|
-
assert_equal "overwrite", GridStore.read(
|
57
|
+
GridStore.open(@@db, 'foobar', 'w') { |f| f.write("overwrite") }
|
58
|
+
assert_equal "overwrite", GridStore.read(@@db, 'foobar')
|
64
59
|
end
|
65
60
|
|
66
61
|
def test_read_length
|
67
|
-
assert_equal "hello", GridStore.read(
|
62
|
+
assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
|
68
63
|
end
|
69
64
|
|
70
65
|
# Also tests seek
|
71
66
|
def test_read_with_offset
|
72
|
-
assert_equal "world", GridStore.read(
|
73
|
-
assert_equal "world!", GridStore.read(
|
67
|
+
assert_equal "world", GridStore.read(@@db, 'foobar', 5, 7)
|
68
|
+
assert_equal "world!", GridStore.read(@@db, 'foobar', nil, 7)
|
74
69
|
end
|
75
70
|
|
76
71
|
def test_seek
|
77
|
-
GridStore.open(
|
72
|
+
GridStore.open(@@db, 'foobar', 'r') { |f|
|
78
73
|
f.seek(0)
|
79
74
|
assert_equal 'h', f.getc.chr
|
80
75
|
f.seek(7)
|
@@ -100,84 +95,84 @@ class GridStoreTest < Test::Unit::TestCase
|
|
100
95
|
end
|
101
96
|
|
102
97
|
def test_multi_chunk
|
103
|
-
|
104
|
-
|
98
|
+
@@chunks.clear
|
99
|
+
@@files.clear
|
105
100
|
|
106
101
|
size = 512
|
107
|
-
GridStore.open(
|
102
|
+
GridStore.open(@@db, 'biggie', 'w') { |f|
|
108
103
|
f.chunk_size = size
|
109
104
|
f.write('x' * size)
|
110
105
|
f.write('y' * size)
|
111
106
|
f.write('z' * size)
|
112
107
|
}
|
113
108
|
|
114
|
-
assert_equal 3,
|
115
|
-
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(
|
109
|
+
assert_equal 3, @@chunks.count
|
110
|
+
assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
|
116
111
|
end
|
117
112
|
|
118
113
|
def test_puts_and_readlines
|
119
|
-
GridStore.open(
|
114
|
+
GridStore.open(@@db, 'multiline', 'w') { |f|
|
120
115
|
f.puts "line one"
|
121
116
|
f.puts "line two\n"
|
122
117
|
f.puts "line three"
|
123
118
|
}
|
124
119
|
|
125
|
-
lines = GridStore.readlines(
|
120
|
+
lines = GridStore.readlines(@@db, 'multiline')
|
126
121
|
assert_equal ["line one\n", "line two\n", "line three\n"], lines
|
127
122
|
end
|
128
123
|
|
129
124
|
def test_unlink
|
130
|
-
assert_equal 1,
|
131
|
-
assert_equal 1,
|
132
|
-
GridStore.unlink(
|
133
|
-
assert_equal 0,
|
134
|
-
assert_equal 0,
|
125
|
+
assert_equal 1, @@files.count
|
126
|
+
assert_equal 1, @@chunks.count
|
127
|
+
GridStore.unlink(@@db, 'foobar')
|
128
|
+
assert_equal 0, @@files.count
|
129
|
+
assert_equal 0, @@chunks.count
|
135
130
|
end
|
136
131
|
|
137
132
|
def test_append
|
138
|
-
GridStore.open(
|
139
|
-
assert_equal 1,
|
140
|
-
assert_equal "hello, world! how are you?", GridStore.read(
|
133
|
+
GridStore.open(@@db, 'foobar', 'w+') { |f| f.write(" how are you?") }
|
134
|
+
assert_equal 1, @@chunks.count
|
135
|
+
assert_equal "hello, world! how are you?", GridStore.read(@@db, 'foobar')
|
141
136
|
end
|
142
137
|
|
143
138
|
def test_rewind_and_truncate_on_write
|
144
|
-
GridStore.open(
|
139
|
+
GridStore.open(@@db, 'foobar', 'w') { |f|
|
145
140
|
f.write("some text is inserted here")
|
146
141
|
f.rewind
|
147
142
|
f.write("abc")
|
148
143
|
}
|
149
|
-
assert_equal "abc", GridStore.read(
|
144
|
+
assert_equal "abc", GridStore.read(@@db, 'foobar')
|
150
145
|
end
|
151
146
|
|
152
147
|
def test_tell
|
153
|
-
GridStore.open(
|
148
|
+
GridStore.open(@@db, 'foobar', 'r') { |f|
|
154
149
|
f.read(5)
|
155
150
|
assert_equal 5, f.tell
|
156
151
|
}
|
157
152
|
end
|
158
153
|
|
159
154
|
def test_empty_block_ok
|
160
|
-
GridStore.open(
|
155
|
+
GridStore.open(@@db, 'empty', 'w')
|
161
156
|
end
|
162
157
|
|
163
158
|
def test_save_empty_file
|
164
|
-
|
165
|
-
|
166
|
-
GridStore.open(
|
167
|
-
assert_equal 1,
|
168
|
-
assert_equal 0,
|
159
|
+
@@chunks.clear
|
160
|
+
@@files.clear
|
161
|
+
GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
|
162
|
+
assert_equal 1, @@files.count
|
163
|
+
assert_equal 0, @@chunks.count
|
169
164
|
end
|
170
165
|
|
171
166
|
def test_empty_file_eof
|
172
|
-
GridStore.open(
|
173
|
-
GridStore.open(
|
167
|
+
GridStore.open(@@db, 'empty', 'w')
|
168
|
+
GridStore.open(@@db, 'empty', 'r') { |f|
|
174
169
|
assert f.eof?
|
175
170
|
}
|
176
171
|
end
|
177
172
|
|
178
173
|
def test_cannot_change_chunk_size_on_read
|
179
174
|
begin
|
180
|
-
GridStore.open(
|
175
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| f.chunk_size = 42 }
|
181
176
|
fail "should have seen error"
|
182
177
|
rescue => ex
|
183
178
|
assert_match /error: can only change chunk size/, ex.to_s
|
@@ -186,7 +181,7 @@ class GridStoreTest < Test::Unit::TestCase
|
|
186
181
|
|
187
182
|
def test_cannot_change_chunk_size_after_data_written
|
188
183
|
begin
|
189
|
-
GridStore.open(
|
184
|
+
GridStore.open(@@db, 'foobar', 'w') { |f|
|
190
185
|
f.write("some text")
|
191
186
|
f.chunk_size = 42
|
192
187
|
}
|
@@ -197,18 +192,18 @@ class GridStoreTest < Test::Unit::TestCase
|
|
197
192
|
end
|
198
193
|
|
199
194
|
def test_change_chunk_size
|
200
|
-
GridStore.open(
|
195
|
+
GridStore.open(@@db, 'new-file', 'w') { |f|
|
201
196
|
f.chunk_size = 42
|
202
197
|
f.write("foo")
|
203
198
|
}
|
204
|
-
GridStore.open(
|
199
|
+
GridStore.open(@@db, 'new-file', 'r') { |f|
|
205
200
|
assert f.chunk_size == 42
|
206
201
|
}
|
207
202
|
end
|
208
203
|
|
209
204
|
def test_chunk_size_in_option
|
210
|
-
GridStore.open(
|
211
|
-
GridStore.open(
|
205
|
+
GridStore.open(@@db, 'new-file', 'w', :chunk_size => 42) { |f| f.write("foo") }
|
206
|
+
GridStore.open(@@db, 'new-file', 'r') { |f|
|
212
207
|
assert f.chunk_size == 42
|
213
208
|
}
|
214
209
|
end
|
@@ -216,46 +211,46 @@ class GridStoreTest < Test::Unit::TestCase
|
|
216
211
|
def test_upload_date
|
217
212
|
now = Time.now
|
218
213
|
orig_file_upload_date = nil
|
219
|
-
GridStore.open(
|
214
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| orig_file_upload_date = f.upload_date }
|
220
215
|
assert_not_nil orig_file_upload_date
|
221
216
|
assert (orig_file_upload_date - now) < 5 # even a really slow system < 5 secs
|
222
217
|
|
223
218
|
sleep(2)
|
224
|
-
GridStore.open(
|
219
|
+
GridStore.open(@@db, 'foobar', 'w') { |f| f.write "new data" }
|
225
220
|
file_upload_date = nil
|
226
|
-
GridStore.open(
|
221
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| file_upload_date = f.upload_date }
|
227
222
|
assert_equal orig_file_upload_date, file_upload_date
|
228
223
|
end
|
229
224
|
|
230
225
|
def test_content_type
|
231
226
|
ct = nil
|
232
|
-
GridStore.open(
|
227
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| ct = f.content_type }
|
233
228
|
assert_equal GridStore::DEFAULT_CONTENT_TYPE, ct
|
234
229
|
|
235
|
-
GridStore.open(
|
230
|
+
GridStore.open(@@db, 'foobar', 'w+') { |f| f.content_type = 'text/html' }
|
236
231
|
ct2 = nil
|
237
|
-
GridStore.open(
|
232
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| ct2 = f.content_type }
|
238
233
|
assert_equal 'text/html', ct2
|
239
234
|
end
|
240
235
|
|
241
236
|
def test_content_type_option
|
242
|
-
GridStore.open(
|
237
|
+
GridStore.open(@@db, 'new-file', 'w', :content_type => 'image/jpg') { |f| f.write('foo') }
|
243
238
|
ct = nil
|
244
|
-
GridStore.open(
|
239
|
+
GridStore.open(@@db, 'new-file', 'r') { |f| ct = f.content_type }
|
245
240
|
assert_equal 'image/jpg', ct
|
246
241
|
end
|
247
242
|
|
248
243
|
def test_unknown_mode
|
249
|
-
GridStore.open(
|
244
|
+
GridStore.open(@@db, 'foobar', 'x')
|
250
245
|
fail 'should have seen "illegal mode" error raised'
|
251
246
|
rescue => ex
|
252
247
|
assert_equal "error: illegal mode x", ex.to_s
|
253
248
|
end
|
254
249
|
|
255
250
|
def test_metadata
|
256
|
-
GridStore.open(
|
257
|
-
GridStore.open(
|
258
|
-
GridStore.open(
|
251
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| assert_nil f.metadata }
|
252
|
+
GridStore.open(@@db, 'foobar', 'w+') { |f| f.metadata = {'a' => 1} }
|
253
|
+
GridStore.open(@@db, 'foobar', 'r') { |f| assert_equal({'a' => 1}, f.metadata) }
|
259
254
|
end
|
260
255
|
|
261
256
|
end
|