1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Rearrange deck chairs on the titanic. Organize connection handling test cases.

This commit is contained in:
Jeremy Kemper 2014-04-21 15:54:28 -05:00
parent 5adf6ca971
commit 6e6c76d2ff
4 changed files with 246 additions and 249 deletions

View file

@ -1,56 +0,0 @@
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class ConnectionPool
def insert_connection_for_test!(c)
synchronize do
@connections << c
@available.add c
end
end
end
class AbstractAdapterTest < ActiveRecord::TestCase
attr_reader :adapter
def setup
@adapter = AbstractAdapter.new nil, nil
end
def test_in_use?
assert_not adapter.in_use?, 'adapter is not in use'
assert adapter.lease, 'lease adapter'
assert adapter.in_use?, 'adapter is in use'
end
def test_lease_twice
assert adapter.lease, 'should lease adapter'
assert_not adapter.lease, 'should not lease adapter'
end
def test_expire_mutates_in_use
assert adapter.lease, 'lease adapter'
assert adapter.in_use?, 'adapter is in use'
adapter.expire
assert_not adapter.in_use?, 'adapter is in use'
end
def test_close
pool = ConnectionPool.new(ConnectionSpecification.new({}, nil))
pool.insert_connection_for_test! adapter
adapter.pool = pool
# Make sure the pool marks the connection in use
assert_equal adapter, pool.connection
assert adapter.in_use?
# Close should put the adapter back in the pool
adapter.close
assert_not adapter.in_use?
assert_equal adapter, pool.connection
end
end
end
end

View file

@ -0,0 +1,54 @@
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class AdapterLeasingTest < ActiveRecord::TestCase
class Pool < ConnectionPool
def insert_connection_for_test!(c)
synchronize do
@connections << c
@available.add c
end
end
end
def setup
@adapter = AbstractAdapter.new nil, nil
end
def test_in_use?
assert_not @adapter.in_use?, 'adapter is not in use'
assert @adapter.lease, 'lease adapter'
assert @adapter.in_use?, 'adapter is in use'
end
def test_lease_twice
assert @adapter.lease, 'should lease adapter'
assert_not @adapter.lease, 'should not lease adapter'
end
def test_expire_mutates_in_use
assert @adapter.lease, 'lease adapter'
assert @adapter.in_use?, 'adapter is in use'
@adapter.expire
assert_not @adapter.in_use?, 'adapter is in use'
end
def test_close
pool = Pool.new(ConnectionSpecification.new({}, nil))
pool.insert_connection_for_test! @adapter
@adapter.pool = pool
# Make sure the pool marks the connection in use
assert_equal @adapter, pool.connection
assert @adapter.in_use?
# Close should put the adapter back in the pool
@adapter.close
assert_not @adapter.in_use?
assert_equal @adapter, pool.connection
end
end
end
end

View file

@ -2,199 +2,6 @@ require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class MergeAndResolveDefaultUrlConfigTest < ActiveRecord::TestCase
def klass
ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig
end
def setup
@previous_database_url = ENV.delete("DATABASE_URL")
end
teardown do
ENV["DATABASE_URL"] = @previous_database_url
end
def resolve(spec, config)
ConnectionSpecification::Resolver.new(klass.new(config).resolve).resolve(spec)
end
def spec(spec, config)
ConnectionSpecification::Resolver.new(klass.new(config).resolve).spec(spec)
end
def test_resolver_with_database_uri_and_current_env_symbol_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = resolve(:default_env, config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_and_current_env_string_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = assert_deprecated { resolve("default_env", config) }
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_known_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo", "host" => "localhost" } }
actual = resolve(:production, config)
expected = { "adapter"=>"not_postgres", "database"=>"not_foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_unknown_symbol_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
assert_raises AdapterNotSpecified do
resolve(:production, config)
end
end
def test_resolver_with_database_uri_and_unknown_string_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
assert_deprecated do
assert_raises AdapterNotSpecified do
spec("production", config)
end
end
end
def test_resolver_with_database_uri_and_supplied_url
ENV['DATABASE_URL'] = "not-postgres://not-localhost/not_foo"
config = { "production" => { "adapter" => "also_not_postgres", "database" => "also_not_foo" } }
actual = resolve("postgres://localhost/foo", config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_jdbc_url
config = { "production" => { "url" => "jdbc:postgres://localhost/foo" } }
actual = klass.new(config).resolve
assert_equal config, actual
end
def test_environment_does_not_exist_in_config_url_does_exist
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = klass.new(config).resolve
expect_prod = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expect_prod, actual["default_env"]
end
def test_url_with_hyphenated_scheme
ENV['DATABASE_URL'] = "ibm-db://localhost/foo"
config = { "default_env" => { "adapter" => "not_postgres", "database" => "not_foo", "host" => "localhost" } }
actual = resolve(:default_env, config)
expected = { "adapter"=>"ibm_db", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_string_connection
config = { "default_env" => "postgres://localhost/foo" }
actual = klass.new(config).resolve
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_url_sub_key
config = { "default_env" => { "url" => "postgres://localhost/foo" } }
actual = klass.new(config).resolve
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_hash
config = { "production" => { "adapter" => "postgres", "database" => "foo" } }
actual = klass.new(config).resolve
assert_equal config, actual
end
def test_blank
config = {}
actual = klass.new(config).resolve
assert_equal config, actual
end
def test_blank_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {}
actual = klass.new(config).resolve
expected = { "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost" }
assert_equal expected, actual["default_env"]
assert_equal nil, actual["production"]
assert_equal nil, actual["development"]
assert_equal nil, actual["test"]
assert_equal nil, actual[:production]
assert_equal nil, actual[:development]
assert_equal nil, actual[:test]
end
def test_url_sub_key_with_database_url
ENV['DATABASE_URL'] = "NOT-POSTGRES://localhost/NOT_FOO"
config = { "default_env" => { "url" => "postgres://localhost/foo" } }
actual = klass.new(config).resolve
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_merge_no_conflicts_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {"default_env" => { "pool" => "5" } }
actual = klass.new(config).resolve
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost",
"pool" => "5"
}
}
assert_equal expected, actual
end
def test_merge_conflicts_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {"default_env" => { "adapter" => "NOT-POSTGRES", "database" => "NOT-FOO", "pool" => "5" } }
actual = klass.new(config).resolve
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost",
"pool" => "5"
}
}
assert_equal expected, actual
end
end
class ConnectionHandlerTest < ActiveRecord::TestCase
def setup
@klass = Class.new(Base) { def self.name; 'klass'; end }

