From 0d419f6c5a0ee8a5bb5e94d1e4b30d76c64848c9 Mon Sep 17 00:00:00 2001 From: Tarun <35888688+tarun-kavipurapu@users.noreply.github.com> Date: Sun, 20 Oct 2024 19:19:27 +0530 Subject: [PATCH] #30: Add integration tests for commands execution related to hyperloglog (#38) --- .../tests/integration/commands/pfadd_test.go | 71 ++++++++++++++++ .../integration/commands/pfcount_test.go | 84 ++++++++++++++++++ .../integration/commands/pfmerge_test.go | 85 +++++++++++++++++++ 3 files changed, 240 insertions(+) create mode 100644 internal/tests/integration/commands/pfadd_test.go create mode 100644 internal/tests/integration/commands/pfcount_test.go create mode 100644 internal/tests/integration/commands/pfmerge_test.go diff --git a/internal/tests/integration/commands/pfadd_test.go b/internal/tests/integration/commands/pfadd_test.go new file mode 100644 index 0000000..1250a57 --- /dev/null +++ b/internal/tests/integration/commands/pfadd_test.go @@ -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) + } + }) + } +} diff --git a/internal/tests/integration/commands/pfcount_test.go b/internal/tests/integration/commands/pfcount_test.go new file mode 100644 index 0000000..b8e62df --- /dev/null +++ b/internal/tests/integration/commands/pfcount_test.go @@ -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) + } + }) + } + +} diff --git a/internal/tests/integration/commands/pfmerge_test.go b/internal/tests/integration/commands/pfmerge_test.go new file mode 100644 index 0000000..19ee5a8 --- /dev/null +++ b/internal/tests/integration/commands/pfmerge_test.go @@ -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) + + } + }) + } + +}