Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1021 : Add integration tests for HSET/HGET/HDEL #1235

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions integration_tests/commands/async/object_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package async

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -124,7 +123,6 @@ func TestObjectCommand(t *testing.T) {

result := FireCommand(conn, cmd)

fmt.Println(cmd, result, tc.expected[i])
if tc.assertType[i] == "equal" {
assert.Equal(t, tc.expected[i], result)
} else {
Expand Down
100 changes: 100 additions & 0 deletions integration_tests/commands/http/hdel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package http

import (
"log"
"testing"
"time"

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

func TestHDel(t *testing.T) {
exec := NewHTTPCommandExecutor()

testCases := []struct {
name string
commands []HTTPCommand
expected []interface{}
delays []time.Duration
}{
{
name: "HDEL with wrong number of arguments",
commands: []HTTPCommand{
{Command: "HDEL", Body: map[string]interface{}{"key": nil}},
{Command: "HDEL", Body: map[string]interface{}{"key": "key_hDel1"}},
},
expected: []interface{}{
"ERR wrong number of arguments for 'hdel' command",
"ERR wrong number of arguments for 'hdel' command"},
delays: []time.Duration{0, 0},
},
{
name: "HDEL with single field",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hDel2", "field": "field1", "value": "value1"}},
{Command: "HLEN", Body: map[string]interface{}{"key": "key_hDel2"}},
{Command: "HDEL", Body: map[string]interface{}{"key": "key_hDel2", "field": "field1"}},
{Command: "HLEN", Body: map[string]interface{}{"key": "key_hDel2"}},
},
expected: []interface{}{float64(1), float64(1), float64(1), float64(0)},
delays: []time.Duration{0, 0, 0, 0},
},
{
name: "HDEL with multiple fields",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hDel3", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2", "field3": "value3", "field4": "value4"}}},
{Command: "HLEN", Body: map[string]interface{}{"key": "key_hDel3"}},
{Command: "HDEL", Body: map[string]interface{}{"key": "key_hDel3", "values": []string{"field1", "field2"}}},
{Command: "HLEN", Body: map[string]interface{}{"key": "key_hDel3"}},
},
expected: []interface{}{float64(4), float64(4), float64(2), float64(2)},
delays: []time.Duration{0, 0, 0, 0},
},
{
name: "HDEL on non-existent field",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hDel4", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2"}}},
{Command: "HDEL", Body: map[string]interface{}{"key": "key_hDel4", "field": "field3"}},
},
expected: []interface{}{float64(2), float64(0)},
delays: []time.Duration{0, 0},
},
{
name: "HDEL on non-existent hash",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hDel5", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2"}}},
{Command: "HDEL", Body: map[string]interface{}{"key": "wrong_key_hDel5", "field": "field1"}},
},
expected: []interface{}{float64(2), float64(0)},
delays: []time.Duration{0, 0},
},
{
name: "HDEL with wrong type",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "string_key", "value": "value"}},
{Command: "HDEL", Body: map[string]interface{}{"key": "string_key", "field": "field"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
delays: []time.Duration{0, 0},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
defer exec.FireCommand(HTTPCommand{Command: "DEL", Body: map[string]interface{}{"keys": [...]string{"key_hDel1", "key_hDel2", "key_hDel3", "key_hDel4", "key_hDel5", "string_key"}}})

for i, cmd := range tc.commands {
if tc.delays[i] > 0 {
time.Sleep(tc.delays[i])
}
result, err := exec.FireCommand(cmd)
if err != nil {
log.Println(tc.expected[i])
assert.Equal(t, tc.expected[i], err.Error(), "Error message mismatch for cmd %s", cmd)
} else {
assert.Equal(t, tc.expected[i], result, "Value mismatch for cmd %s, expected %v, got %v", cmd, tc.expected[i], result)
}
}
})
}
}
90 changes: 90 additions & 0 deletions integration_tests/commands/http/hget_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package http

import (
"log"
"testing"
"time"

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

func TestHGet(t *testing.T) {
exec := NewHTTPCommandExecutor()

testCases := []struct {
name string
commands []HTTPCommand
expected []interface{}
delays []time.Duration
}{
{
name: "HGET with wrong number of arguments",
commands: []HTTPCommand{
{Command: "HGET", Body: map[string]interface{}{"key": nil}},
{Command: "HGET", Body: map[string]interface{}{"key": "key_hGet1"}},
},
expected: []interface{}{
"ERR wrong number of arguments for 'hget' command",
"ERR wrong number of arguments for 'hget' command"},
delays: []time.Duration{0, 0},
},
{
name: "HGET on existent hash",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hGet2", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2", "field3": "value3"}}},
{Command: "HGET", Body: map[string]interface{}{"key": "key_hGet2", "field": "field2"}},
},
expected: []interface{}{float64(3), "value2"},
delays: []time.Duration{0, 0},
},
{
name: "HGET on non-existent field",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hGet3", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2"}}},
{Command: "HGET", Body: map[string]interface{}{"key": "key_hGet3", "field": "field2"}},
{Command: "HDEL", Body: map[string]interface{}{"key": "key_hGet3", "field": "field2"}},
{Command: "HGET", Body: map[string]interface{}{"key": "key_hGet3", "field": "field2"}},
{Command: "HGET", Body: map[string]interface{}{"key": "key_hGet3", "field": "field3"}},
},
expected: []interface{}{float64(2), "value2", float64(1), nil, nil},
delays: []time.Duration{0, 0, 0, 0, 0},
},
{
name: "HGET on non-existent hash",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hGet4", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2"}}},
{Command: "HGET", Body: map[string]interface{}{"key": "wrong_key_hGet4", "field": "field2"}},
},
expected: []interface{}{float64(2), nil},
delays: []time.Duration{0, 0},
},
{
name: "HGET with wrong type",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "string_key", "value": "value"}},
{Command: "HGET", Body: map[string]interface{}{"key": "string_key", "field": "field"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
delays: []time.Duration{0, 0},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
defer exec.FireCommand(HTTPCommand{Command: "DEL", Body: map[string]interface{}{"keys": [...]string{"key_hGet1", "key_hGet2", "key_hGet3", "key_hGet4", "key_hGet5", "string_key"}}})

for i, cmd := range tc.commands {
if tc.delays[i] > 0 {
time.Sleep(tc.delays[i])
}
result, err := exec.FireCommand(cmd)
if err != nil {
log.Println(tc.expected[i])
assert.Equal(t, tc.expected[i], err.Error(), "Error message mismatch for cmd %s", cmd)
} else {
assert.Equal(t, tc.expected[i], result, "Value mismatch for cmd %s, expected %v, got %v", cmd, tc.expected[i], result)
}
}
})
}
}
90 changes: 90 additions & 0 deletions integration_tests/commands/http/hset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package http

