Skip to content

Commit

Permalink
Refactor tests for private methods
Browse files Browse the repository at this point in the history
Don't directly tests these private methods but test them through the
public interface.

[skip changeset]
[skip review]
  • Loading branch information
tombruijn committed Sep 3, 2024
1 parent c48be2c commit ba9329e
Showing 1 changed file with 97 additions and 93 deletions.
190 changes: 97 additions & 93 deletions spec/lib/appsignal/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@

context "when a transaction has errors" do
let(:error) do
e = ExampleStandardError.new("test message")
allow(e).to receive(:backtrace).and_return(["line 1"])
e
ExampleStandardError.new("test message").tap do |e|
e.set_backtrace(["line 1"])
end
end

let(:other_error) do
e = ExampleStandardError.new("other test message")
allow(e).to receive(:backtrace).and_return(["line 2"])
e
ExampleStandardError.new("other test message").tap do |e|
e.set_backtrace(["line 2"])
end
end

context "when an error is already set on the transaction" do
Expand Down Expand Up @@ -1526,9 +1526,9 @@ def to_s
let(:transaction) { create_transaction }

let(:error) do
e = ExampleStandardError.new("test message")
allow(e).to receive(:backtrace).and_return(["line 1"])
e
ExampleStandardError.new("test message").tap do |e|
e.set_backtrace(["line 1"])
end
end

context "when error argument is not an error" do
Expand Down Expand Up @@ -1588,9 +1588,9 @@ def to_s

context "when an error is already set in the transaction" do
let(:other_error) do
e = ExampleStandardError.new("other test message")
allow(e).to receive(:backtrace).and_return(["line 2"])
e
ExampleStandardError.new("other test message").tap do |e|
e.set_backtrace(["line 2"])
end
end

before { transaction.set_error(other_error) }
Expand Down Expand Up @@ -1697,12 +1697,92 @@ def to_s
end
end
end

context "with a PG::UniqueViolation" do
let(:error) do
PG::UniqueViolation.new(
"ERROR: duplicate key value violates unique constraint " \
"\"index_users_on_email\" DETAIL: Key (email)=(test@test.com) already exists."
)
end
before do
stub_const("PG::UniqueViolation", Class.new(StandardError))
transaction.add_error(error)
end

it "returns a sanizited error message" do
expect(transaction).to have_error(
"PG::UniqueViolation",
"ERROR: duplicate key value violates unique constraint " \
"\"index_users_on_email\" DETAIL: Key (email)=(?) already exists."
)
end
end

context "with a ActiveRecord::RecordNotUnique" do
let(:error) do
ActiveRecord::RecordNotUnique.new(
"PG::UniqueViolation: ERROR: duplicate key value violates unique constraint " \
"\"example_constraint\"\nDETAIL: Key (email)=(foo@example.com) already exists."
)
end
before do
stub_const("ActiveRecord::RecordNotUnique", Class.new(StandardError))
transaction.add_error(error)
end

it "returns a sanizited error message" do
expect(transaction).to have_error(
"ActiveRecord::RecordNotUnique",
"PG::UniqueViolation: ERROR: duplicate key value violates unique constraint " \
"\"example_constraint\"\nDETAIL: Key (email)=(?) already exists."
)
end
end

context "with Rails module but without backtrace_cleaner method" do
it "returns the backtrace uncleaned" do
stub_const("Rails", Module.new)
error = ExampleStandardError.new("error message")
error.set_backtrace(["line 1", "line 2"])
transaction.add_error(error)

expect(last_transaction).to have_error(
"ExampleStandardError",
"error message",
["line 1", "line 2"]
)
end
end

if rails_present?
context "with Rails" do
it "cleans the backtrace with the Rails backtrace cleaner" do
::Rails.backtrace_cleaner.add_filter do |line|
line.tr("2", "?")
end

error = ExampleStandardError.new("error message")
error.set_backtrace(["line 1", "line 2"])
transaction.add_error(error)
expect(last_transaction).to have_error(
"ExampleStandardError",
"error message",
["line 1", "line ?"]
)
end
end
end
end

describe "#_set_error" do
let(:transaction) { new_transaction }
let(:env) { http_request_env_with_data }
let(:error) { ExampleStandardError.new("test message") }
let(:error) do
ExampleStandardError.new("test message").tap do |e|
e.set_backtrace(["line 1"])
end
end

