-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Fix Redis script arguments handling and add unit tests
Corrected the handling of arguments in Redis script execution by adjusting the evaluation arguments assignment. Added unit tests to ensure the proper execution and error handling of Redis scripts. Signed-off-by: Christian Roessner <c@roessner.co>
- Loading branch information
Christian Roessner
committed
Oct 24, 2024
1 parent
490053a
commit 6921e37
Showing
3 changed files
with
102 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package redislib | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/croessner/nauthilus/server/rediscli" | ||
"github.com/go-redis/redismock/v9" | ||
"github.com/stretchr/testify/assert" | ||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
func TestRedisRunScript(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
script string | ||
keys []string | ||
args []any | ||
expectErr bool | ||
expectRes string | ||
prepareMockRedis func(mock redismock.ClientMock) | ||
}{ | ||
{ | ||
name: "ValidScript", | ||
script: "return redis.call('set',KEYS[1],'bar')", | ||
keys: []string{"foo"}, | ||
args: []any{}, | ||
expectErr: false, | ||
expectRes: "mock result", | ||
prepareMockRedis: func(mock redismock.ClientMock) { | ||
mock.ExpectEval("return redis.call('set',KEYS[1],'bar')", []string{"foo"}).SetVal("mock result") | ||
}, | ||
}, | ||
{ | ||
name: "InvalidScript", | ||
script: "return redis.call('set',KEYS[1])", // missing value for 'set' | ||
keys: []string{"foo"}, | ||
args: []any{}, | ||
expectErr: true, | ||
expectRes: "", | ||
prepareMockRedis: func(mock redismock.ClientMock) { | ||
mock.ExpectEval("return redis.call('set',KEYS[1])", []string{"foo"}).SetErr(errors.New("missing value for 'set'")) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
db, mock := redismock.NewClientMock() | ||
if db == nil || mock == nil { | ||
t.Fatalf("Failed to create Redis mock client.") | ||
} | ||
|
||
tc.prepareMockRedis(mock) | ||
rediscli.WriteHandle = db | ||
|
||
L := lua.NewState() | ||
|
||
defer L.Close() | ||
|
||
// Set up script | ||
L.Push(lua.LString(tc.script)) | ||
|
||
// Set up keys | ||
keys := L.CreateTable(len(tc.keys), 0) | ||
for _, k := range tc.keys { | ||
keys.Append(lua.LString(k)) | ||
} | ||
|
||
L.Push(keys) | ||
|
||
// Set up args | ||
args := L.CreateTable(len(tc.args), 0) | ||
for _, a := range tc.args { | ||
args.Append(lua.LString(a.(string))) // Annahme, dass args vom Typ string sind | ||
} | ||
|
||
L.Push(args) | ||
|
||
// Call function and check error | ||
numReturned := RedisRunScript(L) | ||
errReturned := L.Get(-2).String() != "nil" | ||
|
||
assert.Equal(t, tc.expectErr, errReturned, "") | ||
assert.Equal(t, 2, numReturned, "") | ||
|
||
// Check result if no error | ||
if !tc.expectErr && numReturned > 0 { | ||
resReturned := L.Get(-1).String() | ||
|
||
assert.Equal(t, tc.expectRes, resReturned, "") | ||
} | ||
|
||
// Check if everything expected was done | ||
assert.NoError(t, mock.ExpectationsWereMet()) | ||
}) | ||
} | ||
} |