From 92ac8219bc1b5a77d10b635e2eef4fb9e3902e01 Mon Sep 17 00:00:00 2001 From: Sahdev Zala Date: Wed, 29 Jun 2022 16:39:13 -0400 Subject: [PATCH] Client: fix check for WithPrefix op Make sure that WithPrefix correctly set the flag, and add test. Also, add test for WithFromKey. fixes #14056 Signed-off-by: Sahdev Zala --- client/v3/op.go | 2 +- client/v3/op_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/client/v3/op.go b/client/v3/op.go index e8c0c1e08c96..5251906322c9 100644 --- a/client/v3/op.go +++ b/client/v3/op.go @@ -389,12 +389,12 @@ func getPrefix(key []byte) []byte { // can return 'foo1', 'foo2', and so on. func WithPrefix() OpOption { return func(op *Op) { + op.isOptsWithPrefix = true if len(op.key) == 0 { op.key, op.end = []byte{0}, []byte{0} return } op.end = getPrefix(op.key) - op.isOptsWithPrefix = true } } diff --git a/client/v3/op_test.go b/client/v3/op_test.go index 762044fc5e21..f1890eafafc3 100644 --- a/client/v3/op_test.go +++ b/client/v3/op_test.go @@ -36,3 +36,66 @@ func TestOpWithSort(t *testing.T) { t.Fatalf("expected %+v, got %+v", wreq, req) } } + +func TestIsSortOptionValid(t *testing.T) { + rangeReqs := []struct { + sortOrder pb.RangeRequest_SortOrder + sortTarget pb.RangeRequest_SortTarget + expectedValid bool + }{ + { + sortOrder: pb.RangeRequest_ASCEND, + sortTarget: pb.RangeRequest_CREATE, + expectedValid: true, + }, + { + sortOrder: pb.RangeRequest_ASCEND, + sortTarget: 100, + expectedValid: false, + }, + { + sortOrder: 200, + sortTarget: pb.RangeRequest_MOD, + expectedValid: false, + }, + } + + for _, req := range rangeReqs { + getOp := Op{ + sort: &SortOption{ + Order: SortOrder(req.sortOrder), + Target: SortTarget(req.sortTarget), + }, + } + + actualRet := getOp.IsSortOptionValid() + if actualRet != req.expectedValid { + t.Errorf("expected sortOrder (%d) and sortTarget (%d) to be %t, but got %t", + req.sortOrder, req.sortTarget, req.expectedValid, actualRet) + } + } +} + +func TestIsOptsWithPrefix(t *testing.T) { + optswithprefix := []OpOption{WithPrefix()} + if !IsOptsWithPrefix(optswithprefix) { + t.Errorf("IsOptsWithPrefix = false, expected true") + } + + optswithfromkey := []OpOption{WithFromKey()} + if IsOptsWithPrefix(optswithfromkey) { + t.Errorf("IsOptsWithPrefix = true, expected false") + } +} + +func TestIsOptsWithFromKey(t *testing.T) { + optswithfromkey := []OpOption{WithFromKey()} + if !IsOptsWithFromKey(optswithfromkey) { + t.Errorf("IsOptsWithFromKey = false, expected true") + } + + optswithprefix := []OpOption{WithPrefix()} + if IsOptsWithFromKey(optswithprefix) { + t.Errorf("IsOptsWithFromKey = true, expected false") + } +}