Skip to content

Commit e03c573

Browse files
Refactor: Change interface for MultiCommandTestCase
1 parent 09ed1a4 commit e03c573

25 files changed

+466
-374
lines changed

internal/test_cases/multi_command_test_case.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
package test_cases
22

33
import (
4-
"fmt"
5-
64
resp_client "github.com/codecrafters-io/redis-tester/internal/resp/connection"
75
"github.com/codecrafters-io/redis-tester/internal/resp_assertions"
86
"github.com/codecrafters-io/tester-utils/logger"
97
)
108

9+
type CommandWithAssertion struct {
10+
Command []string
11+
Assertion resp_assertions.RESPAssertion
12+
}
13+
1114
// MultiCommandTestCase is a concise & easier way to define & run multiple SendCommandTestCase
1215
type MultiCommandTestCase struct {
13-
Commands [][]string
14-
Assertions []resp_assertions.RESPAssertion
16+
CommandWithAssertions []CommandWithAssertion
1517
}
1618

1719
func (t *MultiCommandTestCase) RunAll(client *resp_client.RespConnection, logger *logger.Logger) error {
18-
if len(t.Assertions) != len(t.Commands) {
19-
return fmt.Errorf("CodeCrafters internal error. Number of commands and assertions should be equal in MultiCommandTestCase")
20-
}
21-
22-
for i, command := range t.Commands {
20+
for _, cwa := range t.CommandWithAssertions {
2321
setCommandTestCase := SendCommandTestCase{
24-
Command: command[0],
25-
Args: command[1:],
26-
Assertion: t.Assertions[i],
22+
Command: cwa.Command[0],
23+
Args: cwa.Command[1:],
24+
Assertion: cwa.Assertion,
2725
}
2826

2927
if err := setCommandTestCase.Run(client, logger); err != nil {

internal/test_helpers/fixtures/lists/pass

Lines changed: 131 additions & 117 deletions
Large diffs are not rendered by default.

internal/test_list_llen.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ func testListLlen(stageHarness *test_case_harness.TestCaseHarness) error {
3131
missingKey := fmt.Sprintf("missing_key_%d", testerutils_random.RandomInt(1, 100))
3232

3333
multiCommandTestCase := test_cases.MultiCommandTestCase{
34-
Commands: [][]string{
35-
append([]string{"RPUSH", randomKey}, randomList...),
36-
{"LLEN", randomKey},
37-
{"LLEN", missingKey},
38-
},
39-
Assertions: []resp_assertions.RESPAssertion{
40-
resp_assertions.NewIntegerAssertion(listSize),
41-
resp_assertions.NewIntegerAssertion(listSize),
42-
resp_assertions.NewIntegerAssertion(0),
34+
CommandWithAssertions: []test_cases.CommandWithAssertion{
35+
{
36+
Command: append([]string{"RPUSH", randomKey}, randomList...),
37+
Assertion: resp_assertions.NewIntegerAssertion(listSize),
38+
},
39+
{
40+
Command: []string{"LLEN", randomKey},
41+
Assertion: resp_assertions.NewIntegerAssertion(listSize),
42+
},
43+
{
44+
Command: []string{"LLEN", missingKey},
45+
Assertion: resp_assertions.NewIntegerAssertion(0),
46+
},
4347
},
4448
}
4549

internal/test_list_lpop1.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ func testListLpop1(stageHarness *test_case_harness.TestCaseHarness) error {
3030
elements := testerutils_random.RandomWords(listSize)
3131

3232
multiCommandTestCase := test_cases.MultiCommandTestCase{
33-
Commands: [][]string{
34-
append([]string{"RPUSH", randomListKey}, elements...),
35-
{"LPOP", randomListKey},
36-
{"LRANGE", randomListKey, strconv.Itoa(0), strconv.Itoa(-1)},
37-
},
38-
Assertions: []resp_assertions.RESPAssertion{
39-
resp_assertions.NewIntegerAssertion(listSize),
40-
resp_assertions.NewStringAssertion(elements[0]),
41-
resp_assertions.NewOrderedStringArrayAssertion(elements[1:]),
33+
CommandWithAssertions: []test_cases.CommandWithAssertion{
34+
{
35+
Command: append([]string{"RPUSH", randomListKey}, elements...),
36+
Assertion: resp_assertions.NewIntegerAssertion(listSize),
37+
},
38+
{
39+
Command: []string{"LPOP", randomListKey},
40+
Assertion: resp_assertions.NewStringAssertion(elements[0]),
41+
},
42+
{
43+
Command: []string{"LRANGE", randomListKey, strconv.Itoa(0), strconv.Itoa(-1)},
44+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(elements[1:]),
45+
},
4246
},
4347
}
4448

internal/test_list_lpop2.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ func testListLpop2(stageHarness *test_case_harness.TestCaseHarness) error {
3131
toRemoveCount := testerutils_random.RandomInt(2, 5)
3232

3333
multiCommandTestCase := test_cases.MultiCommandTestCase{
34-
Commands: [][]string{
35-
append([]string{"RPUSH", randomListKey}, elements...),
36-
{"LPOP", randomListKey, strconv.Itoa(toRemoveCount)},
37-
{"LRANGE", randomListKey, strconv.Itoa(0), strconv.Itoa(-1)},
38-
},
39-
Assertions: []resp_assertions.RESPAssertion{
40-
resp_assertions.NewIntegerAssertion(listSize),
41-
resp_assertions.NewOrderedStringArrayAssertion(elements[0:toRemoveCount]),
42-
resp_assertions.NewOrderedStringArrayAssertion(elements[toRemoveCount:]),
34+
CommandWithAssertions: []test_cases.CommandWithAssertion{
35+
{
36+
Command: append([]string{"RPUSH", randomListKey}, elements...),
37+
Assertion: resp_assertions.NewIntegerAssertion(listSize),
38+
},
39+
{
40+
Command: []string{"LPOP", randomListKey, strconv.Itoa(toRemoveCount)},
41+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(elements[0:toRemoveCount]),
42+
},
43+
{
44+
Command: []string{"LRANGE", randomListKey, strconv.Itoa(0), strconv.Itoa(-1)},
45+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(elements[toRemoveCount:]),
46+
},
4347
},
4448
}
4549

internal/test_list_lpush.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ func testListLpush(stageHarness *test_case_harness.TestCaseHarness) error {
2929
randomValues := testerutils_random.RandomWords(3)
3030

3131
multiCommandTestCase := test_cases.MultiCommandTestCase{
32-
Commands: [][]string{
33-
{"LPUSH", randomListKey, randomValues[2]},
34-
{"LPUSH", randomListKey, randomValues[1], randomValues[0]},
35-
{"LRANGE", randomListKey, strconv.Itoa(0), strconv.Itoa(-1)},
36-
},
37-
Assertions: []resp_assertions.RESPAssertion{
38-
resp_assertions.NewIntegerAssertion(1),
39-
resp_assertions.NewIntegerAssertion(3),
40-
resp_assertions.NewOrderedStringArrayAssertion(randomValues),
32+
CommandWithAssertions: []test_cases.CommandWithAssertion{
33+
{
34+
Command: []string{"LPUSH", randomListKey, randomValues[2]},
35+
Assertion: resp_assertions.NewIntegerAssertion(1),
36+
},
37+
{
38+
Command: []string{"LPUSH", randomListKey, randomValues[1], randomValues[0]},
39+
Assertion: resp_assertions.NewIntegerAssertion(3),
40+
},
41+
{
42+
Command: []string{"LRANGE", randomListKey, strconv.Itoa(0), strconv.Itoa(-1)},
43+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomValues),
44+
},
4145
},
4246
}
4347

internal/test_list_lrange_neg_index.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,44 @@ func testListLrangeNegIndex(stageHarness *test_case_harness.TestCaseHarness) err
2626
defer client.Close()
2727

2828
randomListKey := testerutils_random.RandomWord()
29-
listSize := testerutils_random.RandomInt(4, 5)
29+
listSize := testerutils_random.RandomInt(4, 6)
3030
randomElements := testerutils_random.RandomWords(listSize)
3131

3232
startIndex := -listSize
3333
endIndex := -1
3434

35-
middleIndex := testerutils_random.RandomInt(startIndex, -1)
35+
middleIndex := testerutils_random.RandomInt(startIndex+1, -1)
3636
middleIndexTranslated := listSize + middleIndex
3737

3838
multiCommandTestCase := test_cases.MultiCommandTestCase{
39-
Commands: [][]string{
40-
append([]string{"RPUSH", randomListKey}, randomElements...),
41-
39+
CommandWithAssertions: []test_cases.CommandWithAssertion{
40+
{
41+
Command: append([]string{"RPUSH", randomListKey}, randomElements...),
42+
Assertion: resp_assertions.NewIntegerAssertion(listSize),
43+
},
4244
// usual test cases
43-
{"LRANGE", randomListKey, "0", strconv.Itoa(middleIndex)},
44-
{"LRANGE", randomListKey, strconv.Itoa(middleIndex), strconv.Itoa(endIndex)},
45-
{"LRANGE", randomListKey, "0", strconv.Itoa(endIndex)},
46-
45+
{
46+
Command: []string{"LRANGE", randomListKey, "0", strconv.Itoa(middleIndex)},
47+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomElements[0 : middleIndexTranslated+1]),
48+
},
49+
{
50+
Command: []string{"LRANGE", randomListKey, strconv.Itoa(middleIndex), strconv.Itoa(endIndex)},
51+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomElements[middleIndexTranslated:listSize]),
52+
},
53+
{
54+
Command: []string{"LRANGE", randomListKey, "0", strconv.Itoa(endIndex)},
55+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomElements[0:listSize]),
56+
},
4757
// start index > end index
48-
{"LRANGE", randomListKey, "-1", "-2"},
49-
58+
{
59+
Command: []string{"LRANGE", randomListKey, "-1", "-2"},
60+
Assertion: resp_assertions.NewOrderedStringArrayAssertion([]string{}),
61+
},
5062
// end index out of bounds
51-
{"LRANGE", randomListKey, strconv.Itoa(startIndex - 1), strconv.Itoa(endIndex)},
52-
},
53-
Assertions: []resp_assertions.RESPAssertion{
54-
resp_assertions.NewIntegerAssertion(listSize),
55-
resp_assertions.NewOrderedStringArrayAssertion(randomElements[0 : middleIndexTranslated+1]),
56-
resp_assertions.NewOrderedStringArrayAssertion(randomElements[middleIndexTranslated:listSize]),
57-
resp_assertions.NewOrderedStringArrayAssertion(randomElements[0:listSize]),
58-
resp_assertions.NewOrderedStringArrayAssertion([]string{}),
59-
resp_assertions.NewOrderedStringArrayAssertion(randomElements[0:listSize]),
63+
{
64+
Command: []string{"LRANGE", randomListKey, strconv.Itoa(startIndex - 1), strconv.Itoa(endIndex)},
65+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomElements[0:listSize]),
66+
},
6067
},
6168
}
6269

internal/test_list_lrange_pos_index.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,45 @@ func testListLrangePosIdx(stageHarness *test_case_harness.TestCaseHarness) error
2727
defer client.Close()
2828

2929
randomListKey := testerutils_random.RandomWord()
30-
listSize := testerutils_random.RandomInt(4, 5)
30+
listSize := testerutils_random.RandomInt(4, 6)
3131
randomList := testerutils_random.RandomWords(listSize)
3232
middleIndex := testerutils_random.RandomInt(1, listSize-1)
3333
missingKey := fmt.Sprintf("missing_key_%d", testerutils_random.RandomInt(1, 100))
3434

3535
multiCommandTestCase := test_cases.MultiCommandTestCase{
36-
Commands: [][]string{
37-
append([]string{"RPUSH", randomListKey}, randomList...),
38-
36+
CommandWithAssertions: []test_cases.CommandWithAssertion{
37+
{
38+
Command: append([]string{"RPUSH", randomListKey}, randomList...),
39+
Assertion: resp_assertions.NewIntegerAssertion(listSize),
40+
},
3941
// usual test cases
40-
{"LRANGE", randomListKey, "0", strconv.Itoa(middleIndex)},
41-
{"LRANGE", randomListKey, strconv.Itoa(middleIndex), strconv.Itoa(listSize - 1)},
42-
{"LRANGE", randomListKey, "0", strconv.Itoa(listSize - 1)},
43-
42+
{
43+
Command: []string{"LRANGE", randomListKey, "0", strconv.Itoa(middleIndex)},
44+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomList[0 : middleIndex+1]),
45+
},
46+
{
47+
Command: []string{"LRANGE", randomListKey, strconv.Itoa(middleIndex), strconv.Itoa(listSize - 1)},
48+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomList[middleIndex:listSize]),
49+
},
50+
{
51+
Command: []string{"LRANGE", randomListKey, "0", strconv.Itoa(listSize - 1)},
52+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomList[0:listSize]),
53+
},
4454
// start index > end index
45-
{"LRANGE", randomListKey, "1", "0"},
46-
55+
{
56+
Command: []string{"LRANGE", randomListKey, "1", "0"},
57+
Assertion: resp_assertions.NewOrderedStringArrayAssertion([]string{}),
58+
},
4759
// end index out of bounds
48-
{"LRANGE", randomListKey, "0", strconv.Itoa(listSize * 2)},
49-
60+
{
61+
Command: []string{"LRANGE", randomListKey, "0", strconv.Itoa(listSize * 2)},
62+
Assertion: resp_assertions.NewOrderedStringArrayAssertion(randomList[0:listSize]),
63+
},
5064
// key doesn't exist
51-
{"LRANGE", missingKey, "0", "1"},
52-
},
53-
Assertions: []resp_assertions.RESPAssertion{
54-
resp_assertions.NewIntegerAssertion(listSize),
55-
resp_assertions.NewOrderedStringArrayAssertion(randomList[0 : middleIndex+1]),
56-
resp_assertions.NewOrderedStringArrayAssertion(randomList[middleIndex:listSize]),
57-
resp_assertions.NewOrderedStringArrayAssertion(randomList[0:listSize]),
58-
resp_assertions.NewOrderedStringArrayAssertion([]string{}),
59-
resp_assertions.NewOrderedStringArrayAssertion(randomList[0:listSize]),
60-
resp_assertions.NewOrderedStringArrayAssertion([]string{}),
65+
{
66+
Command: []string{"LRANGE", missingKey, "0", "1"},
67+
Assertion: resp_assertions.NewOrderedStringArrayAssertion([]string{}),
68+
},
6169
},
6270
}
6371

internal/test_list_rpush2.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@ func testListRpush2(stageHarness *test_case_harness.TestCaseHarness) error {
2727
randomElements := testerutils_random.RandomWords(3)
2828

2929
multiCommandTestCase := test_cases.MultiCommandTestCase{
30-
Commands: [][]string{
31-
{"RPUSH", randomListKey, randomElements[0]},
32-
{"RPUSH", randomListKey, randomElements[1]},
33-
{"RPUSH", randomListKey, randomElements[2]},
34-
},
35-
Assertions: []resp_assertions.RESPAssertion{
36-
resp_assertions.NewIntegerAssertion(1),
37-
resp_assertions.NewIntegerAssertion(2),
38-
resp_assertions.NewIntegerAssertion(3),
30+
CommandWithAssertions: []test_cases.CommandWithAssertion{
31+
{
32+
Command: []string{"RPUSH", randomListKey, randomElements[0]},
33+
Assertion: resp_assertions.NewIntegerAssertion(1),
34+
},
35+
{
36+
Command: []string{"RPUSH", randomListKey, randomElements[1]},
37+
Assertion: resp_assertions.NewIntegerAssertion(2),
38+
},
39+
{
40+
Command: []string{"RPUSH", randomListKey, randomElements[2]},
41+
Assertion: resp_assertions.NewIntegerAssertion(3),
42+
},
3943
},
4044
}
4145

internal/test_list_rpush3.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ func testListRpush3(stageHarness *test_case_harness.TestCaseHarness) error {
2929
elementsForSecondCmd := testerutils_random.RandomWords(3)
3030

3131
multiCommandTestCase := test_cases.MultiCommandTestCase{
32-
Commands: [][]string{
33-
append([]string{"RPUSH", randomListKey}, elementsForFirstCmd...),
34-
append([]string{"RPUSH", randomListKey}, elementsForSecondCmd...),
35-
},
36-
Assertions: []resp_assertions.RESPAssertion{
37-
resp_assertions.NewIntegerAssertion(2),
38-
resp_assertions.NewIntegerAssertion(5),
32+
CommandWithAssertions: []test_cases.CommandWithAssertion{
33+
{
34+
Command: append([]string{"RPUSH", randomListKey}, elementsForFirstCmd...),
35+
Assertion: resp_assertions.NewIntegerAssertion(2),
36+
},
37+
{
38+
Command: append([]string{"RPUSH", randomListKey}, elementsForSecondCmd...),
39+
Assertion: resp_assertions.NewIntegerAssertion(5),
40+
},
3941
},
4042
}
4143

0 commit comments

Comments
 (0)