mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Enable the sqlite3 in-memory test connection to work
This commit is contained in:
committed by
Aaron Patterson
parent
c6e10b0f60
commit
80df74bf51
@@ -26,6 +26,11 @@ def current_adapter?(*types)
|
||||
end
|
||||
end
|
||||
|
||||
def in_memory_db?
|
||||
current_adapter?(:SQLiteAdapter) &&
|
||||
ActiveRecord::Base.connection_pool.spec.config[:database] == ":memory:"
|
||||
end
|
||||
|
||||
def with_env_tz(new_tz = 'US/Eastern')
|
||||
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
|
||||
yield
|
||||
@@ -100,11 +105,11 @@ class ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
# silence verbose schema loading
|
||||
original_stdout = $stdout
|
||||
$stdout = StringIO.new
|
||||
def load_schema
|
||||
# silence verbose schema loading
|
||||
original_stdout = $stdout
|
||||
$stdout = StringIO.new
|
||||
|
||||
begin
|
||||
adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
|
||||
adapter_specific_schema_file = SCHEMA_ROOT + "/#{adapter_name}_specific_schema.rb"
|
||||
|
||||
@@ -117,6 +122,8 @@ ensure
|
||||
$stdout = original_stdout
|
||||
end
|
||||
|
||||
load_schema
|
||||
|
||||
class << Time
|
||||
unless method_defined? :now_before_time_travel
|
||||
alias_method :now_before_time_travel, :now
|
||||
@@ -133,4 +140,3 @@ class << Time
|
||||
@now = nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -252,13 +252,13 @@ end
|
||||
|
||||
# TODO: The Sybase, and OpenBase adapters currently have no support for pessimistic locking
|
||||
|
||||
unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter)
|
||||
unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter) || in_memory_db?
|
||||
class PessimisticLockingTest < ActiveRecord::TestCase
|
||||
self.use_transactional_fixtures = false
|
||||
fixtures :people, :readers
|
||||
|
||||
def setup
|
||||
Person.connection_pool.clear_reloadable_connections!
|
||||
Person.connection_pool.clear_reloadable_connections
|
||||
# Avoid introspection queries during tests.
|
||||
Person.columns; Reader.columns
|
||||
end
|
||||
|
||||
@@ -137,4 +137,4 @@ class PooledConnectionsTest < ActiveRecord::TestCase
|
||||
def add_record(name)
|
||||
ActiveRecord::Base.connection_pool.with_connection { Project.create! :name => name }
|
||||
end
|
||||
end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name
|
||||
end unless current_adapter?(:FrontBase) || in_memory_db?
|
||||
|
||||
@@ -14,6 +14,7 @@ class TestUnconnectedAdapter < ActiveRecord::TestCase
|
||||
def teardown
|
||||
@underlying = nil
|
||||
ActiveRecord::Base.establish_connection(@specification)
|
||||
load_schema if in_memory_db?
|
||||
end
|
||||
|
||||
def test_connection_no_longer_established
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
print "Using native SQLite3\n"
|
||||
# This file connects to an in-memory SQLite3 database, which is a very fast way to run the tests.
|
||||
# The downside is that disconnect from the database results in the database effectively being
|
||||
# wiped. For this reason, pooled_connections_test.rb is disabled when using an in-memory database.
|
||||
|
||||
print "Using native SQLite3 (in memory)\n"
|
||||
require_dependency 'models/course'
|
||||
require 'logger'
|
||||
ActiveRecord::Base.logger = Logger.new("debug.log")
|
||||
@@ -6,13 +10,10 @@ ActiveRecord::Base.logger = Logger.new("debug.log")
|
||||
class SqliteError < StandardError
|
||||
end
|
||||
|
||||
def make_connection(clazz, db_definitions_file)
|
||||
clazz.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
||||
File.read(SCHEMA_ROOT + "/#{db_definitions_file}").split(';').each do |command|
|
||||
clazz.connection.execute(command) unless command.strip.empty?
|
||||
end
|
||||
def make_connection(clazz)
|
||||
ActiveRecord::Base.configurations = { clazz.name => { :adapter => 'sqlite3', :database => ':memory:' } }
|
||||
clazz.establish_connection(clazz.name)
|
||||
end
|
||||
|
||||
make_connection(ActiveRecord::Base, 'sqlite.sql')
|
||||
make_connection(Course, 'sqlite2.sql')
|
||||
load(SCHEMA_ROOT + "/schema.rb")
|
||||
make_connection(ActiveRecord::Base)
|
||||
make_connection(Course)
|
||||
|
||||
Reference in New Issue
Block a user