Skip to content

Commit

Permalink
#30: Add integration tests for commands execution related to hyperlog…
Browse files Browse the repository at this point in the history
…log (#38)
  • Loading branch information
tarun-kavipurapu authored Oct 20, 2024
1 parent 62d5d90 commit 0d419f6
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 0 deletions.
71 changes: 71 additions & 0 deletions internal/tests/integration/commands/pfadd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package commands

import (
"server/internal/tests/integration/commands/assertions"
"testing"
)

func TestPfAdd(t *testing.T) {
exec, err := NewHTTPCommandExecutor()

if err != nil {
t.Fatal(err)
}

testCases := []TestCase{
{
Name: "Adding a single element to a HyperLogLog",
Commands: []HTTPCommand{
{Command: "PFADD", Body: []string{"myhyperloglog", "element1"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 1"},
},
},
{
Name: "Adding multiple elements to a HyperLogLog",
Commands: []HTTPCommand{
{Command: "PFADD", Body: []string{"myhyperloglog", "element1", "element2", "element3"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 1"},
},
},
{
Name: "Checking if HyperLogLog was modified (element doesn't alter internal registers)",
Commands: []HTTPCommand{
{Command: "PFADD", Body: []string{"myhyperloglog", "element1"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 0"},
},
},
{
Name: "Adding to a key that is not a HyperLogLog",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"mykey", "notahyperloglog"}},
{Command: "PFADD", Body: []string{"mykey", "element1"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{ErrorExpected: true, Expected: "(error) WRONGTYPE Key is not a valid HyperLogLog string value."},
},
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
for i, cmd := range tc.Commands {
response, err := exec.FireCommand(cmd)
if err != nil {
t.Logf("Error in executing command: %s - %v", cmd.Command, err)
} else {
t.Logf("Response for command %s: %s", cmd.Command, response)
}

result := tc.Result[i]
assertions.AssertResult(t, err, response, result.Expected, result.ErrorExpected)
}
})
}
}
84 changes: 84 additions & 0 deletions internal/tests/integration/commands/pfcount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package commands

import (
"server/internal/tests/integration/commands/assertions"
"testing"
)

func TestPfCount(t *testing.T) {
exec, err := NewHTTPCommandExecutor()
if err != nil {
t.Fatal(err)
}
testCases := []TestCase{
{
Name: "PFCOUNT on non-existent key",
Commands: []HTTPCommand{
{Command: "PFCOUNT", Body: []string{"non_existent_key"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 0"},
},
},
{
Name: "PFCOUNT on wrong type of key",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"mykey", "value"}},
{Command: "PFCOUNT", Body: []string{"mykey"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{ErrorExpected: true, Expected: "(error) WRONGTYPE Key is not a valid HyperLogLog string value."},
},
},
{
Name: "PFCOUNT with invalid arguments (no arguments)",
Commands: []HTTPCommand{
{Command: "PFCOUNT", Body: []string{}},
},
Result: []TestCaseResult{
{ErrorExpected: true, Expected: "(error) ERR wrong number of arguments for 'pfcount' command"},
},
},
{
Name: "PFCOUNT on single key",
Commands: []HTTPCommand{
{Command: "PFADD", Body: []string{"hll1", "foo", "bar", "baz"}},
{Command: "PFCOUNT", Body: []string{"hll1"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 1"},
{Expected: "(integer) 3"},
},
},
{
Name: "PFCOUNT on multiple keys",
Commands: []HTTPCommand{
{Command: "PFADD", Body: []string{"hll1", "foo", "bar"}},
{Command: "PFADD", Body: []string{"hll2", "baz", "qux"}},
{Command: "PFCOUNT", Body: []string{"hll1", "hll2"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 0"},
{Expected: "(integer) 1"},
{Expected: "(integer) 4"},
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
for i, cmd := range tc.Commands {
response, err := exec.FireCommand(cmd)
if err != nil {
t.Logf("Error in executing command: %s - %v", cmd.Command, err)
} else {
t.Logf("Response for command %s: %s", cmd.Command, response)
}

result := tc.Result[i]
assertions.AssertResult(t, err, response, result.Expected, result.ErrorExpected)
}
})
}

}
85 changes: 85 additions & 0 deletions internal/tests/integration/commands/pfmerge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package commands

import (
"server/internal/tests/integration/commands/assertions"
"testing"
)

func TestPfMerge(t *testing.T) {
exec, err := NewHTTPCommandExecutor()
if err != nil {
t.Fatal(err)
}
testCases := []TestCase{
{
Name: "PFMERGE multiple HyperLogLogs into a new one",
Commands: []HTTPCommand{
{Command: "PFADD", Body: []string{"hll1", "a", "b", "c"}},
{Command: "PFADD", Body: []string{"hll2", "c", "d", "e"}},
{Command: "PFADD", Body: []string{"hll3", "e", "f", "g"}},
{Command: "PFMERGE", Body: []string{"hll_merged", "hll1", "hll2", "hll3"}},
{Command: "PFCOUNT", Body: []string{"hll_merged"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 1"},
{Expected: "(integer) 1"},
{Expected: "(integer) 1"},
{Expected: "OK"},
{Expected: "(integer) 11"},
},
},
{
Name: "PFMERGE overwrites existing destination key",
Commands: []HTTPCommand{
{Command: "PFADD", Body: []string{"hll_merged", "x", "y", "z"}},
{Command: "PFMERGE", Body: []string{"hll_merged", "hll1", "hll2", "hll3"}},
{Command: "PFCOUNT", Body: []string{"hll_merged"}},
},
Result: []TestCaseResult{
{Expected: "(integer) 1"},
{Expected: "OK"},
{Expected: "(integer) 14"},
},
},
{
Name: "PFMERGE with non-existent source key",
Commands: []HTTPCommand{

{Command: "PFMERGE", Body: []string{"hll_merged", "hll1", "hll2", "non_existent_key"}},
{Command: "PFCOUNT", Body: []string{"hll_merged"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{Expected: "(integer) 14"},
},
},
{
Name: "PFMERGE with wrong type of key",
Commands: []HTTPCommand{
{Command: "SET", Body: []string{"not_hyperLogLog", "some_value"}},
{Command: "PFMERGE", Body: []string{"hll_merged", "not_hyperLogLog"}},
},
Result: []TestCaseResult{
{Expected: "OK"},
{ErrorExpected: true, Expected: "(error) WRONGTYPE Key is not a valid HyperLogLog string value."},
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
for i, cmd := range tc.Commands {
response, err := exec.FireCommand(cmd)
if err != nil {
t.Logf("Error in executing command: %s - %v", cmd.Command, err)
} else {
t.Logf("Response for command %s: %s", cmd.Command, response)
}

result := tc.Result[i]
assertions.AssertResult(t, err, response, result.Expected, result.ErrorExpected)

}
})
}

}

0 comments on commit 0d419f6

Please sign in to comment.