mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-06 00:16:53 +08:00
Updated test suites to set mock server only once in each suite instead of instantiating it in every test.
This commit is contained in:
@@ -12,9 +12,18 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_HandleSetRange(t *testing.T) {
|
||||
mockServer := server.NewServer(server.Opts{})
|
||||
var mockServer *server.Server
|
||||
|
||||
func init() {
|
||||
mockServer = server.NewServer(server.Opts{
|
||||
Config: utils.Config{
|
||||
DataDir: "",
|
||||
EvictionPolicy: utils.NoEviction,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func Test_HandleSetRange(t *testing.T) {
|
||||
tests := []struct {
|
||||
preset bool
|
||||
key string
|
||||
@@ -26,54 +35,54 @@ func Test_HandleSetRange(t *testing.T) {
|
||||
}{
|
||||
{ // Test that SETRANGE on non-existent string creates new string
|
||||
preset: false,
|
||||
key: "test1",
|
||||
key: "SetRangeKey1",
|
||||
presetValue: "",
|
||||
command: []string{"SETRANGE", "test1", "10", "New String Value"},
|
||||
command: []string{"SETRANGE", "SetRangeKey1", "10", "New String Value"},
|
||||
expectedValue: "New String Value",
|
||||
expectedResponse: len("New String Value"),
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // Test SETRANGE with an offset that leads to a longer resulting string
|
||||
preset: true,
|
||||
key: "test2",
|
||||
key: "SetRangeKey2",
|
||||
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",
|
||||
expectedResponse: len("Original String Portion Replaced With This New String"),
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // SETRANGE with negative offset prepends the string
|
||||
preset: true,
|
||||
key: "test3",
|
||||
key: "SetRangeKey3",
|
||||
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",
|
||||
expectedResponse: len("Prepended This is a preset value"),
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // SETRANGE with offset that embeds new string inside the old string
|
||||
preset: true,
|
||||
key: "test4",
|
||||
key: "SetRangeKey4",
|
||||
presetValue: "This is a preset value",
|
||||
command: []string{"SETRANGE", "test4", "0", "That"},
|
||||
command: []string{"SETRANGE", "SetRangeKey4", "0", "That"},
|
||||
expectedValue: "That is a preset value",
|
||||
expectedResponse: len("That is a preset value"),
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // SETRANGE with offset longer than original lengths appends the string
|
||||
preset: true,
|
||||
key: "test5",
|
||||
key: "SetRangeKey5",
|
||||
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",
|
||||
expectedResponse: len("This is a preset value Appended"),
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // SETRANGE with offset on the last character replaces last character with new string
|
||||
preset: true,
|
||||
key: "test6",
|
||||
key: "SetRangeKey6",
|
||||
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",
|
||||
expectedResponse: len("This is a preset valu replaced"),
|
||||
expectedError: nil,
|
||||
@@ -155,8 +164,6 @@ func Test_HandleSetRange(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_HandleStrLen(t *testing.T) {
|
||||
mockServer := server.NewServer(server.Opts{})
|
||||
|
||||
tests := []struct {
|
||||
preset bool
|
||||
key string
|
||||
@@ -167,23 +174,23 @@ func Test_HandleStrLen(t *testing.T) {
|
||||
}{
|
||||
{ // Return the correct string length for an existing string
|
||||
preset: true,
|
||||
key: "test1",
|
||||
key: "StrLenKey1",
|
||||
presetValue: "Test String",
|
||||
command: []string{"STRLEN", "test1"},
|
||||
command: []string{"STRLEN", "StrLenKey1"},
|
||||
expectedResponse: len("Test String"),
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // If the string does not exist, return 0
|
||||
preset: false,
|
||||
key: "test2",
|
||||
key: "StrLenKey2",
|
||||
presetValue: "",
|
||||
command: []string{"STRLEN", "test2"},
|
||||
command: []string{"STRLEN", "StrLenKey2"},
|
||||
expectedResponse: 0,
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // Too few args
|
||||
preset: false,
|
||||
key: "test3",
|
||||
key: "StrLenKey3",
|
||||
presetValue: "",
|
||||
command: []string{"STRLEN"},
|
||||
expectedResponse: 0,
|
||||
@@ -191,9 +198,9 @@ func Test_HandleStrLen(t *testing.T) {
|
||||
},
|
||||
{ // Too many args
|
||||
preset: false,
|
||||
key: "test4",
|
||||
key: "StrLenKey4",
|
||||
presetValue: "",
|
||||
command: []string{"STRLEN", "test4", "test5"},
|
||||
command: []string{"STRLEN", "StrLenKey4", "StrLenKey5"},
|
||||
expectedResponse: 0,
|
||||
expectedError: errors.New(utils.WrongArgsResponse),
|
||||
},
|
||||
@@ -231,8 +238,6 @@ func Test_HandleStrLen(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_HandleSubStr(t *testing.T) {
|
||||
mockServer := server.NewServer(server.Opts{})
|
||||
|
||||
tests := []struct {
|
||||
preset bool
|
||||
key string
|
||||
@@ -243,33 +248,33 @@ func Test_HandleSubStr(t *testing.T) {
|
||||
}{
|
||||
{ // Return substring within the range of the string
|
||||
preset: true,
|
||||
key: "test1",
|
||||
key: "SubStrKey1",
|
||||
presetValue: "Test String One",
|
||||
command: []string{"SUBSTR", "test1", "5", "10"},
|
||||
command: []string{"SUBSTR", "SubStrKey1", "5", "10"},
|
||||
expectedResponse: "String",
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // Return substring at the end of the string with exact end index
|
||||
preset: true,
|
||||
key: "test2",
|
||||
key: "SubStrKey2",
|
||||
presetValue: "Test String Two",
|
||||
command: []string{"SUBSTR", "test2", "12", "14"},
|
||||
command: []string{"SUBSTR", "SubStrKey2", "12", "14"},
|
||||
expectedResponse: "Two",
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // Return substring at the end of the string with end index greater than length
|
||||
preset: true,
|
||||
key: "test3",
|
||||
key: "SubStrKey3",
|
||||
presetValue: "Test String Three",
|
||||
command: []string{"SUBSTR", "test3", "12", "75"},
|
||||
command: []string{"SUBSTR", "SubStrKey3", "12", "75"},
|
||||
expectedResponse: "Three",
|
||||
expectedError: nil,
|
||||
},
|
||||
{ // Return the substring at the start of the string with 0 start index
|
||||
preset: true,
|
||||
key: "test4",
|
||||
key: "SubStrKey4",
|
||||
presetValue: "Test String Four",
|
||||
command: []string{"SUBSTR", "test4", "0", "3"},
|
||||
command: []string{"SUBSTR", "SubStrKey4", "0", "3"},
|
||||
expectedResponse: "Test",
|
||||
expectedError: nil,
|
||||
},
|
||||
@@ -277,9 +282,9 @@ func Test_HandleSubStr(t *testing.T) {
|
||||
// Return the substring with negative start index.
|
||||
// Substring should begin abs(start) from the end of the string when start is negative.
|
||||
preset: true,
|
||||
key: "test5",
|
||||
key: "SubStrKey5",
|
||||
presetValue: "Test String Five",
|
||||
command: []string{"SUBSTR", "test5", "-11", "10"},
|
||||
command: []string{"SUBSTR", "SubStrKey5", "-11", "10"},
|
||||
expectedResponse: "String",
|
||||
expectedError: nil,
|
||||
},
|
||||
@@ -287,9 +292,9 @@ func Test_HandleSubStr(t *testing.T) {
|
||||
// Return reverse substring with end index smaller than start index.
|
||||
// When end index is smaller than start index, the 2 indices are reversed.
|
||||
preset: true,
|
||||
key: "test6",
|
||||
key: "SubStrKey6",
|
||||
presetValue: "Test String Six",
|
||||
command: []string{"SUBSTR", "test6", "4", "0"},
|
||||
command: []string{"SUBSTR", "SubStrKey6", "4", "0"},
|
||||
expectedResponse: "tseT",
|
||||
expectedError: nil,
|
||||
},
|
||||
|
Reference in New Issue
Block a user