1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/test/execution_context_test.rb
Jean Boussier 6bad959565 Extract ActiveSupport::ExecutionContext out of ActiveRecord::QueryLogs
I'm working on a standardized error reporting interface
(https://github.com/rails/rails/issues/43472) and it has the same need
for a `context` than Active Record's query logs.

Just like query logs, when reporting an error you want to know what the
current controller or job is, etc.

So by extracting it we can allow both API to use the same store.
2021-11-10 09:36:02 +01:00

47 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require_relative "abstract_unit"
require "active_support/execution_context/test_helper"
class ExecutionContextTest < ActiveSupport::TestCase
# ExecutionContext is automatically reset in Rails app via executor hooks set in railtie
# But not in Active Support's own test suite.
include ActiveSupport::ExecutionContext::TestHelper
test "#set restore the modified keys when the block exits" do
assert_nil ActiveSupport::ExecutionContext.to_h[:foo]
ActiveSupport::ExecutionContext.set(foo: "bar") do
assert_equal "bar", ActiveSupport::ExecutionContext.to_h[:foo]
ActiveSupport::ExecutionContext.set(foo: "plop") do
assert_equal "plop", ActiveSupport::ExecutionContext.to_h[:foo]
end
assert_equal "bar", ActiveSupport::ExecutionContext.to_h[:foo]
ActiveSupport::ExecutionContext[:direct_assignment] = "present"
ActiveSupport::ExecutionContext.set(multi_assignment: "present")
end
assert_nil ActiveSupport::ExecutionContext.to_h[:foo]
assert_equal "present", ActiveSupport::ExecutionContext.to_h[:direct_assignment]
assert_equal "present", ActiveSupport::ExecutionContext.to_h[:multi_assignment]
end
test "#set coerce keys to symbol" do
ActiveSupport::ExecutionContext.set("foo" => "bar") do
assert_equal "bar", ActiveSupport::ExecutionContext.to_h[:foo]
end
end
test "#[]= coerce keys to symbol" do
ActiveSupport::ExecutionContext["symbol_key"] = "symbolized"
assert_equal "symbolized", ActiveSupport::ExecutionContext.to_h[:symbol_key]
end
test "#to_h returns a copy of the context" do
ActiveSupport::ExecutionContext[:foo] = 42
context = ActiveSupport::ExecutionContext.to_h
context[:foo] = 43
assert_equal 42, ActiveSupport::ExecutionContext.to_h[:foo]
end
end