Code

Add hasattr() check for backend in mysqlOpener.
[roundup.git] / test / test_mysql.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 # $Id: test_mysql.py,v 1.2 2003-10-26 14:13:04 jlgijsbers Exp $ 
20 import unittest, os, shutil, time, imp
22 from roundup.hyperdb import DatabaseError
23 from roundup import init, backends
25 from db_test_base import DBTest, ROTest, config, SchemaTest, nodbconfig, \
26     ClassicInitTest
28 class mysqlOpener:
29     if hasattr(backends, 'mysql'):
30         from roundup.backends import mysql as module
32     def tearDown(self):
33         self.db.close()
34         self.module.db_nuke(config)
36 class mysqlDBTest(mysqlOpener, DBTest):
37     pass
39 class mysqlROTest(mysqlOpener, ROTest):
40     pass
42 class mysqlSchemaTest(mysqlOpener, SchemaTest):
43     pass
45 class mysqlClassicInitTest(ClassicInitTest):
46     backend = 'mysql'
48     def testCreation(self):
49         ae = self.assertEqual
51         # create the instance
52         init.install(self.dirname, 'templates/classic')
53         init.write_select_db(self.dirname, self.backend)
54         f = open(os.path.join(self.dirname, 'config.py'), 'a')
55         try:
56             f.write('''
57 MYSQL_DBHOST = 'localhost'
58 MYSQL_DBUSER = 'rounduptest'
59 MYSQL_DBPASSWORD = 'rounduptest'
60 MYSQL_DBNAME = 'rounduptest'
61 MYSQL_DATABASE = (MYSQL_DBHOST, MYSQL_DBUSER, MYSQL_DBPASSWORD, MYSQL_DBNAME)
62             ''')
63         finally:
64             f.close()
65         init.initialise(self.dirname, 'sekrit')
67         # check we can load the package
68         instance = imp.load_package(self.dirname, self.dirname)
70         # and open the database
71         db = instance.open()
73         # check the basics of the schema and initial data set
74         l = db.priority.list()
75         ae(l, ['1', '2', '3', '4', '5'])
76         l = db.status.list()
77         ae(l, ['1', '2', '3', '4', '5', '6', '7', '8'])
78         l = db.keyword.list()
79         ae(l, [])
80         l = db.user.list()
81         ae(l, ['1', '2'])
82         l = db.msg.list()
83         ae(l, [])
84         l = db.file.list()
85         ae(l, [])
86         l = db.issue.list()
87         ae(l, [])
89     from roundup.backends import mysql as module
90     def tearDown(self):
91         ClassicInitTest.tearDown(self)
92         self.module.db_nuke(config)
94 def test_suite():
95     suite = unittest.TestSuite()
96     if not hasattr(backends, 'mysql'):
97         return suite
99     from roundup.backends import mysql
100     try:
101         # Check if we can run mysql tests
102         import MySQLdb
103         db = mysql.Database(nodbconfig, 'admin')
104         db.conn.select_db(config.MYSQL_DBNAME)
105         db.sql("SHOW TABLES");
106         tables = db.sql_fetchall()
107         if 0: #tables:
108             # Database should be empty. We don't dare to delete any data
109             raise DatabaseError, "Database %s contains tables"%\
110                 config.MYSQL_DBNAME
111         db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME)
112         db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME)
113         db.close()
114     except (MySQLdb.ProgrammingError, DatabaseError), msg:
115         print "Skipping mysql tests (%s)"%msg
116     else:
117         print 'Including mysql tests'
118         suite.addTest(unittest.makeSuite(mysqlDBTest))
119         suite.addTest(unittest.makeSuite(mysqlROTest))
120         suite.addTest(unittest.makeSuite(mysqlSchemaTest))
121         suite.addTest(unittest.makeSuite(mysqlClassicInitTest))
122     return suite
124 if __name__ == '__main__':
125     runner = unittest.TextTestRunner()
126     unittest.main(testRunner=runner)