ppf/new: Use subprocess module instead of sqlparse. Connection to database
authorMariana Mărășoiu <mariana.marasoiu@gmail.com>
Wed, 24 Aug 2011 11:31:39 +0000 (14:31 +0300)
committerMariana Mărășoiu <mariana.marasoiu@gmail.com>
Wed, 24 Aug 2011 11:31:39 +0000 (14:31 +0300)
is made in setUp.

ppf/new/tests/test_storage.py

index ac4827a..33fe26b 100644 (file)
@@ -11,7 +11,6 @@ import os.path
 import shutil
 import sqlite3
 import MySQLdb
-import sqlparse
 import subprocess
 import sys
 import datetime
@@ -519,84 +518,68 @@ class MySQLDatabaseAccessTest(unittest.TestCase):
         """ Retrieve number of entries in `swarms` table. """
 
         self.cursor.execute('SELECT COUNT(id) FROM swarms')
-        self.count = self.cursor.fetchone()[0]
-
-        self.cursor.close()
-        self.conn.close()
+        count = self.cursor.fetchone()[0]
 
         return count
 
     def get_session_count(self):
         """ Retrieve number of entries in `client_sessions` table. """
         self.cursor.execute('SELECT COUNT(id) FROM client_sessions')
-        self.count = self.cursor.fetchone()[0]
-
-        self.cursor.close()
-        self.conn.close()
+        count = self.cursor.fetchone()[0]
 
         return count
 
     def get_statmsg_count(self):
         """ Retrieve number of entries in `status_messages` table. """
         self.cursor.execute('SELECT COUNT(id) FROM status_messages')
-        self.count = self.cursor.fetchone()[0]
-
-        self.cursor.close()
-        self.conn.close()
+        count = self.cursor.fetchone()[0]
 
         return count
 
     def get_pstatmsg_count(self):
         """ Retrieve number of entries in `peer_status_messages` table. """
         self.cursor.execute('SELECT COUNT(id) FROM peer_status_messages')
-        self.count = self.cursor.fetchone()[0]
-
-        self.cursor.close()
-        self.conn.close()
+        count = self.cursor.fetchone()[0]
 
         return count
 
     def get_verbmsg_count(self):
         """ Retrieve number of entries in `verbose_messages` table. """
         self.cursor.execute('SELECT COUNT(id) FROM verbose_messages')
-        self.count = self.cursor.fetchone()[0]
-
-        self.cursor.close()
-        self.conn.close()
+        count = self.cursor.fetchone()[0]
 
         return count
 
     def setUp(self):
-        """ Create database file and instantiate objects. """
-        # Create database file. Create tables and indices.
+        """ Create database and instantiate objects. """
+        # Create database. Create tables and indices.
         # TODO: Check exceptions.
 
         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.cursor = self.conn.cursor()
 
-        self.cursor.execute("CREATE DATABASE p2p_test")
-        self.cursor.execute("USE p2p_test")
+        self.conn = MySQLdb.Connection(user=self.user, passwd=self.password)
+        self.cursor = self.conn.cursor()
 
-        f = open(sql_create_script_path).read()
-        sql_parts = sqlparse.split(f)
-        for sql_part in sql_parts:
-            if sql_part.strip() == '':
-                continue
-            self.cursor.execute(sql_part)
+        self.cursor.execute("CREATE DATABASE %s" %self.database)
+        self.cursor.execute("USE %s" %self.database)
 
+        f = open(sql_create_script_path, 'r')
+        p = subprocess.Popen(["mysql", self.database, "--user=%s" %self.user,
+                             "--password=%s" %self.password], stdin=f)
+        ret = os.waitpid(p.pid, 0)[1]
+        f.close()
+        
         # Populate database.
         # TODO: Check exceptions.
         sql_init_script_path = os.path.join(os.path.dirname(__file__),
                 self.sql_init_script)
 
-        f = open(sql_init_script_path).read()
-        sql_parts = sqlparse.split(f)
-        for sql_part in sql_parts:
-            if sql_part.strip() == '':
-                continue
-            self.cursor.execute(sql_part)
+        f = open(sql_init_script_path, 'r')
+        p = subprocess.Popen(["mysql", self.database, "--user=%s" %self.user,
+                             "--password=%s" %self.password], stdin=f)
+        ret = os.waitpid(p.pid, 0)[1]
+        f.close()
 
         # Initialize to number of table entries inserted in the init script.
         self.expected_swarm_count = 2
@@ -606,9 +589,8 @@ class MySQLDatabaseAccessTest(unittest.TestCase):
         self.expected_verbmsg_count = 2
 
     def tearDown(self):
-        """ Close connection and remove database file. """
-        self.cursor.execute("DROP DATABASE p2p_test")
-        print "database deleted"
+        """ Delete database and close connection. """
+        self.cursor.execute("DROP DATABASE %s" %self.database)
         self.cursor.close()
         self.conn.close()
 
@@ -633,7 +615,7 @@ class MySQLDatabaseAccessTest(unittest.TestCase):
                 system_os="Linux", system_os_version="2.6.26",
                 system_ram=2048, system_cpu=3000, public_ip="141.85.224.201",
                 public_port="50500", ds_limit=300, us_limit=200)
-        a = storage.SQLiteDatabaseAccess(self.database)
+        a = storage.MySQLDatabaseAccess(self.database)
         a.connect()
         a.add_client_session(cs)
         a.disconnect()
@@ -651,7 +633,7 @@ class MySQLDatabaseAccessTest(unittest.TestCase):
         msg = storage.StatusMessage(client_session_id=1, timestamp=ts,
                 num_peers=10, num_dht_peers=3, download_speed=102,
                 upload_speed=99, download_size=10213, upload_size=3301)
-        a = storage.SQLiteDatabaseAccess(self.database)
+        a = storage.MySQLDatabaseAccess(self.database)
         a.connect()
         a.add_status_message(msg)
         a.disconnect()
@@ -669,7 +651,7 @@ class MySQLDatabaseAccessTest(unittest.TestCase):
         msg = storage.PeerStatusMessage(client_session_id=1, timestamp=ts,
                 peer_ip="141.85.224.202", peer_port="12345",
                 download_speed=13, upload_speed=98)
-        a = storage.SQLiteDatabaseAccess(self.database)
+        a = storage.MySQLDatabaseAccess(self.database)
         a.connect()
         a.add_peer_status_message(msg)
         a.disconnect()
@@ -687,7 +669,7 @@ class MySQLDatabaseAccessTest(unittest.TestCase):
         msg = storage.VerboseMessage(client_session_id=1, timestamp=ts,
             transfer_direction="send", peer_ip="141.85.224.202",
             peer_port="12345", message_type="CHOKE")
-        a = storage.SQLiteDatabaseAccess(self.database)
+        a = storage.MySQLDatabaseAccess(self.database)
         a.connect()
         a.add_verbose_message(msg)
         a.disconnect()