From 4510669da14757750fff6cbcbfb24e7e272935db Mon Sep 17 00:00:00 2001 From: Cody Littley Date: Mon, 21 Oct 2024 13:28:37 -0500 Subject: [PATCH] Add test for keys. Signed-off-by: Cody Littley --- common/kvstore/tablestore/key_test.go | 101 ++++++++++++++++++ .../kvstore/tablestore/table_store_batch.go | 2 +- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 common/kvstore/tablestore/key_test.go diff --git a/common/kvstore/tablestore/key_test.go b/common/kvstore/tablestore/key_test.go new file mode 100644 index 0000000000..10de729c3d --- /dev/null +++ b/common/kvstore/tablestore/key_test.go @@ -0,0 +1,101 @@ +package tablestore + +import ( + tu "github.com/Layr-Labs/eigenda/common/testutils" + "github.com/stretchr/testify/assert" + "math/rand" + "testing" +) + +func TestGetName(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + + kb := newKeyBuilder(tableName, 0) + assert.Equal(t, tableName, kb.TableName()) +} + +func TestGetBuilder(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + + kb := newKeyBuilder(tableName, 0) + k := kb.StringKey("asdf") + assert.Same(t, kb, k.Builder()) +} + +func TestStringRoundTrip(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + str := tu.RandomString(10) + + kb := newKeyBuilder(tableName, 0) + k := kb.StringKey(str) + assert.Equal(t, str, k.AsString()) +} + +func TestBytesRoundTrip(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + b := tu.RandomBytes(10) + + kb := newKeyBuilder(tableName, 0) + k := kb.Key(b) + assert.Equal(t, b, k.AsBytes()) +} + +func TestUint64RoundTrip(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + u := rand.Uint64() + + kb := newKeyBuilder(tableName, 0) + k := kb.Uint64Key(u) + u2, err := k.AsUint64() + assert.NoError(t, err) + assert.Equal(t, u, u2) +} + +func TestInt64RoundTrip(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + i := rand.Int63() + + kb := newKeyBuilder(tableName, 0) + k := kb.Int64Key(i) + u2, err := k.AsInt64() + assert.NoError(t, err) + assert.Equal(t, i, u2) +} + +func TestUint32RoundTrip(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + u := rand.Uint32() + + kb := newKeyBuilder(tableName, 0) + k := kb.Uint32Key(u) + u2, err := k.AsUint32() + assert.NoError(t, err) + assert.Equal(t, u, u2) +} + +func TestInt32RoundTrip(t *testing.T) { + tu.InitializeRandom() + + tableName := tu.RandomString(10) + i := rand.Int31() + + kb := newKeyBuilder(tableName, 0) + k := kb.Int32Key(i) + u2, err := k.AsInt32() + assert.NoError(t, err) + assert.Equal(t, i, u2) +} diff --git a/common/kvstore/tablestore/table_store_batch.go b/common/kvstore/tablestore/table_store_batch.go index faf417dae7..91f19c6fb2 100644 --- a/common/kvstore/tablestore/table_store_batch.go +++ b/common/kvstore/tablestore/table_store_batch.go @@ -56,4 +56,4 @@ func (t *tableStoreBatch) Apply() error { // Size returns the number of operations in the batch. func (t *tableStoreBatch) Size() uint32 { return t.baseBatch.Size() -} // TODO do we ever call this? +}