View file

@ -0,0 +1,192 @@
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class MergeAndResolveDefaultUrlConfigTest < ActiveRecord::TestCase
def setup
@previous_database_url = ENV.delete("DATABASE_URL")
end
teardown do
ENV["DATABASE_URL"] = @previous_database_url
end
def resolve_config(config)
ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig.new(config).resolve
end
def resolve_spec(spec, config)
ConnectionSpecification::Resolver.new(resolve_config(config)).resolve(spec)
end
def test_resolver_with_database_uri_and_current_env_symbol_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = resolve_spec(:default_env, config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_and_current_env_string_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = assert_deprecated { resolve_spec("default_env", config) }
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_known_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "production" => { "adapter" => "not_postgres", "database" => "not_foo", "host" => "localhost" } }
actual = resolve_spec(:production, config)
expected = { "adapter"=>"not_postgres", "database"=>"not_foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_resolver_with_database_uri_and_unknown_symbol_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
assert_raises AdapterNotSpecified do
resolve_spec(:production, config)
end
end
def test_resolver_with_database_uri_and_unknown_string_key
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
assert_deprecated do
assert_raises AdapterNotSpecified do
resolve_spec("production", config)
end
end
end
def test_resolver_with_database_uri_and_supplied_url
ENV['DATABASE_URL'] = "not-postgres://not-localhost/not_foo"
config = { "production" => { "adapter" => "also_not_postgres", "database" => "also_not_foo" } }
actual = resolve_spec("postgres://localhost/foo", config)
expected = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_jdbc_url
config = { "production" => { "url" => "jdbc:postgres://localhost/foo" } }
actual = resolve_config(config)
assert_equal config, actual
end
def test_environment_does_not_exist_in_config_url_does_exist
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = { "not_default_env" => { "adapter" => "not_postgres", "database" => "not_foo" } }
actual = resolve_config(config)
expect_prod = { "adapter"=>"postgresql", "database"=>"foo", "host"=>"localhost" }
assert_equal expect_prod, actual["default_env"]
end
def test_url_with_hyphenated_scheme
ENV['DATABASE_URL'] = "ibm-db://localhost/foo"
config = { "default_env" => { "adapter" => "not_postgres", "database" => "not_foo", "host" => "localhost" } }
actual = resolve_spec(:default_env, config)
expected = { "adapter"=>"ibm_db", "database"=>"foo", "host"=>"localhost" }
assert_equal expected, actual
end
def test_string_connection
config = { "default_env" => "postgres://localhost/foo" }
actual = resolve_config(config)
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_url_sub_key
config = { "default_env" => { "url" => "postgres://localhost/foo" } }
actual = resolve_config(config)
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_hash
config = { "production" => { "adapter" => "postgres", "database" => "foo" } }
actual = resolve_config(config)
assert_equal config, actual
end
def test_blank
config = {}
actual = resolve_config(config)
assert_equal config, actual
end
def test_blank_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {}
actual = resolve_config(config)
expected = { "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost" }
assert_equal expected, actual["default_env"]
assert_equal nil, actual["production"]
assert_equal nil, actual["development"]
assert_equal nil, actual["test"]
assert_equal nil, actual[:production]
assert_equal nil, actual[:development]
assert_equal nil, actual[:test]
end
def test_url_sub_key_with_database_url
ENV['DATABASE_URL'] = "NOT-POSTGRES://localhost/NOT_FOO"
config = { "default_env" => { "url" => "postgres://localhost/foo" } }
actual = resolve_config(config)
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost"
}
}
assert_equal expected, actual
end
def test_merge_no_conflicts_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {"default_env" => { "pool" => "5" } }
actual = resolve_config(config)
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost",
"pool" => "5"
}
}
assert_equal expected, actual
end
def test_merge_conflicts_with_database_url
ENV['DATABASE_URL'] = "postgres://localhost/foo"
config = {"default_env" => { "adapter" => "NOT-POSTGRES", "database" => "NOT-FOO", "pool" => "5" } }
actual = resolve_config(config)
expected = { "default_env" =>
{ "adapter" => "postgresql",
"database" => "foo",
"host" => "localhost",
"pool" => "5"
}
}
assert_equal expected, actual
end
end
end
end