"""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."""
"""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"
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()
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()
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()