mirror of
https://github.com/vale981/lack
synced 2025-03-05 09:21:39 -05:00
Add tests of lack-session-store-dbi.
This commit is contained in:
parent
d08b5ca99f
commit
0acf96a5c1
4 changed files with 81 additions and 2 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -5,4 +5,5 @@
|
||||||
*.lx64fsl
|
*.lx64fsl
|
||||||
*.x86f
|
*.x86f
|
||||||
*~
|
*~
|
||||||
.#*
|
.#*
|
||||||
|
data/test.db
|
||||||
|
|
|
@ -11,4 +11,5 @@
|
||||||
:dbi
|
:dbi
|
||||||
:marshal
|
:marshal
|
||||||
:cl-base64)
|
:cl-base64)
|
||||||
:components ((:file "src/middleware/session/store/dbi")))
|
:components ((:file "src/middleware/session/store/dbi"))
|
||||||
|
:in-order-to ((test-op (test-op t-lack-session-store-dbi))))
|
||||||
|
|
19
t-lack-session-store-dbi.asd
Normal file
19
t-lack-session-store-dbi.asd
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
(in-package :cl-user)
|
||||||
|
(defpackage t-lack-session-store-dbi-asd
|
||||||
|
(:use :cl :asdf))
|
||||||
|
(in-package :t-lack-session-store-dbi-asd)
|
||||||
|
|
||||||
|
(defsystem t-lack-session-store-dbi
|
||||||
|
:author "Eitaro Fukamachi"
|
||||||
|
:license "LLGPL"
|
||||||
|
:depends-on (:lack
|
||||||
|
:lack-test
|
||||||
|
:lack-session-store-dbi
|
||||||
|
:prove
|
||||||
|
:dbi)
|
||||||
|
:components
|
||||||
|
((:test-file "t/session/store/dbi"))
|
||||||
|
|
||||||
|
:defsystem-depends-on (:prove-asdf)
|
||||||
|
:perform (test-op :after (op c)
|
||||||
|
(funcall (intern #.(string :run-test-system) :prove) c)))
|
58
t/session/store/dbi.lisp
Normal file
58
t/session/store/dbi.lisp
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
(in-package :cl-user)
|
||||||
|
(defpackage t.lack.session.store.dbi
|
||||||
|
(:use :cl
|
||||||
|
:lack
|
||||||
|
:lack.test
|
||||||
|
:lack.session.store.dbi
|
||||||
|
:dbi
|
||||||
|
:prove))
|
||||||
|
(in-package :t.lack.session.store.dbi)
|
||||||
|
|
||||||
|
(plan 2)
|
||||||
|
|
||||||
|
(defvar *test-db* (asdf:system-relative-pathname :lack "data/test.db"))
|
||||||
|
(when (probe-file *test-db*)
|
||||||
|
(delete-file *test-db*))
|
||||||
|
|
||||||
|
(defparameter *conn*
|
||||||
|
(dbi:connect :sqlite3 :database-name *test-db*))
|
||||||
|
|
||||||
|
(dbi:do-sql *conn*
|
||||||
|
"CREATE TABLE sessions (id CHAR(72) PRIMARY KEY, session_data TEXT)")
|
||||||
|
|
||||||
|
(subtest "session middleware"
|
||||||
|
(let ((app
|
||||||
|
(builder
|
||||||
|
(:session
|
||||||
|
:store (make-dbi-store
|
||||||
|
:connector (lambda () *conn*)))
|
||||||
|
(lambda (env)
|
||||||
|
(unless (gethash :counter (getf env :lack.session))
|
||||||
|
(setf (gethash :counter (getf env :lack.session)) 0))
|
||||||
|
`(200
|
||||||
|
(:content-type "text/plain")
|
||||||
|
(,(format nil "Hello, you've been here for ~Ath times!"
|
||||||
|
(incf (gethash :counter (getf env :lack.session)))))))))
|
||||||
|
session)
|
||||||
|
(diag "1st request")
|
||||||
|
(destructuring-bind (status headers body)
|
||||||
|
(funcall app (generate-env "/"))
|
||||||
|
(is status 200)
|
||||||
|
(setf session (parse-lack-session headers))
|
||||||
|
(ok session)
|
||||||
|
(is body '("Hello, you've been here for 1th times!")))
|
||||||
|
|
||||||
|
(diag "2nd request")
|
||||||
|
(destructuring-bind (status headers body)
|
||||||
|
(funcall app (generate-env "/" :cookies `(("lack.session" . ,session))))
|
||||||
|
(declare (ignore headers))
|
||||||
|
(is status 200)
|
||||||
|
(is body '("Hello, you've been here for 2th times!")))))
|
||||||
|
|
||||||
|
(let ((session (dbi:fetch (dbi:execute (dbi:prepare *conn* "SELECT COUNT(*) AS count FROM sessions")))))
|
||||||
|
(is (getf session :|count|) 1
|
||||||
|
"'sessions' has a single record"))
|
||||||
|
|
||||||
|
(dbi:disconnect *conn*)
|
||||||
|
|
||||||
|
(finalize)
|
Loading…
Add table
Reference in a new issue