]> git.saurik.com Git - redis.git/commitdiff
Scripting tests added
authorantirez <antirez@gmail.com>
Tue, 24 May 2011 16:40:37 +0000 (18:40 +0200)
committerantirez <antirez@gmail.com>
Wed, 25 May 2011 10:32:50 +0000 (12:32 +0200)
tests/test_helper.tcl
tests/unit/scripting.tcl [new file with mode: 0644]

index 6dc85eff37c4a137fad0c382d76af675335f0d52..e2a654285dd2165d965e336dd36af725fcfa5b37 100644 (file)
@@ -127,6 +127,7 @@ proc execute_everything {} {
     execute_tests "integration/aof"
 #    execute_tests "integration/redis-cli"
     execute_tests "unit/pubsub"
+    execute_tests "unit/scripting"
 
     return; # No diskstore tests for now...
     # run tests with diskstore enabled
diff --git a/tests/unit/scripting.tcl b/tests/unit/scripting.tcl
new file mode 100644 (file)
index 0000000..22b553d
--- /dev/null
@@ -0,0 +1,126 @@
+start_server {tags {"scripting"}} {
+    test {EVAL - Does Lua interpreter replies to our requests?} {
+        r eval {return 'hello'} 0
+    } {hello}
+
+    test {EVAL - Lua integer -> Redis protocol type conversion} {
+        r eval {return 100.5} 0
+    } {100}
+
+    test {EVAL - Lua string -> Redis protocol type conversion} {
+        r eval {return 'hello world'} 0
+    } {hello world}
+
+    test {EVAL - Lua true boolean -> Redis protocol type conversion} {
+        r eval {return true} 0
+    } {1}
+
+    test {EVAL - Lua false boolean -> Redis protocol type conversion} {
+        r eval {return false} 0
+    } {}
+
+    test {EVAL - Lua status code reply -> Redis protocol type conversion} {
+        r eval {return {ok='fine'}} 0
+    } {fine}
+
+    test {EVAL - Lua error reply -> Redis protocol type conversion} {
+        catch {
+            r eval {return {err='this is an error'}} 0
+        } e
+        set _ $e
+    } {this is an error}
+
+    test {EVAL - Lua table -> Redis protocol type conversion} {
+        r eval {return {1,2,3,'ciao',{1,2}}} 0
+    } {1 2 3 ciao {1 2}}
+
+    test {EVAL - Are the KEYS and ARGS arrays populated correctly?} {
+        r eval {return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}} 2 a b c d
+    } {a b c d}
+
+    test {EVAL - is Lua able to call Redis API?} {
+        r set mykey myval
+        r eval {return redis.call('get','mykey')} 0
+    } {myval}
+
+    test {EVALSHA - Can we call a SHA1 if already defined?} {
+        r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0
+    } {myval}
+
+    test {EVALSHA - Do we get an error on non defined SHA1?} {
+        catch {r evalsha ffffffffffffffffffffffffffffffffffffffff 0} e
+        set _ $e
+    } {NOSCRIPT*}
+
+    test {EVAL - Redis integer -> Lua type conversion} {
+        r eval {
+            local foo = redis.call('incr','x')
+            return {type(foo),foo}
+        } 0
+    } {number 1}
+
+    test {EVAL - Redis bulk -> Lua type conversion} {
+        r set mykey myval
+        r eval {
+            local foo = redis.call('get','mykey')
+            return {type(foo),foo}
+        } 0
+    } {string myval}
+
+    test {EVAL - Redis multi bulk -> Lua type conversion} {
+        r del mylist
+        r rpush mylist a
+        r rpush mylist b
+        r rpush mylist c
+        r eval {
+            local foo = redis.call('lrange','mylist',0,-1)
+            return {type(foo),foo[1],foo[2],foo[3],# foo}
+        } 0
+    } {table a b c 3}
+
+    test {EVAL - Redis status reply -> Lua type conversion} {
+        r eval {
+            local foo = redis.call('set','mykey','myval')
+            return {type(foo),foo['ok']}
+        } 0
+    } {table OK}
+
+    test {EVAL - Redis error reply -> Lua type conversion} {
+        r set mykey myval
+        r eval {
+            local foo = redis.call('incr','mykey')
+            return {type(foo),foo['err']}
+        } 0
+    } {table {ERR value is not an integer or out of range}}
+
+    test {EVAL - Redis nil bulk reply -> Lua type conversion} {
+        r del mykey
+        r eval {
+            local foo = redis.call('get','mykey')
+            return {type(foo),foo == false}
+        } 0
+    } {boolean 1}
+
+    test {EVAL - Is Lua affecting the currently selected DB?} {
+        r set mykey "this is DB 9"
+        r select 10
+        r set mykey "this is DB 10"
+        r eval {return redis.call('get','mykey')} 0
+    } {this is DB 10}
+
+    test {EVAL - Is Lua seleced DB retained?} {
+        r eval {return redis.call('select','9')} 0
+        r get mykey
+    } {this is DB 9}
+
+    test {EVAL - Script can't run more than configured time limit} {
+        r config set lua-time-limit 1
+        catch {
+            r eval {
+                local i = 0
+                while true do i=i+1 end
+            } 0
+        } e
+        set _ $e
+    } {*execution time*}
+}