/
connection_test.rb
134 lines (110 loc) · 4.75 KB
/
connection_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require File.expand_path("../../test_helper", __FILE__)
include Mongo
class ConnectionTest < Test::Unit::TestCase
context "Initialization: " do
context "given a single node" do
setup do
@conn = Connection.new('localhost', 27017, :connect => false)
TCPSocket.stubs(:new).returns(new_mock_socket)
admin_db = new_mock_db
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
@conn.expects(:[]).with('admin').returns(admin_db)
@conn.connect
end
should "set localhost and port to master" do
assert_equal 'localhost', @conn.primary_pool.host
assert_equal 27017, @conn.primary_pool.port
end
should "set connection pool to 1" do
assert_equal 1, @conn.primary_pool.size
end
should "default slave_ok to false" do
assert !@conn.slave_ok?
end
should "warn if invalid options are specified" do
conn = Connection.allocate
opts = {:connect => false}
ReplSetConnection::REPL_SET_OPTS.each do |opt|
conn.expects(:warn).with("#{opt} is not a valid option for #{conn.class}")
opts[opt] = true
end
args = ['localhost', 27017, opts]
conn.send(:initialize, *args)
end
context "given a replica set" do
#should "enforce a minimum refresh_interval" do
# @conn = ReplSetConnection.new(['localhost:27017'],
# :connect => false, :refresh_mode => :sync, :refresh_interval => 10)
# assert_equal 60, @conn.refresh_interval
#end
should "warn if invalid options are specified" do
conn = ReplSetConnection.allocate
opts = {:connect => false}
Connection::CONNECTION_OPTS.each do |opt|
conn.expects(:warn).with("#{opt} is not a valid option for #{conn.class}")
opts[opt] = true
end
args = [['localhost:27017'], opts]
conn.send(:initialize, *args)
end
end
end
context "initializing with a mongodb uri" do
should "parse a simple uri" do
@conn = Connection.from_uri("mongodb://localhost", :connect => false)
assert_equal ['localhost', 27017], @conn.host_to_try
end
should "allow a complex host names" do
host_name = "foo.bar-12345.org"
@conn = Connection.from_uri("mongodb://#{host_name}", :connect => false)
assert_equal [host_name, 27017], @conn.host_to_try
end
should "allow db without username and password" do
host_name = "foo.bar-12345.org"
@conn = Connection.from_uri("mongodb://#{host_name}/foo", :connect => false)
assert_equal [host_name, 27017], @conn.host_to_try
end
should "set safe options on connection" do
host_name = "localhost"
opts = "safe=true&w=2&wtimeoutMS=1000&fsync=true&journal=true"
@conn = Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @conn.safe)
end
should "set timeout options on connection" do
host_name = "localhost"
opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
@conn = Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
assert_equal 1, @conn.connect_timeout
assert_equal 5, @conn.op_timeout
end
should "parse a uri with a hyphen & underscore in the username or password" do
@conn = Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
assert_equal ['localhost', 27017], @conn.host_to_try
auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
assert_equal auth_hash, @conn.auths[0]
end
should "attempt to connect" do
TCPSocket.stubs(:new).returns(new_mock_socket)
@conn = Connection.from_uri("mongodb://localhost", :connect => false)
admin_db = new_mock_db
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
@conn.expects(:[]).with('admin').returns(admin_db)
@conn.connect
end
should "raise an error on invalid uris" do
assert_raise MongoArgumentError do
Connection.from_uri("mongo://localhost", :connect => false)
end
assert_raise MongoArgumentError do
Connection.from_uri("mongodb://localhost:abc", :connect => false)
end
end
should "require all of username, if password and db are specified" do
assert Connection.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
assert_raise MongoArgumentError do
Connection.from_uri("mongodb://kyle:password@localhost", :connect => false)
end
end
end
end
end