Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp/lighthorizon: Modify trie tests to use assert over raw comparisons. #4373

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 33 additions & 41 deletions exp/lighthorizon/index/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"encoding/hex"
"encoding/json"
"math/rand"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,7 +23,7 @@ func randomTrie(t *testing.T, index *TrieIndex) (*TrieIndex, map[string]uint32)
ledger := uint32(rand.Int63())
hashBytes := make([]byte, 32)
if _, err := rand.Read(hashBytes); err != nil {
t.Error(err.Error())
assert.NoError(t, err)
}
hash := hex.EncodeToString(hashBytes)

Expand All @@ -39,14 +41,13 @@ func TestTrieIndex(t *testing.T) {

for key, expected := range inserts {
value, ok := index.Get([]byte(key))
if !ok {
t.Errorf("Key not found: %s", key)
} else {
ledger := binary.BigEndian.Uint32(value)
if ledger != expected {
t.Errorf("Key %s found: %v, expected: %v", key, ledger, expected)
}
if !assert.Truef(t, ok, "Key not found: %s", key) {
t.FailNow()
continue
}
ledger := binary.BigEndian.Uint32(value)
assert.Equalf(t, expected, ledger,
"Key %s found: %v, expected: %v", key, ledger, expected)
}
}
}
Expand All @@ -56,19 +57,16 @@ func TestTrieIndexUpsertBasic(t *testing.T) {

key := "key"
prev, ok := index.Upsert([]byte(key), []byte("a"))
if ok || prev != nil {
t.Errorf("Unexpected previous value: %q, expected: nil", string(prev))
}
assert.Nil(t, prev)
assert.Falsef(t, ok, "expected nil, got prev: %q", string(prev))

prev, ok = index.Upsert([]byte(key), []byte("b"))
if !ok || string(prev) != "a" {
t.Errorf("Unexpected previous value: %q, expected: a", string(prev))
}
assert.Equal(t, "a", string(prev))
assert.Truef(t, ok, "expected 'a', got prev: %q", string(prev))

prev, ok = index.Upsert([]byte(key), []byte("c"))
if !ok || string(prev) != "b" {
t.Errorf("Unexpected previous value: %q, expected: b", string(prev))
}
assert.Equal(t, "b", string(prev))
assert.Truef(t, ok, "expected 'b', got prev: %q", string(prev))
}

func TestTrieIndexSuffixes(t *testing.T) {
Expand Down Expand Up @@ -101,36 +99,30 @@ func TestTrieIndexSuffixes(t *testing.T) {

func TestTrieIndexSerialization(t *testing.T) {
for i := 0; i < 10_000; i++ {
index, inserts := randomTrie(t, nil)
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
index, inserts := randomTrie(t, nil)

// Round-trip it to serialization and back
buf := &bytes.Buffer{}
nWritten, err := index.WriteTo(buf)
if err != nil {
t.Error(err.Error())
}
// Round-trip it to serialization and back
buf := &bytes.Buffer{}
nWritten, err := index.WriteTo(buf)
assert.NoError(t, err)

read := &TrieIndex{}
nRead, err := read.ReadFrom(buf)
if err != nil {
t.Error(err.Error())
}
read := &TrieIndex{}
nRead, err := read.ReadFrom(buf)
assert.NoError(t, err)

if nWritten != nRead {
t.Errorf("Wrote %d bytes, but read %d bytes", nWritten, nRead)
}
assert.Equal(t, nWritten, nRead, "read more or less than we wrote")

for key, expected := range inserts {
value, ok := read.Get([]byte(key))
if !ok {
t.Errorf("Key not found: %s", key)
} else {
ledger := binary.BigEndian.Uint32(value)
if ledger != expected {
t.Errorf("Key %s found: %v, expected: %v", key, ledger, expected)
for key, expected := range inserts {
value, ok := read.Get([]byte(key))
if !assert.Truef(t, ok, "Key not found: %s", key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !assert.Truef(t, ok, "Key not found: %s", key) {
require.Truef(t, ok, "Key not found: %s", key)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require is generally closer to what you mean than assert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good call! Honestly was too lazy to look into the difference and now thinking we should use require everywhere we currently use assert 🤦

t.FailNow()
}

ledger := binary.BigEndian.Uint32(value)
assert.Equal(t, expected, ledger, "for key %s", key)
}
}
})
}
}

Expand Down