Skip to content

Commit

Permalink
feat: add tests for stages 1 - 3
Browse files Browse the repository at this point in the history
Use MultiCommandTestCase for testTxIncr1, testTxIncr2, and testTxIncr3
  • Loading branch information
ryan-gang committed Jun 13, 2024
1 parent 67b14ec commit fecf766
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
44 changes: 44 additions & 0 deletions internal/test_txn_incr1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package internal

import (
"fmt"

"github.com/codecrafters-io/redis-tester/internal/redis_executable"
"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"
)

func testTxIncr1(stageHarness *test_case_harness.TestCaseHarness) error {

Check failure on line 15 in internal/test_txn_incr1.go

View workflow job for this annotation

GitHub Actions / lint

func testTxIncr1 is unused (U1000)
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", "client")
if err != nil {
logFriendlyError(logger, err)
return err
}
defer client.Close()

randomValue := random.RandomInt(1, 100)

multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"SET", "foo", fmt.Sprint(randomValue)},
{"INCR", "foo"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
resp_assertions.NewIntegerAssertion(randomValue + 1),
},
}

return multiCommandTestCase.RunAll(client, logger)
}
39 changes: 39 additions & 0 deletions internal/test_txn_incr2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
"github.com/codecrafters-io/redis-tester/internal/redis_executable"
"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 testTxIncr2(stageHarness *test_case_harness.TestCaseHarness) error {

Check failure on line 12 in internal/test_txn_incr2.go

View workflow job for this annotation

GitHub Actions / lint

func testTxIncr2 is unused (U1000)
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", "client")
if err != nil {
logFriendlyError(logger, err)
return err
}
defer client.Close()

multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"INCR", "foo"},
{"INCR", "foo"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewIntegerAssertion(1),
resp_assertions.NewIntegerAssertion(2),
},
}

return multiCommandTestCase.RunAll(client, logger)
}
41 changes: 41 additions & 0 deletions internal/test_txn_incr3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package internal

import (
"github.com/codecrafters-io/redis-tester/internal/redis_executable"
"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 testTxIncr3(stageHarness *test_case_harness.TestCaseHarness) error {

Check failure on line 12 in internal/test_txn_incr3.go

View workflow job for this annotation

GitHub Actions / lint

func testTxIncr3 is unused (U1000)
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", "client")
if err != nil {
logFriendlyError(logger, err)
return err
}
defer client.Close()

randomValue := "xyz"

multiCommandTestCase := test_cases.MultiCommandTestCase{
Commands: [][]string{
{"SET", "foo", randomValue},
{"INCR", "foo"},
},
Assertions: []resp_assertions.RESPAssertion{
resp_assertions.NewStringAssertion("OK"),
resp_assertions.NewErrorAssertion("ERR value is not an integer or out of range"),
},
}

return multiCommandTestCase.RunAll(client, logger)
}

0 comments on commit fecf766

Please sign in to comment.