Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify ActiveRecord::TestFixtures to not rely on AS::TestCase: #37770

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/test_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def fixtures(*fixture_set_names)
if fixture_set_names.first == :all
raise StandardError, "No fixture path found. Please set `#{self}.fixture_path`." if fixture_path.blank?
fixture_set_names = Dir[::File.join(fixture_path, "{**,*}/*.{yml}")].uniq
fixture_set_names.reject! { |f| f.starts_with?(file_fixture_path.to_s) } if file_fixture_path
fixture_set_names.reject! { |f| f.starts_with?(file_fixture_path.to_s) } if defined?(file_fixture_path) && file_fixture_path
fixture_set_names.map! { |f| f[fixture_path.to_s.size..-5].delete_prefix("/") }
else
fixture_set_names = fixture_set_names.flatten.map(&:to_s)
Expand Down Expand Up @@ -98,7 +98,7 @@ def uses_transaction?(method)

def run_in_transaction?
use_transactional_tests &&
!self.class.uses_transaction?(method_name)
!self.class.uses_transaction?(name)
end

def setup_fixtures(config = ActiveRecord::Base)
Expand Down
40 changes: 40 additions & 0 deletions activerecord/test/cases/test_fixtures_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

require "cases/helper"
require "tempfile"
require "fileutils"
require "models/zine"

class TestFixturesTest < ActiveRecord::TestCase
setup do
Expand All @@ -17,4 +20,41 @@ def test_use_transactional_tests_can_be_overridden

assert_equal "foobar", @klass.use_transactional_tests
end

unless in_memory_db?
def test_doesnt_rely_on_active_support_test_case_specific_methods
tmp_dir = Dir.mktmpdir
File.write(File.join(tmp_dir, "zines.yml"), <<~YML)
going_out:
title: Hello
YML

klass = Class.new(Minitest::Test) do
include ActiveRecord::TestFixtures

self.fixture_path = tmp_dir
self.use_transactional_tests = true

fixtures :all

def test_run_successfuly
assert_equal("Hello", Zine.first.title)
assert_equal("Hello", zines(:going_out).title)
end
end

old_handlers = ActiveRecord::Base.connection_handlers
old_handler = ActiveRecord::Base.connection_handler
ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
ActiveRecord::Base.connection_handlers = {}
ActiveRecord::Base.establish_connection(:arunit)

test_result = klass.new("test_run_successfuly").run
assert_predicate(test_result, :passed?)
ensure
ActiveRecord::Base.connection_handler = old_handler
ActiveRecord::Base.connection_handlers = old_handlers
FileUtils.rm_r(tmp_dir)
end
end
end