r1678: *** empty log message ***
[clsql.git] / test-suite / xptest-clsql.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          xptest-clsql.cl
6 ;;;; Purpose:       Test of CLSQL using XPTest package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id: xptest-clsql.cl,v 1.3 2002/03/27 09:21:46 kevin Exp $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :cl-user)
21 (mk:load-system "XPTest")
22
23 (in-package :clsql-user)
24 (use-package :xptest)
25
26 (def-test-fixture clsql-fixture ()
27   ((aodbc-spec :accessor aodbc-spec)
28    (mysql-spec :accessor mysql-spec)
29    (postgresql-spec :accessor postgresql-spec)
30    (postgresql-socket-spec :accessor postgresql-socket-spec))
31   (:documentation "Test fixture for CLSQL testing"))
32
33 (defvar *config-pathname* (make-pathname :name "test"
34                                          :type "config"
35                                          :defaults *load-truename*))
36 (defmethod setup ((fix clsql-fixture))
37   (if (probe-file *config-pathname*)
38       (let (config)
39         (with-open-file (stream *config-pathname* :direction :input)
40           (setq config (read stream)))
41         (setf (aodbc-spec fix) (cadr (assoc :aodbc config)))
42         (setf (mysql-spec fix) (cadr (assoc :mysql config)))
43         (setf (postgresql-spec fix) (cadr (assoc :postgresql config)))
44         (setf (postgresql-socket-spec fix) 
45               (cadr (assoc :postgresql-socket config))))
46       (error "XPTest Config file ~S not found" *config-pathname*)))
47
48 (defmethod teardown ((fix clsql-fixture))
49   t)
50
51 (defmethod mysql-test ((test clsql-fixture))
52   (let ((spec (mysql-spec test)))
53     (when spec
54       (let ((db (clsql:connect spec :database-type :mysql 
55                                :if-exists :new)))
56         (unwind-protect
57              (progn
58                (create-test-table db)
59                (query "select * from test_clsql" 
60                       :database db
61                       :types :auto)
62                (map-query 'vector #'list "select * from test_clsql" 
63                           :database db
64                           :types :auto)
65                (drop-test-table db)
66                )
67           (disconnect :database db))))))
68
69 (defmethod mysql-low-level ((test clsql-fixture))
70   (let ((spec (mysql-spec test)))
71     (when spec
72       (let ((db (clsql-mysql::database-connect spec :mysql)))
73         (clsql-mysql::database-execute-command "DROP TABLE IF EXISTS test_clsql" db)
74         (clsql-mysql::database-execute-command 
75          "CREATE TABLE test_clsql (i integer, sqrt double, sqrt_str CHAR(20))" db)
76         (dotimes (i 10)
77           (clsql-mysql::database-execute-command
78            (format nil "INSERT INTO test_clsql VALUES (~d,~d,'~a')"
79                    i (sqrt i) (format nil "~d" (sqrt i)))
80            db))
81         (let ((res (clsql-mysql::database-query-result-set "select * from test_clsql" db :full-set t :types nil)))
82           (unless (= 10 (mysql:mysql-num-rows (clsql-mysql::mysql-result-set-res-ptr res)))
83             (failure "Error calling mysql-num-rows"))
84           (clsql-mysql::database-dump-result-set res db))
85         (clsql-mysql::database-execute-command "DROP TABLE test_clsql" db)
86         (clsql-mysql::database-disconnect db)))))
87
88 (defparameter clsql-test-suite 
89     (make-test-suite
90      "CLSQL Test Suite"
91      "Basic test suite for database operations."
92      ("MySQL Low Level Interface Test" 'clsql-fixture
93                    :test-thunk 'mysql-low-level
94                    :description "A test of MySQL low-level interface")
95      ("MySQL Test" 'clsql-fixture
96                    :test-thunk 'mysql-test
97                    :description "A test of MySQL")))
98
99
100 ;;;; Testing functions
101
102 (defun create-test-table (db)
103   (ignore-errors
104     (clsql:execute-command 
105      "DROP TABLE test_clsql" :database db))
106   (clsql:execute-command 
107    "CREATE TABLE test_clsql (n integer, n_pi float, n_pi_str CHAR(20))" 
108    :database db)
109   (dotimes (i 11)
110     (let ((n (- i 5)))
111       (clsql:execute-command
112        (format nil "INSERT INTO test_clsql VALUES (~a,~a,'~a')"
113                n (clsql:float-to-sql-string (* pi n))
114                (clsql:float-to-sql-string (* pi n)))
115        :database db))))
116
117 (defun drop-test-table (db)
118   (clsql:execute-command "DROP TABLE test_clsql"))
119
120 (report-result (run-test clsql-test-suite) :verbose t)
121
122