Skip to content

Commit 3031702

Browse files
author
Andrey Lebedev
committed
mysql tests will not be run if there is no chance of passing.
Notes about mysql backend added
1 parent b5c23e0 commit 3031702

File tree

3 files changed

+99
-31
lines changed

3 files changed

+99
-31
lines changed

doc/mysql.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=============
2+
MySQL Backend
3+
=============
4+
5+
This is notes about mysql backend for roundup issue tracker.
6+
7+
8+
Prerequisites
9+
=============
10+
11+
To use MySQL as backend for storing roundup data, you should additionally
12+
install:
13+
14+
1. MySQL RDBMS 3.23.34 or higher - http://www.mysql.com. Your MySQL
15+
installation should support Berkeley DB (BDB) tabes for transaction
16+
support.
17+
2. Python interface to mysql - http://sourceforge.net/projects/mysql-python
18+
19+
20+
How to run mysql tests?
21+
=======================
22+
23+
Roundup tests expect empty database available for use. There are two ways how to
24+
provide it:
25+
26+
1. If you have root permissions on mysql server, you can create necessary
27+
database using this SQL sequence:
28+
29+
CREATE DATABASE rounduptest
30+
GRANT ALL PRIVILEGES ON rounduptest TO rounduptest@localhost IDENTIFIED BY 'rounduptest'
31+
FLUSH PRIVILEGES
32+
33+
2. If your administrator has provided you with database connection info, you
34+
can modify MYSQL_* constants in test/test_db.py with corresponding
35+
values.
36+
37+
Note, that mysql database should not contain any tables. Tests will not dare to
38+
drop database with data.
39+
40+
41+
Andrey Lebedev <[email protected]>
42+
43+
44+
vim: et tw=80

roundup/backends/back_mysql.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2003 Martynas Sklyzmantas, Andrey Lebedev
2+
# Copyright (c) 2003 Martynas Sklyzmantas, Andrey Lebedev <[email protected]>
33
#
44
# This module is free software, and you may redistribute it and/or modify
55
# under the same terms as Python, so long as this copyright message and
@@ -29,6 +29,9 @@ def open_connection(self):
2929
self.sql("BEGIN")
3030
try:
3131
self.database_schema = self.load_dbschema()
32+
except MySQLdb.OperationalError, message:
33+
if message[0] != ER.NO_DB_ERROR:
34+
raise
3235
except MySQLdb.ProgrammingError, message:
3336
if message[0] != ER.NO_SUCH_TABLE:
3437
raise DatabaseError, message
@@ -171,7 +174,7 @@ def find(self, **propspec):
171174
self.db.sql(query, vals)
172175
l += [x[0] for x in self.db.sql_fetchall()]
173176
if __debug__:
174-
print >>hyperdb.DEBUG, 'find ... ', l
177+
print >>hyperdb.DEBUG, 'find ... ', l #db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME)
175178

176179
# Remove duplicated ids
177180
d = {}
@@ -188,3 +191,9 @@ class IssueClass(MysqlClass, rdbms_common.IssueClass):
188191
class FileClass(MysqlClass, rdbms_common.FileClass):
189192
pass
190193

194+
def nuke(config):
195+
""" Clear all database contents and drop database itself"""
196+
# Connect to db
197+
db = Database(config, 'admin')
198+
db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME)
199+
db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME)

test/test_db.py

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_db.py,v 1.69 2003-02-08 15:31:28 kedder Exp $
18+
# $Id: test_db.py,v 1.70 2003-02-15 14:26:38 kedder Exp $
1919

2020
import unittest, os, shutil, time
2121

@@ -65,6 +65,15 @@ class config:
6565
ANONYMOUS_REGISTER = 'deny' # either 'deny' or 'allow'
6666
MESSAGES_TO_AUTHOR = 'no' # either 'yes' or 'no'
6767
EMAIL_SIGNATURE_POSITION = 'bottom'
68+
# Mysql connection data
69+
MYSQL_DBHOST = 'localhost'
70+
MYSQL_DBUSER = 'rounduptest'
71+
MYSQL_DBPASSWORD = 'rounduptest'
72+
MYSQL_DBNAME = 'rounduptest'
73+
MYSQL_DATABASE = (MYSQL_DBHOST, MYSQL_DBUSER, MYSQL_DBPASSWORD, MYSQL_DBNAME)
74+
75+
class nodbconfig(config):
76+
MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD)
6877

