diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-07-17 04:19:23 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-17 04:19:23 -0300 |
commit | 9fcfb86c42d116cd4145b702dd5c73ce1493b5b9 (patch) | |
tree | 6589938e9c2ce9ed901947b3cfb1d1453a2843b8 /activerecord/test/cases | |
parent | bad3a120f1690f393d8f6204b3ceee60f0ce707b (diff) | |
parent | 31a8588a01cf5aadb864f609efad6ee24b5d4055 (diff) | |
download | rails-9fcfb86c42d116cd4145b702dd5c73ce1493b5b9.tar.gz rails-9fcfb86c42d116cd4145b702dd5c73ce1493b5b9.tar.bz2 rails-9fcfb86c42d116cd4145b702dd5c73ce1493b5b9.zip |
Merge pull request #20818 from jeremywadsack/use_transactional_fixtures_all_databases
Use notification to ensure that lazy-loaded model classes have transactions
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/connection_pool_test.rb | 12 | ||||
-rw-r--r-- | activerecord/test/cases/fixtures_test.rb | 40 |
2 files changed, 52 insertions, 0 deletions
diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb index 09e7848bda..2bdabaee62 100644 --- a/activerecord/test/cases/connection_pool_test.rb +++ b/activerecord/test/cases/connection_pool_test.rb @@ -341,6 +341,18 @@ module ActiveRecord end end + def test_connection_notification_is_called + payloads = [] + subscription = ActiveSupport::Notifications.subscribe('!connection.active_record') do |name, started, finished, unique_id, payload| + payloads << payload + end + ActiveRecord::Base.establish_connection :arunit + assert_equal [:class_name, :config, :connection_id], payloads[0].keys.sort + assert_equal 'primary', payloads[0][:class_name] + ensure + ActiveSupport::Notifications.unsubscribe(subscription) if subscription + end + def test_pool_sets_connection_schema_cache connection = pool.checkout schema_cache = SchemaCache.new connection diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 9455d4886c..a06aa4b247 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -622,6 +622,46 @@ class TransactionalFixturesOnCustomConnectionTest < ActiveRecord::TestCase end end +class TransactionalFixturesOnConnectionNotification < ActiveRecord::TestCase + self.use_transactional_tests = true + self.use_instantiated_fixtures = false + + def test_transaction_created_on_connection_notification + connection = stub(:transaction_open? => false) + connection.expects(:begin_transaction).with(joinable: false) + fire_connection_notification(connection) + end + + def test_notification_established_transactions_are_rolled_back + # Mocha is not thread-safe so define our own stub to test + connection = Class.new do + attr_accessor :rollback_transaction_called + def transaction_open?; true; end + def begin_transaction(*args); end + def rollback_transaction(*args) + @rollback_transaction_called = true + end + end.new + fire_connection_notification(connection) + teardown_fixtures + assert(connection.rollback_transaction_called, "Expected <mock connection>#rollback_transaction to be called but was not") + end + + private + + def fire_connection_notification(connection) + ActiveRecord::Base.connection_handler.stubs(:retrieve_connection).with(Book).returns(connection) + message_bus = ActiveSupport::Notifications.instrumenter + payload = { + class_name: 'Book', + config: nil, + connection_id: connection.object_id + } + + message_bus.instrument('!connection.active_record', payload) {} + end +end + class InvalidTableNameFixturesTest < ActiveRecord::TestCase fixtures :funny_jokes # Set to false to blow away fixtures cache and ensure our fixtures are loaded |