Skip to content

Commit

Permalink
feat: refactor testTxDiscard function to use MultiCommandTestCase
Browse files Browse the repository at this point in the history
The `testTxDiscard` function in the `test_txn_9.go` file has been refactored to use the `MultiCommandTestCase` struct for executing multiple commands within a transaction. 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 9e28d76 commit d82bbb6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
62 changes: 62 additions & 0 deletions internal/test_txn_9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package internal

import (
"github.com/codecrafters-io/redis-tester/internal/redis_executable"
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value"
"github.com/codecrafters-io/redis-tester/internal/resp_assertions"

"github.com/codecrafters-io/redis-tester/internal/instrumented_resp_connection"
"github.com/codecrafters-io/redis-tester/internal/test_cases"
"github.com/codecrafters-io/tester-utils/test_case_harness"
)

func testTxDiscard(stageHarness *test_case_harness.TestCaseHarness) error {
b := redis_executable.NewRedisExecutable(stageHarness)
if err := b.Run(); err != nil {
return err
}

logger := stageHarness.Logger

client, err := instrumented_resp_connection.NewFromAddr(stageHarness, "localhost:6379", "client1")
if err != nil {
logFriendlyError(logger, err)
return err
}
defer client.Close()

transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"SET", "foo", "41"},
{"INCR", "foo"},
},
ResultArray: []resp_value.Value{},
}

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

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

multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"DISCARD"},
{"GET", "foo"},
{"DISCARD"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
resp_assertions.NewNilAssertion(),
resp_assertions.NewErrorAssertion("ERR DISCARD without MULTI"),
},
}

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

return nil
}
5 changes: 5 additions & 0 deletions internal/tester_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ var testerDefinition = tester_definition.TesterDefinition{
Slug: "xu1",
TestFunc: testStreamsXreadBlockMaxID,
},
// ToDo: Slugs are placeholders
{
Slug: "xv0",
TestFunc: testTxDiscard,
},
{
Slug: "xv1",
TestFunc: testTxSuccess,
Expand Down

0 comments on commit d82bbb6

Please sign in to comment.