-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tarun-kavipurapu
committed
Oct 14, 2024
1 parent
ddbf512
commit a004c8b
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package commands | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
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) 7"}, | ||
}, | ||
}, | ||
{ | ||
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) 10"}, | ||
}, | ||
}, | ||
{ | ||
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) 10"}, | ||
}, | ||
}, | ||
{ | ||
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) | ||
} | ||
|
||
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) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
} |