mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-15 04:20:57 +08:00
Created writer mock.
Created mock server. Added test for ping command plugin.
This commit is contained in:
@@ -39,8 +39,8 @@ func (server *Server) Lock() {
|
|||||||
server.data.mu.Lock()
|
server.data.mu.Lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Server *Server) Unlock() {
|
func (server *Server) Unlock() {
|
||||||
Server.data.mu.Unlock()
|
server.data.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) GetData(key string) interface{} {
|
func (server *Server) GetData(key string) interface{} {
|
||||||
|
33
server/plugins/commands/ping/ping_test.go
Normal file
33
server/plugins/commands/ping/ping_test.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kelvinmwinuka/memstore/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandleCommand(t *testing.T) {
|
||||||
|
server := &utils.MockServer{}
|
||||||
|
|
||||||
|
cw := &utils.CustomWriter{}
|
||||||
|
writer := bufio.NewWriter(cw)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
cmd []string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{[]string{"ping"}, "+PONG\r\n\n"},
|
||||||
|
{[]string{"ping", "Ping Test"}, "+Ping Test\r\n\n"},
|
||||||
|
{[]string{"ping", "Ping Test", "Error"}, "-Error wrong number of arguments for PING command\r\n\n"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
cw.Buf.Reset()
|
||||||
|
Plugin.HandleCommand(tt.cmd, server, writer)
|
||||||
|
if tt.expected != cw.Buf.String() {
|
||||||
|
t.Errorf("Expected %s, Got %s", strings.TrimSpace(tt.expected), strings.TrimSpace(cw.Buf.String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
utils/mock.go
Normal file
51
utils/mock.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CustomWriter struct {
|
||||||
|
Buf bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *CustomWriter) Write(p []byte) (int, error) {
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
for _, b := range p {
|
||||||
|
cw.Buf.WriteByte(b)
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if count != len(p) {
|
||||||
|
return count, io.ErrShortWrite
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type MockData struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
data map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type MockServer struct {
|
||||||
|
data MockData
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *MockServer) Lock() {
|
||||||
|
server.data.mu.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *MockServer) Unlock() {
|
||||||
|
server.data.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *MockServer) GetData(key string) interface{} {
|
||||||
|
return server.data.data[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *MockServer) SetData(key string, value interface{}) {
|
||||||
|
server.data.data[key] = value
|
||||||
|
}
|
Reference in New Issue
Block a user