Updated test suites to set mock server only once in each suite instead of instantiating it in every test.

This commit is contained in:
Kelvin Mwinuka
2024-03-15 03:44:42 +08:00
parent b49a0e24bc
commit e685d5041b
10 changed files with 1316 additions and 1411 deletions

View File

@@ -69,7 +69,7 @@ func NewAppendStore(options ...func(store *AppendStore)) *AppendStore {
} }
// If rw is nil, use a default file at the provided directory // If rw is nil, use a default file at the provided directory
if store.rw == nil { if store.rw == nil && store.directory != "" {
// Create the directory if it does not exist // Create the directory if it does not exist
err := os.MkdirAll(path.Join(store.directory, "aof"), os.ModePerm) err := os.MkdirAll(path.Join(store.directory, "aof"), os.ModePerm)
if err != nil { if err != nil {

View File

@@ -79,7 +79,7 @@ func NewPreambleStore(options ...func(store *PreambleStore)) *PreambleStore {
} }
// If rw is nil, create the default // If rw is nil, create the default
if store.rw == nil { if store.rw == nil && store.directory != "" {
err := os.MkdirAll(path.Join(store.directory, "aof"), os.ModePerm) err := os.MkdirAll(path.Join(store.directory, "aof"), os.ModePerm)
if err != nil { if err != nil {
log.Println(fmt.Errorf("new preamble store -> mkdir error: %+v", err)) log.Println(fmt.Errorf("new preamble store -> mkdir error: %+v", err))

View File

@@ -10,9 +10,19 @@ import (
"testing" "testing"
) )
var mockServer *server.Server
func init() {
mockServer = server.NewServer(server.Opts{
Config: utils.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
},
})
}
func Test_HandlePing(t *testing.T) { func Test_HandlePing(t *testing.T) {
ctx := context.Background() ctx := context.Background()
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
command []string command []string

File diff suppressed because it is too large Load Diff

View File

@@ -12,10 +12,19 @@ import (
"testing" "testing"
) )
var mockServer *server.Server
func init() {
mockServer = server.NewServer(server.Opts{
Config: utils.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
},
})
}
func Test_HandleHSET(t *testing.T) { func Test_HandleHSET(t *testing.T) {
// Tests for both HSET and HSETNX // Tests for both HSET and HSETNX
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -27,70 +36,70 @@ func Test_HandleHSET(t *testing.T) {
}{ }{
{ // HSETNX set field on non-existent hash map { // HSETNX set field on non-existent hash map
preset: false, preset: false,
key: "key1", key: "HsetKey1",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HSETNX", "key1", "field1", "value1"}, command: []string{"HSETNX", "HsetKey1", "field1", "value1"},
expectedResponse: 1, expectedResponse: 1,
expectedValue: map[string]interface{}{"field1": "value1"}, expectedValue: map[string]interface{}{"field1": "value1"},
expectedError: nil, expectedError: nil,
}, },
{ // HSETNX set field on existing hash map { // HSETNX set field on existing hash map
preset: true, preset: true,
key: "key2", key: "HsetKey2",
presetValue: map[string]interface{}{"field1": "value1"}, presetValue: map[string]interface{}{"field1": "value1"},
command: []string{"HSETNX", "key2", "field2", "value2"}, command: []string{"HSETNX", "HsetKey2", "field2", "value2"},
expectedResponse: 1, expectedResponse: 1,
expectedValue: map[string]interface{}{"field1": "value1", "field2": "value2"}, expectedValue: map[string]interface{}{"field1": "value1", "field2": "value2"},
expectedError: nil, expectedError: nil,
}, },
{ // HSETNX skips operation when setting on existing field { // HSETNX skips operation when setting on existing field
preset: true, preset: true,
key: "key3", key: "HsetKey3",
presetValue: map[string]interface{}{"field1": "value1"}, presetValue: map[string]interface{}{"field1": "value1"},
command: []string{"HSETNX", "key3", "field1", "value1-new"}, command: []string{"HSETNX", "HsetKey3", "field1", "value1-new"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{"field1": "value1"}, expectedValue: map[string]interface{}{"field1": "value1"},
expectedError: nil, expectedError: nil,
}, },
{ // Regular HSET command on non-existent hash map { // Regular HSET command on non-existent hash map
preset: false, preset: false,
key: "key4", key: "HsetKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HSET", "key4", "field1", "value1", "field2", "value2"}, command: []string{"HSET", "HsetKey4", "field1", "value1", "field2", "value2"},
expectedResponse: 2, expectedResponse: 2,
expectedValue: map[string]interface{}{"field1": "value1", "field2": "value2"}, expectedValue: map[string]interface{}{"field1": "value1", "field2": "value2"},
expectedError: nil, expectedError: nil,
}, },
{ // Regular HSET update on existing hash map { // Regular HSET update on existing hash map
preset: true, preset: true,
key: "key5", key: "HsetKey5",
presetValue: map[string]interface{}{"field1": "value1", "field2": "value2"}, presetValue: map[string]interface{}{"field1": "value1", "field2": "value2"},
command: []string{"HSET", "key5", "field1", "value1-new", "field2", "value2-ne2", "field3", "value3"}, command: []string{"HSET", "HsetKey5", "field1", "value1-new", "field2", "value2-ne2", "field3", "value3"},
expectedResponse: 3, expectedResponse: 3,
expectedValue: map[string]interface{}{"field1": "value1-new", "field2": "value2-ne2", "field3": "value3"}, expectedValue: map[string]interface{}{"field1": "value1-new", "field2": "value2-ne2", "field3": "value3"},
expectedError: nil, expectedError: nil,
}, },
{ // HSET returns error when the target key is not a map { // HSET returns error when the target key is not a map
preset: true, preset: true,
key: "key6", key: "HsetKey6",
presetValue: "Default preset value", presetValue: "Default preset value",
command: []string{"HSET", "key6", "field1", "value1"}, command: []string{"HSET", "HsetKey6", "field1", "value1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key6 is not a hash"), expectedError: errors.New("value at HsetKey6 is not a hash"),
}, },
{ // HSET returns error when there's a mismatch in key/values { // HSET returns error when there's a mismatch in key/values
preset: false, preset: false,
key: "key7", key: "HsetKey7",
presetValue: nil, presetValue: nil,
command: []string{"HSET", "key7", "field1", "value1", "field2"}, command: []string{"HSET", "HsetKey7", "field1", "value1", "field2"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("each field must have a corresponding value"), expectedError: errors.New("each field must have a corresponding value"),
}, },
{ // Command too short { // Command too short
preset: true, preset: true,
key: "key8", key: "HsetKey8",
presetValue: nil, presetValue: nil,
command: []string{"HSET", "field1"}, command: []string{"HSET", "field1"},
expectedResponse: 0, expectedResponse: 0,
@@ -143,8 +152,6 @@ func Test_HandleHSET(t *testing.T) {
func Test_HandleHINCRBY(t *testing.T) { func Test_HandleHINCRBY(t *testing.T) {
// Tests for both HINCRBY and HINCRBYFLOAT // Tests for both HINCRBY and HINCRBYFLOAT
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -156,90 +163,90 @@ func Test_HandleHINCRBY(t *testing.T) {
}{ }{
{ // Increment by integer on non-existent hash should create a new one { // Increment by integer on non-existent hash should create a new one
preset: false, preset: false,
key: "key1", key: "HincrbyKey1",
presetValue: nil, presetValue: nil,
command: []string{"HINCRBY", "key1", "field1", "1"}, command: []string{"HINCRBY", "HincrbyKey1", "field1", "1"},
expectedResponse: 1, expectedResponse: 1,
expectedValue: map[string]interface{}{"field1": 1}, expectedValue: map[string]interface{}{"field1": 1},
expectedError: nil, expectedError: nil,
}, },
{ // Increment by float on non-existent hash should create one { // Increment by float on non-existent hash should create one
preset: false, preset: false,
key: "key2", key: "HincrbyKey2",
presetValue: nil, presetValue: nil,
command: []string{"HINCRBYFLOAT", "key2", "field1", "3.142"}, command: []string{"HINCRBYFLOAT", "HincrbyKey2", "field1", "3.142"},
expectedResponse: "3.142", expectedResponse: "3.142",
expectedValue: map[string]interface{}{"field1": 3.142}, expectedValue: map[string]interface{}{"field1": 3.142},
expectedError: nil, expectedError: nil,
}, },
{ // Increment by integer on existing hash { // Increment by integer on existing hash
preset: true, preset: true,
key: "key3", key: "HincrbyKey3",
presetValue: map[string]interface{}{"field1": 1}, presetValue: map[string]interface{}{"field1": 1},
command: []string{"HINCRBY", "key3", "field1", "10"}, command: []string{"HINCRBY", "HincrbyKey3", "field1", "10"},
expectedResponse: 11, expectedResponse: 11,
expectedValue: map[string]interface{}{"field1": 11}, expectedValue: map[string]interface{}{"field1": 11},
expectedError: nil, expectedError: nil,
}, },
{ // Increment by float on an existing hash { // Increment by float on an existing hash
preset: true, preset: true,
key: "key4", key: "HincrbyKey4",
presetValue: map[string]interface{}{"field1": 3.142}, presetValue: map[string]interface{}{"field1": 3.142},
command: []string{"HINCRBYFLOAT", "key4", "field1", "3.142"}, command: []string{"HINCRBYFLOAT", "HincrbyKey4", "field1", "3.142"},
expectedResponse: "6.284", expectedResponse: "6.284",
expectedValue: map[string]interface{}{"field1": 6.284}, expectedValue: map[string]interface{}{"field1": 6.284},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key5", key: "HincrbyKey5",
presetValue: nil, presetValue: nil,
command: []string{"HINCRBY", "key5"}, command: []string{"HINCRBY", "HincrbyKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key6", key: "HincrbyKey6",
presetValue: nil, presetValue: nil,
command: []string{"HINCRBY", "key6", "field1", "23", "45"}, command: []string{"HINCRBY", "HincrbyKey6", "field1", "23", "45"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Error when increment by float does not pass valid float { // Error when increment by float does not pass valid float
preset: false, preset: false,
key: "key7", key: "HincrbyKey7",
presetValue: nil, presetValue: nil,
command: []string{"HINCRBYFLOAT", "key7", "field1", "three point one four two"}, command: []string{"HINCRBYFLOAT", "HincrbyKey7", "field1", "three point one four two"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("increment must be a float"), expectedError: errors.New("increment must be a float"),
}, },
{ // Error when increment does not pass valid integer { // Error when increment does not pass valid integer
preset: false, preset: false,
key: "key8", key: "HincrbyKey8",
presetValue: nil, presetValue: nil,
command: []string{"HINCRBY", "key8", "field1", "three"}, command: []string{"HINCRBY", "HincrbyKey8", "field1", "three"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("increment must be an integer"), expectedError: errors.New("increment must be an integer"),
}, },
{ // Error when trying to increment on a key that is not a hash { // Error when trying to increment on a key that is not a hash
preset: true, preset: true,
key: "key9", key: "HincrbyKey9",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HINCRBY", "key9", "field1", "3"}, command: []string{"HINCRBY", "HincrbyKey9", "field1", "3"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key9 is not a hash"), expectedError: errors.New("value at HincrbyKey9 is not a hash"),
}, },
{ // Error when trying to increment a hash field that is not a number { // Error when trying to increment a hash field that is not a number
preset: true, preset: true,
key: "key10", key: "HincrbyKey10",
presetValue: map[string]interface{}{"field1": "value1"}, presetValue: map[string]interface{}{"field1": "value1"},
command: []string{"HINCRBY", "key10", "field1", "3"}, command: []string{"HINCRBY", "HincrbyKey10", "field1", "3"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at field field1 is not a number"), expectedError: errors.New("value at field field1 is not a number"),
@@ -299,8 +306,6 @@ func Test_HandleHINCRBY(t *testing.T) {
} }
func Test_HandleHGET(t *testing.T) { func Test_HandleHGET(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -312,36 +317,36 @@ func Test_HandleHGET(t *testing.T) {
}{ }{
{ // Return nil when attempting to get from non-existed key { // Return nil when attempting to get from non-existed key
preset: true, preset: true,
key: "key1", key: "HgetKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 365, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 365, "field3": 3.142},
command: []string{"HGET", "key1", "field1", "field2", "field3", "field4"}, command: []string{"HGET", "HgetKey1", "field1", "field2", "field3", "field4"},
expectedResponse: []interface{}{"value1", 365, "3.142", nil}, expectedResponse: []interface{}{"value1", 365, "3.142", nil},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Return nil when attempting to get from non-existed key { // Return nil when attempting to get from non-existed key
preset: false, preset: false,
key: "key2", key: "HgetKey2",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HGET", "key2", "field1"}, command: []string{"HGET", "HgetKey2", "field1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Error when trying to get from a value that is not a hash map { // Error when trying to get from a value that is not a hash map
preset: true, preset: true,
key: "key3", key: "HgetKey3",
presetValue: "Default Value", presetValue: "Default Value",
command: []string{"HGET", "key3", "field1"}, command: []string{"HGET", "HgetKey3", "field1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key3 is not a hash"), expectedError: errors.New("value at HgetKey3 is not a hash"),
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key4", key: "HgetKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HGET", "key4"}, command: []string{"HGET", "HgetKey4"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
@@ -401,8 +406,6 @@ func Test_HandleHGET(t *testing.T) {
} }
func Test_HandleHSTRLEN(t *testing.T) { func Test_HandleHSTRLEN(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -416,39 +419,39 @@ func Test_HandleHSTRLEN(t *testing.T) {
// Return lengths of field values. // Return lengths of field values.
// If the key does not exist, its length should be 0. // If the key does not exist, its length should be 0.
preset: true, preset: true,
key: "key1", key: "HstrlenKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HSTRLEN", "key1", "field1", "field2", "field3", "field4"}, command: []string{"HSTRLEN", "HstrlenKey1", "field1", "field2", "field3", "field4"},
expectedResponse: []int{len("value1"), len("123456789"), len("3.142"), 0}, expectedResponse: []int{len("value1"), len("123456789"), len("3.142"), 0},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Nil response when trying to get HSTRLEN non-existent key { // Nil response when trying to get HSTRLEN non-existent key
preset: false, preset: false,
key: "key2", key: "HstrlenKey2",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HSTRLEN", "key2", "field1"}, command: []string{"HSTRLEN", "HstrlenKey2", "field1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "HstrlenKey3",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HSTRLEN", "key3"}, command: []string{"HSTRLEN", "HstrlenKey3"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non hash map returns error { // Trying to get lengths on a non hash map returns error
preset: true, preset: true,
key: "key4", key: "HstrlenKey4",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HSTRLEN", "key4", "field1"}, command: []string{"HSTRLEN", "HstrlenKey4", "field1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key4 is not a hash"), expectedError: errors.New("value at HstrlenKey4 is not a hash"),
}, },
} }
@@ -492,8 +495,6 @@ func Test_HandleHSTRLEN(t *testing.T) {
} }
func Test_HandleHVALS(t *testing.T) { func Test_HandleHVALS(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -506,25 +507,25 @@ func Test_HandleHVALS(t *testing.T) {
{ {
// Return all the values from a hash // Return all the values from a hash
preset: true, preset: true,
key: "key1", key: "HvalsKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HVALS", "key1"}, command: []string{"HVALS", "HvalsKey1"},
expectedResponse: []interface{}{"value1", 123456789, "3.142"}, expectedResponse: []interface{}{"value1", 123456789, "3.142"},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Empty array response when trying to get HSTRLEN non-existent key { // Empty array response when trying to get HSTRLEN non-existent key
preset: false, preset: false,
key: "key2", key: "HvalsKey2",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HVALS", "key2"}, command: []string{"HVALS", "HvalsKey2"},
expectedResponse: []interface{}{}, expectedResponse: []interface{}{},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "HvalsKey3",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HVALS"}, command: []string{"HVALS"},
expectedResponse: nil, expectedResponse: nil,
@@ -533,21 +534,21 @@ func Test_HandleHVALS(t *testing.T) {
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "HvalsKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HVALS", "key4", "key4"}, command: []string{"HVALS", "HvalsKey4", "HvalsKey4"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non hash map returns error { // Trying to get lengths on a non hash map returns error
preset: true, preset: true,
key: "key5", key: "HvalsKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HVALS", "key5"}, command: []string{"HVALS", "HvalsKey5"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key5 is not a hash"), expectedError: errors.New("value at HvalsKey5 is not a hash"),
}, },
} }
@@ -608,8 +609,6 @@ func Test_HandleHVALS(t *testing.T) {
} }
func Test_HandleHRANDFIELD(t *testing.T) { func Test_HandleHRANDFIELD(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -622,9 +621,9 @@ func Test_HandleHRANDFIELD(t *testing.T) {
}{ }{
{ // Get a random field { // Get a random field
preset: true, preset: true,
key: "key1", key: "HrandfieldKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HRANDFIELD", "key1"}, command: []string{"HRANDFIELD", "HrandfieldKey1"},
withValues: false, withValues: false,
expectedCount: 1, expectedCount: 1,
expectedResponse: []string{"field1", "field2", "field3"}, expectedResponse: []string{"field1", "field2", "field3"},
@@ -632,9 +631,9 @@ func Test_HandleHRANDFIELD(t *testing.T) {
}, },
{ // Get a random field with a value { // Get a random field with a value
preset: true, preset: true,
key: "key2", key: "HrandfieldKey2",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HRANDFIELD", "key2", "1", "WITHVALUES"}, command: []string{"HRANDFIELD", "HrandfieldKey2", "1", "WITHVALUES"},
withValues: true, withValues: true,
expectedCount: 2, expectedCount: 2,
expectedResponse: []string{"field1", "value1", "field2", "123456789", "field3", "3.142"}, expectedResponse: []string{"field1", "value1", "field2", "123456789", "field3", "3.142"},
@@ -642,7 +641,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
}, },
{ // Get several random fields { // Get several random fields
preset: true, preset: true,
key: "key3", key: "HrandfieldKey3",
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"field1": "value1", "field1": "value1",
"field2": 123456789, "field2": 123456789,
@@ -650,7 +649,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
"field4": "value4", "field4": "value4",
"field5": "value5", "field5": "value5",
}, },
command: []string{"HRANDFIELD", "key3", "3"}, command: []string{"HRANDFIELD", "HrandfieldKey3", "3"},
withValues: false, withValues: false,
expectedCount: 3, expectedCount: 3,
expectedResponse: []string{"field1", "field2", "field3", "field4", "field5"}, expectedResponse: []string{"field1", "field2", "field3", "field4", "field5"},
@@ -658,7 +657,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
}, },
{ // Get several random fields with their corresponding values { // Get several random fields with their corresponding values
preset: true, preset: true,
key: "key4", key: "HrandfieldKey4",
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"field1": "value1", "field1": "value1",
"field2": 123456789, "field2": 123456789,
@@ -666,7 +665,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
"field4": "value4", "field4": "value4",
"field5": "value5", "field5": "value5",
}, },
command: []string{"HRANDFIELD", "key4", "3", "WITHVALUES"}, command: []string{"HRANDFIELD", "HrandfieldKey4", "3", "WITHVALUES"},
withValues: true, withValues: true,
expectedCount: 6, expectedCount: 6,
expectedResponse: []string{ expectedResponse: []string{
@@ -677,7 +676,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
}, },
{ // Get the entire hash { // Get the entire hash
preset: true, preset: true,
key: "key5", key: "HrandfieldKey5",
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"field1": "value1", "field1": "value1",
"field2": 123456789, "field2": 123456789,
@@ -685,7 +684,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
"field4": "value4", "field4": "value4",
"field5": "value5", "field5": "value5",
}, },
command: []string{"HRANDFIELD", "key5", "5"}, command: []string{"HRANDFIELD", "HrandfieldKey5", "5"},
withValues: false, withValues: false,
expectedCount: 5, expectedCount: 5,
expectedResponse: []string{"field1", "field2", "field3", "field4", "field5"}, expectedResponse: []string{"field1", "field2", "field3", "field4", "field5"},
@@ -693,7 +692,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
}, },
{ // Get the entire hash with values { // Get the entire hash with values
preset: true, preset: true,
key: "key5", key: "HrandfieldKey5",
presetValue: map[string]interface{}{ presetValue: map[string]interface{}{
"field1": "value1", "field1": "value1",
"field2": 123456789, "field2": 123456789,
@@ -701,7 +700,7 @@ func Test_HandleHRANDFIELD(t *testing.T) {
"field4": "value4", "field4": "value4",
"field5": "value5", "field5": "value5",
}, },
command: []string{"HRANDFIELD", "key5", "5", "WITHVALUES"}, command: []string{"HRANDFIELD", "HrandfieldKey5", "5", "WITHVALUES"},
withValues: true, withValues: true,
expectedCount: 10, expectedCount: 10,
expectedResponse: []string{ expectedResponse: []string{
@@ -712,37 +711,37 @@ func Test_HandleHRANDFIELD(t *testing.T) {
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key10", key: "HrandfieldKey10",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HRANDFIELD"}, command: []string{"HRANDFIELD"},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key11", key: "HrandfieldKey11",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HRANDFIELD", "key11", "key11", "key11", "key11"}, command: []string{"HRANDFIELD", "HrandfieldKey11", "HrandfieldKey11", "HrandfieldKey11", "HrandfieldKey11"},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get random field on a non hash map returns error { // Trying to get random field on a non hash map returns error
preset: true, preset: true,
key: "key12", key: "HrandfieldKey12",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HRANDFIELD", "key12"}, command: []string{"HRANDFIELD", "HrandfieldKey12"},
expectedError: errors.New("value at key12 is not a hash"), expectedError: errors.New("value at HrandfieldKey12 is not a hash"),
}, },
{ // Throw error when count provided is not an integer { // Throw error when count provided is not an integer
preset: true, preset: true,
key: "key12", key: "HrandfieldKey12",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HRANDFIELD", "key12", "COUNT"}, command: []string{"HRANDFIELD", "HrandfieldKey12", "COUNT"},
expectedError: errors.New("count must be an integer"), expectedError: errors.New("count must be an integer"),
}, },
{ // If fourth argument is provided, it must be "WITHVALUES" { // If fourth argument is provided, it must be "WITHVALUES"
preset: true, preset: true,
key: "key12", key: "HrandfieldKey12",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HRANDFIELD", "key12", "10", "FLAG"}, command: []string{"HRANDFIELD", "HrandfieldKey12", "10", "FLAG"},
expectedError: errors.New("result modifier must be withvalues"), expectedError: errors.New("result modifier must be withvalues"),
}, },
} }
@@ -806,8 +805,6 @@ func Test_HandleHRANDFIELD(t *testing.T) {
} }
func Test_HandleHLEN(t *testing.T) { func Test_HandleHLEN(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -820,25 +817,25 @@ func Test_HandleHLEN(t *testing.T) {
{ {
// Return the correct length of the hash // Return the correct length of the hash
preset: true, preset: true,
key: "key1", key: "HlenKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HLEN", "key1"}, command: []string{"HLEN", "HlenKey1"},
expectedResponse: 3, expectedResponse: 3,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // 0 response when trying to call HLEN on non-existent key { // 0 response when trying to call HLEN on non-existent key
preset: false, preset: false,
key: "key2", key: "HlenKey2",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HLEN", "key2"}, command: []string{"HLEN", "HlenKey2"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "HlenKey3",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HLEN"}, command: []string{"HLEN"},
expectedResponse: 0, expectedResponse: 0,
@@ -847,21 +844,21 @@ func Test_HandleHLEN(t *testing.T) {
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "HlenKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HLEN", "key4", "key4"}, command: []string{"HLEN", "HlenKey4", "HlenKey4"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non hash map returns error { // Trying to get lengths on a non hash map returns error
preset: true, preset: true,
key: "key5", key: "HlenKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HLEN", "key5"}, command: []string{"HLEN", "HlenKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key5 is not a hash"), expectedError: errors.New("value at HlenKey5 is not a hash"),
}, },
} }
@@ -900,8 +897,6 @@ func Test_HandleHLEN(t *testing.T) {
} }
func Test_HandleHKeys(t *testing.T) { func Test_HandleHKeys(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -914,25 +909,25 @@ func Test_HandleHKeys(t *testing.T) {
{ {
// Return an array containing all the keys of the hash // Return an array containing all the keys of the hash
preset: true, preset: true,
key: "key1", key: "HkeysKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HKEYS", "key1"}, command: []string{"HKEYS", "HkeysKey1"},
expectedResponse: []string{"field1", "field2", "field3"}, expectedResponse: []string{"field1", "field2", "field3"},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Empty array response when trying to call HKEYS on non-existent key { // Empty array response when trying to call HKEYS on non-existent key
preset: false, preset: false,
key: "key2", key: "HkeysKey2",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HKEYS", "key2"}, command: []string{"HKEYS", "HkeysKey2"},
expectedResponse: []string{}, expectedResponse: []string{},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "HkeysKey3",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HKEYS"}, command: []string{"HKEYS"},
expectedResponse: nil, expectedResponse: nil,
@@ -941,21 +936,21 @@ func Test_HandleHKeys(t *testing.T) {
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "HkeysKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HKEYS", "key4", "key4"}, command: []string{"HKEYS", "HkeysKey4", "HkeysKey4"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non hash map returns error { // Trying to get lengths on a non hash map returns error
preset: true, preset: true,
key: "key5", key: "HkeysKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HKEYS", "key5"}, command: []string{"HKEYS", "HkeysKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key5 is not a hash"), expectedError: errors.New("value at HkeysKey5 is not a hash"),
}, },
} }
@@ -1001,8 +996,6 @@ func Test_HandleHKeys(t *testing.T) {
} }
func Test_HandleHGETALL(t *testing.T) { func Test_HandleHGETALL(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -1015,25 +1008,25 @@ func Test_HandleHGETALL(t *testing.T) {
{ {
// Return an array containing all the fields and values of the hash // Return an array containing all the fields and values of the hash
preset: true, preset: true,
key: "key1", key: "HGetAllKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HGETALL", "key1"}, command: []string{"HGETALL", "HGetAllKey1"},
expectedResponse: []string{"field1", "value1", "field2", "123456789", "field3", "3.142"}, expectedResponse: []string{"field1", "value1", "field2", "123456789", "field3", "3.142"},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Empty array response when trying to call HGETALL on non-existent key { // Empty array response when trying to call HGETALL on non-existent key
preset: false, preset: false,
key: "key2", key: "HGetAllKey2",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HGETALL", "key2"}, command: []string{"HGETALL", "HGetAllKey2"},
expectedResponse: []string{}, expectedResponse: []string{},
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "HGetAllKey3",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HGETALL"}, command: []string{"HGETALL"},
expectedResponse: nil, expectedResponse: nil,
@@ -1042,21 +1035,21 @@ func Test_HandleHGETALL(t *testing.T) {
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "HGetAllKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HGETALL", "key4", "key4"}, command: []string{"HGETALL", "HGetAllKey4", "HGetAllKey4"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non hash map returns error { // Trying to get lengths on a non hash map returns error
preset: true, preset: true,
key: "key5", key: "HGetAllKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HGETALL", "key5"}, command: []string{"HGETALL", "HGetAllKey5"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key5 is not a hash"), expectedError: errors.New("value at HGetAllKey5 is not a hash"),
}, },
} }
@@ -1113,8 +1106,6 @@ func Test_HandleHGETALL(t *testing.T) {
} }
func Test_HandleHEXISTS(t *testing.T) { func Test_HandleHEXISTS(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -1127,48 +1118,48 @@ func Test_HandleHEXISTS(t *testing.T) {
{ {
// Return 1 if the field exists in the hash // Return 1 if the field exists in the hash
preset: true, preset: true,
key: "key1", key: "HexistsKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142},
command: []string{"HEXISTS", "key1", "field1"}, command: []string{"HEXISTS", "HexistsKey1", "field1"},
expectedResponse: 1, expectedResponse: 1,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // 0 response when trying to call HEXISTS on non-existent key { // 0 response when trying to call HEXISTS on non-existent key
preset: false, preset: false,
key: "key2", key: "HexistsKey2",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HEXISTS", "key2", "field1"}, command: []string{"HEXISTS", "HexistsKey2", "field1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "HexistsKey3",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HEXISTS", "key3"}, command: []string{"HEXISTS", "HexistsKey3"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "HexistsKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HEXISTS", "key4", "field1", "field2"}, command: []string{"HEXISTS", "HexistsKey4", "field1", "field2"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non hash map returns error { // Trying to get lengths on a non hash map returns error
preset: true, preset: true,
key: "key5", key: "HexistsKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HEXISTS", "key5", "field1"}, command: []string{"HEXISTS", "HexistsKey5", "field1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key5 is not a hash"), expectedError: errors.New("value at HexistsKey5 is not a hash"),
}, },
} }
@@ -1207,8 +1198,6 @@ func Test_HandleHEXISTS(t *testing.T) {
} }
func Test_HandleHDEL(t *testing.T) { func Test_HandleHDEL(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -1221,48 +1210,48 @@ func Test_HandleHDEL(t *testing.T) {
{ {
// Return count of deleted fields in the specified hash // Return count of deleted fields in the specified hash
preset: true, preset: true,
key: "key1", key: "HdelKey1",
presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142, "field7": "value7"}, presetValue: map[string]interface{}{"field1": "value1", "field2": 123456789, "field3": 3.142, "field7": "value7"},
command: []string{"HDEL", "key1", "field1", "field2", "field3", "field4", "field5", "field6"}, command: []string{"HDEL", "HdelKey1", "field1", "field2", "field3", "field4", "field5", "field6"},
expectedResponse: 3, expectedResponse: 3,
expectedValue: map[string]interface{}{"field1": nil, "field2": nil, "field3": nil, "field7": "value1"}, expectedValue: map[string]interface{}{"field1": nil, "field2": nil, "field3": nil, "field7": "value1"},
expectedError: nil, expectedError: nil,
}, },
{ // 0 response when passing delete fields that are non-existent on valid hash { // 0 response when passing delete fields that are non-existent on valid hash
preset: true, preset: true,
key: "key2", key: "HdelKey2",
presetValue: map[string]interface{}{"field1": "value1", "field2": "value2", "field3": "value3"}, presetValue: map[string]interface{}{"field1": "value1", "field2": "value2", "field3": "value3"},
command: []string{"HDEL", "key2", "field4", "field5", "field6"}, command: []string{"HDEL", "HdelKey2", "field4", "field5", "field6"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{"field1": "value1", "field2": "value2", "field3": "value3"}, expectedValue: map[string]interface{}{"field1": "value1", "field2": "value2", "field3": "value3"},
expectedError: nil, expectedError: nil,
}, },
{ // 0 response when trying to call HDEL on non-existent key { // 0 response when trying to call HDEL on non-existent key
preset: false, preset: false,
key: "key3", key: "HdelKey3",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HDEL", "key3", "field1"}, command: []string{"HDEL", "HdelKey3", "field1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key4", key: "HdelKey4",
presetValue: map[string]interface{}{}, presetValue: map[string]interface{}{},
command: []string{"HDEL", "key4"}, command: []string{"HDEL", "HdelKey4"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non hash map returns error { // Trying to get lengths on a non hash map returns error
preset: true, preset: true,
key: "key5", key: "HdelKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"HDEL", "key5", "field1"}, command: []string{"HDEL", "HdelKey5", "field1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: map[string]interface{}{}, expectedValue: map[string]interface{}{},
expectedError: errors.New("value at key5 is not a hash"), expectedError: errors.New("value at HdelKey5 is not a hash"),
}, },
} }

View File

@@ -11,9 +11,18 @@ import (
"testing" "testing"
) )
func Test_HandleLLEN(t *testing.T) { var mockServer *server.Server
mockServer := server.NewServer(server.Opts{})
func init() {
mockServer = server.NewServer(server.Opts{
Config: utils.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
},
})
}
func Test_HandleLLEN(t *testing.T) {
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -25,25 +34,25 @@ func Test_HandleLLEN(t *testing.T) {
}{ }{
{ // If key exists and is a list, return the lists length { // If key exists and is a list, return the lists length
preset: true, preset: true,
key: "key1", key: "LlenKey1",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LLEN", "key1"}, command: []string{"LLEN", "LlenKey1"},
expectedResponse: 4, expectedResponse: 4,
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // If key does not exist, return 0 { // If key does not exist, return 0
preset: false, preset: false,
key: "key2", key: "LlenKey2",
presetValue: nil, presetValue: nil,
command: []string{"LLEN", "key2"}, command: []string{"LLEN", "LlenKey2"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "LlenKey3",
presetValue: nil, presetValue: nil,
command: []string{"LLEN"}, command: []string{"LLEN"},
expectedResponse: 0, expectedResponse: 0,
@@ -52,18 +61,18 @@ func Test_HandleLLEN(t *testing.T) {
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "LlenKey4",
presetValue: nil, presetValue: nil,
command: []string{"LLEN", "key4", "key4"}, command: []string{"LLEN", "LlenKey4", "LlenKey4"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get lengths on a non-list returns error { // Trying to get lengths on a non-list returns error
preset: true, preset: true,
key: "key5", key: "LlenKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LLEN", "key5"}, command: []string{"LLEN", "LlenKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LLEN command on non-list item"), expectedError: errors.New("LLEN command on non-list item"),
@@ -101,8 +110,6 @@ func Test_HandleLLEN(t *testing.T) {
} }
func Test_HandleLINDEX(t *testing.T) { func Test_HandleLINDEX(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -114,90 +121,90 @@ func Test_HandleLINDEX(t *testing.T) {
}{ }{
{ // Return last element within range { // Return last element within range
preset: true, preset: true,
key: "key1", key: "LindexKey1",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LINDEX", "key1", "3"}, command: []string{"LINDEX", "LindexKey1", "3"},
expectedResponse: "value4", expectedResponse: "value4",
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // Return first element within range { // Return first element within range
preset: true, preset: true,
key: "key2", key: "LindexKey2",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LINDEX", "key1", "0"}, command: []string{"LINDEX", "LindexKey1", "0"},
expectedResponse: "value1", expectedResponse: "value1",
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // Return middle element within range { // Return middle element within range
preset: true, preset: true,
key: "key3", key: "LindexKey3",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LINDEX", "key1", "1"}, command: []string{"LINDEX", "LindexKey1", "1"},
expectedResponse: "value2", expectedResponse: "value2",
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // If key does not exist, return error { // If key does not exist, return error
preset: false, preset: false,
key: "key4", key: "LindexKey4",
presetValue: nil, presetValue: nil,
command: []string{"LINDEX", "key4", "0"}, command: []string{"LINDEX", "LindexKey4", "0"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LINDEX command on non-list item"), expectedError: errors.New("LINDEX command on non-list item"),
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "LindexKey3",
presetValue: nil, presetValue: nil,
command: []string{"LINDEX", "key3"}, command: []string{"LINDEX", "LindexKey3"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "LindexKey4",
presetValue: nil, presetValue: nil,
command: []string{"LINDEX", "key4", "0", "20"}, command: []string{"LINDEX", "LindexKey4", "0", "20"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get element by index on a non-list returns error { // Trying to get element by index on a non-list returns error
preset: true, preset: true,
key: "key5", key: "LindexKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LINDEX", "key5", "0"}, command: []string{"LINDEX", "LindexKey5", "0"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LINDEX command on non-list item"), expectedError: errors.New("LINDEX command on non-list item"),
}, },
{ // Trying to get index out of range index beyond last index { // Trying to get index out of range index beyond last index
preset: true, preset: true,
key: "key6", key: "LindexKey6",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LINDEX", "key6", "3"}, command: []string{"LINDEX", "LindexKey6", "3"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("index must be within list range"), expectedError: errors.New("index must be within list range"),
}, },
{ // Trying to get index out of range with negative index { // Trying to get index out of range with negative index
preset: true, preset: true,
key: "key7", key: "LindexKey7",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LINDEX", "key7", "-1"}, command: []string{"LINDEX", "LindexKey7", "-1"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("index must be within list range"), expectedError: errors.New("index must be within list range"),
}, },
{ // Return error when index is not an integer { // Return error when index is not an integer
preset: false, preset: false,
key: "key8", key: "LindexKey8",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LINDEX", "key8", "index"}, command: []string{"LINDEX", "LindexKey8", "index"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("index must be an integer"), expectedError: errors.New("index must be an integer"),
@@ -235,8 +242,6 @@ func Test_HandleLINDEX(t *testing.T) {
} }
func Test_HandleLRANGE(t *testing.T) { func Test_HandleLRANGE(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -251,108 +256,108 @@ func Test_HandleLRANGE(t *testing.T) {
// Both start and end indices are positive. // Both start and end indices are positive.
// End index is greater than start index. // End index is greater than start index.
preset: true, preset: true,
key: "key1", key: "LrangeKey1",
presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
command: []string{"LRANGE", "key1", "3", "6"}, command: []string{"LRANGE", "LrangeKey1", "3", "6"},
expectedResponse: []interface{}{"value4", "value5", "value6", "value7"}, expectedResponse: []interface{}{"value4", "value5", "value6", "value7"},
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // Return sub-list from start index to the end of the list when end index is -1 { // Return sub-list from start index to the end of the list when end index is -1
preset: true, preset: true,
key: "key2", key: "LrangeKey2",
presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
command: []string{"LRANGE", "key2", "3", "-1"}, command: []string{"LRANGE", "LrangeKey2", "3", "-1"},
expectedResponse: []interface{}{"value4", "value5", "value6", "value7", "value8"}, expectedResponse: []interface{}{"value4", "value5", "value6", "value7", "value8"},
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // Return the reversed sub-list when the end index is greater than -1 but less than start index { // Return the reversed sub-list when the end index is greater than -1 but less than start index
preset: true, preset: true,
key: "key3", key: "LrangeKey3",
presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
command: []string{"LRANGE", "key3", "3", "0"}, command: []string{"LRANGE", "LrangeKey3", "3", "0"},
expectedResponse: []interface{}{"value4", "value3", "value2", "value1"}, expectedResponse: []interface{}{"value4", "value3", "value2", "value1"},
expectedValue: nil, expectedValue: nil,
expectedError: nil, expectedError: nil,
}, },
{ // If key does not exist, return error { // If key does not exist, return error
preset: false, preset: false,
key: "key4", key: "LrangeKey4",
presetValue: nil, presetValue: nil,
command: []string{"LRANGE", "key4", "0", "2"}, command: []string{"LRANGE", "LrangeKey4", "0", "2"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LRANGE command on non-list item"), expectedError: errors.New("LRANGE command on non-list item"),
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key5", key: "LrangeKey5",
presetValue: nil, presetValue: nil,
command: []string{"LRANGE", "key5"}, command: []string{"LRANGE", "LrangeKey5"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key6", key: "LrangeKey6",
presetValue: nil, presetValue: nil,
command: []string{"LRANGE", "key6", "0", "element", "element"}, command: []string{"LRANGE", "LrangeKey6", "0", "element", "element"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Error when executing command on non-list command { // Error when executing command on non-list command
preset: true, preset: true,
key: "key5", key: "LrangeKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LRANGE", "key5", "0", "3"}, command: []string{"LRANGE", "LrangeKey5", "0", "3"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LRANGE command on non-list item"), expectedError: errors.New("LRANGE command on non-list item"),
}, },
{ // Error when start index is less than 0 { // Error when start index is less than 0
preset: true, preset: true,
key: "key7", key: "LrangeKey7",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LRANGE", "key7", "-1", "3"}, command: []string{"LRANGE", "LrangeKey7", "-1", "3"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start index must be within list boundary"), expectedError: errors.New("start index must be within list boundary"),
}, },
{ // Error when start index is higher than the length of the list { // Error when start index is higher than the length of the list
preset: true, preset: true,
key: "key8", key: "LrangeKey8",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LRANGE", "key8", "10", "11"}, command: []string{"LRANGE", "LrangeKey8", "10", "11"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start index must be within list boundary"), expectedError: errors.New("start index must be within list boundary"),
}, },
{ // Return error when start index is not an integer { // Return error when start index is not an integer
preset: false, preset: false,
key: "key9", key: "LrangeKey9",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LRANGE", "key9", "start", "7"}, command: []string{"LRANGE", "LrangeKey9", "start", "7"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start and end indices must be integers"), expectedError: errors.New("start and end indices must be integers"),
}, },
{ // Return error when end index is not an integer { // Return error when end index is not an integer
preset: false, preset: false,
key: "key10", key: "LrangeKey10",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LRANGE", "key10", "0", "end"}, command: []string{"LRANGE", "LrangeKey10", "0", "end"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start and end indices must be integers"), expectedError: errors.New("start and end indices must be integers"),
}, },
{ // Error when start and end indices are equal { // Error when start and end indices are equal
preset: true, preset: true,
key: "key11", key: "LrangeKey11",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LRANGE", "key11", "1", "1"}, command: []string{"LRANGE", "LrangeKey11", "1", "1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start and end indices cannot be equal"), expectedError: errors.New("start and end indices cannot be equal"),
@@ -396,8 +401,6 @@ func Test_HandleLRANGE(t *testing.T) {
} }
func Test_HandleLSET(t *testing.T) { func Test_HandleLSET(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -409,90 +412,90 @@ func Test_HandleLSET(t *testing.T) {
}{ }{
{ // Return last element within range { // Return last element within range
preset: true, preset: true,
key: "key1", key: "LsetKey1",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LSET", "key1", "3", "new-value"}, command: []string{"LSET", "LsetKey1", "3", "new-value"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value1", "value2", "value3", "new-value"}, expectedValue: []interface{}{"value1", "value2", "value3", "new-value"},
expectedError: nil, expectedError: nil,
}, },
{ // Return first element within range { // Return first element within range
preset: true, preset: true,
key: "key2", key: "LsetKey2",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LSET", "key2", "0", "new-value"}, command: []string{"LSET", "LsetKey2", "0", "new-value"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"new-value", "value2", "value3", "value4"}, expectedValue: []interface{}{"new-value", "value2", "value3", "value4"},
expectedError: nil, expectedError: nil,
}, },
{ // Return middle element within range { // Return middle element within range
preset: true, preset: true,
key: "key3", key: "LsetKey3",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LSET", "key3", "1", "new-value"}, command: []string{"LSET", "LsetKey3", "1", "new-value"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value1", "new-value", "value3", "value4"}, expectedValue: []interface{}{"value1", "new-value", "value3", "value4"},
expectedError: nil, expectedError: nil,
}, },
{ // If key does not exist, return error { // If key does not exist, return error
preset: false, preset: false,
key: "key4", key: "LsetKey4",
presetValue: nil, presetValue: nil,
command: []string{"LSET", "key4", "0", "element"}, command: []string{"LSET", "LsetKey4", "0", "element"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LSET command on non-list item"), expectedError: errors.New("LSET command on non-list item"),
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key5", key: "LsetKey5",
presetValue: nil, presetValue: nil,
command: []string{"LSET", "key5"}, command: []string{"LSET", "LsetKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key6", key: "LsetKey6",
presetValue: nil, presetValue: nil,
command: []string{"LSET", "key6", "0", "element", "element"}, command: []string{"LSET", "LsetKey6", "0", "element", "element"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get element by index on a non-list returns error { // Trying to get element by index on a non-list returns error
preset: true, preset: true,
key: "key5", key: "LsetKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LSET", "key5", "0", "element"}, command: []string{"LSET", "LsetKey5", "0", "element"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LSET command on non-list item"), expectedError: errors.New("LSET command on non-list item"),
}, },
{ // Trying to get index out of range index beyond last index { // Trying to get index out of range index beyond last index
preset: true, preset: true,
key: "key6", key: "LsetKey6",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LSET", "key6", "3", "element"}, command: []string{"LSET", "LsetKey6", "3", "element"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("index must be within list range"), expectedError: errors.New("index must be within list range"),
}, },
{ // Trying to get index out of range with negative index { // Trying to get index out of range with negative index
preset: true, preset: true,
key: "key7", key: "LsetKey7",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LSET", "key7", "-1", "element"}, command: []string{"LSET", "LsetKey7", "-1", "element"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("index must be within list range"), expectedError: errors.New("index must be within list range"),
}, },
{ // Return error when index is not an integer { // Return error when index is not an integer
preset: false, preset: false,
key: "key8", key: "LsetKey8",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LSET", "key8", "index", "element"}, command: []string{"LSET", "LsetKey8", "index", "element"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("index must be an integer"), expectedError: errors.New("index must be an integer"),
@@ -546,8 +549,6 @@ func Test_HandleLSET(t *testing.T) {
} }
func Test_HandleLTRIM(t *testing.T) { func Test_HandleLTRIM(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -562,99 +563,99 @@ func Test_HandleLTRIM(t *testing.T) {
// Both start and end indices are positive. // Both start and end indices are positive.
// End index is greater than start index. // End index is greater than start index.
preset: true, preset: true,
key: "key1", key: "LtrimKey1",
presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
command: []string{"LTRIM", "key1", "3", "6"}, command: []string{"LTRIM", "LtrimKey1", "3", "6"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value4", "value5", "value6"}, expectedValue: []interface{}{"value4", "value5", "value6"},
expectedError: nil, expectedError: nil,
}, },
{ // Return element from start index to end index when end index is greater than length of the list { // Return element from start index to end index when end index is greater than length of the list
preset: true, preset: true,
key: "key2", key: "LtrimKey2",
presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"}, presetValue: []interface{}{"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"},
command: []string{"LTRIM", "key2", "5", "-1"}, command: []string{"LTRIM", "LtrimKey2", "5", "-1"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value6", "value7", "value8"}, expectedValue: []interface{}{"value6", "value7", "value8"},
expectedError: nil, expectedError: nil,
}, },
{ // Return error when end index is smaller than start index but greater than -1 { // Return error when end index is smaller than start index but greater than -1
preset: true, preset: true,
key: "key3", key: "LtrimKey3",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LTRIM", "key3", "3", "1"}, command: []string{"LTRIM", "LtrimKey3", "3", "1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("end index must be greater than start index or -1"), expectedError: errors.New("end index must be greater than start index or -1"),
}, },
{ // If key does not exist, return error { // If key does not exist, return error
preset: false, preset: false,
key: "key4", key: "LtrimKey4",
presetValue: nil, presetValue: nil,
command: []string{"LTRIM", "key4", "0", "2"}, command: []string{"LTRIM", "LtrimKey4", "0", "2"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LTRIM command on non-list item"), expectedError: errors.New("LTRIM command on non-list item"),
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key5", key: "LtrimKey5",
presetValue: nil, presetValue: nil,
command: []string{"LTRIM", "key5"}, command: []string{"LTRIM", "LtrimKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key6", key: "LtrimKey6",
presetValue: nil, presetValue: nil,
command: []string{"LTRIM", "key6", "0", "element", "element"}, command: []string{"LTRIM", "LtrimKey6", "0", "element", "element"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to get element by index on a non-list returns error { // Trying to get element by index on a non-list returns error
preset: true, preset: true,
key: "key5", key: "LtrimKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LTRIM", "key5", "0", "3"}, command: []string{"LTRIM", "LtrimKey5", "0", "3"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LTRIM command on non-list item"), expectedError: errors.New("LTRIM command on non-list item"),
}, },
{ // Error when start index is less than 0 { // Error when start index is less than 0
preset: true, preset: true,
key: "key7", key: "LtrimKey7",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LTRIM", "key7", "-1", "3"}, command: []string{"LTRIM", "LtrimKey7", "-1", "3"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start index must be within list boundary"), expectedError: errors.New("start index must be within list boundary"),
}, },
{ // Error when start index is higher than the length of the list { // Error when start index is higher than the length of the list
preset: true, preset: true,
key: "key8", key: "LtrimKey8",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LTRIM", "key8", "10", "11"}, command: []string{"LTRIM", "LtrimKey8", "10", "11"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start index must be within list boundary"), expectedError: errors.New("start index must be within list boundary"),
}, },
{ // Return error when start index is not an integer { // Return error when start index is not an integer
preset: false, preset: false,
key: "key9", key: "LtrimKey9",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LTRIM", "key9", "start", "7"}, command: []string{"LTRIM", "LtrimKey9", "start", "7"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start and end indices must be integers"), expectedError: errors.New("start and end indices must be integers"),
}, },
{ // Return error when end index is not an integer { // Return error when end index is not an integer
preset: false, preset: false,
key: "key10", key: "LtrimKey10",
presetValue: []interface{}{"value1", "value2", "value3"}, presetValue: []interface{}{"value1", "value2", "value3"},
command: []string{"LTRIM", "key10", "0", "end"}, command: []string{"LTRIM", "LtrimKey10", "0", "end"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("start and end indices must be integers"), expectedError: errors.New("start and end indices must be integers"),
@@ -708,8 +709,6 @@ func Test_HandleLTRIM(t *testing.T) {
} }
func Test_HandleLREM(t *testing.T) { func Test_HandleLREM(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -721,63 +720,63 @@ func Test_HandleLREM(t *testing.T) {
}{ }{
{ // Remove the first 3 elements that appear in the list { // Remove the first 3 elements that appear in the list
preset: true, preset: true,
key: "key1", key: "LremKey1",
presetValue: []interface{}{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"}, presetValue: []interface{}{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"},
command: []string{"LREM", "key1", "3", "4"}, command: []string{"LREM", "LremKey1", "3", "4"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"1", "2", "5", "6", "7", "8", "4", "9", "10", "5", "4"}, expectedValue: []interface{}{"1", "2", "5", "6", "7", "8", "4", "9", "10", "5", "4"},
expectedError: nil, expectedError: nil,
}, },
{ // Remove the last 3 elements that appear in the list { // Remove the last 3 elements that appear in the list
preset: true, preset: true,
key: "key1", key: "LremKey1",
presetValue: []interface{}{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"}, presetValue: []interface{}{"1", "2", "4", "4", "5", "6", "7", "4", "8", "4", "9", "10", "5", "4"},
command: []string{"LREM", "key1", "-3", "4"}, command: []string{"LREM", "LremKey1", "-3", "4"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"1", "2", "4", "4", "5", "6", "7", "8", "9", "10", "5"}, expectedValue: []interface{}{"1", "2", "4", "4", "5", "6", "7", "8", "9", "10", "5"},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key5", key: "LremKey5",
presetValue: nil, presetValue: nil,
command: []string{"LREM", "key5"}, command: []string{"LREM", "LremKey5"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key6", key: "LremKey6",
presetValue: nil, presetValue: nil,
command: []string{"LREM", "key6", "0", "element", "element"}, command: []string{"LREM", "LremKey6", "0", "element", "element"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Throw error when count is not an integer { // Throw error when count is not an integer
preset: false, preset: false,
key: "key7", key: "LremKey7",
presetValue: nil, presetValue: nil,
command: []string{"LREM", "key7", "count", "value1"}, command: []string{"LREM", "LremKey7", "count", "value1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("count must be an integer"), expectedError: errors.New("count must be an integer"),
}, },
{ // Throw error on non-list item { // Throw error on non-list item
preset: true, preset: true,
key: "key8", key: "LremKey8",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LREM", "key8", "0", "value1"}, command: []string{"LREM", "LremKey8", "0", "value1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LREM command on non-list item"), expectedError: errors.New("LREM command on non-list item"),
}, },
{ // Throw error on non-existent item { // Throw error on non-existent item
preset: false, preset: false,
key: "key9", key: "LremKey9",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LREM", "key9", "0", "value1"}, command: []string{"LREM", "LremKey9", "0", "value1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LREM command on non-list item"), expectedError: errors.New("LREM command on non-list item"),
@@ -831,8 +830,6 @@ func Test_HandleLREM(t *testing.T) {
} }
func Test_HandleLMOVE(t *testing.T) { func Test_HandleLMOVE(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
presetValue map[string]interface{} presetValue map[string]interface{}
@@ -1050,8 +1047,6 @@ func Test_HandleLMOVE(t *testing.T) {
} }
func Test_HandleLPUSH(t *testing.T) { func Test_HandleLPUSH(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -1063,45 +1058,45 @@ func Test_HandleLPUSH(t *testing.T) {
}{ }{
{ // LPUSHX to existing list prepends the element to the list { // LPUSHX to existing list prepends the element to the list
preset: true, preset: true,
key: "key1", key: "LpushKey1",
presetValue: []interface{}{"1", "2", "4", "5"}, presetValue: []interface{}{"1", "2", "4", "5"},
command: []string{"LPUSHX", "key1", "value1", "value2"}, command: []string{"LPUSHX", "LpushKey1", "value1", "value2"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value1", "value2", "1", "2", "4", "5"}, expectedValue: []interface{}{"value1", "value2", "1", "2", "4", "5"},
expectedError: nil, expectedError: nil,
}, },
{ // LPUSH on existing list prepends the elements to the list { // LPUSH on existing list prepends the elements to the list
preset: true, preset: true,
key: "key2", key: "LpushKey2",
presetValue: []interface{}{"1", "2", "4", "5"}, presetValue: []interface{}{"1", "2", "4", "5"},
command: []string{"LPUSH", "key2", "value1", "value2"}, command: []string{"LPUSH", "LpushKey2", "value1", "value2"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value1", "value2", "1", "2", "4", "5"}, expectedValue: []interface{}{"value1", "value2", "1", "2", "4", "5"},
expectedError: nil, expectedError: nil,
}, },
{ // LPUSH on non-existent list creates the list { // LPUSH on non-existent list creates the list
preset: false, preset: false,
key: "key3", key: "LpushKey3",
presetValue: nil, presetValue: nil,
command: []string{"LPUSH", "key3", "value1", "value2"}, command: []string{"LPUSH", "LpushKey3", "value1", "value2"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value1", "value2"}, expectedValue: []interface{}{"value1", "value2"},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key5", key: "LpushKey5",
presetValue: nil, presetValue: nil,
command: []string{"LPUSH", "key5"}, command: []string{"LPUSH", "LpushKey5"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // LPUSHX command returns error on non-existent list { // LPUSHX command returns error on non-existent list
preset: false, preset: false,
key: "key6", key: "LpushKey6",
presetValue: nil, presetValue: nil,
command: []string{"LPUSHX", "key7", "count", "value1"}, command: []string{"LPUSHX", "LpushKey7", "count", "value1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LPUSHX command on non-list item"), expectedError: errors.New("LPUSHX command on non-list item"),
@@ -1155,8 +1150,6 @@ func Test_HandleLPUSH(t *testing.T) {
} }
func Test_HandleRPUSH(t *testing.T) { func Test_HandleRPUSH(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -1168,45 +1161,45 @@ func Test_HandleRPUSH(t *testing.T) {
}{ }{
{ // RPUSHX to existing list prepends the element to the list { // RPUSHX to existing list prepends the element to the list
preset: true, preset: true,
key: "key1", key: "RpushKey1",
presetValue: []interface{}{"1", "2", "4", "5"}, presetValue: []interface{}{"1", "2", "4", "5"},
command: []string{"RPUSHX", "key1", "value1", "value2"}, command: []string{"RPUSHX", "RpushKey1", "value1", "value2"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"1", "2", "4", "5", "value1", "value2"}, expectedValue: []interface{}{"1", "2", "4", "5", "value1", "value2"},
expectedError: nil, expectedError: nil,
}, },
{ // RPUSH on existing list prepends the elements to the list { // RPUSH on existing list prepends the elements to the list
preset: true, preset: true,
key: "key2", key: "RpushKey2",
presetValue: []interface{}{"1", "2", "4", "5"}, presetValue: []interface{}{"1", "2", "4", "5"},
command: []string{"RPUSH", "key2", "value1", "value2"}, command: []string{"RPUSH", "RpushKey2", "value1", "value2"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"1", "2", "4", "5", "value1", "value2"}, expectedValue: []interface{}{"1", "2", "4", "5", "value1", "value2"},
expectedError: nil, expectedError: nil,
}, },
{ // RPUSH on non-existent list creates the list { // RPUSH on non-existent list creates the list
preset: false, preset: false,
key: "key3", key: "RpushKey3",
presetValue: nil, presetValue: nil,
command: []string{"RPUSH", "key3", "value1", "value2"}, command: []string{"RPUSH", "RpushKey3", "value1", "value2"},
expectedResponse: "OK", expectedResponse: "OK",
expectedValue: []interface{}{"value1", "value2"}, expectedValue: []interface{}{"value1", "value2"},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key5", key: "RpushKey5",
presetValue: nil, presetValue: nil,
command: []string{"RPUSH", "key5"}, command: []string{"RPUSH", "RpushKey5"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // RPUSHX command returns error on non-existent list { // RPUSHX command returns error on non-existent list
preset: false, preset: false,
key: "key6", key: "RpushKey6",
presetValue: nil, presetValue: nil,
command: []string{"RPUSHX", "key7", "count", "value1"}, command: []string{"RPUSHX", "RpushKey7", "count", "value1"},
expectedResponse: nil, expectedResponse: nil,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("RPUSHX command on non-list item"), expectedError: errors.New("RPUSHX command on non-list item"),
@@ -1260,8 +1253,6 @@ func Test_HandleRPUSH(t *testing.T) {
} }
func Test_HandlePop(t *testing.T) { func Test_HandlePop(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -1273,25 +1264,25 @@ func Test_HandlePop(t *testing.T) {
}{ }{
{ // LPOP returns last element and removed first element from the list { // LPOP returns last element and removed first element from the list
preset: true, preset: true,
key: "key1", key: "PopKey1",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"LPOP", "key1"}, command: []string{"LPOP", "PopKey1"},
expectedResponse: "value1", expectedResponse: "value1",
expectedValue: []interface{}{"value2", "value3", "value4"}, expectedValue: []interface{}{"value2", "value3", "value4"},
expectedError: nil, expectedError: nil,
}, },
{ // RPOP returns last element and removed last element from the list { // RPOP returns last element and removed last element from the list
preset: true, preset: true,
key: "key2", key: "PopKey2",
presetValue: []interface{}{"value1", "value2", "value3", "value4"}, presetValue: []interface{}{"value1", "value2", "value3", "value4"},
command: []string{"RPOP", "key2"}, command: []string{"RPOP", "PopKey2"},
expectedResponse: "value4", expectedResponse: "value4",
expectedValue: []interface{}{"value1", "value2", "value3"}, expectedValue: []interface{}{"value1", "value2", "value3"},
expectedError: nil, expectedError: nil,
}, },
{ // Command too short { // Command too short
preset: false, preset: false,
key: "key3", key: "PopKey3",
presetValue: nil, presetValue: nil,
command: []string{"LPOP"}, command: []string{"LPOP"},
expectedResponse: 0, expectedResponse: 0,
@@ -1300,27 +1291,27 @@ func Test_HandlePop(t *testing.T) {
}, },
{ // Command too long { // Command too long
preset: false, preset: false,
key: "key4", key: "PopKey4",
presetValue: nil, presetValue: nil,
command: []string{"LPOP", "key4", "key4"}, command: []string{"LPOP", "PopKey4", "PopKey4"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
{ // Trying to execute LPOP from a non-list item return an error { // Trying to execute LPOP from a non-list item return an error
preset: true, preset: true,
key: "key5", key: "PopKey5",
presetValue: "Default value", presetValue: "Default value",
command: []string{"LPOP", "key5"}, command: []string{"LPOP", "PopKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("LPOP command on non-list item"), expectedError: errors.New("LPOP command on non-list item"),
}, },
{ // Trying to execute RPOP from a non-list item return an error { // Trying to execute RPOP from a non-list item return an error
preset: true, preset: true,
key: "key6", key: "PopKey6",
presetValue: "Default value", presetValue: "Default value",
command: []string{"RPOP", "key6"}, command: []string{"RPOP", "PopKey6"},
expectedResponse: 0, expectedResponse: 0,
expectedValue: nil, expectedValue: nil,
expectedError: errors.New("RPOP command on non-list item"), expectedError: errors.New("RPOP command on non-list item"),

View File

@@ -1,6 +1,21 @@
package pubsub package pubsub
import "testing" import (
"github.com/echovault/echovault/src/server"
"github.com/echovault/echovault/src/utils"
"testing"
)
var mockServer *server.Server
func init() {
mockServer = server.NewServer(server.Opts{
Config: utils.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
},
})
}
func Test_HandleSubscribe(t *testing.T) { func Test_HandleSubscribe(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

@@ -12,9 +12,18 @@ import (
"testing" "testing"
) )
func Test_HandleSetRange(t *testing.T) { var mockServer *server.Server
mockServer := server.NewServer(server.Opts{})
func init() {
mockServer = server.NewServer(server.Opts{
Config: utils.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
},
})
}
func Test_HandleSetRange(t *testing.T) {
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -26,54 +35,54 @@ func Test_HandleSetRange(t *testing.T) {
}{ }{
{ // Test that SETRANGE on non-existent string creates new string { // Test that SETRANGE on non-existent string creates new string
preset: false, preset: false,
key: "test1", key: "SetRangeKey1",
presetValue: "", presetValue: "",
command: []string{"SETRANGE", "test1", "10", "New String Value"}, command: []string{"SETRANGE", "SetRangeKey1", "10", "New String Value"},
expectedValue: "New String Value", expectedValue: "New String Value",
expectedResponse: len("New String Value"), expectedResponse: len("New String Value"),
expectedError: nil, expectedError: nil,
}, },
{ // Test SETRANGE with an offset that leads to a longer resulting string { // Test SETRANGE with an offset that leads to a longer resulting string
preset: true, preset: true,
key: "test2", key: "SetRangeKey2",
presetValue: "Original String Value", presetValue: "Original String Value",
command: []string{"SETRANGE", "test2", "16", "Portion Replaced With This New String"}, command: []string{"SETRANGE", "SetRangeKey2", "16", "Portion Replaced With This New String"},
expectedValue: "Original String Portion Replaced With This New String", expectedValue: "Original String Portion Replaced With This New String",
expectedResponse: len("Original String Portion Replaced With This New String"), expectedResponse: len("Original String Portion Replaced With This New String"),
expectedError: nil, expectedError: nil,
}, },
{ // SETRANGE with negative offset prepends the string { // SETRANGE with negative offset prepends the string
preset: true, preset: true,
key: "test3", key: "SetRangeKey3",
presetValue: "This is a preset value", presetValue: "This is a preset value",
command: []string{"SETRANGE", "test3", "-10", "Prepended "}, command: []string{"SETRANGE", "SetRangeKey3", "-10", "Prepended "},
expectedValue: "Prepended This is a preset value", expectedValue: "Prepended This is a preset value",
expectedResponse: len("Prepended This is a preset value"), expectedResponse: len("Prepended This is a preset value"),
expectedError: nil, expectedError: nil,
}, },
{ // SETRANGE with offset that embeds new string inside the old string { // SETRANGE with offset that embeds new string inside the old string
preset: true, preset: true,
key: "test4", key: "SetRangeKey4",
presetValue: "This is a preset value", presetValue: "This is a preset value",
command: []string{"SETRANGE", "test4", "0", "That"}, command: []string{"SETRANGE", "SetRangeKey4", "0", "That"},
expectedValue: "That is a preset value", expectedValue: "That is a preset value",
expectedResponse: len("That is a preset value"), expectedResponse: len("That is a preset value"),
expectedError: nil, expectedError: nil,
}, },
{ // SETRANGE with offset longer than original lengths appends the string { // SETRANGE with offset longer than original lengths appends the string
preset: true, preset: true,
key: "test5", key: "SetRangeKey5",
presetValue: "This is a preset value", presetValue: "This is a preset value",
command: []string{"SETRANGE", "test5", "100", " Appended"}, command: []string{"SETRANGE", "SetRangeKey5", "100", " Appended"},
expectedValue: "This is a preset value Appended", expectedValue: "This is a preset value Appended",
expectedResponse: len("This is a preset value Appended"), expectedResponse: len("This is a preset value Appended"),
expectedError: nil, expectedError: nil,
}, },
{ // SETRANGE with offset on the last character replaces last character with new string { // SETRANGE with offset on the last character replaces last character with new string
preset: true, preset: true,
key: "test6", key: "SetRangeKey6",
presetValue: "This is a preset value", presetValue: "This is a preset value",
command: []string{"SETRANGE", "test6", strconv.Itoa(len("This is a preset value") - 1), " replaced"}, command: []string{"SETRANGE", "SetRangeKey6", strconv.Itoa(len("This is a preset value") - 1), " replaced"},
expectedValue: "This is a preset valu replaced", expectedValue: "This is a preset valu replaced",
expectedResponse: len("This is a preset valu replaced"), expectedResponse: len("This is a preset valu replaced"),
expectedError: nil, expectedError: nil,
@@ -155,8 +164,6 @@ func Test_HandleSetRange(t *testing.T) {
} }
func Test_HandleStrLen(t *testing.T) { func Test_HandleStrLen(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -167,23 +174,23 @@ func Test_HandleStrLen(t *testing.T) {
}{ }{
{ // Return the correct string length for an existing string { // Return the correct string length for an existing string
preset: true, preset: true,
key: "test1", key: "StrLenKey1",
presetValue: "Test String", presetValue: "Test String",
command: []string{"STRLEN", "test1"}, command: []string{"STRLEN", "StrLenKey1"},
expectedResponse: len("Test String"), expectedResponse: len("Test String"),
expectedError: nil, expectedError: nil,
}, },
{ // If the string does not exist, return 0 { // If the string does not exist, return 0
preset: false, preset: false,
key: "test2", key: "StrLenKey2",
presetValue: "", presetValue: "",
command: []string{"STRLEN", "test2"}, command: []string{"STRLEN", "StrLenKey2"},
expectedResponse: 0, expectedResponse: 0,
expectedError: nil, expectedError: nil,
}, },
{ // Too few args { // Too few args
preset: false, preset: false,
key: "test3", key: "StrLenKey3",
presetValue: "", presetValue: "",
command: []string{"STRLEN"}, command: []string{"STRLEN"},
expectedResponse: 0, expectedResponse: 0,
@@ -191,9 +198,9 @@ func Test_HandleStrLen(t *testing.T) {
}, },
{ // Too many args { // Too many args
preset: false, preset: false,
key: "test4", key: "StrLenKey4",
presetValue: "", presetValue: "",
command: []string{"STRLEN", "test4", "test5"}, command: []string{"STRLEN", "StrLenKey4", "StrLenKey5"},
expectedResponse: 0, expectedResponse: 0,
expectedError: errors.New(utils.WrongArgsResponse), expectedError: errors.New(utils.WrongArgsResponse),
}, },
@@ -231,8 +238,6 @@ func Test_HandleStrLen(t *testing.T) {
} }
func Test_HandleSubStr(t *testing.T) { func Test_HandleSubStr(t *testing.T) {
mockServer := server.NewServer(server.Opts{})
tests := []struct { tests := []struct {
preset bool preset bool
key string key string
@@ -243,33 +248,33 @@ func Test_HandleSubStr(t *testing.T) {
}{ }{
{ // Return substring within the range of the string { // Return substring within the range of the string
preset: true, preset: true,
key: "test1", key: "SubStrKey1",
presetValue: "Test String One", presetValue: "Test String One",
command: []string{"SUBSTR", "test1", "5", "10"}, command: []string{"SUBSTR", "SubStrKey1", "5", "10"},
expectedResponse: "String", expectedResponse: "String",
expectedError: nil, expectedError: nil,
}, },
{ // Return substring at the end of the string with exact end index { // Return substring at the end of the string with exact end index
preset: true, preset: true,
key: "test2", key: "SubStrKey2",
presetValue: "Test String Two", presetValue: "Test String Two",
command: []string{"SUBSTR", "test2", "12", "14"}, command: []string{"SUBSTR", "SubStrKey2", "12", "14"},
expectedResponse: "Two", expectedResponse: "Two",
expectedError: nil, expectedError: nil,
}, },
{ // Return substring at the end of the string with end index greater than length { // Return substring at the end of the string with end index greater than length
preset: true, preset: true,
key: "test3", key: "SubStrKey3",
presetValue: "Test String Three", presetValue: "Test String Three",
command: []string{"SUBSTR", "test3", "12", "75"}, command: []string{"SUBSTR", "SubStrKey3", "12", "75"},
expectedResponse: "Three", expectedResponse: "Three",
expectedError: nil, expectedError: nil,
}, },
{ // Return the substring at the start of the string with 0 start index { // Return the substring at the start of the string with 0 start index
preset: true, preset: true,
key: "test4", key: "SubStrKey4",
presetValue: "Test String Four", presetValue: "Test String Four",
command: []string{"SUBSTR", "test4", "0", "3"}, command: []string{"SUBSTR", "SubStrKey4", "0", "3"},
expectedResponse: "Test", expectedResponse: "Test",
expectedError: nil, expectedError: nil,
}, },
@@ -277,9 +282,9 @@ func Test_HandleSubStr(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.
preset: true, preset: true,
key: "test5", key: "SubStrKey5",
presetValue: "Test String Five", presetValue: "Test String Five",
command: []string{"SUBSTR", "test5", "-11", "10"}, command: []string{"SUBSTR", "SubStrKey5", "-11", "10"},
expectedResponse: "String", expectedResponse: "String",
expectedError: nil, expectedError: nil,
}, },
@@ -287,9 +292,9 @@ func Test_HandleSubStr(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.
preset: true, preset: true,
key: "test6", key: "SubStrKey6",
presetValue: "Test String Six", presetValue: "Test String Six",
command: []string{"SUBSTR", "test6", "4", "0"}, command: []string{"SUBSTR", "SubStrKey6", "4", "0"},
expectedResponse: "tseT", expectedResponse: "tseT",
expectedError: nil, expectedError: nil,
}, },