Skip to content

Commit

Permalink
Add tests for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
david-krentzlin committed Aug 7, 2024
1 parent 8c979aa commit dfdd263
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/beetle/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,18 @@ def redis_options
end

def connection_options_for_server(server)
default_server_connection_options(server).merge(server_connection_options[server] || {})
end

private

def default_server_connection_options(server)
host, port = server.split(':')
port ||= 5672

default_opts = { host: host, port: port.to_i, user: user, pass: password, vhost: vhost }

server_connection_options.fetch(server, default_opts)
{ host: host, port: port.to_i, user: user, pass: password, vhost: vhost }
end

private
def load_config
raw = ERB.new(IO.read(config_file)).result
hash = if config_file =~ /\.json$/
Expand Down
46 changes: 46 additions & 0 deletions test/beetle/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,50 @@ class ConfigurationTest < Minitest::Test
assert_equal "10.0.0.1:3001", config.additional_subscription_servers
end
end

class ConnectionOptionsForServerTest < Minitest::Test
test "returns the options for the server provided" do
config = Configuration.new
config.servers = 'localhost:5672'
config.server_connection_options["localhost:5672"] = {host: 'localhost', port: 5672, user: "john", pass: "doe", vhost: "test", ssl: "0"}

config.connection_options_for_server("localhost:5672").tap do |options|
assert_equal "localhost", options[:host]
assert_equal 5672, options[:port]
assert_equal "john", options[:user]
assert_equal "doe", options[:pass]
assert_equal "test", options[:vhost]
assert_equal "0", options[:ssl]
end
end

test "returns default options if no options are set for the server" do
config = Configuration.new
config.servers = 'localhost:5672'

config.connection_options_for_server("localhost:5672").tap do |options|
assert_equal "localhost", options[:host]
assert_equal 5672, options[:port]
assert_equal "guest", options[:user]
assert_equal "guest", options[:pass]
assert_equal "/", options[:vhost]
assert_nil options[:ssl]
end
end

test "allows to set specific options while retaining defaults for the rest" do
config = Configuration.new
config.servers = 'localhost:5672'
config.server_connection_options["localhost:5672"] = { pass: "another_pass", ssl: "1" }

config.connection_options_for_server("localhost:5672").tap do |options|
assert_equal "localhost", options[:host]
assert_equal 5672, options[:port]
assert_equal "guest", options[:user]
assert_equal "another_pass", options[:pass]
assert_equal "/", options[:vhost]
assert_equal "1", options[:ssl]
end
end
end
end

0 comments on commit dfdd263

Please sign in to comment.