newlisp-redis / test-redis.lsp

#! /usr/bin/newlisp

(define-macro (assert expr)
    (let ((assertion-string (join (list "Assertion failed: " (string expr)) "")))
        (unless (true? (eval expr)) (throw-error assertion-string))))

(define-macro (assert-false expr)
    (let ((assertion-string (join (list "Assertion false failed: " (string expr)) "")))
        (if (true? (eval expr)) (throw-error assertion-string))))

(define-macro (assert-equals expr val)
  (letex ((e (eval expr)))
    (assert (= e val))))

(define-macro (assert-fail expr)
    (if (nil? (catch (eval expr) 'exc))
      true ; it failed, just what we wanted
      (throw-error (append "Should have failed: " (string expr)))))

(load "redis.lsp")
(setf conn (redis))

(constant 'TEST_DB 15)
(constant 'MOVE_DB 14)

(assert (:select conn MOVE_DB))
(assert (:flushdb conn))
(assert (:select conn TEST_DB))
(assert (:flushdb conn))

(let (info (:info conn))
  (assert (lookup "redis_version" info))
  (assert (lookup "connected_clients" info))
  (assert (int (lookup "uptime_in_seconds" info)))
  (assert (int (lookup "connected_clients" info))))

;
; SET
;
(assert (:set conn "crap" "bag"))
(assert (:set conn "foo" "bar"))
(assert (:set conn "count" 0))

(assert-equals (:setnx conn "foo" "baz") 0)
(assert-equals (:setnx conn "boo" "unset") 1)

;
; GET
; 
(assert-equals (:get conn "foo") "bar")
(assert-equals (:get conn "boo") "unset")

(let (rep (:mget conn "foo" "boo"))
  (assert-equals (rep 0) "bar")
  (assert-equals (rep 1) "unset"))

(assert-equals (:getset conn "foo" "crackle") "bar")

;(let (uni (string "�" "�" (char 937)))
;  (assert (:set conn "unicode" uni))
;  (assert-equals (:get conn "unicode") uni))

;
; INCR
;
(assert-equals (:incr conn "count") 1)
(assert-equals (:incr conn "count") 2)

(assert-equals (:incrby conn "count" 4) 6)
(assert-equals (:incrby conn "count" -2) 4)

(assert-equals (:decr conn "count") 3)

(assert-equals (:decrby conn "count" 6) -3)

;
; EXISTS
;
(assert (:exists conn "count"))
(assert (:exists conn "foo"))
(assert-false (:exists conn "counta"))

(assert (:del conn "count"))
(assert-false (:exists conn "count"))

;
; KEYS
;
(assert (:set conn "foo2" "value"))

(let (keys (:keys conn "foo*"))
  (assert-equals (length keys) 2))

(let (keys (:keys conn "*"))
  (assert-equals (length keys) 4))

(let (keys (:keys conn "?oo"))
  (assert-equals (length keys) 2)
  (assert (member "boo" keys))
  (assert (member "foo" keys)))

(dotimes (x 20)
  (assert (member (:randomkey conn) '("boo" "foo" "count" "crap" "foo2"))))



(println "All tests...OK")
(exit)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.