Skip to content

Commit

Permalink
test: add test for SetCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
ThreeDP committed Feb 25, 2024
1 parent 9bec9ca commit 5d2263f
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 23 deletions.
6 changes: 6 additions & 0 deletions app/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func (t TTime) Now() time.Time {
return time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
}

func TestTimeStructNow(t *testing.T) {
time := TTime{}
expected := time.Now()
checkVarDate(t, expected, time.Now())
}

func checkVarValue(t *testing.T, ok bool, received, expected string) {
t.Helper()
if !ok {
Expand Down
24 changes: 24 additions & 0 deletions app/builtin/ping_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builtin

import (
"reflect"
"testing"
)

Expand All @@ -20,6 +21,29 @@ func TestPingBuiltinRequest(t *testing.T) {
})
}

func TestPingSets(t *testing.T) {
s := SetupFilesRDWR{}
s.config(nil)

t.Run("Test set Ping time now", func(t *testing.T) {
ping := Ping{Conn: s.Conn}
ping.SetTimeNow(s.TimeNow)

if ping.Now != s.TimeNow {
t.Errorf("Expected %v, but has %v\n", s.TimeNow, ping.Now)
}
})

t.Run("Test set Ping conn", func(t *testing.T) {
ping := Ping{}
ping.SetConn(s.Conn)

if !reflect.DeepEqual(ping.Conn, s.Conn) {
t.Errorf("Expected %v, but has %v\n", s.Conn, ping.Conn)
}
})
}

func BenchmarkPingBuiltin(b *testing.B) {
s := SetupFilesRDWR{}
s.config(nil)
Expand Down
2 changes: 1 addition & 1 deletion app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (s *RedisServer) SetCommands() {
"replconf": &builtin.ReplConf{Conn: nil, Env: s.Env, Now: time.Time{}},
}
s.Commands = func(key string, conn net.Conn, now time.Time) (builtin.Builtin, bool) {
elem, ok := commands[key]
elem, ok := commands[strings.ToLower(key)]
if ok {
elem.SetConn(conn)
elem.SetTimeNow(now)
Expand Down
128 changes: 106 additions & 22 deletions app/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ func checkInfosMap(t *testing.T,
}
}

func setupRedisServer() RedisServer {
return RedisServer{
Env: make(map[string]builtin.EnvData),
Time: TTime{},
Args: []string{}, //os.Args[1:]
Infos: make(map[string]map[string]string),
}
}

func validateSeverAddr(t *testing.T, s *RedisServer, expectedAddr *TListener, err error) {
t.Helper()
if err != nil {
t.Errorf("Error: listen on addr: %v", s.Listener.Addr())
}
if expectedAddr.Addr().Network() != s.Listener.Addr().Network() {
t.Errorf("Expected network %s, but has %s\n", expectedAddr.Addr().Network(), s.Listener.Addr().Network())
}
if expectedAddr.Addr().String() != s.Listener.Addr().String() {
t.Errorf("Expected host and port %s, but has %s\n", expectedAddr.Addr().String(), s.Listener.Addr().String())
}
}

func TestListenServer(t *testing.T) {

t.Run("Test Listen in default", func(t *testing.T) {
Expand Down Expand Up @@ -90,20 +112,6 @@ func TestListenServer(t *testing.T) {
})
}

func validateSeverAddr(t *testing.T, s *RedisServer, expectedAddr *TListener, err error) {
t.Helper()
if err != nil {
t.Errorf("Error: listen on addr: %v", s.Listener.Addr())
}
if expectedAddr.Addr().Network() != s.Listener.Addr().Network() {
t.Errorf("Expected network %s, but has %s\n", expectedAddr.Addr().Network(), s.Listener.Addr().Network())
}
if expectedAddr.Addr().String() != s.Listener.Addr().String() {
t.Errorf("Expected host and port %s, but has %s\n", expectedAddr.Addr().String(), s.Listener.Addr().String())
}
}


func TestHandleArgs(t *testing.T) {
t.Run("Test HandleArgs with no flags", func(t *testing.T) {
s := setupRedisServer()
Expand Down Expand Up @@ -136,11 +144,87 @@ func TestHandleArgs(t *testing.T) {
})
}

func setupRedisServer() RedisServer {
return RedisServer{
Env: make(map[string]builtin.EnvData),
Time: TTime{},
Args: []string{}, //os.Args[1:]
Infos: make(map[string]map[string]string),
}
}
func TestSetCommands(t *testing.T) {
s := setupRedisServer()

t.Run("Test SetCommands with 'echo' command", func(t *testing.T) {
s.SetCommands()
b, ok := s.Commands("echo", nil, s.Time.Now())
if !ok {
t.Errorf("Expected command echo, but has not\n")
}
_, ok = b.(*builtin.Echo)
if !ok {
t.Errorf("Expected conn %T, but has %T\n", builtin.Echo{}, b)
}
})

t.Run("Test SetCommands with 'ping' command", func(t *testing.T) {
s.SetCommands()
b, ok := s.Commands("pinG", nil, s.Time.Now())
if !ok {
t.Errorf("Expected command ping, but has not\n")
}
_, ok = b.(*builtin.Ping)
if !ok {
t.Errorf("Expected conn %T, but has %T\n", builtin.Ping{}, b)
}
})

t.Run("Test SetCommands with 'info' command", func(t *testing.T) {
s.SetCommands()
b, ok := s.Commands("iNfo", nil, s.Time.Now())
if !ok {
t.Errorf("Expected command info, but has not\n")
}
_, ok = b.(*builtin.Info)
if !ok {
t.Errorf("Expected conn %T, but has %T\n", builtin.Info{}, b)
}
})

t.Run("Test SetCommands with 'get' command", func(t *testing.T) {
s.SetCommands()
b, ok := s.Commands("Get", nil, s.Time.Now())
if !ok {
t.Errorf("Expected command get, but has not\n")
}
_, ok = b.(*builtin.Get)
if !ok {
t.Errorf("Expected conn %T, but has %T\n", builtin.Get{}, b)
}
})

t.Run("Test SetCommands with 'set' command", func(t *testing.T) {
s.SetCommands()
b, ok := s.Commands("SET", nil, s.Time.Now())
if !ok {
t.Errorf("Expected command set, but has not\n")
}
_, ok = b.(*builtin.Set)
if !ok {
t.Errorf("Expected conn %T, but has %T\n", builtin.Set{}, b)
}
})

t.Run("Test SetCommands with 'replconf' command", func(t *testing.T) {
s.SetCommands()
b, ok := s.Commands("replconf", nil, s.Time.Now())
if !ok {
t.Errorf("Expected command replconf, but has not\n")
}
_, ok = b.(*builtin.ReplConf)
if !ok {
t.Errorf("Expected conn %T, but has %T\n", builtin.ReplConf{}, b)
}
})

t.Run("Test SetCommands with unknown command", func(t *testing.T) {
s.SetCommands()
_, ok := s.Commands("unknown", nil, s.Time.Now())
if ok {
t.Errorf("Expected unknown command, but has not\n")
}
})
}

0 comments on commit 5d2263f

Please sign in to comment.