it "responds to add_exception for backwards compatibility" do
expect(transaction).to respond_to(:add_exception)
Expand All @@ -1716,7 +1796,6 @@ def to_s

context "for a http request" do
it "sets an error on the transaction" do
allow(error).to receive(:backtrace).and_return(["line 1"])
transaction.send(:_set_error, error)

expect(transaction).to have_error(
Expand All @@ -1738,9 +1817,9 @@ def to_s
context "when the error has multiple causes" do
let(:error) do
e = ExampleStandardError.new("test message")
e.set_backtrace(["line 1"])
e2 = RuntimeError.new("cause message")
e3 = StandardError.new("cause message 2")
allow(e).to receive(:backtrace).and_return(["line 1"])
allow(e).to receive(:cause).and_return(e2)
allow(e2).to receive(:cause).and_return(e3)
e
Expand Down Expand Up @@ -1789,14 +1868,13 @@ def to_s
context "when the error has too many causes" do
let(:error) do
e = ExampleStandardError.new("root cause error")
e.set_backtrace(["line 1"])

11.times do |i|
next_e = ExampleStandardError.new("wrapper error #{i}")
allow(next_e).to receive(:cause).and_return(e)
e = next_e
end

allow(e).to receive(:backtrace).and_return(["line 1"])
e
end

Expand Down Expand Up @@ -1829,9 +1907,9 @@ def to_s

context "when error message is nil" do
let(:error) do
e = ExampleStandardError.new
e = ExampleStandardError.new(nil)
allow(e).to receive(:message).and_return(nil)
allow(e).to receive(:backtrace).and_return(["line 1"])
e.set_backtrace(["line 1"])
e
end

Expand Down Expand Up @@ -1985,80 +2063,6 @@ def to_s

# private

describe "#cleaned_backtrace" do
let(:transaction) { new_transaction }
subject { transaction.send(:cleaned_backtrace, ["line 1", "line 2"]) }

it "returns the backtrace" do
expect(subject).to eq ["line 1", "line 2"]
end

context "with Rails module but without backtrace_cleaner method" do
it "returns the backtrace uncleaned" do
stub_const("Rails", Module.new)
expect(subject).to eq ["line 1", "line 2"]
end
end

if rails_present?
context "with rails" do
it "cleans the backtrace with the Rails backtrace cleaner" do
::Rails.backtrace_cleaner.add_filter do |line|
line.tr("2", "?")
end
expect(subject).to eq ["line 1", "line ?"]
end
end
end
end

describe "#cleaned_error_message" do
let(:transaction) { new_transaction }
let(:error) { StandardError.new("Error message") }
subject { transaction.send(:cleaned_error_message, error) }

it "returns the error message" do
expect(subject).to eq "Error message"
end

context "with a PG::UniqueViolation" do
before do
stub_const("PG::UniqueViolation", Class.new(StandardError))
end

let(:error) do
PG::UniqueViolation.new(
"ERROR: duplicate key value violates unique constraint " \
"\"index_users_on_email\" DETAIL: Key (email)=(test@test.com) already exists."
)
end

it "returns a sanizited error message" do
expect(subject).to eq "ERROR: duplicate key value violates unique constraint " \
"\"index_users_on_email\" DETAIL: Key (email)=(?) already exists."
end
end

context "with a ActiveRecord::RecordNotUnique" do
before do
stub_const("ActiveRecord::RecordNotUnique", Class.new(StandardError))
end

let(:error) do
ActiveRecord::RecordNotUnique.new(
"PG::UniqueViolation: ERROR: duplicate key value violates unique constraint " \
"\"example_constraint\"\nDETAIL: Key (email)=(foo@example.com) already exists."
)
end

it "returns a sanizited error message" do
expect(subject).to eq \
"PG::UniqueViolation: ERROR: duplicate key value violates unique constraint " \
"\"example_constraint\"\nDETAIL: Key (email)=(?) already exists."
end
end
end

describe ".to_hash / .to_h" do
let(:transaction) { new_transaction }
subject { transaction.to_hash }
Expand Down

0 comments on commit ba9329e

Please sign in to comment.