Skip to content

Commit

Permalink
Start support for testing with multiple different database adapters. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspth authored Aug 29, 2023
1 parent 0aac4d8 commit 888d9b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ gem "minitest-sprint"

gem "activerecord"
gem "sqlite3"
gem "mysql2"
gem "pg"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ GEM
minitest (5.18.1)
minitest-sprint (1.2.2)
path_expander (~> 1.1)
mysql2 (0.5.5)
path_expander (1.1.1)
pg (1.5.3)
rake (13.0.6)
sqlite3 (1.6.3-arm64-darwin)
sqlite3 (1.6.3-x86_64-linux)
Expand All @@ -38,7 +40,9 @@ DEPENDENCIES
activerecord
minitest (~> 5.0)
minitest-sprint
mysql2
oaken!
pg
rake (~> 13.0)
sqlite3

Expand Down
29 changes: 28 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,34 @@
require "active_record"
require "minitest/autorun"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
module Rails
def self.root() = __dir__ # Needed for the sqlite3 tasks.
end

ActiveRecord::Base.configurations = {
sqlite: { adapter: "sqlite3", pool: 5, database: "tmp/oaken_test.sqlite3" },
mysql: { adapter: "mysql2", pool: 5, encoding: "utf8mb4", database: "oaken_test", username: "root", host: "localhost" },
postgres: { adapter: "postgresql", pool: 5, encoding: "unicode", database: "oaken_test" }
}

adapter = :sqlite

database = ActiveRecord::Base.configurations.resolve(adapter).then do |config|
case adapter
when :sqlite then ActiveRecord::Tasks::SQLiteDatabaseTasks.new(config)
when :mysql then ActiveRecord::Tasks::MySQLDatabaseTasks.new(config)
when :postgres then ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(config)
end
end

begin
database.create
rescue ActiveRecord::DatabaseAlreadyExists
end

Minitest.after_run { database.drop }

ActiveRecord::Base.establish_connection(adapter)
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
Expand Down

0 comments on commit 888d9b1

Please sign in to comment.