import (
"log"
"testing"
"time"

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

func TestHSet(t *testing.T) {
exec := NewHTTPCommandExecutor()

testCases := []struct {
name string
commands []HTTPCommand
expected []interface{}
delays []time.Duration
}{
{
name: "HSET with wrong number of arguments",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": nil}},
{Command: "HSET", Body: map[string]interface{}{"key": "key_hSet1", "field": nil}},
},
expected: []interface{}{
"ERR wrong number of arguments for 'hset' command",
"ERR wrong number of arguments for 'hset' command"},
delays: []time.Duration{0, 0},
},
{
name: "HSET with single field",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hSet2", "field": "field1", "value": "value1"}},
{Command: "HLEN", Body: map[string]interface{}{"key": "key_hSet2"}},
},
expected: []interface{}{float64(1), float64(1)},
delays: []time.Duration{0, 0},
},
{
name: "HSET with multiple fields",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hSet3", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2", "field3": "value3"}}},
{Command: "HLEN", Body: map[string]interface{}{"key": "key_hSet3"}},
},
expected: []interface{}{float64(3), float64(3)},
delays: []time.Duration{0, 0},
},
{
name: "HSET on existing hash",
commands: []HTTPCommand{
{Command: "HSET", Body: map[string]interface{}{"key": "key_hSet4", "key_values": map[string]interface{}{"field1": "value1", "field2": "value2"}}},
{Command: "HGET", Body: map[string]interface{}{"key": "key_hSet4", "field": "field2"}},
{Command: "HSET", Body: map[string]interface{}{"key": "key_hSet4", "key_values": map[string]interface{}{"field2": "newvalue2"}}},
{Command: "HGET", Body: map[string]interface{}{"key": "key_hSet4", "field": "field2"}},
},
expected: []interface{}{float64(2), "value2", float64(0), "newvalue2"},
delays: []time.Duration{0, 0, 0, 0},
},
{
name: "HSET with wrong type",
commands: []HTTPCommand{
{Command: "SET", Body: map[string]interface{}{"key": "string_key", "value": "value"}},
{Command: "HSET", Body: map[string]interface{}{"key": "string_key", "field": "field", "value": "value"}},
},
expected: []interface{}{"OK", "WRONGTYPE Operation against a key holding the wrong kind of value"},
delays: []time.Duration{0, 0},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
defer exec.FireCommand(HTTPCommand{Command: "DEL", Body: map[string]interface{}{"keys": [...]string{"key_hSet1", "key_hSet2", "key_hSet3", "key_hSet4", "string_key"}}})

for i, cmd := range tc.commands {
if tc.delays[i] > 0 {
time.Sleep(tc.delays[i])
}

result, err := exec.FireCommand(cmd)
if err != nil {
log.Println(tc.expected[i])
assert.Equal(t, tc.expected[i], err.Error(), "Error message mismatch for cmd %s", cmd)
} else {
assert.Equal(t, tc.expected[i], result, "Value mismatch for cmd %s, expected %v, got %v", cmd, tc.expected[i], result)
}
}
})
}
}
2 changes: 0 additions & 2 deletions integration_tests/commands/http/hvals_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package http

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -42,7 +41,6 @@ func TestHVals(t *testing.T) {

for i, cmd := range tc.commands {
result, _ := cmdExec.FireCommand(cmd)
fmt.Printf("%v | %v\n", result, tc.expected[i])
switch e := tc.expected[i].(type) {
case []interface{}:
assert.ElementsMatch(t, e, tc.expected[i])
Expand Down
5 changes: 2 additions & 3 deletions integration_tests/commands/http/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,6 @@ func TestJSONMGET(t *testing.T) {
},
})

fmt.Printf("expacting: %s with got: %s\n", "OK", resp)
assert.Equal(t, "OK", resp)
}

Expand Down Expand Up @@ -1433,8 +1432,8 @@ func TestJsonObjKeys(t *testing.T) {
if _, ok := result.([]interface{}); ok {
assert.ElementsMatch(t, tc.expected[i].([]interface{}), result.([]interface{}))
} else {
// handle the case where result is not a []interface{}
assert.Equal(t, tc.expected[i], result)
// handle the case where result is not a []interface{}
assert.Equal(t, tc.expected[i], result)
}

}
Expand Down
Loading