Skip to content

Commit

Permalink
feat: Refactor testTxMulti function to use MultiCommandTestCase
Browse files Browse the repository at this point in the history
The `testTxMulti` function in the `test_txn_11.go` file has been refactored to use the `MultiCommandTestCase` struct for executing multiple transactions. This change improves code readability and maintainability by encapsulating the commands and assertions within a single test case object.
  • Loading branch information
ryan-gang committed Jun 12, 2024
1 parent af0e0d9 commit 7087489
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
35 changes: 35 additions & 0 deletions internal/test_cases/multi_command_test_case.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test_cases

import (
"fmt"

resp_client "github.com/codecrafters-io/redis-tester/internal/resp/connection"
"github.com/codecrafters-io/redis-tester/internal/resp_assertions"
"github.com/codecrafters-io/tester-utils/logger"
)

// MultiCommandTestCase is a concise & easier way to define & run multiple SendCommandTestCase
type MultiCommandTestCase struct {
Commands [][]string
Assertions []resp_assertions.RESPAssertion
}

func (t *MultiCommandTestCase) RunAll(client *resp_client.RespConnection, logger *logger.Logger) error {
if len(t.Assertions) != len(t.Commands) {
return fmt.Errorf("Number of commands and assertions should be equal")
}

for i, command := range t.Commands {
setCommandTestCase := SendCommandTestCase{
Command: command[0],
Args: command[1:],
Assertion: t.Assertions[i],
}

if err := setCommandTestCase.Run(client, logger); err != nil {
return err
}
}

return nil
}
25 changes: 10 additions & 15 deletions internal/test_txn_10.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,18 @@ func testTxErr(stageHarness *test_case_harness.TestCaseHarness) error {
}
defer client.Close()

setCommandTestCase := test_cases.SendCommandTestCase{
Command: "SET",
Args: []string{"foo", "abc"},
Assertion: resp_assertions.NewStringAssertion("OK"),
}

if err := setCommandTestCase.Run(client, logger); err != nil {
return err
}

setCommandTestCase = test_cases.SendCommandTestCase{
Command: "SET",
Args: []string{"bar", "7"},
Assertion: resp_assertions.NewStringAssertion("OK"),
multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"SET", "foo", "abc"},
{"SET", "bar", "7"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
resp_assertions.NewStringAssertion("OK"),
},
}

if err := setCommandTestCase.Run(client, logger); err != nil {
if err := multiCommandTestCase.RunAll(client, logger); err != nil {
return err
}

Expand Down
42 changes: 22 additions & 20 deletions internal/test_txn_11.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,46 @@ func testTxMulti(stageHarness *test_case_harness.TestCaseHarness) error {
}

for i, client := range clients {
commandTestCase := test_cases.SendCommandTestCase{
Command: "SET",
Args: []string{"bar", "7"},
Assertion: resp_assertions.NewStringAssertion("OK"),
}

if err := commandTestCase.Run(client, logger); err != nil {
return err
}

commandTestCase = test_cases.SendCommandTestCase{
Command: "INCR",
Args: []string{"foo"},
Assertion: resp_assertions.NewIntegerAssertion(i + 1),
multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"SET", "bar", "7"},
{"INCR", "foo"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
resp_assertions.NewIntegerAssertion(i + 1),
},
}

if err := commandTestCase.Run(client, logger); err != nil {
if err := multiCommandTestCase.RunAll(client, logger); err != nil {
return err
}
}

for _, client := range clients {
for i, client := range clients {
transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"INCR", "foo"},
{"INCR", "bar"},
},
ResultArray: []resp_value.Value{},
ResultArray: []resp_value.Value{resp_value.NewIntegerValue(4 + i), resp_value.NewIntegerValue(8 + i)},
}
if err := transactionTestCase.RunMulti(client, logger); err != nil {
return err
}
if err := transactionTestCase.RunAll(client, logger); err != nil {

if err := transactionTestCase.RunQueueAll(client, logger); err != nil {
return err
}
}

for i, client := range clients {
transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{},
ResultArray: []resp_value.Value{resp_value.NewIntegerValue(4 + i), resp_value.NewIntegerValue(8 + i)},
CommandQueue: [][]string{
{"INCR", "foo"},
{"INCR", "bar"},
},
ResultArray: []resp_value.Value{resp_value.NewIntegerValue(4 + i), resp_value.NewIntegerValue(8 + i)},
}
if err := transactionTestCase.RunExec(client, logger); err != nil {
return err
Expand Down

0 comments on commit 7087489

Please sign in to comment.