ppf: Add insert_test_*_messages methods in test suite.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 19 Aug 2011 08:50:18 +0000 (11:50 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 19 Aug 2011 08:50:29 +0000 (11:50 +0300)
ppf/db/tests/test_access_sqlite.py

index 9bc680b..ed4b67a 100644 (file)
@@ -89,6 +89,41 @@ insert into swarms(torrent, filesize, purpose, source)
         values('DarkKnight', '123000', 'experiment', 'TVTorrents');
 insert into swarms(torrent, filesize, purpose, source)
         values('Fedora', '1024', 'experiment', 'local');
+
+-- insert sessions in `client_sessions` table
+
+insert into client_sessions(swarm_id, client_id, system_os,
+    system_os_version, system_ram, system_cpu, public_ip, public_port,
+    ds_limit, us_limit, start_time)
+        values ('1', '2', 'Linux', '2.6.26', '512', '1500',
+            '141.85.224.205', '50500', '512', '64', '2455125.02409722');
+insert into client_sessions(swarm_id, client_id, system_os,
+    system_os_version, system_ram, system_cpu, public_ip, public_port,
+    ds_limit, us_limit, start_time)
+        values('2', '4', 'Linux', '2.6.26', '512', '1500',
+            '141.85.224.209', '40400', '512', '64', '2455125.03174769');
+
+-- insert messages in `status_messages` table
+
+insert into status_messages(cs_id, timestamp, peer_num, dht,
+    download_speed, upload_speed, download_size, upload_size, eta)
+        values('1', '2455128.10', '222', '0', '213', '56', '200',
+            '300', '121.324');
+insert into status_messages(cs_id, timestamp, peer_num, dht,
+    download_speed, upload_speed, download_size, upload_size, eta)
+        values('2', '2455128.10', '222', '0', '213', '56', '200',
+            '300', '121.324');
+
+-- insert messages in `verbose_messages` table
+
+insert into verbose_messages(cs_id, timestamp, direction, peer_ip,
+    peer_port, message_type, _index, begin, length, listen_port)
+        values('1', '2455128.121295811', '0', '127.0.0.1', '1345',
+            '0', '3', '4', '13', '777');
+insert into verbose_messages(cs_id, timestamp, direction, peer_ip,
+    peer_port, message_type, _index, begin, length, listen_port)
+        values('2', '2455128.121295811', '1', '127.0.0.1', '1345',
+            '0', '3', '4', '13', '777');
 """
 
 class AccessTest(unittest.TestCase):
@@ -103,6 +138,9 @@ class AccessTest(unittest.TestCase):
     cursor = None
     expected_swarms_count = 0
     expected_btclients_count = 0
+    expected_sessions_count = 0
+    expected_statmsg_count = 0
+    expected_verbmsg_count = 0
 
     def setUp(self):
         """ Create database file and instantiate objects. """
@@ -111,9 +149,12 @@ class AccessTest(unittest.TestCase):
 
         cursor.executescript(sql_create_script)
         cursor.executescript(sql_init_script)
-        # Number of table entries in the init script
+        # Initialize to number of table entries inserted in the init script.
         self.expected_swarms_count = 2
         self.expected_btclients_count = 6
+        self.expected_sessions_count = 2
+        self.expected_statmsg_count = 2
+        self.expected_verbmsg_count = 2
 
         cursor.close()
         conn.close()
@@ -138,7 +179,17 @@ class AccessTest(unittest.TestCase):
         return num_rows
 
     def insert_test_swarms(self):
-        """ Insert test entries through insert_swarms_row method. """
+        """ Insert test entries through insert_swarms(). """
+        self.dba.insert_swarms('Pulp Fiction', '102400',
+            'streaming', 'isohunt')
+        self.dba.insert_swarms('Karaoke', '50122',
+            'streaming', 'local')
+
+        # Increment number of entries in the swarm table.
+        self.expected_swarms_count = self.expected_swarms_count + 2
+
+    def insert_test_swarms_row(self):
+        """ Insert test entries through insert_swarms_row(). """
         self.dba.insert_swarms_row(['Pulp Fiction', '102400',
             'streaming', 'isohunt'])
         self.dba.insert_swarms_row(['Karaoke', '50122',
@@ -148,13 +199,111 @@ class AccessTest(unittest.TestCase):
         self.expected_swarms_count = self.expected_swarms_count + 2
 
     def insert_test_btclients(self):
-        """ Insert test entries through insert_btclients_row method. """
+        """ Insert test entries through insert_btclients(). """
+        self.dba.insert_btclients('NextShare', 'Python', 1, 1);
+        self.dba.insert_btclients('Deluge', 'C++', 1, 0);
+
+        # Increment number of entries in the btclients table.
+        self.expected_btclients_count = self.expected_btclients_count + 2
+
+    def insert_test_btclients_row(self):
+        """ Insert test entries through insert_btclients_row(). """
         self.dba.insert_btclients_row(['NextShare', 'Python', 1, 1]);
         self.dba.insert_btclients_row(['Deluge', 'C++', 1, 0]);
 
         # Increment number of entries in the btclients table.
         self.expected_btclients_count = self.expected_btclients_count + 2
 
+    def insert_test_client_sessions(self):
+        """ Insert test entries through insert_client_sessions(). """
+        # First insert appropiate swarm entries.
+        self.insert_test_swarms()
+
+        # Both client sessions are part of the same swarm and
+        # use the same client.
+        self.dba.insert_client_sessions('3', '5', 'Linux', '2.6.26',
+            '512', '1500', '141.85.224.209', '50500', '512', '64',
+            '2455125.97479338');
+        self.dba.insert_client_sessions('3', '5', 'Linux', '2.6.26',
+            '512', '1500', '141.85.224.202', '50500', '512', '64',
+            '2455125.97479338');
+
+        # Increment number of entries in client_sessions table.
+        self.expected_sessions_count = self.expected_sessions_count + 2
+
+    def insert_test_client_sessions_row(self):
+        """ Insert test entries through insert_client_sessions_row(). """
+        # First insert appropiate swarm entries.
+        self.insert_test_swarms()
+
+        # Both client sessions are part of the same swarm and
+        # use the same client.
+        self.dba.insert_client_sessions_row(['3', '5', 'Linux', '2.6.26',
+            '512', '1500', '141.85.224.209', '50500', '512', '64',
+            '2455125.97479338']);
+        self.dba.insert_client_sessions_row(['3', '5', 'Linux', '2.6.26',
+            '512', '1500', '141.85.224.202', '50500', '512', '64',
+            '2455125.97479338']);
+
+        # Increment number of entries in client_sessions table.
+        self.expected_sessions_count = self.expected_sessions_count + 2
+
+    def insert_test_status_messages(self):
+        """ Insert test entries through insert_status_messages(). """
+        # First insert appropiate swarm entries.
+        self.insert_test_client_sessions()
+
+        # Both entries are part of client session 3.
+        self.dba.insert_status_messages('3', '2455128.10', '222', '0',
+                '213', '56', '200', '300', '121.324');
+        self.dba.insert_status_messages('3', '2455143.10', '223', '0',
+                '213', '56', '300', '400', '121.324');
+
+        # Increment number of entries in status_messages table.
+        self.expected_statmsg_count = self.expected_statmsg_count + 2
+
+    def insert_test_status_messages_row(self):
+        """ Insert test entries through insert_status_messages_row(). """
+        # First insert appropiate swarm entries.
+        self.insert_test_client_sessions()
+
+        # Both entries are part of client session 3.
+        self.dba.insert_status_messages_row(['3', '2455128.10', '222', '0',
+                '213', '56', '200', '300', '121.324']);
+        self.dba.insert_status_messages_row(['3', '2455143.10', '223', '0',
+                '213', '56', '300', '400', '121.324']);
+
+        # Increment number of entries in status_messages table.
+        self.expected_statmsg_count = self.expected_statmsg_count + 2
+
+    def insert_test_verbose_messages(self):
+        """ Insert test entries through insert_verbose_messages(). """
+        # First insert appropiate client sessions entries.
+        self.insert_test_client_sessions()
+
+        # Both entries are part of client session 3.
+        self.dba.insert_verbose_messages('3', '2455128.121295811', '0',
+                '127.0.0.1', '1345', '0', '3', '4', '13', '777');
+        self.dba.insert_verbose_messages('3', '2455129.125292872', '0',
+                '127.0.1.1', '1345', '0', '3', '4', '13', '778');
+
+        # Increment number of entries in verbose_messsages table.
+        self.expected_verbmsg_count = self.expected_verbmsg_count + 2
+
+    def insert_test_verbose_messages_row(self):
+        """ Insert test entries through insert_verbose_messages_row(). """
+        # First insert appropiate client sessions entries.
+        self.insert_test_client_sessions()
+
+        # Both entries are part of client session 3.
+        self.dba.insert_vebose_messages_row(['3', '2455128.121295811', '0',
+                '127.0.0.1', '1345', '0', '3', '4', '13', '777']);
+        self.dba.insert_vebose_messages_row(['3', '2455129.125292872', '0',
+                '127.0.1.1', '1345', '0', '3', '4', '13', '778']);
+
+        # Increment number of entries in verbose_messsages table.
+        self.expected_verbmsg_count = self.expected_verbmsg_count + 2
+
     def test_connect(self):
         """ If connection is successful, no exception is raised. """
         try:
@@ -247,9 +396,10 @@ class AccessTest(unittest.TestCase):
         Select btclients by name. Use "Tribler" name resulting in one entry.
         """
         self.dba.select_btclients_by_name("Tribler", False)
+        self.expected_btclients_count = 1
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 1)
+        self.assertEqual(num_rows, self.expected_btclients_count)
 
     def test_select_btclients_id_by_name(self):
         """
@@ -257,8 +407,9 @@ class AccessTest(unittest.TestCase):
         """
         # TODO: unify return values for select_* methods in access.py
         id = self.dba.select_btclients_id_by_name("Tribler")
