Skip to content

Commit

Permalink
feat: Add randomness to test cases for transaction discard
Browse files Browse the repository at this point in the history
This commit adds new test cases to the `testTxDiscard` function in `internal/test_txn_discard.go`. The changes include:
- Importing the `fmt` package
- Generating unique keys using the `random.RandomWords` function
- Generating random integers using the `random.RandomInt` function
- Modifying the command arguments to use the generated keys and random integers
- Updating assertions to use the generated values

These changes enhance the test coverage for transaction discards.
  • Loading branch information
ryan-gang committed Jun 14, 2024
1 parent 4bf9120 commit 63b9b01
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions internal/test_txn_discard.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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/instrumented_resp_connection"
"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,9 +28,13 @@ func testTxDiscard(stageHarness *test_case_harness.TestCaseHarness) error {
}
defer client.Close()

uniqueKeys := random.RandomWords(3)
key1, key2 := uniqueKeys[0], uniqueKeys[1]
randomInt1, randomInt2 := random.RandomInt(1, 100), random.RandomInt(1, 100)

commandTestCase := test_cases.SendCommandTestCase{
Command: "SET",
Args: []string{"bar", "42"},
Args: []string{key2, fmt.Sprint(randomInt2)},
Assertion: resp_assertions.NewStringAssertion("OK"),
}

Expand All @@ -37,8 +44,8 @@ func testTxDiscard(stageHarness *test_case_harness.TestCaseHarness) error {

transactionTestCase := test_cases.TransactionTestCase{
CommandQueue: [][]string{
{"SET", "foo", "41"},
{"INCR", "foo"},
{"SET", key1, fmt.Sprint(randomInt1)},
{"INCR", key1},
},
ResultArray: []resp_value.Value{},
}
Expand All @@ -50,14 +57,14 @@ func testTxDiscard(stageHarness *test_case_harness.TestCaseHarness) error {
multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"DISCARD"},
{"GET", "foo"},
{"GET", "bar"},
{"GET", key1},
{"GET", key2},
{"DISCARD"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
resp_assertions.NewNilAssertion(),
resp_assertions.NewStringAssertion("42"),
resp_assertions.NewStringAssertion(fmt.Sprint(randomInt2)),
resp_assertions.NewErrorAssertion("ERR DISCARD without MULTI"),
},
}
Expand Down

0 comments on commit 63b9b01

Please sign in to comment.