Skip to content

Commit

Permalink
add get and incr command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhroov7 committed Oct 15, 2024
1 parent 953ef3f commit 7c8c570
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
56 changes: 56 additions & 0 deletions internal/tests/integration/commands/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package commands

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
exec, err := NewHTTPCommandExecutor()
if err != nil {
t.Fatal(err)
}

defer exec.FlushDB()

testCases := []TestCase{
{
Name: "Get with expiration",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"k", "v", "EX", "4"}},
{Command: "GET", Body: []string{"k"}},
{Command: "GET", Body: []string{"k"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{Expected: "v"},
{Expected: "(nil)"},
},
Delays: []time.Duration{0, 0, 5 * time.Second},
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
for i, cmd := range tc.Commands {
if tc.Delays[i] > 0 {
time.Sleep(tc.Delays[i])
}
response, err := exec.FireCommand(cmd)
if err != nil {
t.Logf("error in executing command: %s - %v", cmd.Command, err)
}

result := tc.Result[i]
if result.ErrorExpected {
assert.NotNil(t, err)
assert.Equal(t, result.Expected, err.Error())
} else {
assert.Equal(t, result.Expected, response)
}
}
})
}
}
124 changes: 124 additions & 0 deletions internal/tests/integration/commands/incr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package commands

import (
"math"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestIncr(t *testing.T) {
exec, err := NewHTTPCommandExecutor()
if err != nil {
t.Fatal(err)
}

defer exec.FlushDB()

testCases := []TestCase{
{
Name: "Increment multiple keys",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"key1", "0"}},
{Command: "INCR", Body: []string{"key1"}},
{Command: "INCR", Body: []string{"key1"}},
{Command: "INCR", Body: []string{"key2"}},
{Command: "GET", Body: []string{"key1"}},
{Command: "GET", Body: []string{"key2"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{Expected: "1"},
{Expected: "2"},
{Expected: "1"},
{Expected: "2"},
{Expected: "1"},
},
},
{
Name: "Increment from min int64",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"min_int", strconv.Itoa(math.MinInt64)}},
{Command: "INCR", Body: []string{"min_int"}},
{Command: "INCR", Body: []string{"min_int"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{Expected: strconv.FormatInt(math.MinInt64+1, 10)},
{Expected: strconv.FormatInt(math.MinInt64+2, 10)},
},
},
{
Name: "Increment non-existent key",
Commands: []HTTPCommand{
{Command: "INCR", Body: []string{"non_existent"}},
{Command: "GET", Body: []string{"non_existent"}},
{Command: "INCR", Body: []string{"non_existent"}},
},
Result: []TestCaseResult{
{Expected: "1"},
{Expected: "1"},
{Expected: "2"},
},
},
{
Name: "Increment string representing integers",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"str_int1", "42"}},
{Command: "INCR", Body: []string{"str_int1"}},
{Command: "SET", Body: []string{"str_int2", "-10"}},
{Command: "INCR", Body: []string{"str_int2"}},
{Command: "SET", Body: []string{"str_int3", "0"}},
{Command: "INCR", Body: []string{"str_int3"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{Expected: "43"},
{Expected: "OK"},
{Expected: "-9"},
{Expected: "OK"},
{Expected: "1"},
},
},
{
Name: "Increment with expiry",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"expiry_key", "0", "EX", "1"}},
{Command: "INCR", Body: []string{"expiry_key"}},
{Command: "INCR", Body: []string{"expiry_key"}},
{Command: "GET", Body: []string{"expiry_key"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{Expected: "1"},
{Expected: "2"},
{Expected: "(nil)"},
},
Delays: []time.Duration{0, 0, 0, 2 * time.Second},
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
for i, cmd := range tc.Commands {
if tc.Delays[i] > 0 {
time.Sleep(tc.Delays[i])
}
response, err := exec.FireCommand(cmd)
if err != nil {
t.Logf("error in executing command: %s - %v", cmd.Command, err)
}

result := tc.Result[i]
if result.ErrorExpected {
assert.NotNil(t, err)
assert.Equal(t, result.Expected, err.Error())
} else {
assert.Equal(t, result.Expected, response)
}
}
})
}
}
2 changes: 2 additions & 0 deletions internal/tests/integration/commands/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"server/config"
"server/internal/db"
"server/internal/server"
"time"
)

type HTTPCommand struct {
Expand All @@ -35,6 +36,7 @@ type TestCase struct {
Name string
Commands []HTTPCommand
Result []TestCaseResult
Delays []time.Duration
}

func NewHTTPCommandExecutor() (*HTTPCommandExecutor, error) {
Expand Down

0 comments on commit 7c8c570

Please sign in to comment.