+        self.expected_btclients_count = 1
 
-        self.assertEqual(id, 1)
+        self.assertEqual(id, self.expected_btclients_count)
 
     def test_insert_btclients_row(self):
         """
@@ -285,126 +436,168 @@ class AccessTest(unittest.TestCase):
         self.assertEqual(num_rows, self.expected_btclients_count)
 
     def test_select_client_sessions_by_id(self):
-        # TODO: An entry should be added in the startup script.
+        """
+        Select initial client_sessions entries.
+        Check against expected_sessions_count.
+        """
         self.dba.select_client_sessions_by_id(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_sessions_count)
 
     def test_select_client_sessions_by_swarm(self):
+        """
+        Select initial client_sessions entries.
+        Check against expected_sessions_count.
+        """
         self.dba.select_client_sessions_by_swarm(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_sessions_count)
 
     def test_insert_client_sessions_row(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
+        """
+        Use insert_test_client_sessions to add new entries.
+        Test number of rows selected against expected_sessions_count.
+        """
+        self.insert_test_client_sessions()
 
         self.dba.select_client_sessions_by_id(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 1)
+        self.assertEqual(num_rows, self.expected_sessions_count)
 
     def test_delete_client_sessions_by_id(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
+        """
+        Use insert_test_client_sessions to add new entries.
+        Delete client session id '1'.
+        """
+        self.insert_test_client_sessions()
         self.dba.delete_client_sessions_by_id(1)
+        # Number of entries decreases by 1.
+        self.expected_sessions_count = self.expected_sessions_count - 1
 
         self.dba.select_client_sessions_by_id(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_sessions_count)
 
     def test_delete_client_sessions_by_swarm(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
-        self.dba.delete_client_sessions_by_swarm(1)
+        """
+        Use insert_test_client_sessions to add new entries.
+        Delete client sessions for swarm '3'.
+        """
+        self.insert_test_client_sessions()
+        self.dba.delete_client_sessions_by_swarm('3')
+        # Number of entries decreases by 2.
+        self.expected_sessions_count = self.expected_sessions_count - 2
 
         self.dba.select_client_sessions_by_id(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_sessions_count)
 
     def test_select_status_messages(self):
+        """
+        Select initial status messsage entries.
+        Check against expected_statmsg_count.
+        """
         # TODO: An entry should be added in the startup script.
         self.dba.select_status_messages(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_statmsg_count)
 
     def test_insert_status_messages_row(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
-        self.dba.insert_status_messages_row(['1', '2455128.10', '222', '0', '213', '56', '200', '300', '121.324'])
+        """
+        Use insert_test_status_messages to add new entries.
+        Test number of rows selected against expected_statmsg_count.
+        """
+        self.insert_test_status_messages()
 
         self.dba.select_status_messages(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 1)
+        self.assertEqual(num_rows, self.expected_statmsg_count)
 
     def test_insert_status_messages(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
-        self.dba.insert_status_messages('1', '2455128.10', '222', '0', '213', '56', '200', '300', '121.324')
+        """
+        Use insert_test_status_messages to add new entries.
+        Test number of rows selected against expected_statmsg_count.
+        """
+        """
+        Use insert_test_swarms to add new entries in swarms table.
+        Test number of rows selected against expected_swarms_count.
+        """
+        self.insert_test_status_messages()
 
         self.dba.select_status_messages(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 1)
+        self.assertEqual(num_rows, self.expected_statmsg_count)
 
     def test_delete_status_messages(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
-        self.dba.insert_status_messages('1', '2455128.10', '222', '0', '213', '56', '200', '300', '121.324')
+        """
+        Use insert_test_status_nessages to add new entries.
+        Delete all entries. Number of rows should be 0.
+        """
+        self.insert_test_status_messages()
         self.dba.delete_status_messages()
+        self.expected_statmsg_count = 0
 
         self.dba.select_status_messages(False)
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_statmsg_count)
 
     def test_select_verbose_messages(self):
+        """
+        Select initial verbose messages entries.
+        Check against expected_verbmsg_count.
+        """
         # TODO: An entry should be added in the startup script.
         self.dba.select_verbose_messages(False)
-
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_verbmsg_count)
 
     def test_insert_verbose_messages_row(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
-        self.dba.insert_verbose_messages_row(['1', '2455128.121295811', '0', '127.0.0.1', '1345', '0', '3', '4', '13', '777'])
+        """
+        Use insert_test_verbose_messages to add new entries.
+        Test number of rows selected against expected_verbmsg_count.
+        """
+        self.insert_test_verbose_messages()
 
         self.dba.select_verbose_messages(False)
-
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 1)
+        self.assertEqual(num_rows, self.expected_verbmsg_count)
 
     def test_insert_verbose_messages(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
-        self.dba.insert_verbose_messages('1', '2455128.121295811', '0', '127.0.0.1', '1345', '0', '3', '4', '13', '777')
+        """
+        Use insert_test_verbose_messages to add new entries.
+        Test number of rows selected against expected_verbmsg_count.
+        """
+        self.insert_test_verbose_messages()
 
         self.dba.select_verbose_messages(False)
-
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 1)
+        self.assertEqual(num_rows, self.expected_verbmsg_count)
 
     def test_delete_verbose_messages(self):
-        self.dba.insert_swarms("fedora.torrent", 10000, "btex", "isohunt")
-        self.dba.insert_client_sessions_row(['1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'])
-        self.dba.insert_verbose_messages_row(['1', '2455128.121295811', '0', '127.0.0.1', '1345', '0', '3', '4', '13', '777'])
+        """
+        Use insert_test_verbose_nessages to add new entries.
+        Delete all entries. Number of rows should be 0.
+        """
+        self.insert_test_verbose_messages()
         self.dba.delete_verbose_messages()
+        self.expected_verbmsg_count = 0
 
         self.dba.select_verbose_messages(False)
-
         num_rows = self.get_cursor_num_rows()
 
-        self.assertEqual(num_rows, 0)
+        self.assertEqual(num_rows, self.expected_verbmsg_count)
 
 #
 #        if len(sys.argv) != 2: