Skip to content

Commit

Permalink
setOptions with errors, start tests
Browse files Browse the repository at this point in the history
Signed-off-by: Maayan Shani <[email protected]>
  • Loading branch information
Maayanshani25 committed Jan 22, 2025
1 parent f67e8d0 commit 62b6d2b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (setOptions *SetOptions) SetReturnOldValue(returnOldValue bool) *SetOptions
return setOptions
}

func (SetOptions *SetOptions) SetIfeqConditional(comparisonValue string) *SetOptions {
func (SetOptions *SetOptions) SetComparisonValue(comparisonValue string) *SetOptions {
SetOptions.ComparisonValue = comparisonValue
return SetOptions
}
Expand All @@ -57,8 +57,15 @@ func (opts *SetOptions) toArgs() ([]string, error) {
var err error
if opts.ConditionalSet != "" {
args = append(args, string(opts.ConditionalSet))
} else if opts.ComparisonValue != "" {
}

if opts.ConditionalSet == OnlyIfEqual {
if opts.ComparisonValue == "" {
return nil, &RequestError{"comparisonValue must be set when conditionalSet is ONLY_IF_EQUAL"}
}
args = append(args, "IFEQ", opts.ComparisonValue)
} else if opts.ComparisonValue != "" {
return nil, &RequestError{"comparisonValue can only be set when conditionalSet is ONLY_IF_EQUAL"}
}

if opts.ReturnOldValue {
Expand Down Expand Up @@ -127,6 +134,8 @@ const (
OnlyIfExists ConditionalSet = "XX"
// OnlyIfDoesNotExist only sets the key if it does not already exist. Equivalent to "NX" in the valkey API.
OnlyIfDoesNotExist ConditionalSet = "NX"
// OnlyIfEqual only sets the key if the current value is equal to the comparisonValue. Equivalent to "IFEQ" in the valkey API.
OnlyIfEqual ConditionalSet = "IFEQ"
)

type ExpireCondition string
Expand Down
26 changes: 26 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfDoesNotExist_existingKey()
})
}

func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfEQual_CorrectKey() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
suite.verifyOK(client.Set(key, initialValue))
// initialValue == current value of key
opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfEqual).SetComparisonValue(initialValue)
result, err := client.SetWithOptions(key, anotherValue, opts)

assert.Nil(suite.T(), err)
assert.Equal(suite.T(), anotherValue, result.Value())
})
}

func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfEQual_WrongKey() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
suite.verifyOK(client.Set(key, initialValue))
// anotherValue != current value of key
opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfEqual).SetComparisonValue(anotherValue)
result, err := client.SetWithOptions(key, anotherValue, opts)

assert.Nil(suite.T(), err)
assert.Equal(suite.T(), anotherValue, result.Value())
})
}

func (suite *GlideTestSuite) TestSetWithOptions_KeepExistingExpiry() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
Expand Down

0 comments on commit 62b6d2b

Please sign in to comment.