Permalink
Browse files

added dbi.test to test each drivers.

  • Loading branch information...
1 parent 8821520 commit 8b29ec45c30d70a39ef533075f1f596ba95c67ea @fukamachi committed Dec 18, 2011
Showing with 71 additions and 55 deletions.
  1. +9 −2 dbi-test.asd
  2. +60 −0 src/test.lisp
  3. +2 −53 t/dbd/sqlite3.lisp
View
11 dbi-test.asd
@@ -13,8 +13,15 @@
:license "LLGPL"
:depends-on (:dbi
:cl-test-more
- :closer-mop)
- :components ((:module "t"
+ :closer-mop
+ :cl-syntax
+ :cl-syntax-annot
+ :trivial-types)
+ :components ((:module "src"
+ :components
+ ((:file "test")))
+ (:module "t"
+ :depends-on ("src")
:components
((:file "driver")
(:module "dbd"
View
60 src/test.lisp
@@ -0,0 +1,60 @@
+#|
+ This file is a part of CL-DBI project.
+ Copyright (c) 2011 Eitarow Fukamachi (e.arrows@gmail.com)
+|#
+
+(in-package :cl-user)
+(defpackage dbi.test
+ (:use :cl
+ :dbi
+ :dbi.driver
+ :cl-test-more
+ :trivial-types))
+(in-package :dbi.test)
+
+(cl-syntax:use-syntax :annot)
+
+(defparameter *db* nil)
+
+@export
+(defun run-driver-tests (driver-name &rest params)
+ (let ((*db* (apply #'connect driver-name params)))
+ (plan 15)
+ (run-test-all)))
+
+(deftest |connect|
+ (is-type *db* '<dbi-connection>))
+
+(deftest |do-sql|
+ (is (do-sql *db* "DROP TABLE IF EXISTS person") nil)
+ (is (do-sql *db* "CREATE TABLE person (id INTEGER PRIMARY KEY, name VARCHAR(24) NOT NULL)")
+ nil)
+ (is (do-sql *db* "INSERT INTO person (id, name) VALUES (1, 'fukamachi')")
+ nil)
+ (is (do-sql *db* "INSERT INTO person (id, name) VALUES (2, 'matsuyama')")
+ nil))
+
+(deftest |prepare, execute & fetch|
+ (let (query result)
+ (setf query (prepare *db* "SELECT * FROM person"))
+ (is-type query '<dbd-query>)
+ (setf result (execute query))
+ (ok result)
+ (ok (property-list-p (fetch result)))
+ (ok (property-list-p (fetch result)))
+ (is (fetch result) nil)))
+
+(deftest |place holder|
+ (let (query result)
+ (setf query (prepare *db* "SELECT * FROM person WHERE name = ?"))
+ (is-type query '<dbd-query>)
+ (setf result (execute query "matsuyama"))
+ (ok result)
+ (ok (property-list-p (fetch result)))
+ (is (fetch result) nil)))
+
+(deftest |with-transaction|
+ (with-transaction *db*
+ (do-sql *db* "INSERT INTO person (id, name) VALUES (3, 'meymao')"))
+ (is (fetch (execute (prepare *db* "SELECT * FROM person WHERE name = 'meymao'")))
+ '(:|id| 3 :|name| "meymao")))
View
55 t/dbd/sqlite3.lisp
@@ -7,58 +7,7 @@
(defpackage dbd-sqlite3-test
(:use :cl
:cl-test-more
- :dbi))
+ :dbi.test))
(in-package :dbd-sqlite3-test)
-(plan 5)
-
-(defvar *db-path*
- (asdf:system-relative-pathname
- (asdf:find-system :dbd-sqlite3)
- #p"t/dbd/tmp.db"))
-
-(ignore-errors (delete-file *db-path*))
-
-(defparameter *db* (connect :sqlite3 :database-name *db-path*))
-
-(do-sql *db* "CREATE TABLE user ( id INTEGER PRIMARY KEY, name VARCHAR(24) NOT NULL )")
-(do-sql *db* "INSERT INTO user (id, name) VALUES (1, 'fukamachi')")
-
-(let* ((query (prepare *db* "SELECT name FROM user"))
- (result (execute query)))
- (is (fetch result) '(:|name| "fukamachi") "fetch"))
-
-(begin-transaction *db*)
-(do-sql *db* "INSERT INTO user (id, name) VALUES (2, 'matsuyama')")
-(commit *db*)
-
-(let* ((query (prepare *db* "SELECT name FROM user WHERE name = 'matsuyama'"))
- (result (execute query)))
- (is (fetch result)
- '(:|name| "matsuyama")
- "begin-transaction & commit"))
-
-(with-transaction *db*
- (do-sql *db* "INSERT INTO user (id, name) VALUES (3, 'meymao')"))
-
-(let* ((query (prepare *db* "SELECT name FROM user WHERE name = 'meymao'"))
- (result (execute query)))
- (is (fetch result)
- '(:|name| "meymao")
- "with-transaction"))
-
-;; raise a simple-error to test `rollback'.
-(ignore-errors
- (with-transaction *db*
- (do-sql *db* "INSERT INTO user (id, name) VALUES (4, 'foobar')")
- (error 'simple-error)))
-
-(let* ((query (prepare *db* "SELECT name FROM user WHERE name = 'foobar'"))
- (result (execute query)))
- (is (fetch result)
- nil
- "with-transaction & rollback"))
-
-(ok (disconnect *db*) "disconnect")
-
-(finalize)
+(dbi.test:run-driver-tests :sqlite3 :database-name ":memory:")

0 comments on commit 8b29ec4

Please sign in to comment.