Skip to content

Commit

Permalink
Return ok boolean instead of OK string in embedded api methods that r…
Browse files Browse the repository at this point in the history
…eturn ok status. Updated tests to match new return types
  • Loading branch information
kelvinmwinuka committed May 18, 2024
1 parent 7fb236e commit c634b1b
Show file tree
Hide file tree
Showing 15 changed files with 1,324 additions and 1,270 deletions.
7 changes: 0 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,4 @@ func main() {
<-cancelCh

server.ShutDown()

// TODO: For example purposes only! Delete before PR!
// vault, err := echovault.NewEchoVault()
// if err != nil {
// log.Fatal(err)
// }
// newValue, err := vault.HIncrByFloat("key", "field", 7.75)
}
2,214 changes: 1,109 additions & 1,105 deletions coverage/coverage.out

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions echovault/api_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"github.com/echovault/echovault/internal"
"github.com/tidwall/resp"
"strings"
)

// ACLLoadOptions modifies the behaviour of the ACLLoad function.
Expand Down Expand Up @@ -150,8 +151,8 @@ func (server *EchoVault) ACLUsers() ([]string, error) {
//
// `user` - User - The user object to add/update.
//
// Returns: "OK" if the user is successfully created/updated.
func (server *EchoVault) ACLSetUser(user User) (string, error) {
// Returns: true if the user is successfully created/updated.
func (server *EchoVault) ACLSetUser(user User) (bool, error) {

Check warning on line 155 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L155

Added line #L155 was not covered by tests
cmd := []string{"ACL", "SETUSER", user.Username}

if user.Enabled {
Expand Down Expand Up @@ -238,10 +239,11 @@ func (server *EchoVault) ACLSetUser(user User) (string, error) {

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return false, err

Check warning on line 242 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L242

Added line #L242 was not covered by tests
}

return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err

Check warning on line 246 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L245-L246

Added lines #L245 - L246 were not covered by tests
}

// ACLGetUser gets the ACL configuration of the name with the given username.
Expand Down Expand Up @@ -323,14 +325,15 @@ func (server *EchoVault) ACLGetUser(username string) (map[string][]string, error
//
// `usernames` - ...string - A string of usernames to delete from the ACL module.
//
// Returns: "OK" if the deletion is successful.
func (server *EchoVault) ACLDelUser(usernames ...string) (string, error) {
// Returns: true if the deletion is successful.
func (server *EchoVault) ACLDelUser(usernames ...string) (bool, error) {

Check warning on line 329 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L329

Added line #L329 was not covered by tests
cmd := append([]string{"ACL", "DELUSER"}, usernames...)
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return false, err

Check warning on line 333 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L333

Added line #L333 was not covered by tests
}
return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err

Check warning on line 336 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L335-L336

Added lines #L335 - L336 were not covered by tests
}

// ACLList lists all the currently loaded ACL users and their rules.
Expand All @@ -349,8 +352,8 @@ func (server *EchoVault) ACLList() ([]string, error) {
//
// `options` - ACLLoadOptions - modifies the load behaviour.
//
// Returns: "OK" if the load is successful.
func (server *EchoVault) ACLLoad(options ACLLoadOptions) (string, error) {
// Returns: true if the load is successful.
func (server *EchoVault) ACLLoad(options ACLLoadOptions) (bool, error) {

Check warning on line 356 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L356

Added line #L356 was not covered by tests
cmd := []string{"ACL", "LOAD"}
switch {
case options.Merge:
Expand All @@ -363,19 +366,21 @@ func (server *EchoVault) ACLLoad(options ACLLoadOptions) (string, error) {

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return false, err

Check warning on line 369 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L369

Added line #L369 was not covered by tests
}

return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err

Check warning on line 373 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L372-L373

Added lines #L372 - L373 were not covered by tests
}

// ACLSave saves the current ACL configuration to the configured ACL file.
//
// Returns: "OK" if the save is successful.
func (server *EchoVault) ACLSave() (string, error) {
// Returns: true if the save is successful.
func (server *EchoVault) ACLSave() (bool, error) {

Check warning on line 379 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L379

Added line #L379 was not covered by tests
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"ACL", "SAVE"}), nil, false, true)
if err != nil {
return "", err
return false, err

Check warning on line 382 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L382

Added line #L382 was not covered by tests
}
return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err

Check warning on line 385 in echovault/api_acl.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_acl.go#L384-L385

Added lines #L384 - L385 were not covered by tests
}
2 changes: 1 addition & 1 deletion echovault/api_admin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2024 Kelvin Clement Mwinuka
//
// Licensed under the Apache License, Version 2.0 (the "License");s
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
Expand Down
16 changes: 12 additions & 4 deletions echovault/api_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ type PExpireAtOptions ExpireOptions
//
// `options` - SetOptions.
//
// Returns: "OK" if the set is successful, If the "Get" flag in SetOptions is set to true, the previous value is returned.
// Returns: true if the set is successful, If the "Get" flag in SetOptions is set to true, the previous value is returned.
//
// Errors:
//
// "key <key> does not exist"" - when the XX flag is set to true and the key does not exist.
//
// "key <key> does already exists" - when the NX flag is set to true and the key already exists.
func (server *EchoVault) Set(key, value string, options SetOptions) (string, error) {
func (server *EchoVault) Set(key, value string, options SetOptions) (string, bool, error) {
cmd := []string{"SET", key, value}

switch {
Expand All @@ -112,10 +112,18 @@ func (server *EchoVault) Set(key, value string, options SetOptions) (string, err

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return "", false, err
}

return internal.ParseStringResponse(b)
previousValue, err := internal.ParseStringResponse(b)
if err != nil {
return "", false, err

Check warning on line 120 in echovault/api_generic.go

View check run for this annotation

Codecov / codecov/patch

echovault/api_generic.go#L120

Added line #L120 was not covered by tests
}
if !options.GET {
previousValue = ""
}

return previousValue, true, nil
}

// MSet set multiple values at multiple keys with one command. Existing keys are overwritten and non-existent
Expand Down
69 changes: 42 additions & 27 deletions echovault/api_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ func TestEchoVault_SET(t *testing.T) {
key string
value string
options SetOptions
want string
wantOk bool
wantPrev string
wantErr bool
}{
{
Expand All @@ -618,7 +619,8 @@ func TestEchoVault_SET(t *testing.T) {
key: "key1",
value: "value1",
options: SetOptions{},
want: "OK",
wantOk: true,
wantPrev: "",
wantErr: false,
},
{
Expand All @@ -627,7 +629,8 @@ func TestEchoVault_SET(t *testing.T) {
key: "key2",
value: "value2",
options: SetOptions{NX: true},
want: "OK",
wantOk: true,
wantPrev: "",
wantErr: false,
},
{
Expand All @@ -638,11 +641,12 @@ func TestEchoVault_SET(t *testing.T) {
ExpireAt: time.Time{},
},
},
key: "key3",
value: "value3",
options: SetOptions{NX: true},
want: "",
wantErr: true,
key: "key3",
value: "value3",
options: SetOptions{NX: true},
wantOk: false,
wantPrev: "",
wantErr: true,
},
{
name: "Set new key value when key exists with XX flag passed",
Expand All @@ -652,19 +656,21 @@ func TestEchoVault_SET(t *testing.T) {
ExpireAt: time.Time{},
},
},
key: "key4",
value: "value4",
options: SetOptions{XX: true},
want: "OK",
wantErr: false,
key: "key4",
value: "value4",
options: SetOptions{XX: true},
wantOk: true,
wantPrev: "",
wantErr: false,
},
{
name: "Return error when setting non-existent key with XX flag",
presetValues: nil,
key: "key5",
value: "value5",
options: SetOptions{XX: true},
want: "",
wantOk: false,
wantPrev: "",
wantErr: true,
},
{
Expand All @@ -673,7 +679,8 @@ func TestEchoVault_SET(t *testing.T) {
key: "key6",
value: "value6",
options: SetOptions{EX: 100},
want: "OK",
wantOk: true,
wantPrev: "",
wantErr: false,
},
{
Expand All @@ -682,7 +689,8 @@ func TestEchoVault_SET(t *testing.T) {
key: "key7",
value: "value7",
options: SetOptions{PX: 4096},
want: "OK",
wantOk: true,
wantPrev: "",
wantErr: false,
},
{
Expand All @@ -691,7 +699,8 @@ func TestEchoVault_SET(t *testing.T) {
key: "key8",
value: "value8",
options: SetOptions{EXAT: int(mockClock.Now().Add(200 * time.Second).Unix())},
want: "OK",
wantOk: true,
wantPrev: "",
wantErr: false,
},
{
Expand All @@ -700,7 +709,8 @@ func TestEchoVault_SET(t *testing.T) {
value: "value9",
options: SetOptions{PXAT: int(mockClock.Now().Add(4096 * time.Millisecond).UnixMilli())},
presetValues: nil,
want: "OK",
wantOk: true,
wantPrev: "",
wantErr: false,
},
{
Expand All @@ -711,19 +721,21 @@ func TestEchoVault_SET(t *testing.T) {
ExpireAt: time.Time{},
},
},
key: "key10",
value: "value10",
options: SetOptions{GET: true, EX: 1000},
want: "previous-value",
wantErr: false,
key: "key10",
value: "value10",
options: SetOptions{GET: true, EX: 1000},
wantOk: true,
wantPrev: "previous-value",
wantErr: false,
},
{
name: "Return nil when GET value is passed and no previous value exists",
presetValues: nil,
key: "key11",
value: "value11",
options: SetOptions{GET: true, EX: 1000},
want: "",
wantOk: true,
wantPrev: "",
wantErr: false,
},
}
Expand All @@ -734,13 +746,16 @@ func TestEchoVault_SET(t *testing.T) {
presetKeyData(server, context.Background(), k, d)
}
}
got, err := server.Set(tt.key, tt.value, tt.options)
previousValue, ok, err := server.Set(tt.key, tt.value, tt.options)
if (err != nil) != tt.wantErr {
t.Errorf("SET() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("SET() got = %v, want %v", got, tt.want)
if ok != tt.wantOk {
t.Errorf("SET() ok got = %v, want %v", ok, tt.wantOk)
}
if previousValue != tt.wantPrev {
t.Errorf("SET() previous value got = %v, want %v", previousValue, tt.wantPrev)
}
})
}
Expand Down
Loading

0 comments on commit c634b1b

Please sign in to comment.