Skip to content

Commit

Permalink
assert_same_hash makes large hash comparisons easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
martinemde committed Dec 3, 2024
1 parent fb1b507 commit b04b418
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
14 changes: 7 additions & 7 deletions test/system/avo/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Reset User 2FA", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -123,7 +123,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Block User", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -208,7 +208,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Reset Api Key", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -294,7 +294,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
end
rubygem_updated_at_changes = rubygem_audit["gid://gemcutter/Rubygem/#{rubygem.id}"]["changes"]["updated_at"]

assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/Deletion/#{deletion.id}" => {
Expand Down Expand Up @@ -398,7 +398,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
password_changed_event = user.events.where(tag: Events::UserEvent::PASSWORD_CHANGED).sole
version_yanked_event = rubygem.events.where(tag: Events::RubygemEvent::VERSION_YANKED).sole

assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/Deletion/#{deletion.id}" => {
Expand Down Expand Up @@ -519,7 +519,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Change User Email", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -593,7 +593,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Create User", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down
22 changes: 22 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ def assert_event(tag, expected_additional, actual)
assert_equal actual.additional_type.new(user_agent_info:, **expected_additional), actual.additional
end

# Asserts that both are the same nested hash, using equality checks unless
# the value is a hash, in which case it will recursively check the nested hash
# The purpose of this assertion is mostly to provide a more useful error when
# a nested hash contains different elements instead of overwhelming the user
# with a giant nested hash diff that is mostly sorting problems.
def assert_equal_hash(expected, actual, context = "expected")
assert_equal deep_sort_hashes(expected), deep_sort_hashes(actual), "Expected equal elements in #{context}"
end

# sort the hash keys and recursively sort nested hashes within enumberables
def deep_sort_hashes(obj)
if obj.is_a?(Hash)
obj.map do |k, v|
[k, deep_sort_hashes(v)]
end.sort!.to_h
elsif obj.respond_to?(:map)
obj.map { |v| deep_sort_hashes(v) }
else
obj
end
end

def headless_chrome_driver
Capybara.current_driver = :selenium_chrome_headless
Capybara.default_max_wait_time = 2
Expand Down

0 comments on commit b04b418

Please sign in to comment.