Skip to content

Commit 32c8197

Browse files
author
Martin Kleppmann
committed
Support test db config in yaml file (runcoderun)
1 parent e999982 commit 32c8197

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

config/database.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Details of the database connection to use for running unit tests.
2+
test:
3+
adapter: mysql
4+
encoding: utf8
5+
database: invoicing_test
6+
pool: 5
7+
username: root
8+
password:
9+
socket: /tmp/mysql.sock

invoicing/test/test_helper.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
require 'invoicing'
1313

14+
# Overridden by ../../config/database.yml if it exists.
1415
TEST_DB_CONFIG = {
1516
:postgresql => {:adapter => "postgresql", :host => "localhost", :database => "invoicing_test",
1617
:username => "postgres", :password => ""},
1718
:mysql => {:adapter => "mysql", :host => "localhost", :database => "invoicing_test",
1819
:username => "root", :password => ""}
1920
}
21+
TEST_DB_CONFIG_FILE = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'database.yml'))
2022

2123
def database_used_for_testing
2224
(ENV['DATABASE'] || :mysql).to_sym
@@ -27,7 +29,16 @@ def test_in_all_databases
2729
end
2830

2931
def connect_to_testing_database
30-
ActiveRecord::Base.establish_connection(TEST_DB_CONFIG[database_used_for_testing])
32+
db_config = TEST_DB_CONFIG[database_used_for_testing]
33+
34+
if File.exists? TEST_DB_CONFIG_FILE
35+
yaml = YAML::load File.open(TEST_DB_CONFIG_FILE)
36+
if yaml && yaml['test'] && (yaml['test']['adapter'].to_s == database_used_for_testing.to_s)
37+
db_config = yaml['test']
38+
end
39+
end
40+
41+
ActiveRecord::Base.establish_connection(db_config)
3142
end
3243

3344
connect_to_testing_database

0 commit comments

Comments
 (0)