|
15 | 15 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
16 | 16 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
17 | 17 | # |
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 $ |
19 | 19 |
|
20 | 20 | import unittest, os, shutil, time |
21 | 21 |
|
@@ -65,6 +65,15 @@ class config: |
65 | 65 | ANONYMOUS_REGISTER = 'deny' # either 'deny' or 'allow' |
66 | 66 | MESSAGES_TO_AUTHOR = 'no' # either 'yes' or 'no' |
67 | 67 | 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) |
68 | 77 |
|
69 | 78 | class anydbmDBTestCase(MyTestCase): |
70 | 79 | def setUp(self): |
@@ -715,51 +724,39 @@ def setUp(self): |
715 | 724 | self.db = gadfly.Database(config) |
716 | 725 | setupSchema(self.db, 0, gadfly) |
717 | 726 |
|
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... |
726 | 727 | class mysqlDBTestCase(anydbmDBTestCase): |
727 | 728 | def setUp(self): |
728 | 729 | from roundup.backends import mysql |
729 | 730 | # remove previous test, ignore errors |
730 | 731 | if os.path.exists(config.DATABASE): |
731 | 732 | shutil.rmtree(config.DATABASE) |
732 | | - config.MYSQL_DATABASE = ('localhost', 'rounduptest', 'rounduptest', |
733 | | - 'rounduptest') |
734 | 733 | 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() |
740 | 734 | # open database for testing |
741 | | - self.db = mysql.Database(config, 'admin') |
742 | | - |
| 735 | + self.db = mysql.Database(config, 'admin') |
743 | 736 | 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) |
744 | 743 |
|
745 | 744 | class mysqlReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): |
746 | 745 | def setUp(self): |
747 | 746 | from roundup.backends import mysql |
748 | 747 | # remove previous test, ignore errors |
749 | 748 | if os.path.exists(config.DATABASE): |
750 | 749 | shutil.rmtree(config.DATABASE) |
751 | | - config.MYSQL_DATABASE = ('localhost', 'rounduptest', 'rounduptest', |
752 | | - 'rounduptest') |
753 | 750 | 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 |
760 | 751 | self.db = mysql.Database(config) |
761 | 752 | setupSchema(self.db, 0, mysql) |
762 | 753 |
|
| 754 | + def tearDown(self): |
| 755 | + from roundup.backends import mysql |
| 756 | + self.db.close() |
| 757 | + mysql.nuke(config) |
| 758 | + anydbmReadOnlyDBTestCase.tearDown(self) |
| 759 | + |
763 | 760 | class sqliteDBTestCase(anydbmDBTestCase): |
764 | 761 | def setUp(self): |
765 | 762 | from roundup.backends import sqlite |
@@ -865,9 +862,27 @@ def suite(): |
865 | 862 | from roundup import backends |
866 | 863 | p = [] |
867 | 864 | 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')) |
871 | 886 | #return unittest.TestSuite(l) |
872 | 887 |
|
873 | 888 | if hasattr(backends, 'gadfly'): |
|
0 commit comments