From: Mariana Mărășoiu Date: Fri, 2 Sep 2011 16:53:03 +0000 (+0300) Subject: ppf/new: Fix initialization in MySQLDatabaseAccess. Add verification for optional... X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=83ddf9a33824bf8b31f56480fa96e1b199d3ade6;p=cs-p2p-next.git ppf/new: Fix initialization in MySQLDatabaseAccess. Add verification for optional arguments. --- diff --git a/ppf/new/storage.py b/ppf/new/storage.py index a469595..6cf68e8 100644 --- a/ppf/new/storage.py +++ b/ppf/new/storage.py @@ -617,9 +617,14 @@ class MySQLDatabaseAccess(DatabaseAccess): """Initialize the database attribute of the class. Use superclass method for initialisation of constructor. + Check also for the optional arguments; if they are not, use defaults. """ super(MySQLDatabaseAccess, self).__init__(database) + if 'host' not in self.database: + self.database['host'] = 'localhost' + if 'port' not in self.database: + self.database['port'] = 3306 def connect(self): """Connect to MySQL database.""" diff --git a/ppf/new/tests/test_storage.py b/ppf/new/tests/test_storage.py index 66c15de..643dbd6 100644 --- a/ppf/new/tests/test_storage.py +++ b/ppf/new/tests/test_storage.py @@ -522,9 +522,8 @@ class MySQLDatabaseAccessTest(unittest.TestCase): """Test suite for MySQLDatabaseAccess class in storage.py.""" # Class specific variables. Initialized in setUp, used throughout tests. - database = "p2p_test" - user = "root" - password = "p2p4th3m45535" + database = {"database": "p2p_test", "user": "root", + "password": "p2p4th3m45535"} conn = None cursor = None sql_create_script = "p2p-log-mysql.sql" @@ -578,15 +577,18 @@ class MySQLDatabaseAccessTest(unittest.TestCase): sql_create_script_path = os.path.join(os.path.dirname(__file__), self.sql_create_script) - self.conn = MySQLdb.Connection(user=self.user, passwd=self.password) + self.conn = MySQLdb.Connection(user=self.database['user'], + passwd=self.database['password']) self.cursor = self.conn.cursor() - self.cursor.execute("CREATE DATABASE %s" %self.database) - self.cursor.execute("USE %s" %self.database) + self.cursor.execute("CREATE DATABASE %s" %self.database['database']) + self.cursor.execute("USE %s" %self.database['database']) f = open(sql_create_script_path, 'r') - p = subprocess.Popen(["mysql", self.database, "--user=%s" %self.user, - "--password=%s" %self.password], stdin=f) + p = subprocess.Popen(["mysql", self.database['database'], + "--user=%s" %self.database['user'], + "--password=%s" %self.database['password']], + stdin=f) ret = os.waitpid(p.pid, 0)[1] f.close() @@ -596,8 +598,10 @@ class MySQLDatabaseAccessTest(unittest.TestCase): self.sql_init_script) f = open(sql_init_script_path, 'r') - p = subprocess.Popen(["mysql", self.database, "--user=%s" %self.user, - "--password=%s" %self.password], stdin=f) + p = subprocess.Popen(["mysql", self.database['database'], + "--user=%s" %self.database['user'], + "--password=%s" %self.database['password']], + stdin=f) ret = os.waitpid(p.pid, 0)[1] f.close() @@ -610,7 +614,7 @@ class MySQLDatabaseAccessTest(unittest.TestCase): def tearDown(self): """ Delete database and close connection. """ - self.cursor.execute("DROP DATABASE %s" %self.database) + self.cursor.execute("DROP DATABASE %s" %self.database['database']) self.cursor.close() self.conn.close()