Improvements for spec/support/active_record

- All adapters rely on db/config.yml configuration
- All adapters load the same schema
This commit is contained in:
stanislaw 2012-08-02 23:35:23 +03:00 committed by Ben Mabey
parent 64e77d0bfa
commit d8be115e92
6 changed files with 55 additions and 79 deletions

26
db/config.yml Normal file
View file

@ -0,0 +1,26 @@
mysql:
adapter: mysql
database: database_cleaner_test
username: root
password:
host: 127.0.0.1
port: 3306
encoding: utf8
mysql2:
adapter: mysql
database: database_cleaner_test
username: root
password:
host: 127.0.0.1
port: 3306
encoding: utf8
postgres:
adapter: postgresql
database: database_cleaner_test
username: postgres
password:
host: 127.0.0.1
encoding: unicode
template: template0

View file

@ -0,0 +1,4 @@
def db_config
config_path = 'db/config.yml'
@db_config ||= YAML.load(IO.read(config_path))
end

View file

@ -1,39 +1,19 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
module MySQL2Helper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
# ActiveRecord::Base.logger = Logger.new(STDERR)
@@mysql2_db_spec = {
:adapter => 'mysql2',
:host => 'localhost',
:username => 'root',
:password => '',
:encoding => 'utf8'
}
@@db = {:database => 'database_cleaner_test'}
def active_record_mysql2_setup
ActiveRecord::Base.establish_connection(@@mysql2_db_spec)
ActiveRecord::Base.connection.drop_database @@db[:database] rescue nil
ActiveRecord::Base.connection.create_database @@db[:database]
ActiveRecord::Base.establish_connection(@@mysql2_db_spec.merge(@@db))
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.integer :name
end
end
ActiveRecord::Base.establish_connection db_config['mysql2']
load_schema
end
def active_record_mysql2_connection
ActiveRecord::Base.connection
end
class ::User < ActiveRecord::Base
end
end
RSpec.configure do |c|

View file

@ -1,39 +1,19 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
module MySQLHelper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
# ActiveRecord::Base.logger = Logger.new(STDERR)
@@mysql_db_spec = {
:adapter => 'mysql',
:host => 'localhost',
:username => 'root',
:password => '',
:encoding => 'utf8'
}
@@db = {:database => 'database_cleaner_test'}
def active_record_mysql_setup
ActiveRecord::Base.establish_connection(@@mysql_db_spec)
ActiveRecord::Base.connection.drop_database @@db[:database] rescue nil
ActiveRecord::Base.connection.create_database @@db[:database]
ActiveRecord::Base.establish_connection(@@mysql_db_spec.merge(@@db))
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.integer :name
end
end
ActiveRecord::Base.establish_connection db_config['mysql']
load_schema
end
def active_record_mysql_connection
ActiveRecord::Base.connection
end
class ::User < ActiveRecord::Base
end
end
RSpec.configure do |c|

View file

@ -1,43 +1,19 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
module PostgreSQLHelper
puts "Active Record #{ActiveRecord::VERSION::STRING}, pg"
# ActiveRecord::Base.logger = Logger.new(STDERR)
# createdb database_cleaner_test -E UTF8 -T template0
@@pg_db_spec = {
:adapter => 'postgresql',
:database => 'postgres',
:host => '127.0.0.1',
:username => 'postgres',
:password => '',
:encoding => 'unicode',
:template => 'template0'
}
@@db = {:database => 'database_cleaner_test'}
# ActiveRecord::Base.establish_connection(@@pg_db_spec)
# ActiveRecord::Base.connection.drop_database db[:database] rescue nil
# ActiveRecord::Base.connection.create_database db[:database]
def active_record_pg_setup
ActiveRecord::Base.establish_connection(@@pg_db_spec.merge(@@db))
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.integer :name
end
end
ActiveRecord::Base.establish_connection db_config['postgres']
load_schema
end
def active_record_pg_connection
ActiveRecord::Base.connection
end
class ::User < ActiveRecord::Base
end
end
RSpec.configure do |c|

View file

@ -0,0 +1,10 @@
def load_schema
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.integer :name
end
end
end
class ::User < ActiveRecord::Base
end