r1676: *** 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.1 2002/03/27 09:00:15 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-clsql"
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                (pprint (query "select * from test_clsql" 
60                                     :database db
61                                     :types :auto))
62                (pprint (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 (defparameter clsql-test-suite 
70     (make-test-suite
71      "CLSQL Test Suite"
72      "Basic test suite for database operations."
73      ("MySQL Test" 'clsql-fixture
74                    :test-thunk 'mysql-test
75                    :description "A test of MySQL")))
76
77 (report-result (run-test clsql-test-suite) :verbose t)
78
79
80 ;;;; Testing functions
81
82 (defun create-test-table (db)
83   (ignore-errors
84     (clsql:execute-command 
85      "DROP TABLE test_clsql" :database db))
86   (clsql:execute-command 
87    "CREATE TABLE test_clsql (n integer, n_pi float, n_pi_str CHAR(20))" 
88    :database db)
89   (dotimes (i 11)
90     (let ((n (- i 5)))
91       (clsql:execute-command
92        (format nil "INSERT INTO test_clsql VALUES (~a,~a,'~a')"
93                n (clsql:float-to-sql-string (* pi n))
94                (clsql:float-to-sql-string (* pi n)))
95        :database db))))
96
97 (defun drop-test-table (db)
98   (clsql:execute-command "DROP TABLE test_clsql"))