6978
class anydbmDBTestCase(MyTestCase):
7079
def setUp(self):
@@ -715,51 +724,39 @@ def setUp(self):
715724
self.db = gadfly.Database(config)
716725
setupSchema(self.db, 0, gadfly)
717726

718-
719-
# XXX to fix the mysql tests...
720-
# From: Andrey Lebedev <[email protected]>
721-
# I believe we can DROP DATABASE <dbname> and then CREATE DATABASE
722-
# <dbname>.. it's an easiest way. This will work if db user has all
723-
# privileges on database.
724-
# Another way - to perform "SHOW TABLES" SQL and then perform DROP TABLE
725-
# <tblname> on each table...
726727
class mysqlDBTestCase(anydbmDBTestCase):
727728
def setUp(self):
728729
from roundup.backends import mysql
729730
# remove previous test, ignore errors
730731
if os.path.exists(config.DATABASE):
731732
shutil.rmtree(config.DATABASE)
732-
config.MYSQL_DATABASE = ('localhost', 'rounduptest', 'rounduptest',
733-
'rounduptest')
734733
os.makedirs(config.DATABASE + '/files')
735-
# open database for cleaning
736-
self.db = mysql.Database(config, 'admin')
737-
self.db.sql("DROP DATABASE %s" % config.MYSQL_DATABASE[1])
738-
self.db.sql("CREATE DATABASE %s" % config.MYSQL_DATABASE[1])
739-
self.db.close()
740734
# open database for testing
741-
self.db = mysql.Database(config, 'admin')
742-
735+
self.db = mysql.Database(config, 'admin')
743736
setupSchema(self.db, 1, mysql)
737+
738+
def tearDown(self):
739+
from roundup.backends import mysql
740+
self.db.close()
741+
mysql.nuke(config)
742+
anydbmDBTestCase.tearDown(self)
744743

745744
class mysqlReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
746745
def setUp(self):
747746
from roundup.backends import mysql
748747
# remove previous test, ignore errors
749748
if os.path.exists(config.DATABASE):
750749
shutil.rmtree(config.DATABASE)
751-
config.MYSQL_DATABASE = ('localhost', 'rounduptest', 'rounduptest',
752-
'rounduptest')
753750
os.makedirs(config.DATABASE + '/files')
754-
# open database for cleaning
755-
self.db = mysql.Database(config, 'admin')
756-
self.db.sql("DROP DATABASE %s" % config.MYSQL_DATABASE[1])
757-
self.db.sql("CREATE DATABASE %s" % config.MYSQL_DATABASE[1])
758-
self.db.close()
759-
# open database for testing
760751
self.db = mysql.Database(config)
761752
setupSchema(self.db, 0, mysql)
762753

754+
def tearDown(self):
755+
from roundup.backends import mysql
756+
self.db.close()
757+
mysql.nuke(config)
758+
anydbmReadOnlyDBTestCase.tearDown(self)
759+
763760
class sqliteDBTestCase(anydbmDBTestCase):
764761
def setUp(self):
765762
from roundup.backends import sqlite
@@ -865,9 +862,27 @@ def suite():
865862
from roundup import backends
866863
p = []
867864
if hasattr(backends, 'mysql'):
868-
p.append('mysql')
869-
l.append(unittest.makeSuite(mysqlDBTestCase, 'test'))
870-
l.append(unittest.makeSuite(mysqlReadOnlyDBTestCase, 'test'))
865+
from roundup.backends import mysql
866+
try:
867+
# Check if we can run mysql tests
868+
import MySQLdb
869+
db = mysql.Database(nodbconfig, 'admin')
870+
db.conn.select_db(config.MYSQL_DBNAME)
871+
db.sql("SHOW TABLES");
872+
tables = db.sql_fetchall()
873+
if tables:
874+
# Database should be empty. We don't dare to delete any data
875+
raise DatabaseError, "(Database %s contains tables)" % config.MYSQL_DBNAME
876+
db.sql("DROP DATABASE IF EXISTS %s" % config.MYSQL_DBNAME)
877+
db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME)
878+
db.close()
879+
except (MySQLdb.ProgrammingError, DatabaseError), msg:
880+
print "Warning! Mysql tests will not be performed", msg
881+
print "See doc/mysql.txt for more details."
882+
else:
883+
p.append('mysql')
884+
l.append(unittest.makeSuite(mysqlDBTestCase, 'test'))
885+
l.append(unittest.makeSuite(mysqlReadOnlyDBTestCase, 'test'))
871886
#return unittest.TestSuite(l)
872887

873888
if hasattr(backends, 'gadfly'):

0 commit comments

Comments
 (0)