Refactor tests (#171)

- Refactored tests to improve execution time - @kelvinmwinuka
This commit is contained in:
Kelvin Mwinuka
2025-01-27 01:42:41 +08:00
committed by GitHub
parent ec69e52a5b
commit 63a4652d9f
12 changed files with 12079 additions and 11853 deletions

View File

@@ -14,12 +14,18 @@ build-modules-test:
test: test:
env RACE=false OUT=internal/modules/admin/testdata make build-modules-test && \ env RACE=false OUT=internal/modules/admin/testdata make build-modules-test && \
env RACE=false OUT=sugardb/testdata make build-modules-test && \ env RACE=false OUT=sugardb/testdata make build-modules-test && \
CGO_ENABLED=1 go test ./... -coverprofile coverage/coverage.out CGO_ENABLED=1 go test ./... -coverprofile coverage/coverage.out && \
rm -rf ./internal/modules/admin/testdata && \
rm -rf ./sugardb/testdata && \
rm -rf ./sugardb/aof
test-race: test-race:
env RACE=true OUT=internal/modules/admin/testdata make build-modules-test && \ env RACE=true OUT=internal/modules/admin/testdata make build-modules-test && \
env RACE=true OUT=sugardb/testdata make build-modules-test && \ env RACE=true OUT=sugardb/testdata make build-modules-test && \
CGO_ENABLED=1 go test ./... --race CGO_ENABLED=1 go test ./... --race && \
rm -rf ./internal/modules/admin/testdata && \
rm -rf ./sugardb/testdata && \
rm -rf ./sugardb/aof
testenv-run: testenv-run:
docker-compose -f test_env/run/docker-compose.yaml build docker-compose -f test_env/run/docker-compose.yaml build

File diff suppressed because it is too large Load Diff

View File

@@ -124,8 +124,14 @@ func generateSHA256Password(plain string) string {
return hex.EncodeToString(h.Sum(nil)) return hex.EncodeToString(h.Sum(nil))
} }
func TestSugarDB_ACLCat(t *testing.T) { func TestSugarDB_ACL(t *testing.T) {
t.Run("TestSugarDB_ACLCat", func(t *testing.T) {
t.Parallel()
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
getCategoryCommands := func(category string) []string { getCategoryCommands := func(category string) []string {
var commands []string var commands []string
@@ -262,10 +268,15 @@ func TestSugarDB_ACLCat(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_ACLUsers", func(t *testing.T) {
t.Parallel()
func TestSugarDB_ACLUsers(t *testing.T) {
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
// Set Users // Set Users
users := []User{ users := []User{
@@ -367,9 +378,11 @@ func TestSugarDB_ACLUsers(t *testing.T) {
if len(list) != 2 { if len(list) != 2 {
t.Errorf("ACLList() got list length %d, want %d", len(list), 2) t.Errorf("ACLList() got list length %d, want %d", len(list), 2)
} }
} })
t.Run("TestSugarDB_ACLConfig", func(t *testing.T) {
t.Parallel()
func TestSugarDB_ACLConfig(t *testing.T) {
t.Run("Test_HandleSave", func(t *testing.T) { t.Run("Test_HandleSave", func(t *testing.T) {
baseDir := path.Join(".", "testdata", "save") baseDir := path.Join(".", "testdata", "save")
t.Cleanup(func() { t.Cleanup(func() {
@@ -418,11 +431,15 @@ func TestSugarDB_ACLConfig(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
t.Parallel()
// Create new server instance // Create new server instance
conf := DefaultConfig() conf := DefaultConfig()
conf.DataDir = "" conf.DataDir = ""
conf.AclConfig = test.path conf.AclConfig = test.path
server := createSugarDBWithConfig(conf) server := createSugarDBWithConfig(conf)
// Add the initial test users to the ACL module. // Add the initial test users to the ACL module.
for _, user := range generateInitialTestUsers() { for _, user := range generateInitialTestUsers() {
if _, err := server.ACLSetUser(user); err != nil { if _, err := server.ACLSetUser(user); err != nil {
@@ -596,11 +613,15 @@ func TestSugarDB_ACLConfig(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
t.Parallel()
// Create server. // Create server.
conf := DefaultConfig() conf := DefaultConfig()
conf.DataDir = "" conf.DataDir = ""
conf.AclConfig = test.path conf.AclConfig = test.path
server := createSugarDBWithConfig(conf) server := createSugarDBWithConfig(conf)
// Add the initial test users to the ACL module. // Add the initial test users to the ACL module.
for _, user := range generateInitialTestUsers() { for _, user := range generateInitialTestUsers() {
if _, err := server.ACLSetUser(user); err != nil { if _, err := server.ACLSetUser(user); err != nil {
@@ -615,8 +636,9 @@ func TestSugarDB_ACLConfig(t *testing.T) {
return return
} }
ticker := time.NewTicker(200 * time.Millisecond) ticker := time.NewTicker(100 * time.Millisecond)
<-ticker.C <-ticker.C
ticker.Stop()
// Add some users to the ACL. // Add some users to the ACL.
for _, user := range test.users { for _, user := range test.users {
@@ -659,4 +681,5 @@ func TestSugarDB_ACLConfig(t *testing.T) {
}) })
} }
}) })
})
} }

View File

@@ -31,7 +31,10 @@ import (
"time" "time"
) )
func TestSugarDB_AddCommand(t *testing.T) { func TestSugarDB_Admin(t *testing.T) {
t.Run("TestSugarDB_AddCommand", func(t *testing.T) {
t.Parallel()
type args struct { type args struct {
command CommandOptions command CommandOptions
} }
@@ -176,8 +179,14 @@ The value passed must be an integer.`,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
server := createSugarDB()
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
if err := server.AddCommand(tt.args.command); (err != nil) != tt.wantErr { if err := server.AddCommand(tt.args.command); (err != nil) != tt.wantErr {
t.Errorf("AddCommand() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("AddCommand() error = %v, wantErr %v", err, tt.wantErr)
} }
@@ -197,9 +206,11 @@ The value passed must be an integer.`,
} }
}) })
} }
} })
t.Run("TestSugarDB_ExecuteCommand", func(t *testing.T) {
t.Parallel()
func TestSugarDB_ExecuteCommand(t *testing.T) {
type args struct { type args struct {
key string key string
presetValue []string presetValue []string
@@ -233,8 +244,13 @@ func TestSugarDB_ExecuteCommand(t *testing.T) {
}, },
} }
for _, tt := range tests { for _, tt := range tests {
server := createSugarDB()
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
if tt.args.presetValue != nil { if tt.args.presetValue != nil {
_, _ = server.LPush(tt.args.key, tt.args.presetValue...) _, _ = server.LPush(tt.args.key, tt.args.presetValue...)
} }
@@ -251,9 +267,11 @@ func TestSugarDB_ExecuteCommand(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_RemoveCommand", func(t *testing.T) {
t.Parallel()
func TestSugarDB_RemoveCommand(t *testing.T) {
type args struct { type args struct {
removeCommand []string removeCommand []string
executeCommand []string executeCommand []string
@@ -289,8 +307,14 @@ func TestSugarDB_RemoveCommand(t *testing.T) {
}, },
} }
for _, tt := range tests { for _, tt := range tests {
server := createSugarDB()
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
server.RemoveCommand(tt.args.removeCommand...) server.RemoveCommand(tt.args.removeCommand...)
_, err := server.ExecuteCommand(tt.args.executeCommand...) _, err := server.ExecuteCommand(tt.args.executeCommand...)
if tt.wantErr != nil { if tt.wantErr != nil {
@@ -300,14 +324,19 @@ func TestSugarDB_RemoveCommand(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_Plugins", func(t *testing.T) {
t.Parallel()
func TestSugarDB_Plugins(t *testing.T) {
t.Cleanup(func() { t.Cleanup(func() {
_ = os.RemoveAll("./testdata/modules") _ = os.RemoveAll("./testdata/modules")
}) })
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
tests := []struct { tests := []struct {
name string name string
@@ -490,10 +519,15 @@ func TestSugarDB_Plugins(t *testing.T) {
t.Errorf("expected modules list to not contain module \"%s\" but found it", test.path) t.Errorf("expected modules list to not contain module \"%s\" but found it", test.path)
} }
} }
} })
t.Run("TestSugarDB_CommandList", func(t *testing.T) {
t.Parallel()
func TestSugarDB_CommandList(t *testing.T) {
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
tests := []struct { tests := []struct {
name string name string
@@ -564,6 +598,7 @@ func TestSugarDB_CommandList(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var got []string var got []string
var err error var err error
if tt.options == nil { if tt.options == nil {
@@ -580,10 +615,15 @@ func TestSugarDB_CommandList(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_CommandCount", func(t *testing.T) {
t.Parallel()
func TestSugarDB_CommandCount(t *testing.T) {
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
tests := []struct { tests := []struct {
name string name string
@@ -610,6 +650,7 @@ func TestSugarDB_CommandCount(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := server.CommandCount() got, err := server.CommandCount()
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("CommandCount() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CommandCount() error = %v, wantErr %v", err, tt.wantErr)
@@ -620,13 +661,19 @@ func TestSugarDB_CommandCount(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_Save", func(t *testing.T) {
t.Parallel()
func TestSugarDB_Save(t *testing.T) {
conf := DefaultConfig() conf := DefaultConfig()
conf.DataDir = path.Join(".", "testdata", "data") conf.DataDir = path.Join(".", "testdata", "data")
conf.EvictionPolicy = constants.NoEviction conf.EvictionPolicy = constants.NoEviction
server := createSugarDBWithConfig(conf) server := createSugarDBWithConfig(conf)
t.Cleanup(func() {
server.ShutDown()
})
tests := []struct { tests := []struct {
name string name string
@@ -651,11 +698,16 @@ func TestSugarDB_Save(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_LastSave", func(t *testing.T) {
t.Parallel()
func TestSugarDB_LastSave(t *testing.T) {
server := createSugarDB() server := createSugarDB()
server.setLatestSnapshot(clock.NewClock().Now().Add(5 * time.Minute).UnixMilli()) server.setLatestSnapshot(clock.NewClock().Now().Add(5 * time.Minute).UnixMilli())
t.Cleanup(func() {
server.ShutDown()
})
tests := []struct { tests := []struct {
name string name string
@@ -680,4 +732,5 @@ func TestSugarDB_LastSave(t *testing.T) {
} }
}) })
} }
})
} }

View File

@@ -25,7 +25,8 @@ import (
"testing" "testing"
) )
func TestSugarDB_Hello(t *testing.T) { func TestSugarDB_Connection(t *testing.T) {
t.Run("TestSugarDB_Hello", func(t *testing.T) {
t.Parallel() t.Parallel()
port, err := internal.GetFreePort() port, err := internal.GetFreePort()
@@ -145,9 +146,9 @@ func TestSugarDB_Hello(t *testing.T) {
// Close connection // Close connection
_ = conn.Close() _ = conn.Close()
} }
} })
func TestSugarDB_SelectDB(t *testing.T) { t.Run("TestSugarDB_SelectDB", func(t *testing.T) {
t.Parallel() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -184,7 +185,11 @@ func TestSugarDB_SelectDB(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel() t.Parallel()
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
if tt.presetValues != nil { if tt.presetValues != nil {
for db, data := range tt.presetValues { for db, data := range tt.presetValues {
@@ -235,11 +240,16 @@ func TestSugarDB_SelectDB(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_SetProtocol(t *testing.T) { t.Run("TestSugarDB_SetProtocol", func(t *testing.T) {
t.Parallel() t.Parallel()
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
tests := []struct { tests := []struct {
name string name string
protocol int protocol int
@@ -282,4 +292,5 @@ func TestSugarDB_SetProtocol(t *testing.T) {
} }
}) })
} }
})
} }

File diff suppressed because it is too large Load Diff

View File

@@ -16,16 +16,21 @@ package sugardb
import ( import (
"context" "context"
"github.com/echovault/sugardb/internal/modules/hash"
"reflect" "reflect"
"slices" "slices"
"testing" "testing"
"time" "time"
"github.com/echovault/sugardb/internal/modules/hash"
) )
func TestSugarDB_HDEL(t *testing.T) { func TestSugarDB_Hash(t *testing.T) {
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
t.Run("TestSugarDB_HDEL", func(t *testing.T) {
t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -36,8 +41,8 @@ func TestSugarDB_HDEL(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Return count of deleted fields in the specified hash", name: "1. Return count of deleted fields in the specified hash",
key: "key1", key: "hdel_key1",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
@@ -49,8 +54,8 @@ func TestSugarDB_HDEL(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "0 response when passing delete fields that are non-existent on valid hash", name: "2. 0 response when passing delete fields that are non-existent on valid hash",
key: "key2", key: "hdel_key2",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: "value2"}, "field2": {Value: "value2"},
@@ -61,17 +66,17 @@ func TestSugarDB_HDEL(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "0 response when trying to call HDEL on non-existent key", name: "3. 0 response when trying to call HDEL on non-existent key",
key: "key3", key: "hdel_key3",
presetValue: nil, presetValue: nil,
fields: []string{"field1"}, fields: []string{"field1"},
want: 0, want: 0,
wantErr: false, wantErr: false,
}, },
{ {
name: "Trying to get lengths on a non hash map returns error", name: "4. Trying to get lengths on a non hash map returns error",
presetValue: "Default value", presetValue: "Default value",
key: "key5", key: "hdel_key5",
fields: []string{"field1"}, fields: []string{"field1"},
want: 0, want: 0,
wantErr: true, wantErr: true,
@@ -79,6 +84,7 @@ func TestSugarDB_HDEL(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -96,10 +102,10 @@ func TestSugarDB_HDEL(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HEXISTS(t *testing.T) { t.Run("TestSugarDB_HEXISTS", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -110,29 +116,29 @@ func TestSugarDB_HEXISTS(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Return 1 if the field exists in the hash", name: "1. Return 1 if the field exists in the hash",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
"field3": {Value: 3.142}, "field3": {Value: 3.142},
}, },
key: "key1", key: "hexists_key1",
field: "field1", field: "field1",
want: true, want: true,
wantErr: false, wantErr: false,
}, },
{ {
name: "False response when trying to call HEXISTS on non-existent key", name: "2. False response when trying to call HEXISTS on non-existent key",
presetValue: hash.Hash{}, presetValue: hash.Hash{},
key: "key2", key: "hexists_key2",
field: "field1", field: "field1",
want: false, want: false,
wantErr: false, wantErr: false,
}, },
{ {
name: "Trying to get lengths on a non hash map returns error", name: "3. Trying to get lengths on a non hash map returns error",
presetValue: "Default value", presetValue: "Default value",
key: "key5", key: "hexists_key5",
field: "field1", field: "field1",
want: false, want: false,
wantErr: true, wantErr: true,
@@ -140,6 +146,7 @@ func TestSugarDB_HEXISTS(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -157,10 +164,10 @@ func TestSugarDB_HEXISTS(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HGETALL(t *testing.T) { t.Run("TestSugarDB_HGETALL", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -170,8 +177,8 @@ func TestSugarDB_HGETALL(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Return an array containing all the fields and values of the hash", name: "1. Return an array containing all the fields and values of the hash",
key: "key1", key: "hgetall_key1",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
@@ -181,15 +188,15 @@ func TestSugarDB_HGETALL(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Empty array response when trying to call HGETALL on non-existent key", name: "2. Empty array response when trying to call HGETALL on non-existent key",
key: "key2", key: "hgetall_key2",
presetValue: hash.Hash{}, presetValue: hash.Hash{},
want: []string{}, want: []string{},
wantErr: false, wantErr: false,
}, },
{ {
name: "Trying to get lengths on a non hash map returns error", name: "3. Trying to get lengths on a non hash map returns error",
key: "key5", key: "hgetall_key5",
presetValue: "Default value", presetValue: "Default value",
want: nil, want: nil,
wantErr: true, wantErr: true,
@@ -197,6 +204,7 @@ func TestSugarDB_HGETALL(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -221,10 +229,10 @@ func TestSugarDB_HGETALL(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HINCRBY(t *testing.T) { t.Run("TestSugarDB_HINCRBY", func(t *testing.T) {
server := createSugarDB() t.Parallel()
const ( const (
HINCRBY = "HINCRBY" HINCRBY = "HINCRBY"
@@ -243,60 +251,60 @@ func TestSugarDB_HINCRBY(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Increment by integer on non-existent hash should create a new one", name: "1. Increment by integer on non-existent hash should create a new one",
presetValue: nil, presetValue: nil,
incr_type: HINCRBY, incr_type: HINCRBY,
key: "key1", key: "hincrby_key1",
field: "field1", field: "field1",
increment_int: 1, increment_int: 1,
want: 1, want: 1,
wantErr: false, wantErr: false,
}, },
{ {
name: "Increment by float on non-existent hash should create one", name: "2. Increment by float on non-existent hash should create one",
presetValue: nil, presetValue: nil,
incr_type: HINCRBYFLOAT, incr_type: HINCRBYFLOAT,
key: "key2", key: "hincrby_key2",
field: "field1", field: "field1",
increment_float: 3.142, increment_float: 3.142,
want: 3.142, want: 3.142,
wantErr: false, wantErr: false,
}, },
{ {
name: "Increment by integer on existing hash", name: "3. Increment by integer on existing hash",
presetValue: hash.Hash{"field1": {Value: 1}}, presetValue: hash.Hash{"field1": {Value: 1}},
incr_type: HINCRBY, incr_type: HINCRBY,
key: "key3", key: "hincrby_key3",
field: "field1", field: "field1",
increment_int: 10, increment_int: 10,
want: 11, want: 11,
wantErr: false, wantErr: false,
}, },
{ {
name: "Increment by float on an existing hash", name: "4. Increment by float on an existing hash",
presetValue: hash.Hash{"field1": {Value: 3.142}}, presetValue: hash.Hash{"field1": {Value: 3.142}},
incr_type: HINCRBYFLOAT, incr_type: HINCRBYFLOAT,
key: "key4", key: "hincrby_key4",
field: "field1", field: "field1",
increment_float: 3.142, increment_float: 3.142,
want: 6.284, want: 6.284,
wantErr: false, wantErr: false,
}, },
{ {
name: "Error when trying to increment on a key that is not a hash", name: "5. Error when trying to increment on a key that is not a hash",
presetValue: "Default value", presetValue: "Default value",
incr_type: HINCRBY, incr_type: HINCRBY,
key: "key9", key: "hincrby_key9",
field: "field1", field: "field1",
increment_int: 3, increment_int: 3,
want: 0, want: 0,
wantErr: true, wantErr: true,
}, },
{ {
name: "Error when trying to increment a hash field that is not a number", name: "6. Error when trying to increment a hash field that is not a number",
presetValue: hash.Hash{"field1": {Value: "value1"}}, presetValue: hash.Hash{"field1": {Value: "value1"}},
incr_type: HINCRBY, incr_type: HINCRBY,
key: "key10", key: "hincrby_key10",
field: "field1", field: "field1",
increment_int: 1, increment_int: 1,
want: 0, want: 0,
@@ -305,6 +313,7 @@ func TestSugarDB_HINCRBY(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -333,10 +342,10 @@ func TestSugarDB_HINCRBY(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HKEYS(t *testing.T) { t.Run("TestSugarDB_HKEYS", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -346,33 +355,34 @@ func TestSugarDB_HKEYS(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Return an array containing all the keys of the hash", name: "1. Return an array containing all the keys of the hash",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
"field3": {Value: 3.142}, "field3": {Value: 3.142},
}, },
key: "key1", key: "hkeys_key1",
want: []string{"field1", "field2", "field3"}, want: []string{"field1", "field2", "field3"},
wantErr: false, wantErr: false,
}, },
{ {
name: "Empty array response when trying to call HKEYS on non-existent key", name: "2. Empty array response when trying to call HKEYS on non-existent key",
presetValue: hash.Hash{}, presetValue: hash.Hash{},
key: "key2", key: "hkeys_key2",
want: []string{}, want: []string{},
wantErr: false, wantErr: false,
}, },
{ {
name: "Trying to get lengths on a non hash map returns error", name: "3. Trying to get lengths on a non hash map returns error",
presetValue: "Default value", presetValue: "Default value",
key: "key3", key: "hkeys_key3",
want: nil, want: nil,
wantErr: true, wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -395,10 +405,10 @@ func TestSugarDB_HKEYS(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HLEN(t *testing.T) { t.Run("TestSugarDB_HLEN", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -408,33 +418,34 @@ func TestSugarDB_HLEN(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Return the correct length of the hash", name: "1. Return the correct length of the hash",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
"field3": {Value: 3.142}, "field3": {Value: 3.142},
}, },
key: "key1", key: "hlen_key1",
want: 3, want: 3,
wantErr: false, wantErr: false,
}, },
{ {
name: "0 Response when trying to call HLEN on non-existent key", name: "2. 0 Response when trying to call HLEN on non-existent key",
presetValue: nil, presetValue: nil,
key: "key2", key: "hlen_key2",
want: 0, want: 0,
wantErr: false, wantErr: false,
}, },
{ {
name: "Trying to get lengths on a non hash map returns error", name: "3. Trying to get lengths on a non hash map returns error",
presetValue: "Default value", presetValue: "Default value",
key: "key5", key: "hlen_key5",
want: 0, want: 0,
wantErr: true, wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -452,10 +463,10 @@ func TestSugarDB_HLEN(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HRANDFIELD(t *testing.T) { t.Run("TestSugarDB_HRANDFIELD", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -467,33 +478,33 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Get a random field", name: "1. Get a random field",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
"field3": {Value: 3.142}, "field3": {Value: 3.142},
}, },
key: "key1", key: "hrandfield_key1",
options: HRandFieldOptions{Count: 1}, options: HRandFieldOptions{Count: 1},
wantCount: 1, wantCount: 1,
want: []string{"field1", "field2", "field3"}, want: []string{"field1", "field2", "field3"},
wantErr: false, wantErr: false,
}, },
{ {
name: "Get a random field with a value", name: "2. Get a random field with a value",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
"field3": {Value: 3.142}, "field3": {Value: 3.142},
}, },
key: "key2", key: "hrandfield_key2",
options: HRandFieldOptions{WithValues: true, Count: 1}, options: HRandFieldOptions{WithValues: true, Count: 1},
wantCount: 2, wantCount: 2,
want: []string{"field1", "value1", "field2", "123456789", "field3", "3.142"}, want: []string{"field1", "value1", "field2", "123456789", "field3", "3.142"},
wantErr: false, wantErr: false,
}, },
{ {
name: "Get several random fields", name: "3. Get several random fields",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
@@ -501,14 +512,14 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
"field4": {Value: "value4"}, "field4": {Value: "value4"},
"field5": {Value: "value6"}, "field5": {Value: "value6"},
}, },
key: "key3", key: "hrandfield_key3",
options: HRandFieldOptions{Count: 3}, options: HRandFieldOptions{Count: 3},
wantCount: 3, wantCount: 3,
want: []string{"field1", "field2", "field3", "field4", "field5"}, want: []string{"field1", "field2", "field3", "field4", "field5"},
wantErr: false, wantErr: false,
}, },
{ {
name: "Get several random fields with their corresponding values", name: "4. Get several random fields with their corresponding values",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
@@ -516,7 +527,7 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
"field4": {Value: "value4"}, "field4": {Value: "value4"},
"field5": {Value: "value5"}, "field5": {Value: "value5"},
}, },
key: "key4", key: "hrandfield_key4",
options: HRandFieldOptions{WithValues: true, Count: 3}, options: HRandFieldOptions{WithValues: true, Count: 3},
wantCount: 6, wantCount: 6,
want: []string{ want: []string{
@@ -526,7 +537,7 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Get the entire hash", name: "5. Get the entire hash",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
@@ -534,14 +545,14 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
"field4": {Value: "value4"}, "field4": {Value: "value4"},
"field5": {Value: "value5"}, "field5": {Value: "value5"},
}, },
key: "key5", key: "hrandfield_key5",
options: HRandFieldOptions{Count: 5}, options: HRandFieldOptions{Count: 5},
wantCount: 5, wantCount: 5,
want: []string{"field1", "field2", "field3", "field4", "field5"}, want: []string{"field1", "field2", "field3", "field4", "field5"},
wantErr: false, wantErr: false,
}, },
{ {
name: "Get the entire hash with values", name: "6. Get the entire hash with values",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
@@ -549,7 +560,7 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
"field4": {Value: "value4"}, "field4": {Value: "value4"},
"field5": {Value: "value5"}, "field5": {Value: "value5"},
}, },
key: "key5", key: "hrandfield_key6",
options: HRandFieldOptions{WithValues: true, Count: 5}, options: HRandFieldOptions{WithValues: true, Count: 5},
wantCount: 10, wantCount: 10,
want: []string{ want: []string{
@@ -559,9 +570,9 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Trying to get random field on a non hash map returns error", name: "7. Trying to get random field on a non hash map returns error",
presetValue: "Default value", presetValue: "Default value",
key: "key12", key: "hrandfield_key7",
options: HRandFieldOptions{}, options: HRandFieldOptions{},
wantCount: 0, wantCount: 0,
want: nil, want: nil,
@@ -570,6 +581,7 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -592,10 +604,10 @@ func TestSugarDB_HRANDFIELD(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HSET(t *testing.T) { t.Run("TestSugarDB_HSET", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -607,8 +619,8 @@ func TestSugarDB_HSET(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "HSETNX set field on non-existent hash map", name: "1. HSETNX set field on non-existent hash map",
key: "key1", key: "hset_key1",
presetValue: nil, presetValue: nil,
hsetFunc: server.HSetNX, hsetFunc: server.HSetNX,
fieldValuePairs: map[string]string{"field1": "value1"}, fieldValuePairs: map[string]string{"field1": "value1"},
@@ -616,8 +628,8 @@ func TestSugarDB_HSET(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "HSETNX set field on existing hash map", name: "2. HSETNX set field on existing hash map",
key: "key2", key: "hset_key2",
presetValue: hash.Hash{"field1": {Value: "value1"}}, presetValue: hash.Hash{"field1": {Value: "value1"}},
hsetFunc: server.HSetNX, hsetFunc: server.HSetNX,
fieldValuePairs: map[string]string{"field2": "value2"}, fieldValuePairs: map[string]string{"field2": "value2"},
@@ -625,8 +637,8 @@ func TestSugarDB_HSET(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "HSETNX skips operation when setting on existing field", name: "3. HSETNX skips operation when setting on existing field",
key: "key3", key: "hset_key3",
presetValue: hash.Hash{"field1": {Value: "value1"}}, presetValue: hash.Hash{"field1": {Value: "value1"}},
hsetFunc: server.HSetNX, hsetFunc: server.HSetNX,
fieldValuePairs: map[string]string{"field1": "value1"}, fieldValuePairs: map[string]string{"field1": "value1"},
@@ -634,8 +646,8 @@ func TestSugarDB_HSET(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Regular HSET command on non-existent hash map", name: "4. Regular HSET command on non-existent hash map",
key: "key4", key: "hset_key4",
presetValue: nil, presetValue: nil,
fieldValuePairs: map[string]string{"field1": "value1", "field2": "value2"}, fieldValuePairs: map[string]string{"field1": "value1", "field2": "value2"},
hsetFunc: server.HSet, hsetFunc: server.HSet,
@@ -643,8 +655,8 @@ func TestSugarDB_HSET(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Regular HSET update on existing hash map", name: "5. Regular HSET update on existing hash map",
key: "key5", key: "hset_key5",
presetValue: hash.Hash{"field1": {Value: "value1"}, "field2": {Value: "value2"}}, presetValue: hash.Hash{"field1": {Value: "value1"}, "field2": {Value: "value2"}},
fieldValuePairs: map[string]string{"field1": "value1-new", "field2": "value2-ne2", "field3": "value3"}, fieldValuePairs: map[string]string{"field1": "value1-new", "field2": "value2-ne2", "field3": "value3"},
hsetFunc: server.HSet, hsetFunc: server.HSet,
@@ -652,8 +664,8 @@ func TestSugarDB_HSET(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "HSET overwrites when the target key is not a map", name: "6. HSET overwrites when the target key is not a map",
key: "key6", key: "hset_key6",
presetValue: "Default preset value", presetValue: "Default preset value",
fieldValuePairs: map[string]string{"field1": "value1"}, fieldValuePairs: map[string]string{"field1": "value1"},
hsetFunc: server.HSet, hsetFunc: server.HSet,
@@ -663,6 +675,7 @@ func TestSugarDB_HSET(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -680,10 +693,10 @@ func TestSugarDB_HSET(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HSTRLEN(t *testing.T) { t.Run("TestSugarDB_HSTRLEN", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -702,7 +715,7 @@ func TestSugarDB_HSTRLEN(t *testing.T) {
"field2": {Value: 123456789}, "field2": {Value: 123456789},
"field3": {Value: 3.142}, "field3": {Value: 3.142},
}, },
key: "key1", key: "hstrlen_key1",
fields: []string{"field1", "field2", "field3", "field4"}, fields: []string{"field1", "field2", "field3", "field4"},
want: []int{len("value1"), len("123456789"), len("3.142"), 0}, want: []int{len("value1"), len("123456789"), len("3.142"), 0},
wantErr: false, wantErr: false,
@@ -710,14 +723,14 @@ func TestSugarDB_HSTRLEN(t *testing.T) {
{ {
name: "2. Response when trying to get HSTRLEN non-existent key", name: "2. Response when trying to get HSTRLEN non-existent key",
presetValue: hash.Hash{}, presetValue: hash.Hash{},
key: "key2", key: "hstrlen_key2",
fields: []string{"field1"}, fields: []string{"field1"},
want: []int{0}, want: []int{0},
wantErr: false, wantErr: false,
}, },
{ {
name: "3. Command too short", name: "3. Command too short",
key: "key3", key: "hstrlen_key3",
presetValue: hash.Hash{}, presetValue: hash.Hash{},
fields: []string{}, fields: []string{},
want: nil, want: nil,
@@ -725,7 +738,7 @@ func TestSugarDB_HSTRLEN(t *testing.T) {
}, },
{ {
name: "4. Trying to get lengths on a non hash map returns error", name: "4. Trying to get lengths on a non hash map returns error",
key: "key4", key: "hstrlen_key4",
presetValue: "Default value", presetValue: "Default value",
fields: []string{"field1"}, fields: []string{"field1"},
want: nil, want: nil,
@@ -734,6 +747,7 @@ func TestSugarDB_HSTRLEN(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
t.Log(tt.name) t.Log(tt.name)
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
@@ -752,10 +766,10 @@ func TestSugarDB_HSTRLEN(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_HVALS(t *testing.T) { t.Run("TestSugarDB_HVALS", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -765,8 +779,8 @@ func TestSugarDB_HVALS(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{ {
name: "Return all the values from a hash", name: "1. Return all the values from a hash",
key: "key1", key: "hvals_key1",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 123456789}, "field2": {Value: 123456789},
@@ -776,15 +790,15 @@ func TestSugarDB_HVALS(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Empty array response when trying to get HSTRLEN non-existent key", name: "2. Empty array response when trying to get HSTRLEN non-existent key",
key: "key2", key: "hvals_key2",
presetValue: nil, presetValue: nil,
want: []string{}, want: []string{},
wantErr: false, wantErr: false,
}, },
{ {
name: "Trying to get lengths on a non hash map returns error", name: "3. Trying to get lengths on a non hash map returns error",
key: "key5", key: "hvals_key5",
presetValue: "Default value", presetValue: "Default value",
want: nil, want: nil,
wantErr: true, wantErr: true,
@@ -792,6 +806,7 @@ func TestSugarDB_HVALS(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -814,10 +829,11 @@ func TestSugarDB_HVALS(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_HGet", func(t *testing.T) {
t.Parallel()
func TestSugarDB_HGet(t *testing.T) {
server := createSugarDB()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -857,6 +873,7 @@ func TestSugarDB_HGet(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -874,10 +891,11 @@ func TestSugarDB_HGet(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_HMGet", func(t *testing.T) {
t.Parallel()
func TestSugarDB_HMGet(t *testing.T) {
server := createSugarDB()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -888,7 +906,7 @@ func TestSugarDB_HMGet(t *testing.T) {
}{ }{
{ {
name: "1. Get values from existing hash.", name: "1. Get values from existing hash.",
key: "HgetKey1", key: "HMgetKey1",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 365}, "field2": {Value: 365},
@@ -901,7 +919,7 @@ func TestSugarDB_HMGet(t *testing.T) {
{ {
name: "2. Return empty slice when attempting to get from non-existed key", name: "2. Return empty slice when attempting to get from non-existed key",
presetValue: nil, presetValue: nil,
key: "HgetKey2", key: "HMgetKey2",
fields: []string{"field1"}, fields: []string{"field1"},
want: []string{}, want: []string{},
wantErr: false, wantErr: false,
@@ -909,7 +927,7 @@ func TestSugarDB_HMGet(t *testing.T) {
{ {
name: "3. Error when trying to get from a value that is not a hash map", name: "3. Error when trying to get from a value that is not a hash map",
presetValue: "Default Value", presetValue: "Default Value",
key: "HgetKey3", key: "HMgetKey3",
fields: []string{"field1"}, fields: []string{"field1"},
want: nil, want: nil,
wantErr: true, wantErr: true,
@@ -917,6 +935,7 @@ func TestSugarDB_HMGet(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -934,10 +953,11 @@ func TestSugarDB_HMGet(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_HExpire", func(t *testing.T) {
t.Parallel()
func TestSugarDB_HExpire(t *testing.T) {
server := createSugarDB()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -1030,6 +1050,7 @@ func TestSugarDB_HExpire(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -1047,10 +1068,11 @@ func TestSugarDB_HExpire(t *testing.T) {
} }
}) })
} }
} })
t.Run("TestSugarDB_HTTL", func(t *testing.T) {
t.Parallel()
func TestSugarDB_HTTL(t *testing.T) {
server := createSugarDB()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -1061,7 +1083,7 @@ func TestSugarDB_HTTL(t *testing.T) {
}{ }{
{ {
name: "1. Get TTL for one field when expireTime is set.", name: "1. Get TTL for one field when expireTime is set.",
key: "HExpireKey1", key: "HTTL_Key1",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1", ExpireAt: server.clock.Now().Add(time.Duration(500) * time.Second)}, "field1": {Value: "value1", ExpireAt: server.clock.Now().Add(time.Duration(500) * time.Second)},
}, },
@@ -1076,7 +1098,7 @@ func TestSugarDB_HTTL(t *testing.T) {
"field2": {Value: "value2", ExpireAt: server.clock.Now().Add(time.Duration(500) * time.Second)}, "field2": {Value: "value2", ExpireAt: server.clock.Now().Add(time.Duration(500) * time.Second)},
"field3": {Value: "value3", ExpireAt: server.clock.Now().Add(time.Duration(500) * time.Second)}, "field3": {Value: "value3", ExpireAt: server.clock.Now().Add(time.Duration(500) * time.Second)},
}, },
key: "HExpireKey2", key: "HTTL_Key2",
fields: []string{"field1", "field2", "field3"}, fields: []string{"field1", "field2", "field3"},
want: []int{500, 500, 500}, want: []int{500, 500, 500},
wantErr: false, wantErr: false,
@@ -1086,14 +1108,14 @@ func TestSugarDB_HTTL(t *testing.T) {
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
}, },
key: "HExpireKey3", key: "HTTL_Key3",
fields: []string{"field1"}, fields: []string{"field1"},
want: []int{-1}, want: []int{-1},
wantErr: false, wantErr: false,
}, },
{ {
name: "4. Get TTL for multiple fields when expireTime is not set.", name: "4. Get TTL for multiple fields when expireTime is not set.",
key: "HExpireKey4", key: "HTTL_Key4",
presetValue: hash.Hash{ presetValue: hash.Hash{
"field1": {Value: "value1"}, "field1": {Value: "value1"},
"field2": {Value: 365}, "field2": {Value: 365},
@@ -1105,7 +1127,7 @@ func TestSugarDB_HTTL(t *testing.T) {
}, },
{ {
name: "5. Try to get TTL for key that doesn't exist.", name: "5. Try to get TTL for key that doesn't exist.",
key: "HExpireKey5", key: "HTTL_Key5",
presetValue: nil, presetValue: nil,
fields: []string{"field1"}, fields: []string{"field1"},
want: []int{-2}, want: []int{-2},
@@ -1113,7 +1135,7 @@ func TestSugarDB_HTTL(t *testing.T) {
}, },
{ {
name: "6. Try to get TTL for key that isn't a hash.", name: "6. Try to get TTL for key that isn't a hash.",
key: "HExpireKey6", key: "HTTL_Key6",
presetValue: "not a hash", presetValue: "not a hash",
fields: []string{"field1", "field2", "field3"}, fields: []string{"field1", "field2", "field3"},
want: nil, want: nil,
@@ -1122,6 +1144,7 @@ func TestSugarDB_HTTL(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -1139,4 +1162,5 @@ func TestSugarDB_HTTL(t *testing.T) {
} }
}) })
} }
})
} }

View File

@@ -20,8 +20,14 @@ import (
"testing" "testing"
) )
func TestSugarDB_LLEN(t *testing.T) { func TestSugarDB_List(t *testing.T) {
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
t.Run("TestSugarDB_LLEN", func(t *testing.T) {
t.Parallel()
tests := []struct { tests := []struct {
preset bool preset bool
@@ -34,7 +40,7 @@ func TestSugarDB_LLEN(t *testing.T) {
{ {
name: "1. If key exists and is a list, return the lists length", name: "1. If key exists and is a list, return the lists length",
preset: true, preset: true,
key: "key1", key: "llen_key1",
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
want: 4, want: 4,
wantErr: false, wantErr: false,
@@ -42,14 +48,14 @@ func TestSugarDB_LLEN(t *testing.T) {
{ {
name: "2. If key does not exist, return 0", name: "2. If key does not exist, return 0",
preset: false, preset: false,
key: "key2", key: "llen_key2",
presetValue: nil, presetValue: nil,
want: 0, want: 0,
wantErr: false, wantErr: false,
}, },
{ {
preset: true, preset: true,
key: "key5", key: "llen_key5",
name: "3. Trying to get lengths on a non-list returns error", name: "3. Trying to get lengths on a non-list returns error",
presetValue: "Default value", presetValue: "Default value",
want: 0, want: 0,
@@ -58,6 +64,7 @@ func TestSugarDB_LLEN(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -75,10 +82,10 @@ func TestSugarDB_LLEN(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_LINDEX(t *testing.T) { t.Run("TestSugarDB_LINDEX", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
preset bool preset bool
@@ -93,7 +100,7 @@ func TestSugarDB_LINDEX(t *testing.T) {
name: "1. Return last element within range", name: "1. Return last element within range",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key1", key: "lindex_key1",
index: 3, index: 3,
want: "value4", want: "value4",
wantErr: false, wantErr: false,
@@ -102,7 +109,7 @@ func TestSugarDB_LINDEX(t *testing.T) {
name: "2. Return first element within range", name: "2. Return first element within range",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key2", key: "lindex_key2",
index: 0, index: 0,
want: "value1", want: "value1",
wantErr: false, wantErr: false,
@@ -111,7 +118,7 @@ func TestSugarDB_LINDEX(t *testing.T) {
name: "3. Return middle element within range", name: "3. Return middle element within range",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key3", key: "lindex_key3",
index: 1, index: 1,
want: "value2", want: "value2",
wantErr: false, wantErr: false,
@@ -120,7 +127,7 @@ func TestSugarDB_LINDEX(t *testing.T) {
name: "4. If key does not exist, return error", name: "4. If key does not exist, return error",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key4", key: "lindex_key4",
index: 0, index: 0,
want: "", want: "",
wantErr: false, wantErr: false,
@@ -129,7 +136,7 @@ func TestSugarDB_LINDEX(t *testing.T) {
name: "5. Trying to get element by index on a non-list returns error", name: "5. Trying to get element by index on a non-list returns error",
preset: true, preset: true,
presetValue: "Default value", presetValue: "Default value",
key: "key5", key: "lindex_key5",
index: 0, index: 0,
want: "", want: "",
wantErr: true, wantErr: true,
@@ -138,7 +145,7 @@ func TestSugarDB_LINDEX(t *testing.T) {
name: "6. Trying to get index out of range index beyond last index", name: "6. Trying to get index out of range index beyond last index",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3"}, presetValue: []string{"value1", "value2", "value3"},
key: "key6", key: "lindex_key6",
index: 3, index: 3,
want: "", want: "",
wantErr: false, wantErr: false,
@@ -153,6 +160,7 @@ func TestSugarDB_LINDEX(t *testing.T) {
} }
} }
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := server.LIndex(tt.key, tt.index) got, err := server.LIndex(tt.key, tt.index)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("LINDEX() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("LINDEX() error = %v, wantErr %v", err, tt.wantErr)
@@ -163,10 +171,10 @@ func TestSugarDB_LINDEX(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_LMOVE(t *testing.T) { t.Run("TestSugarDB_LMOVE", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -183,11 +191,11 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "1. Move element from LEFT of left list to LEFT of right list", name: "1. Move element from LEFT of left list to LEFT of right list",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"source1": []string{"one", "two", "three"}, "lmove_source1": []string{"one", "two", "three"},
"destination1": []string{"one", "two", "three"}, "lmove_destination1": []string{"one", "two", "three"},
}, },
source: "source1", source: "lmove_source1",
destination: "destination1", destination: "lmove_destination1",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "LEFT", whereTo: "LEFT",
want: true, want: true,
@@ -197,11 +205,11 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "2. Move element from LEFT of left list to RIGHT of right list", name: "2. Move element from LEFT of left list to RIGHT of right list",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"source2": []string{"one", "two", "three"}, "lmove_source2": []string{"one", "two", "three"},
"destination2": []string{"one", "two", "three"}, "lmove_destination2": []string{"one", "two", "three"},
}, },
source: "source2", source: "lmove_source2",
destination: "destination2", destination: "lmove_destination2",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "RIGHT", whereTo: "RIGHT",
want: true, want: true,
@@ -211,11 +219,11 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "3. Move element from RIGHT of left list to LEFT of right list", name: "3. Move element from RIGHT of left list to LEFT of right list",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"source3": []string{"one", "two", "three"}, "lmove_source3": []string{"one", "two", "three"},
"destination3": []string{"one", "two", "three"}, "lmove_destination3": []string{"one", "two", "three"},
}, },
source: "source3", source: "lmove_source3",
destination: "destination3", destination: "lmove_destination3",
whereFrom: "RIGHT", whereFrom: "RIGHT",
whereTo: "LEFT", whereTo: "LEFT",
want: true, want: true,
@@ -225,11 +233,11 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "4. Move element from RIGHT of left list to RIGHT of right list", name: "4. Move element from RIGHT of left list to RIGHT of right list",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"source4": []string{"one", "two", "three"}, "lmove_source4": []string{"one", "two", "three"},
"destination4": []string{"one", "two", "three"}, "lmove_destination4": []string{"one", "two", "three"},
}, },
source: "source4", source: "lmove_source4",
destination: "destination4", destination: "lmove_destination4",
whereFrom: "RIGHT", whereFrom: "RIGHT",
whereTo: "RIGHT", whereTo: "RIGHT",
want: true, want: true,
@@ -239,10 +247,10 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "5. Throw error when the right list is non-existent", name: "5. Throw error when the right list is non-existent",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"source5": []string{"one", "two", "three"}, "lmove_source5": []string{"one", "two", "three"},
}, },
source: "source5", source: "lmove_source5",
destination: "destination5", destination: "lmove_destination5",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "LEFT", whereTo: "LEFT",
want: false, want: false,
@@ -252,11 +260,11 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "6. Throw error when right list in not a list", name: "6. Throw error when right list in not a list",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"source6": []string{"one", "two", "tree"}, "lmove_source6": []string{"one", "two", "tree"},
"destination6": "Default value", "lmove_destination6": "Default value",
}, },
source: "source6", source: "lmove_source6",
destination: "destination6", destination: "lmove_destination6",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "LEFT", whereTo: "LEFT",
want: false, want: false,
@@ -266,10 +274,10 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "7. Throw error when left list is non-existent", name: "7. Throw error when left list is non-existent",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"destination7": []string{"one", "two", "three"}, "lmove_destination7": []string{"one", "two", "three"},
}, },
source: "source7", source: "lmove_source7",
destination: "destination7", destination: "lmove_destination7",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "LEFT", whereTo: "LEFT",
want: false, want: false,
@@ -279,11 +287,11 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "8. Throw error when left list is not a list", name: "8. Throw error when left list is not a list",
preset: true, preset: true,
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"source8": "Default value", "lmove_source8": "Default value",
"destination8": []string{"one", "two", "three"}, "lmove_destination8": []string{"one", "two", "three"},
}, },
source: "source8", source: "lmove_source8",
destination: "destination8", destination: "lmove_destination8",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "LEFT", whereTo: "LEFT",
want: false, want: false,
@@ -293,8 +301,8 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "9. Throw error when WHEREFROM argument is not LEFT/RIGHT", name: "9. Throw error when WHEREFROM argument is not LEFT/RIGHT",
preset: false, preset: false,
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
source: "source9", source: "lmove_source9",
destination: "destination9", destination: "lmove_destination9",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "LEFT", whereTo: "LEFT",
want: false, want: false,
@@ -304,8 +312,8 @@ func TestSugarDB_LMOVE(t *testing.T) {
name: "10. Throw error when WHERETO argument is not LEFT/RIGHT", name: "10. Throw error when WHERETO argument is not LEFT/RIGHT",
preset: false, preset: false,
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
source: "source10", source: "lmove_source10",
destination: "destination10", destination: "lmove_destination10",
whereFrom: "LEFT", whereFrom: "LEFT",
whereTo: "LEFT", whereTo: "LEFT",
want: false, want: false,
@@ -314,6 +322,7 @@ func TestSugarDB_LMOVE(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
for k, v := range tt.presetValue { for k, v := range tt.presetValue {
err := presetValue(server, context.Background(), k, v) err := presetValue(server, context.Background(), k, v)
@@ -333,10 +342,10 @@ func TestSugarDB_LMOVE(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_POP(t *testing.T) { t.Run("TestSugarDB_POP", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -352,7 +361,7 @@ func TestSugarDB_POP(t *testing.T) {
name: "1. LPOP returns last element and removed first element from the list", name: "1. LPOP returns last element and removed first element from the list",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key1", key: "pop_key1",
count: 1, count: 1,
popFunc: server.LPop, popFunc: server.LPop,
want: []string{"value1"}, want: []string{"value1"},
@@ -362,7 +371,7 @@ func TestSugarDB_POP(t *testing.T) {
name: "2. RPOP returns last element and removed last element from the list", name: "2. RPOP returns last element and removed last element from the list",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key2", key: "pop_key2",
count: 1, count: 1,
popFunc: server.RPop, popFunc: server.RPop,
want: []string{"value4"}, want: []string{"value4"},
@@ -371,7 +380,7 @@ func TestSugarDB_POP(t *testing.T) {
{ {
name: "3. Trying to execute LPOP from a non-list item return an error", name: "3. Trying to execute LPOP from a non-list item return an error",
preset: true, preset: true,
key: "key3", key: "pop_key3",
count: 1, count: 1,
presetValue: "Default value", presetValue: "Default value",
popFunc: server.LPop, popFunc: server.LPop,
@@ -382,7 +391,7 @@ func TestSugarDB_POP(t *testing.T) {
name: "4. Trying to execute RPOP from a non-list item return an error", name: "4. Trying to execute RPOP from a non-list item return an error",
preset: true, preset: true,
presetValue: "Default value", presetValue: "Default value",
key: "key6", key: "pop_key6",
count: 1, count: 1,
popFunc: server.RPop, popFunc: server.RPop,
want: []string{}, want: []string{},
@@ -391,6 +400,7 @@ func TestSugarDB_POP(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -408,10 +418,10 @@ func TestSugarDB_POP(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_LPUSH(t *testing.T) { t.Run("TestSugarDB_LPUSH", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -427,7 +437,7 @@ func TestSugarDB_LPUSH(t *testing.T) {
name: "1. LPUSHX to existing list prepends the element to the list", name: "1. LPUSHX to existing list prepends the element to the list",
preset: true, preset: true,
presetValue: []string{"1", "2", "4", "5"}, presetValue: []string{"1", "2", "4", "5"},
key: "key1", key: "lpush_key1",
values: []string{"value1", "value2"}, values: []string{"value1", "value2"},
lpushFunc: server.LPushX, lpushFunc: server.LPushX,
want: 6, want: 6,
@@ -437,7 +447,7 @@ func TestSugarDB_LPUSH(t *testing.T) {
name: "2. LPUSH on existing list prepends the elements to the list", name: "2. LPUSH on existing list prepends the elements to the list",
preset: true, preset: true,
presetValue: []string{"1", "2", "4", "5"}, presetValue: []string{"1", "2", "4", "5"},
key: "key2", key: "lpush_key2",
values: []string{"value1", "value2"}, values: []string{"value1", "value2"},
lpushFunc: server.LPush, lpushFunc: server.LPush,
want: 6, want: 6,
@@ -447,7 +457,7 @@ func TestSugarDB_LPUSH(t *testing.T) {
name: "3. LPUSH on non-existent list creates the list", name: "3. LPUSH on non-existent list creates the list",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key3", key: "lpush_key3",
values: []string{"value1", "value2"}, values: []string{"value1", "value2"},
lpushFunc: server.LPush, lpushFunc: server.LPush,
want: 2, want: 2,
@@ -457,7 +467,7 @@ func TestSugarDB_LPUSH(t *testing.T) {
name: "4. LPUSHX command returns error on non-existent list", name: "4. LPUSHX command returns error on non-existent list",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key4", key: "lpush_key4",
values: []string{"value1", "value2"}, values: []string{"value1", "value2"},
lpushFunc: server.LPushX, lpushFunc: server.LPushX,
want: 0, want: 0,
@@ -466,6 +476,7 @@ func TestSugarDB_LPUSH(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -483,10 +494,10 @@ func TestSugarDB_LPUSH(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_RPUSH(t *testing.T) { t.Run("TestSugarDB_RPUSH", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -502,7 +513,7 @@ func TestSugarDB_RPUSH(t *testing.T) {
name: "1. RPUSH on non-existent list creates the list", name: "1. RPUSH on non-existent list creates the list",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key1", key: "rpush_key1",
values: []string{"value1", "value2"}, values: []string{"value1", "value2"},
rpushFunc: server.RPush, rpushFunc: server.RPush,
want: 2, want: 2,
@@ -512,7 +523,7 @@ func TestSugarDB_RPUSH(t *testing.T) {
name: "2. RPUSHX command returns error on non-existent list", name: "2. RPUSHX command returns error on non-existent list",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key2", key: "rpush_key2",
values: []string{"value1", "value2"}, values: []string{"value1", "value2"},
rpushFunc: server.RPushX, rpushFunc: server.RPushX,
want: 0, want: 0,
@@ -521,6 +532,7 @@ func TestSugarDB_RPUSH(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -538,10 +550,10 @@ func TestSugarDB_RPUSH(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_LRANGE(t *testing.T) { t.Run("TestSugarDB_LRANGE", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -560,7 +572,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "1. Return sub-list within range.", name: "1. Return sub-list within range.",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
key: "key1", key: "lrange_key1",
start: 3, start: 3,
end: 6, end: 6,
want: []string{"value4", "value5", "value6", "value7"}, want: []string{"value4", "value5", "value6", "value7"},
@@ -570,7 +582,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "2. Return sub-list from start index to the end of the list when end index is -1", name: "2. Return sub-list from start index to the end of the list when end index is -1",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
key: "key2", key: "lrange_key2",
start: 3, start: 3,
end: -1, end: -1,
want: []string{"value4", "value5", "value6", "value7", "value8"}, want: []string{"value4", "value5", "value6", "value7", "value8"},
@@ -580,7 +592,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "3. Return empty list when the end index is less than start index", name: "3. Return empty list when the end index is less than start index",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
key: "key3", key: "lrange_key3",
start: 3, start: 3,
end: 0, end: 0,
want: []string{}, want: []string{},
@@ -590,7 +602,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "4. If key does not exist, return empty list", name: "4. If key does not exist, return empty list",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key4", key: "lrange_key4",
start: 0, start: 0,
end: 2, end: 2,
want: []string{}, want: []string{},
@@ -601,7 +613,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "5. Error when executing command on non-list command", name: "5. Error when executing command on non-list command",
preset: true, preset: true,
presetValue: "Default value", presetValue: "Default value",
key: "key5", key: "lrange_key5",
start: 0, start: 0,
end: 3, end: 3,
want: nil, want: nil,
@@ -611,7 +623,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "6. Start index calculated from end of list when start index is less than 0", name: "6. Start index calculated from end of list when start index is less than 0",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key6", key: "lrange_key6",
start: -3, start: -3,
end: 3, end: 3,
want: []string{"value2", "value3", "value4"}, want: []string{"value2", "value3", "value4"},
@@ -621,7 +633,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "7. Empty list when start index is higher than the length of the list", name: "7. Empty list when start index is higher than the length of the list",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3"}, presetValue: []string{"value1", "value2", "value3"},
key: "key7", key: "lrange_key7",
start: 10, start: 10,
end: 11, end: 11,
want: []string{}, want: []string{},
@@ -631,7 +643,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
name: "8. One element when start and end indices are equal", name: "8. One element when start and end indices are equal",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3"}, presetValue: []string{"value1", "value2", "value3"},
key: "key8", key: "lrange_key8",
start: 1, start: 1,
end: 1, end: 1,
want: []string{"value2"}, want: []string{"value2"},
@@ -640,6 +652,7 @@ func TestSugarDB_LRANGE(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -657,10 +670,10 @@ func TestSugarDB_LRANGE(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_LREM(t *testing.T) { t.Run("TestSugarDB_LREM", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -676,7 +689,7 @@ func TestSugarDB_LREM(t *testing.T) {
name: "1. Remove the first 3 elements that appear in the list", name: "1. Remove the first 3 elements that appear in the list",
preset: true, preset: true,
presetValue: []string{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"}, presetValue: []string{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"},
key: "key1", key: "lrem_key1",
count: 3, count: 3,
value: "4", value: "4",
want: 3, want: 3,
@@ -686,7 +699,7 @@ func TestSugarDB_LREM(t *testing.T) {
name: "2. Remove the last 3 elements that appear in the list", name: "2. Remove the last 3 elements that appear in the list",
preset: true, preset: true,
presetValue: []string{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"}, presetValue: []string{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"},
key: "key2", key: "lrem_key2",
count: -3, count: -3,
value: "4", value: "4",
want: 3, want: 3,
@@ -696,7 +709,7 @@ func TestSugarDB_LREM(t *testing.T) {
name: "3. Throw error on non-list item", name: "3. Throw error on non-list item",
preset: true, preset: true,
presetValue: "Default value", presetValue: "Default value",
key: "LremKey8", key: "lrem_key8",
count: 0, count: 0,
value: "value1", value: "value1",
want: 0, want: 0,
@@ -704,6 +717,8 @@ func TestSugarDB_LREM(t *testing.T) {
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -711,7 +726,6 @@ func TestSugarDB_LREM(t *testing.T) {
return return
} }
} }
t.Run(tt.name, func(t *testing.T) {
got, err := server.LRem(tt.key, tt.count, tt.value) got, err := server.LRem(tt.key, tt.count, tt.value)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("LREM() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("LREM() error = %v, wantErr %v", err, tt.wantErr)
@@ -722,10 +736,10 @@ func TestSugarDB_LREM(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_LSET(t *testing.T) { t.Run("TestSugarDB_LSET", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -741,7 +755,7 @@ func TestSugarDB_LSET(t *testing.T) {
name: "1. Return last element within range", name: "1. Return last element within range",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key1", key: "lset_key1",
index: 3, index: 3,
value: "new-value", value: "new-value",
want: true, want: true,
@@ -751,7 +765,7 @@ func TestSugarDB_LSET(t *testing.T) {
name: "2. Return first element within range", name: "2. Return first element within range",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key2", key: "lset_key2",
index: 0, index: 0,
value: "new-value", value: "new-value",
want: true, want: true,
@@ -761,7 +775,7 @@ func TestSugarDB_LSET(t *testing.T) {
name: "3. Return middle element within range", name: "3. Return middle element within range",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key3", key: "lset_key3",
index: 1, index: 1,
value: "new-value", value: "new-value",
want: true, want: true,
@@ -771,7 +785,7 @@ func TestSugarDB_LSET(t *testing.T) {
name: "4. If key does not exist, return error", name: "4. If key does not exist, return error",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key4", key: "lset_key4",
index: 0, index: 0,
value: "element", value: "element",
want: false, want: false,
@@ -781,7 +795,7 @@ func TestSugarDB_LSET(t *testing.T) {
name: "5. Trying to get element by index on a non-list returns error", name: "5. Trying to get element by index on a non-list returns error",
preset: true, preset: true,
presetValue: "Default value", presetValue: "Default value",
key: "key5", key: "lset_key5",
index: 0, index: 0,
value: "element", value: "element",
want: false, want: false,
@@ -791,7 +805,7 @@ func TestSugarDB_LSET(t *testing.T) {
name: "6. Trying to get index out of range index beyond last index", name: "6. Trying to get index out of range index beyond last index",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3"}, presetValue: []string{"value1", "value2", "value3"},
key: "key6", key: "lset_key6",
index: 3, index: 3,
value: "element", value: "element",
want: false, want: false,
@@ -801,7 +815,7 @@ func TestSugarDB_LSET(t *testing.T) {
name: "7. Trying to get index out of range with negative index", name: "7. Trying to get index out of range with negative index",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3"}, presetValue: []string{"value1", "value2", "value3"},
key: "key7", key: "lset_key7",
index: -4, index: -4,
value: "element", value: "element",
want: false, want: false,
@@ -810,6 +824,7 @@ func TestSugarDB_LSET(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -827,10 +842,10 @@ func TestSugarDB_LSET(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_LTRIM(t *testing.T) { t.Run("TestSugarDB_LTRIM", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
@@ -849,7 +864,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
name: "1. Return trim within range", name: "1. Return trim within range",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
key: "key1", key: "ltrim_key1",
start: 3, start: 3,
end: 6, end: 6,
want: true, want: true,
@@ -859,7 +874,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
name: "2. Return element from start index to end index when end index is greater than length of the list", name: "2. Return element from start index to end index when end index is greater than length of the list",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []string{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
key: "key2", key: "ltrim_key2",
start: 5, start: 5,
end: -1, end: -1,
want: true, want: true,
@@ -869,7 +884,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
name: "3. Return false when end index is smaller than start index.", name: "3. Return false when end index is smaller than start index.",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key3", key: "ltrim_key3",
start: 3, start: 3,
end: 1, end: 1,
want: true, want: true,
@@ -879,7 +894,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
name: "4. If key does not exist, return true", name: "4. If key does not exist, return true",
preset: false, preset: false,
presetValue: nil, presetValue: nil,
key: "key4", key: "ltrim_key4",
start: 0, start: 0,
end: 2, end: 2,
want: true, want: true,
@@ -889,7 +904,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
name: "5. Trying to get element by index on a non-list returns error", name: "5. Trying to get element by index on a non-list returns error",
preset: true, preset: true,
presetValue: "Default value", presetValue: "Default value",
key: "key5", key: "ltrim_key5",
start: 0, start: 0,
end: 3, end: 3,
want: false, want: false,
@@ -899,7 +914,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
name: "6. Trim from the end when start index is less than 0", name: "6. Trim from the end when start index is less than 0",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3", "value4"}, presetValue: []string{"value1", "value2", "value3", "value4"},
key: "key6", key: "ltrim_key6",
start: -3, start: -3,
end: 3, end: 3,
want: true, want: true,
@@ -909,7 +924,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
name: "7. Return true when start index is higher than the length of the list", name: "7. Return true when start index is higher than the length of the list",
preset: true, preset: true,
presetValue: []string{"value1", "value2", "value3"}, presetValue: []string{"value1", "value2", "value3"},
key: "key7", key: "ltrim_key7",
start: 10, start: 10,
end: 11, end: 11,
want: true, want: true,
@@ -918,6 +933,7 @@ func TestSugarDB_LTRIM(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.preset { if tt.preset {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -935,4 +951,5 @@ func TestSugarDB_LTRIM(t *testing.T) {
} }
}) })
} }
})
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -19,9 +19,16 @@ import (
"testing" "testing"
) )
func TestSugarDB_SUBSTR(t *testing.T) { func TestSugarDB_String(t *testing.T) {
server := createSugarDB() server := createSugarDB()
t.Cleanup(func() {
server.ShutDown()
})
t.Run("TestSugarDB_SUBSTR", func(t *testing.T) {
t.Parallel()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -34,7 +41,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}{ }{
{ {
name: "Return substring within the range of the string", name: "Return substring within the range of the string",
key: "key1", key: "substr_key1",
substrFunc: server.SubStr, substrFunc: server.SubStr,
presetValue: "Test String One", presetValue: "Test String One",
start: 5, start: 5,
@@ -44,7 +51,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}, },
{ {
name: "Return substring at the end of the string with exact end index", name: "Return substring at the end of the string with exact end index",
key: "key2", key: "substr_key2",
substrFunc: server.SubStr, substrFunc: server.SubStr,
presetValue: "Test String Two", presetValue: "Test String Two",
start: 12, start: 12,
@@ -54,7 +61,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}, },
{ {
name: "Return substring at the end of the string with end index greater than length", name: "Return substring at the end of the string with end index greater than length",
key: "key3", key: "substr_key3",
substrFunc: server.SubStr, substrFunc: server.SubStr,
presetValue: "Test String Three", presetValue: "Test String Three",
start: 12, start: 12,
@@ -63,7 +70,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}, },
{ {
name: "Return the substring at the start of the string with 0 start index", name: "Return the substring at the start of the string with 0 start index",
key: "key4", key: "substr_key4",
substrFunc: server.SubStr, substrFunc: server.SubStr,
presetValue: "Test String Four", presetValue: "Test String Four",
start: 0, start: 0,
@@ -75,7 +82,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
// Return the substring with negative start index. // Return the substring with negative start index.
// Substring should begin abs(start) from the end of the string when start is negative. // Substring should begin abs(start) from the end of the string when start is negative.
name: "Return the substring with negative start index", name: "Return the substring with negative start index",
key: "key5", key: "substr_key5",
substrFunc: server.SubStr, substrFunc: server.SubStr,
presetValue: "Test String Five", presetValue: "Test String Five",
start: -11, start: -11,
@@ -87,7 +94,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
// Return reverse substring with end index smaller than start index. // Return reverse substring with end index smaller than start index.
// When end index is smaller than start index, the 2 indices are reversed. // When end index is smaller than start index, the 2 indices are reversed.
name: "Return reverse substring with end index smaller than start index", name: "Return reverse substring with end index smaller than start index",
key: "key6", key: "substr_key6",
substrFunc: server.SubStr, substrFunc: server.SubStr,
presetValue: "Test String Six", presetValue: "Test String Six",
start: 4, start: 4,
@@ -96,7 +103,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}, },
{ {
name: "Return substring within the range of the string", name: "Return substring within the range of the string",
key: "key7", key: "substr_key7",
substrFunc: server.GetRange, substrFunc: server.GetRange,
presetValue: "Test String One", presetValue: "Test String One",
start: 5, start: 5,
@@ -106,7 +113,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}, },
{ {
name: "Return substring at the end of the string with exact end index", name: "Return substring at the end of the string with exact end index",
key: "key8", key: "substr_key8",
substrFunc: server.GetRange, substrFunc: server.GetRange,
presetValue: "Test String Two", presetValue: "Test String Two",
start: 12, start: 12,
@@ -116,7 +123,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}, },
{ {
name: "Return substring at the end of the string with end index greater than length", name: "Return substring at the end of the string with end index greater than length",
key: "key9", key: "substr_key9",
substrFunc: server.GetRange, substrFunc: server.GetRange,
presetValue: "Test String Three", presetValue: "Test String Three",
start: 12, start: 12,
@@ -125,7 +132,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
}, },
{ {
name: "Return the substring at the start of the string with 0 start index", name: "Return the substring at the start of the string with 0 start index",
key: "key10", key: "substr_key10",
substrFunc: server.GetRange, substrFunc: server.GetRange,
presetValue: "Test String Four", presetValue: "Test String Four",
start: 0, start: 0,
@@ -137,7 +144,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
// Return the substring with negative start index. // Return the substring with negative start index.
// Substring should begin abs(start) from the end of the string when start is negative. // Substring should begin abs(start) from the end of the string when start is negative.
name: "Return the substring with negative start index", name: "Return the substring with negative start index",
key: "key11", key: "substr_key11",
substrFunc: server.GetRange, substrFunc: server.GetRange,
presetValue: "Test String Five", presetValue: "Test String Five",
start: -11, start: -11,
@@ -149,7 +156,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
// Return reverse substring with end index smaller than start index. // Return reverse substring with end index smaller than start index.
// When end index is smaller than start index, the 2 indices are reversed. // When end index is smaller than start index, the 2 indices are reversed.
name: "Return reverse substring with end index smaller than start index", name: "Return reverse substring with end index smaller than start index",
key: "key12", key: "substr_key12",
substrFunc: server.GetRange, substrFunc: server.GetRange,
presetValue: "Test String Six", presetValue: "Test String Six",
start: 4, start: 4,
@@ -159,6 +166,7 @@ func TestSugarDB_SUBSTR(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -176,11 +184,10 @@ func TestSugarDB_SUBSTR(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_SETRANGE(t *testing.T) {
server := createSugarDB()
t.Run("TestSugarDB_SETRANGE", func(t *testing.T) {
t.Parallel()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -192,7 +199,7 @@ func TestSugarDB_SETRANGE(t *testing.T) {
}{ }{
{ {
name: "Test that SETRANGE on non-existent string creates new string", name: "Test that SETRANGE on non-existent string creates new string",
key: "key1", key: "setrange_key1",
presetValue: "", presetValue: "",
offset: 10, offset: 10,
new: "New String Value", new: "New String Value",
@@ -201,7 +208,7 @@ func TestSugarDB_SETRANGE(t *testing.T) {
}, },
{ {
name: "Test SETRANGE with an offset that leads to a longer resulting string", name: "Test SETRANGE with an offset that leads to a longer resulting string",
key: "key2", key: "setrange_key2",
presetValue: "Original String Value", presetValue: "Original String Value",
offset: 16, offset: 16,
new: "Portion Replaced With This New String", new: "Portion Replaced With This New String",
@@ -210,7 +217,7 @@ func TestSugarDB_SETRANGE(t *testing.T) {
}, },
{ {
name: "SETRANGE with negative offset prepends the string", name: "SETRANGE with negative offset prepends the string",
key: "key3", key: "setrange_key3",
presetValue: "This is a preset value", presetValue: "This is a preset value",
offset: -10, offset: -10,
new: "Prepended ", new: "Prepended ",
@@ -219,7 +226,7 @@ func TestSugarDB_SETRANGE(t *testing.T) {
}, },
{ {
name: "SETRANGE with offset that embeds new string inside the old string", name: "SETRANGE with offset that embeds new string inside the old string",
key: "key4", key: "setrange_key4",
presetValue: "This is a preset value", presetValue: "This is a preset value",
offset: 0, offset: 0,
new: "That", new: "That",
@@ -228,7 +235,7 @@ func TestSugarDB_SETRANGE(t *testing.T) {
}, },
{ {
name: "SETRANGE with offset longer than original lengths appends the string", name: "SETRANGE with offset longer than original lengths appends the string",
key: "key5", key: "setrange_key5",
presetValue: "This is a preset value", presetValue: "This is a preset value",
offset: 100, offset: 100,
new: " Appended", new: " Appended",
@@ -237,7 +244,7 @@ func TestSugarDB_SETRANGE(t *testing.T) {
}, },
{ {
name: "SETRANGE with offset on the last character replaces last character with new string", name: "SETRANGE with offset on the last character replaces last character with new string",
key: "key6", key: "setrange_key6",
presetValue: "This is a preset value", presetValue: "This is a preset value",
offset: len("This is a preset value") - 1, offset: len("This is a preset value") - 1,
new: " replaced", new: " replaced",
@@ -247,6 +254,7 @@ func TestSugarDB_SETRANGE(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -264,11 +272,10 @@ func TestSugarDB_SETRANGE(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_STRLEN(t *testing.T) {
server := createSugarDB()
t.Run("TestSugarDB_STRLEN", func(t *testing.T) {
t.Parallel()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -278,14 +285,14 @@ func TestSugarDB_STRLEN(t *testing.T) {
}{ }{
{ {
name: "Return the correct string length for an existing string", name: "Return the correct string length for an existing string",
key: "key1", key: "strlen_key1",
presetValue: "Test String", presetValue: "Test String",
want: len("Test String"), want: len("Test String"),
wantErr: false, wantErr: false,
}, },
{ {
name: "If the string does not exist, return 0", name: "If the string does not exist, return 0",
key: "key2", key: "strlen_key2",
presetValue: "", presetValue: "",
want: 0, want: 0,
wantErr: false, wantErr: false,
@@ -293,6 +300,7 @@ func TestSugarDB_STRLEN(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -310,10 +318,10 @@ func TestSugarDB_STRLEN(t *testing.T) {
} }
}) })
} }
} })
func TestSugarDB_APPEND(t *testing.T) { t.Run("TestSugarDB_APPEND", func(t *testing.T) {
server := createSugarDB() t.Parallel()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
@@ -324,7 +332,7 @@ func TestSugarDB_APPEND(t *testing.T) {
}{ }{
{ {
name: "Test APPEND with no preset value", name: "Test APPEND with no preset value",
key: "key1", key: "append_key1",
value: "Hello ", value: "Hello ",
want: 6, want: 6,
wantErr: false, wantErr: false,
@@ -332,14 +340,14 @@ func TestSugarDB_APPEND(t *testing.T) {
{ {
name: "Test APPEND with preset value", name: "Test APPEND with preset value",
presetValue: "Hello ", presetValue: "Hello ",
key: "key2", key: "append_key2",
value: "World", value: "World",
want: 11, want: 11,
wantErr: false, wantErr: false,
}, },
{ {
name: "Test APPEND with integer preset value", name: "Test APPEND with integer preset value",
key: "key3", key: "append_key3",
presetValue: 10, presetValue: 10,
value: "Hello ", value: "Hello ",
wantErr: true, wantErr: true,
@@ -347,6 +355,7 @@ func TestSugarDB_APPEND(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.presetValue != nil { if tt.presetValue != nil {
err := presetValue(server, context.Background(), tt.key, tt.presetValue) err := presetValue(server, context.Background(), tt.key, tt.presetValue)
if err != nil { if err != nil {
@@ -364,4 +373,5 @@ func TestSugarDB_APPEND(t *testing.T) {
} }
}) })
} }
})
} }

View File

@@ -212,6 +212,8 @@ func makeCluster(size int) ([]ClientServerPair, error) {
} }
func Test_Cluster(t *testing.T) { func Test_Cluster(t *testing.T) {
t.Parallel()
nodes, err := makeCluster(5) nodes, err := makeCluster(5)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@@ -274,9 +276,7 @@ func Test_Cluster(t *testing.T) {
// Yield // Yield
ticker := time.NewTicker(200 * time.Millisecond) ticker := time.NewTicker(200 * time.Millisecond)
defer func() { defer ticker.Stop()
ticker.Stop()
}()
<-ticker.C <-ticker.C
// Check if the data has been replicated on a quorum (majority of the cluster). // Check if the data has been replicated on a quorum (majority of the cluster).
@@ -322,10 +322,8 @@ func Test_Cluster(t *testing.T) {
// Yield // Yield
ticker := time.NewTicker(200 * time.Millisecond) ticker := time.NewTicker(200 * time.Millisecond)
defer func() {
ticker.Stop()
}()
<-ticker.C <-ticker.C
ticker.Stop()
// Check if the data has been replicated on a quorum (majority of the cluster). // Check if the data has been replicated on a quorum (majority of the cluster).
quorum := int(math.Ceil(float64(len(nodes)/2)) + 1) quorum := int(math.Ceil(float64(len(nodes)/2)) + 1)
@@ -963,8 +961,18 @@ func Test_Standalone(t *testing.T) {
name: "1. Snapshot in embedded instance", name: "1. Snapshot in embedded instance",
dataDir: path.Join(dataDir, "embedded_instance"), dataDir: path.Join(dataDir, "embedded_instance"),
values: map[int]map[string]string{ values: map[int]map[string]string{
0: {"key5": "value-05", "key6": "value-06", "key7": "value-07", "key8": "value-08"}, 0: {
1: {"key5": "value-15", "key6": "value-16", "key7": "value-17", "key8": "value-18"}, "test_snapshot_key5": "value-05",
"test_snapshot_key6": "value-06",
"test_snapshot_key7": "value-07",
"test_snapshot_key8": "value-08",
},
1: {
"test_snapshot_key5": "value-15",
"test_snapshot_key6": "value-16",
"test_snapshot_key7": "value-17",
"test_snapshot_key8": "value-18",
},
}, },
snapshotFunc: func(mockServer *SugarDB) error { snapshotFunc: func(mockServer *SugarDB) error {
if _, err := mockServer.Save(); err != nil { if _, err := mockServer.Save(); err != nil {
@@ -1022,7 +1030,7 @@ func Test_Standalone(t *testing.T) {
} }
// Yield to allow snapshot to complete sync. // Yield to allow snapshot to complete sync.
ticker := time.NewTicker(20 * time.Millisecond) ticker := time.NewTicker(200 * time.Millisecond)
<-ticker.C <-ticker.C
ticker.Stop() ticker.Stop()
@@ -1121,10 +1129,10 @@ func Test_Standalone(t *testing.T) {
<-ticker.C <-ticker.C
// Rewrite AOF // Rewrite AOF
if _, err := mockServer.RewriteAOF(); err != nil { mockServer.RewriteAOF()
t.Error(err)
return // Yield
} <-ticker.C
// Perform write commands from "after-rewrite" // Perform write commands from "after-rewrite"
for key, value := range data["after-rewrite"] { for key, value := range data["after-rewrite"] {