From dd9572689a23d0f97ac79eeeeb1580f2ba811ecc Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Mon, 13 Apr 2026 00:39:03 +0200 Subject: [PATCH] Extend all test tasks to run against every configured test db (WIP) --- bin/rails | 1 + config/database.yml.dist | 21 ++++++++++-- lib/core_ext/test_multiple_adapters.rb | 42 +++++++++++++++++++++++ test/application_system_test_case.rb | 4 +++ test/controllers/users_controller_test.rb | 2 +- test/system/users_test.rb | 1 + 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 lib/core_ext/test_multiple_adapters.rb diff --git a/bin/rails b/bin/rails index efc0377..25cea87 100755 --- a/bin/rails +++ b/bin/rails @@ -1,4 +1,5 @@ #!/usr/bin/env ruby APP_PATH = File.expand_path("../config/application", __dir__) require_relative "../config/boot" +require_relative "../lib/core_ext/test_multiple_adapters.rb" require "rails/commands" diff --git a/config/database.yml.dist b/config/database.yml.dist index 5d079fe..73fe3e5 100644 --- a/config/database.yml.dist +++ b/config/database.yml.dist @@ -42,9 +42,24 @@ production: # <<: *default # database: fixinme_dev -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. +# Warning: Test databases will be erased and re-generated from your development +# database when you run "rake". Do not set these dbs to the same as development +# or production. +# +# Multiple test databases can be provided. When more than one test db is +# present, every test task (test, test:models, test:system, etc.) automatically +# runs against all of them. +# +# In additional test databases only "adapter", "database", "host", "port" and +# "socket" settings are used. All settings default to values specified in +# "test" - which is required in all test setups. #test: # <<: *default # database: fixinme_test +# +#test_sqlite3: +# adapter: sqlite3 +# database: db/fixinme_test.sqlite3 +# +#test_postgresql: +# adapter: postgresql diff --git a/lib/core_ext/test_multiple_adapters.rb b/lib/core_ext/test_multiple_adapters.rb new file mode 100644 index 0000000..ed7451e --- /dev/null +++ b/lib/core_ext/test_multiple_adapters.rb @@ -0,0 +1,42 @@ +require "byebug" + +module CoreExt + module TestMultipleAdapters + def perform(...) + #require APP_PATH + $LOAD_PATH << Rails::Command.root.join("test").to_s + require "test_helper" + + Rails.application.config.database_configuration.each_pair do |name, config| + next unless name.start_with?('test_') + + puts config['adapter'] + ENV['DATABASE_URL'] = dbconfig_to_url(config) + byebug + super + end + end + + alias_method :test, :perform + + private + + def dbconfig_to_url(cfg) + return cfg['url'] if cfg.has_key?("url") + + config = cfg.transform_values { |v| URI.encode_www_form_component(v) } + + username, password = config.delete('username'), config.delete('password') + auth = username ? "#{username}:#{password}" : '' + host, port = config.delete('host'), config.delete('port') + server = port ? "#{host}:#{port}" : host + url = "#{config.delete('adapter')}:" + url += "//#{auth}@#{server}" if auth || server + url += "/#{config.delete('database')}" + url += "?#{config.map { |k,v| "#{k}=#{v}" }.join('&')}" unless config.empty? + end + end +end + +#require "rails/commands/test/test_command" +#Rails::Command::TestCommand.prepend CoreExt::TestMultipleAdapters diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 4ba8106..65dd2b2 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -56,4 +56,8 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase # find('a[disabled]').click # end #end + + # TODO: override #test, creating per-adapter test methods running within + # #with_connection? + # define shards for test database end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index bda70d8..7a8a39a 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -2,7 +2,7 @@ require "test_helper" class UsersControllerTest < ActionDispatch::IntegrationTest setup do - @user = users(:one) + @user = users(:admin) end test "should get index" do diff --git a/test/system/users_test.rb b/test/system/users_test.rb index b012bf4..7b289b4 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -6,6 +6,7 @@ class UsersTest < ApplicationSystemTestCase end test 'sign in' do + byebug visit root_url assert find_link(href: new_user_session_path)[:disabled]