From 25ac740933c28681fa3b3d9ff9fee88e9ea11787 Mon Sep 17 00:00:00 2001 From: Petr Hanzl Date: Fri, 19 Jul 2024 14:30:37 +0200 Subject: [PATCH] Fix incorrect checks in db tests; Return error using %w to pass error type. --- db/code_db_test.go | 9 +++++++-- db/substate_db_test.go | 9 +++++++-- db/update_db.go | 4 ++-- db/update_db_test.go | 9 +++++++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/db/code_db_test.go b/db/code_db_test.go index db671e4..8caba5d 100644 --- a/db/code_db_test.go +++ b/db/code_db_test.go @@ -2,6 +2,7 @@ package db import ( "bytes" + "errors" "fmt" "testing" @@ -82,8 +83,12 @@ func TestCodeDB_DeleteCode(t *testing.T) { } code, err := db.GetCode(hash) - if err != nil { - t.Fatalf("get code returned error; %v", err) + if err == nil { + t.Fatal("get code must fail") + } + + if got, want := err, leveldb.ErrNotFound; !errors.Is(got, want) { + t.Fatalf("unexpected err, got: %v, want: %v", got, want) } if code != nil { diff --git a/db/substate_db_test.go b/db/substate_db_test.go index c865a6e..571e01d 100644 --- a/db/substate_db_test.go +++ b/db/substate_db_test.go @@ -1,6 +1,7 @@ package db import ( + "errors" "fmt" "math/big" "testing" @@ -98,8 +99,12 @@ func TestSubstateDB_DeleteSubstate(t *testing.T) { } ss, err := db.GetSubstate(37_534_834, 1) - if err != nil { - t.Fatalf("get substate returned error; %v", err) + if err == nil { + t.Fatal("get substate must fail") + } + + if got, want := err, leveldb.ErrNotFound; !errors.Is(got, want) { + t.Fatalf("unexpected err, got: %v, want: %v", got, want) } if ss != nil { diff --git a/db/update_db.go b/db/update_db.go index 3d56e1e..3b3dcdd 100644 --- a/db/update_db.go +++ b/db/update_db.go @@ -120,7 +120,7 @@ func (db *updateDB) GetUpdateSet(block uint64) (*updateset.UpdateSet, error) { key := UpdateDBKey(block) value, err := db.Get(key) if err != nil { - return nil, fmt.Errorf("cannot get updateset block: %v, key %v; %v", block, key, err) + return nil, fmt.Errorf("cannot get updateset block: %v, key %v; %w", block, key, err) } if value == nil { @@ -130,7 +130,7 @@ func (db *updateDB) GetUpdateSet(block uint64) (*updateset.UpdateSet, error) { // decode value var updateSetRLP updateset.UpdateSetRLP if err = trlp.DecodeBytes(value, &updateSetRLP); err != nil { - return nil, fmt.Errorf("cannot decode update-set rlp block: %v, key %v; %v", block, key, err) + return nil, fmt.Errorf("cannot decode update-set rlp block: %v, key %v; %w", block, key, err) } return updateSetRLP.ToWorldState(db.GetCode, block) diff --git a/db/update_db_test.go b/db/update_db_test.go index 5a115ad..3019237 100644 --- a/db/update_db_test.go +++ b/db/update_db_test.go @@ -1,6 +1,7 @@ package db import ( + "errors" "fmt" "math/big" "testing" @@ -98,8 +99,12 @@ func TestUpdateDB_DeleteUpdateSet(t *testing.T) { } us, err := db.GetUpdateSet(testUpdateSet.Block) - if err != nil { - t.Fatalf("get update=set returned error; %v", err) + if err == nil { + t.Fatal("get update-set must fail") + } + + if got, want := err, leveldb.ErrNotFound; !errors.Is(got, want) { + t.Fatalf("unexpected err, got: %v, want: %v", got, want) } if us != nil {