diff --git a/client_test.go b/client_test.go index 2d48425..2aa0605 100644 --- a/client_test.go +++ b/client_test.go @@ -39,14 +39,22 @@ func testContext(t testing.TB) context.Context { return ctx } -func randomKey() string { - return "key-" + strconv.Itoa(rand.IntN(1000000)) +func randomKey(prefix string) string { + return prefix + "-" + strconv.Itoa(rand.IntN(1000000)) +} + +func randomPk() string { + return randomKey("pk") +} + +func randomSk() string { + return randomKey("sk") } func TestClient_InsertItem(t *testing.T) { t.Parallel() f, ctx := newFixture(t) - err := f.cli.InsertItem(ctx, f.bucket, randomKey(), randomKey(), "", []byte("hello")) + err := f.cli.InsertItem(ctx, f.bucket, randomPk(), randomSk(), "", []byte("hello")) require.NoError(t, err) } @@ -54,8 +62,8 @@ func TestClient_ReadItemNotExist(t *testing.T) { t.Parallel() f, ctx := newFixture(t) - pk := randomKey() - sk := randomKey() + pk := randomPk() + sk := randomSk() t.Run("Single", func(t *testing.T) { t.Parallel() @@ -78,8 +86,8 @@ func TestClient_ReadItemTombstone(t *testing.T) { t.Parallel() f, ctx := newFixture(t) - pk := randomKey() - sk := randomKey() + pk := randomPk() + sk := randomSk() t.Logf("Creating item: PK=%s, SK=%s", pk, sk) @@ -109,8 +117,8 @@ func TestClient_ReadItemSingleRevision(t *testing.T) { t.Parallel() f, ctx := newFixture(t) - pk := randomKey() - sk := randomKey() + pk := randomPk() + sk := randomSk() err := f.cli.InsertItem(ctx, f.bucket, pk, sk, "", []byte("hello")) require.NoError(t, err) @@ -137,8 +145,8 @@ func TestClient_ReadItemMultipleRevisions(t *testing.T) { t.Parallel() f, ctx := newFixture(t) - pk := randomKey() - sk := randomKey() + pk := randomPk() + sk := randomSk() err := f.cli.InsertItem(ctx, f.bucket, pk, sk, "", []byte("hello1")) require.NoError(t, err) diff --git a/poll_single_test.go b/poll_single_test.go index 4dc317e..21f3276 100644 --- a/poll_single_test.go +++ b/poll_single_test.go @@ -13,8 +13,8 @@ func TestClient_PollItem(t *testing.T) { f, ctx := newFixture(t) - pk := randomKey() - sk := randomKey() + pk := randomPk() + sk := randomSk() err := f.cli.InsertItem(ctx, f.bucket, pk, sk, "", []byte("hello1")) require.NoError(t, err) diff --git a/read_batch_test.go b/read_batch_test.go index 47bd07b..cbd9ada 100644 --- a/read_batch_test.go +++ b/read_batch_test.go @@ -2,31 +2,32 @@ package k2v_test import ( k2v "code.notaphish.fyi/milas/garage-k2v-go" - "github.com/davecgh/go-spew/spew" + "fmt" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "math/rand/v2" "strconv" + "strings" "testing" ) func TestClient_ReadBatch(t *testing.T) { f, ctx := newFixture(t) - pk1 := randomKey() - sk1 := randomKey() + pk1 := randomKey("pk1") + sk1 := randomKey("sk1") + require.NoError(t, f.cli.InsertItem(ctx, f.bucket, pk1, sk1, "", []byte(strings.Join([]string{"hello", pk1, sk1}, "-")))) - require.NoError(t, f.cli.InsertItem(ctx, f.bucket, pk1, sk1, "", []byte("hello"))) - - pk2 := randomKey() + pk2 := randomKey("pk2") + sk2 := randomKey("sk2") for i := range 5 { - sk := randomKey() - require.NoError(t, f.cli.InsertItem(ctx, f.bucket, pk2, sk, "", []byte("hello-"+strconv.Itoa(i)))) + require.NoError(t, f.cli.InsertItem(ctx, f.bucket, pk2, sk2, "", []byte(strings.Join([]string{"hello", pk2, sk2, strconv.Itoa(i)}, "-")))) } - pk3 := randomKey() - sk3 := randomKey() + pk3 := randomKey("pk3") for i := range 5 { - require.NoError(t, f.cli.InsertItem(ctx, f.bucket, pk3, sk3, "", []byte("hello-"+strconv.Itoa(i)))) + skN := randomKey(fmt.Sprintf("sk%d", i+3)) + require.NoError(t, f.cli.InsertItem(ctx, f.bucket, pk3, skN, "", []byte(strings.Join([]string{"hello", pk3, skN, strconv.Itoa(i)}, "-")))) } q := []k2v.ReadBatchSearch{ @@ -35,19 +36,45 @@ func TestClient_ReadBatch(t *testing.T) { }, { PartitionKey: pk2, + SingleItem: true, + Start: sk2, }, { PartitionKey: pk3, - SingleItem: true, - Start: sk3, }, } - items, err := f.cli.ReadBatch(ctx, f.bucket, q) + results, err := f.cli.ReadBatch(ctx, f.bucket, q) require.NoError(t, err) - require.NotEmpty(t, items) + require.NotEmpty(t, results) + require.Len(t, results, 3) - spew.Dump(items) + assert.Equal(t, pk1, results[0].PartitionKey) + if assert.Len(t, results[0].Items, 1) && assert.Len(t, results[0].Items[0].Values, 1) { + assert.Equal(t, sk1, results[0].Items[0].SortKey) + assert.NotEmpty(t, results[0].Items[0].CausalityToken) + assert.Contains(t, results[0].Items[0].Values[0].GoString(), "hello") + } + + assert.Equal(t, pk2, results[1].PartitionKey) + if assert.Len(t, results[1].Items, 1) && assert.Len(t, results[1].Items[0].Values, 5) { + assert.Equal(t, sk2, results[1].Items[0].SortKey) + assert.NotEmpty(t, results[1].Items[0].CausalityToken) + for i := range results[1].Items[0].Values { + assert.Contains(t, results[1].Items[0].Values[i].GoString(), "hello") + } + } + + assert.Equal(t, pk3, results[2].PartitionKey) + if assert.Len(t, results[2].Items, 5) { + for _, item := range results[2].Items { + assert.NotEmpty(t, item.SortKey) + assert.NotEmpty(t, item.CausalityToken) + if assert.Len(t, item.Values, 1) { + assert.Contains(t, item.Values[0].GoString(), "hello") + } + } + } } func TestBulkGet(t *testing.T) { @@ -56,8 +83,8 @@ func TestBulkGet(t *testing.T) { keys := make([]k2v.ItemKey, 500) for i := range keys { keys[i] = k2v.ItemKey{ - PartitionKey: randomKey(), - SortKey: randomKey(), + PartitionKey: randomPk(), + SortKey: randomSk(), } require.NoError(t, f.cli.InsertItem(ctx, f.bucket, keys[i].PartitionKey, keys[i].SortKey, "", []byte("hello"+strconv.Itoa(i)))) }