Skip to content

Commit

Permalink
feat: add randomness to test case for transaction failure
Browse files Browse the repository at this point in the history
This commit adds a new test case to the `testTxErr` function in `internal/test_txn_tx_failure.go`. The test case includes commands to set random values for two keys, and then increment those values within a transaction. The expected result is an error value indicating that the value is not an integer or out of range, followed by the incremented value. After running the transaction, there are assertions to check if the values were updated correctly.

The purpose of this change is to ensure that transactions handle errors properly and provide accurate results when dealing with non-integer values.
  • Loading branch information
ryan-gang committed Jun 14, 2024
1 parent 715817c commit 571cdf7
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions internal/test_txn_tx_failure.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package internal

import (
"fmt"

"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/test_cases"
"github.com/codecrafters-io/tester-utils/random"
"github.com/codecrafters-io/tester-utils/test_case_harness"
)

Expand All @@ -25,10 +28,14 @@ func testTxErr(stageHarness *test_case_harness.TestCaseHarness) error {
defer client.Close()
}

key1, key2 := random.RandomWord(), random.RandomWord()
randomStringValue := random.RandomWord()
randomIntegerValue := random.RandomInt(1, 100)

multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"SET", "foo", "abc"},
{"SET", "bar", "7"},
{"SET", key1, randomStringValue},
{"SET", key2, fmt.Sprint(randomIntegerValue)},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
Expand All @@ -42,11 +49,11 @@ func testTxErr(stageHarness *test_case_harness.TestCaseHarness) error {

transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"INCR", "foo"},
{"INCR", "bar"},
{"INCR", key1},
{"INCR", key2},
},
ResultArray: []resp_value.Value{
resp_value.NewErrorValue("ERR value is not an integer or out of range"), resp_value.NewIntegerValue(8)},
resp_value.NewErrorValue("ERR value is not an integer or out of range"), resp_value.NewIntegerValue(randomIntegerValue + 1)},
}

if err := transactionTestCase.RunAll(clients[0], logger); err != nil {
Expand All @@ -55,12 +62,12 @@ func testTxErr(stageHarness *test_case_harness.TestCaseHarness) error {

multiCommandTestCase = test_cases.MultiCommandTestCase{
Commands: [][]string{
{"GET", "bar"},
{"GET", "foo"},
{"GET", key2},
{"GET", key1},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("8"),
resp_assertions.NewStringAssertion("abc"),
resp_assertions.NewStringAssertion(fmt.Sprint(randomIntegerValue + 1)),
resp_assertions.NewStringAssertion(randomStringValue),
},
}

Expand Down

0 comments on commit 571cdf7

Please sign in to comment.