-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add transaction test cases for transaction execution
- Loading branch information
Showing
3 changed files
with
95 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package test_cases | ||
|
||
import ( | ||
resp_client "github.com/codecrafters-io/redis-tester/internal/resp/connection" | ||
resp_value "github.com/codecrafters-io/redis-tester/internal/resp/value" | ||
"github.com/codecrafters-io/redis-tester/internal/resp_assertions" | ||
"github.com/codecrafters-io/tester-utils/logger" | ||
) | ||
|
||
// TransactionTestCase is a test case where we connect to a master | ||
// as a replica and perform either all or a subset of the replication handshake. | ||
// | ||
// RunAll will run all the steps in the replication handshake. Alternatively, you | ||
// can run each step individually. | ||
type TransactionTestCase struct { | ||
// All the CommandQueue will be sent in order to client | ||
// And a string "QUEUED" will be expected | ||
CommandQueue [][]string | ||
|
||
// After queueing all the commands, | ||
// if ResultArray is not empty, "EXEC" is sent | ||
// And the response is compared with this ResultArray | ||
ResultArray []resp_value.Value | ||
} | ||
|
||
func (t TransactionTestCase) RunAll(client *resp_client.RespConnection, logger *logger.Logger) error { | ||
if err := t.RunMulti(client, logger); err != nil { | ||
return err | ||
} | ||
|
||
if err := t.RunQueueAll(client, logger); err != nil { | ||
return err | ||
} | ||
|
||
if len(t.ResultArray) > 0 { | ||
if err := t.RunExec(client, logger); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t TransactionTestCase) RunMulti(client *resp_client.RespConnection, logger *logger.Logger) error { | ||
commandTest := SendCommandTestCase{ | ||
Command: "MULTI", | ||
Args: []string{}, | ||
Assertion: resp_assertions.NewStringAssertion("OK"), | ||
} | ||
|
||
return commandTest.Run(client, logger) | ||
} | ||
|
||
func (t TransactionTestCase) RunQueueAll(client *resp_client.RespConnection, logger *logger.Logger) error { | ||
for i, v := range t.CommandQueue { | ||
logger.Debugf("Sent #%d command", i) | ||
commandTest := SendCommandTestCase{ | ||
Command: v[0], | ||
Args: v[1:], | ||
Assertion: resp_assertions.NewStringAssertion("QUEUED"), | ||
} | ||
if err := commandTest.Run(client, logger); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t TransactionTestCase) RunExec(client *resp_client.RespConnection, logger *logger.Logger) error { | ||
setCommandTestCase := SendCommandTestCase{ | ||
Command: "EXEC", | ||
Args: []string{}, | ||
Assertion: resp_assertions.NewOrderedArrayAssertion(t.ResultArray), | ||
} | ||
|
||
return setCommandTestCase.Run(client, logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters