Skip to content

Commit ae9f52e

Browse files
committed
feat(tests): Unit tests for all commands
Signed-off-by: Ankush Chavan <cankush625@gmail.com>
1 parent a982f6a commit ae9f52e

8 files changed

Lines changed: 144 additions & 4 deletions

File tree

cmd/command_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
// TestCommand tests the Command function for
10+
// all possible valid and invalid inputs
11+
func TestCommand(t *testing.T) {
12+
tests := []struct {
13+
command []string
14+
want []byte
15+
wantErr error
16+
}{
17+
{[]string{"COMMAND"}, []byte("+\r\n"), nil},
18+
{[]string{"COMMAND", "DOCS"}, []byte("+OK\r\n"), nil},
19+
{[]string{"COMMAND", "DOCS", "random_arg"}, []byte("+OK\r\n"), nil},
20+
}
21+
for _, test := range tests {
22+
if got, gotErr := Command(test.command); !reflect.DeepEqual(got, test.want) || !errors.Is(gotErr, test.wantErr) {
23+
t.Errorf("Command(%v) = %v, %v", test.command, got, gotErr)
24+
}
25+
}
26+
}

cmd/echo_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
// TestEcho tests the Echo function for
10+
// all possible valid and invalid inputs
11+
func TestEcho(t *testing.T) {
12+
tests := []struct {
13+
command []string
14+
want []byte
15+
wantErr error
16+
}{
17+
{[]string{"ECHO", "hello"}, []byte("+hello\r\n"), nil},
18+
{[]string{"ECHO"}, []byte("-message is required\r\n"), MessageRequiredError},
19+
}
20+
for _, test := range tests {
21+
if got, gotErr := Echo(test.command); !reflect.DeepEqual(got, test.want) || !errors.Is(gotErr, test.wantErr) {
22+
t.Errorf("Echo(%v) = %v, %v", test.command, got, gotErr)
23+
}
24+
}
25+
}

cmd/get_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
// TestGet tests the Get function for
10+
// all possible valid and invalid inputs
11+
func TestGet(t *testing.T) {
12+
// Prepare
13+
// Insert a key in the cache
14+
_, _ = Set([]string{"SET", "tenant", "ACME"})
15+
tests := []struct {
16+
command []string
17+
want []byte
18+
wantErr error
19+
}{
20+
// Key exists in the cache
21+
{[]string{"GET", "tenant"}, []byte("+ACME\r\n"), nil},
22+
// Key doesn't exist in the cache
23+
{[]string{"GET", "org"}, []byte("$-1\r\n"), nil},
24+
// Wrong number of arguments to the GET command
25+
{[]string{"GET"}, []byte("-wrong number of arguments\r\n"), WrongNumberOfArgumentsError},
26+
{[]string{"GET", "tenant", "random_arg"}, []byte("-wrong number of arguments\r\n"), WrongNumberOfArgumentsError},
27+
}
28+
for _, test := range tests {
29+
if got, gotErr := Get(test.command); !reflect.DeepEqual(got, test.want) || !errors.Is(gotErr, test.wantErr) {
30+
t.Errorf("Get(%v) = %v, %v", test.command, got, gotErr)
31+
}
32+
}
33+
}

cmd/ping_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
// TestPing tests the Ping function for
10+
// all possible valid and invalid inputs
11+
func TestPing(t *testing.T) {
12+
tests := []struct {
13+
command []string
14+
want []byte
15+
wantErr error
16+
}{
17+
{[]string{"PING"}, []byte("+PONG\r\n"), nil},
18+
{[]string{"PING", "Hello"}, []byte("+Hello\r\n"), nil},
19+
{[]string{"PING", "hello"}, []byte("+hello\r\n"), nil},
20+
}
21+
for _, test := range tests {
22+
if got, gotErr := Ping(test.command); !reflect.DeepEqual(got, test.want) || !errors.Is(gotErr, test.wantErr) {
23+
t.Errorf("Ping(%v) = %v, %v", test.command, got, gotErr)
24+
}
25+
}
26+
}

cmd/set_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
// TestSet tests the Set function for
10+
// all possible valid and invalid inputs
11+
func TestSet(t *testing.T) {
12+
tests := []struct {
13+
command []string
14+
want []byte
15+
wantErr error
16+
}{
17+
{[]string{"SET", "tenant", "ACME"}, []byte("+OK\r\n"), nil},
18+
{[]string{"SET", "tenant"}, []byte("-missing arguments\r\n"), MissingArgumentsError},
19+
{[]string{"SET"}, []byte("-missing arguments\r\n"), MissingArgumentsError},
20+
{[]string{"SET", "tenant", "ACME", "asd"}, []byte("-syntax error\r\n"), SyntaxError},
21+
}
22+
for _, test := range tests {
23+
if got, gotErr := Set(test.command); !reflect.DeepEqual(got, test.want) || !errors.Is(gotErr, test.wantErr) {
24+
t.Errorf("Set(%v) = %v, %v", test.command, got, gotErr)
25+
}
26+
}
27+
}

exc/command_executor.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package exc
33
import (
44
"HelixDB/cmd"
55
"HelixDB/common"
6-
"fmt"
6+
"errors"
77
"strings"
88
)
99

10+
var UnsupportedCommand = errors.New("unsupported command")
11+
1012
func ExecuteCommand(command []string) ([]byte, error) {
1113
switch strings.ToUpper(command[0]) {
1214
case common.Ping:
@@ -20,5 +22,5 @@ func ExecuteCommand(command []string) ([]byte, error) {
2022
case common.Set:
2123
return cmd.Set(command)
2224
}
23-
return []byte("-unsupported command\r\n"), fmt.Errorf("unsupported command")
25+
return []byte("-unsupported command\r\n"), UnsupportedCommand
2426
}

exc/command_executor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// TestExecuteCommand tests the ExecuteCommand function for
12-
// all possible valid inputs
12+
// all possible valid and invalid inputs
1313
func TestExecuteCommand(t *testing.T) {
1414
tests := []struct {
1515
command []string
@@ -37,6 +37,7 @@ func TestExecuteCommand(t *testing.T) {
3737
{[]string{"GET"}, []byte("-wrong number of arguments\r\n"), cmd.WrongNumberOfArgumentsError},
3838
{[]string{"GET", "tenant", "random_arg"}, []byte("-wrong number of arguments\r\n"), cmd.WrongNumberOfArgumentsError},
3939
// Unsupported/Invalid command
40+
{[]string{"UNSUPPORTED"}, []byte("-unsupported command\r\n"), UnsupportedCommand},
4041
}
4142
for _, test := range tests {
4243
if got, gotErr := ExecuteCommand(test.command); !reflect.DeepEqual(got, test.want) || !errors.Is(gotErr, test.wantErr) {

resp/parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
// TestParseCommand tests the ParseCommand function for
11-
// all possible valid inputs
11+
// all possible valid and invalid inputs
1212
func TestParseCommand(t *testing.T) {
1313
tests := []struct {
1414
command []byte

0 commit comments

Comments
 (0)