From b6781e69e55d9f652dee131866a1b3872acb1b01 Mon Sep 17 00:00:00 2001 From: Juliano Solanho Date: Mon, 3 Jun 2024 17:48:24 -0300 Subject: [PATCH 1/4] Ensure literal argument ordering (failing test) --- nri-redis/test/Spec/Redis.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nri-redis/test/Spec/Redis.hs b/nri-redis/test/Spec/Redis.hs index 2ebecc15..84642966 100644 --- a/nri-redis/test/Spec/Redis.hs +++ b/nri-redis/test/Spec/Redis.hs @@ -400,9 +400,11 @@ queryTests redisHandler = [Redis.script| local a = ${Redis.Key 2} local b = ${Redis.Literal 3} - return b|] + local c = ${Redis.Literal 4} + local d = ${Redis.Literal 5} + return {b, c, d}|] result <- Redis.eval testNS script |> Expect.succeeds - Expect.equal result 3, + Expect.equal result [3, 4, 5], Test.test "eval with arguments namespaces key" <| \() -> do let script = [Redis.script|return ${Redis.Key "hi"}|] (result :: Text) <- Redis.eval testNS script |> Expect.succeeds From 50c67862704ca8c88714424b82aebe20dc66b8e2 Mon Sep 17 00:00:00 2001 From: Juliano Solanho Date: Mon, 3 Jun 2024 17:48:32 -0300 Subject: [PATCH 2/4] Fix literal argument ordering --- nri-redis/src/Redis/Script.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nri-redis/src/Redis/Script.hs b/nri-redis/src/Redis/Script.hs index 1ab3e5a7..d53b644b 100644 --- a/nri-redis/src/Redis/Script.hs +++ b/nri-redis/src/Redis/Script.hs @@ -202,7 +202,7 @@ scriptFromEvaluatedTokens quasiQuotedString' evaluatedTokens = { luaScript = buffer script', quasiQuotedString = quasiQuotedString', keys = keyList script', - arguments = Log.mkSecret (argList script') + arguments = Log.mkSecret (List.reverse (argList script')) } ----------------------------- From 17a5d3dd0b0c40329517559618901b7fff49898a Mon Sep 17 00:00:00 2001 From: Juliano Solanho Date: Mon, 3 Jun 2024 17:54:25 -0300 Subject: [PATCH 3/4] Ditch printScript --- nri-redis/src/Redis/Script.hs | 10 ---------- nri-redis/test/Spec/Redis/Script.hs | 20 ++++++++++++++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/nri-redis/src/Redis/Script.hs b/nri-redis/src/Redis/Script.hs index d53b644b..60b54cc5 100644 --- a/nri-redis/src/Redis/Script.hs +++ b/nri-redis/src/Redis/Script.hs @@ -15,7 +15,6 @@ module Redis.Script Tokens (..), ScriptParam (..), HasScriptParam (..), - printScript, ) where @@ -275,12 +274,3 @@ toHex bytes = |> List.map (Text.Printf.printf "%02x") |> List.concat |> Text.fromList - ---------------------------------------------- --- Helper functions for testing ---------------------------------------------- - -printScript :: Script a -> Text -printScript Script {luaScript, quasiQuotedString, keys, arguments} = - let listStr l = List.map (\s -> "\"" ++ s ++ "\"") l |> Text.join ", " - in "Script { luaScript = \"" ++ luaScript ++ "\", quasiQuotedString = \"" ++ quasiQuotedString ++ "\", keys = [" ++ listStr keys ++ "], arguments = [" ++ listStr (Log.unSecret arguments) ++ "] }" diff --git a/nri-redis/test/Spec/Redis/Script.hs b/nri-redis/test/Spec/Redis/Script.hs index 1c638cf2..622ea102 100644 --- a/nri-redis/test/Spec/Redis/Script.hs +++ b/nri-redis/test/Spec/Redis/Script.hs @@ -113,12 +113,24 @@ thTests :: List Test.Test thTests = [ Test.test "just text" <| \_ -> [script|some text|] - |> printScript - |> Expect.equal "Script { luaScript = \"some text\", quasiQuotedString = \"some text\", keys = [], arguments = [] }", + |> Expect.equal + ( Script + { luaScript = "some text", + quasiQuotedString = "some text", + keys = [], + arguments = Log.mkSecret [] + } + ), Test.test "one key argument" <| \_ -> [script|${Key "hi"}|] - |> printScript - |> Expect.equal "Script { luaScript = \"KEYS[1]\", quasiQuotedString = \"${Key \"hi\"}\", keys = [\"hi\"], arguments = [] }", + |> Expect.equal + ( Script + { luaScript = "KEYS[1]", + quasiQuotedString = "${Key \"hi\"}", + keys = ["hi"], + arguments = Log.mkSecret [] + } + ), Test.test "fails on type-checking when not given Key or Literal" <| \_ -> [script|${False}|] |> arguments From a55a1437a53020de6c8618e246c2b44ff07e5252 Mon Sep 17 00:00:00 2001 From: Juliano Solanho Date: Mon, 3 Jun 2024 18:03:57 -0300 Subject: [PATCH 4/4] Expand script test suite and fix keys out of order --- nri-redis/src/Redis/Script.hs | 2 +- nri-redis/test/Spec/Redis/Script.hs | 30 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/nri-redis/src/Redis/Script.hs b/nri-redis/src/Redis/Script.hs index 60b54cc5..88a1d16a 100644 --- a/nri-redis/src/Redis/Script.hs +++ b/nri-redis/src/Redis/Script.hs @@ -200,7 +200,7 @@ scriptFromEvaluatedTokens quasiQuotedString' evaluatedTokens = in Script { luaScript = buffer script', quasiQuotedString = quasiQuotedString', - keys = keyList script', + keys = List.reverse (keyList script'), arguments = Log.mkSecret (List.reverse (argList script')) } diff --git a/nri-redis/test/Spec/Redis/Script.hs b/nri-redis/test/Spec/Redis/Script.hs index 622ea102..b03c34f6 100644 --- a/nri-redis/test/Spec/Redis/Script.hs +++ b/nri-redis/test/Spec/Redis/Script.hs @@ -131,6 +131,36 @@ thTests = arguments = Log.mkSecret [] } ), + Test.test "one literal argument" <| \_ -> + [script|${Literal "hi"}|] + |> Expect.equal + ( Script + { luaScript = "ARGV[1]", + quasiQuotedString = "${Literal \"hi\"}", + keys = [], + arguments = Log.mkSecret ["hi"] + } + ), + Test.test "one key one literal argument" <| \_ -> + [script|${Key "a key"} ${Literal "a literal"}|] + |> Expect.equal + ( Script + { luaScript = "KEYS[1] ARGV[1]", + quasiQuotedString = "${Key \"a key\"} ${Literal \"a literal\"}", + keys = ["a key"], + arguments = Log.mkSecret ["a literal"] + } + ), + Test.test "multiple keys and literals" <| \_ -> + [script|${Key "key1"} ${Key "key2"} ${Key "key3"} ${Literal "literal1"} ${Literal "literal2"} ${Literal "literal3"}|] + |> Expect.equal + ( Script + { luaScript = "KEYS[1] KEYS[2] KEYS[3] ARGV[1] ARGV[2] ARGV[3]", + quasiQuotedString = "${Key \"key1\"} ${Key \"key2\"} ${Key \"key3\"} ${Literal \"literal1\"} ${Literal \"literal2\"} ${Literal \"literal3\"}", + keys = ["key1", "key2", "key3"], + arguments = Log.mkSecret ["literal1", "literal2", "literal3"] + } + ), Test.test "fails on type-checking when not given Key or Literal" <| \_ -> [script|${False}|] |> arguments