Skip to content

Commit

Permalink
protect against diff guard condition failing
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Nov 5, 2023
1 parent 21507bc commit 52491ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/kennel/models/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def diff(actual)
# strict: ignore Integer vs Float
# similarity: show diff when not 100% similar
# use_lcs: saner output
Hashdiff.diff(actual, expected, use_lcs: false, strict: false, similarity: 1)
result = Hashdiff.diff(actual, expected, use_lcs: false, strict: false, similarity: 1)
raise "Empty diff detected: guard condition failed" if result.empty?
result
end

def tracking_id
Expand Down
7 changes: 7 additions & 0 deletions test/kennel/models/record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ def diff_resource(e, a)
it "ignores numeric class difference since the api is semi random on these" do
diff_resource({ a: 1 }, a: 1.0).must_equal []
end

it "alerts when guard condition is ineffective" do
# if something is "diff equal" but not "==" then we are leaving speed on the table
Hashdiff.expects(:diff).returns []
e = assert_raises(RuntimeError) { diff_resource({ a: 1 }, a: 2) }
e.message.must_include "Empty diff detected"
end
end

describe "#tracking_id" do
Expand Down

0 comments on commit 52491ec

Please sign in to comment.