import unittest
import os
+import sqlite3
-class AccessTest(unittest.TestCase):
- def main():
+sql_script = """
+drop table if exists status_messages;
+drop table if exists verbose_messages;
+drop table if exists client_sessions;
+drop table if exists btclients;
+drop table if exists swarms;
+
+create table swarms(
+ id integer primary key autoincrement,
+ torrent text,
+ filesize integer check(filesize between 0 and 100000000000),
+ purpose text,
+ source text);
+
+create table btclients(
+ id integer primary key autoincrement,
+ name text,
+ language text,
+ dht integer check(dht between 0 and 1),
+ streaming integer check(streaming between 0 and 1));
- """
- Test case
- """
+create table client_sessions(
+ id integer primary key autoincrement,
+ swarm_id integer references swarms(id) on delete cascade on update cascade,
+ client_id integer references btclients(id) on delete cascade on update cascade,
+ system_os text,
+ system_os_version text,
+ system_ram integer check (system_ram between 0 and 32768),
+ system_cpu integer check (system_cpu between 100 and 10000),
+ public_ip text,
+ public_port integer check (public_port between 1 and 65535),
+ ds_limit integer check (ds_limit between 0 and 1000000),
+ us_limit integer check (us_limit between 0 and 1000000),
+ start_time date);
- if len(sys.argv) != 2:
- print "Usage: python DatabaseAccess dbfile"
- sys.exit(2)
+create table status_messages (
+ cs_id integer references client_sessions(id) on delete cascade on update cascade,
+ timestamp date,
+ peer_num integer check (peer_num between 0 and 100000),
+ dht integer check (dht between 0 and 100000),
+ download_speed integer check (download_speed between 0 and 1000000),
+ upload_speed integer check (upload_speed between 0 and 1000000),
+ download_size integer check(download_size between 0 and 100000000000),
+ upload_size integer check(upload_size between 0 and 100000000000),
+ eta integer);
- dba = DatabaseAccess(sys.argv[1])
+---
+--- direction = 0 -> receive (from peer)
+--- direction = 1 -> send (to peer)
+---
- dba.connect()
+create table verbose_messages (
+ cs_id integer references client_sessions(id) on delete cascade on update cascade,
+ timestamp date,
+ direction integer check(direction between 0 and 1),
+ peer_ip text,
+ peer_port integer check(peer_port between 1 and 65535),
+ message_type integer check (message_type between 0 and 100),
+ _index integer check (_index between 0 and 100000),
+ begin integer check (begin between 0 and 10000000),
+ length integer check (length between 0 and 10000000),
+ listen_port integer check(listen_port between 0 and 65535));
- for t in [('DarkKnight', '123000', 'experiment', 'TVTorrents'),
- ('Fedora', '1024', 'experiment', 'local'),
- ('Pulp Fiction', '102400', 'streaming', 'isohunt'),
- ('Karaoke', 'anaaremere', 'streaming', 'local'),
- ]:
- dba.insert_swarms_row(t)
+-- insert BitTorrent clients in `btclients` table
- for t in [('Tribler', 'Python', '1', '1'),
- ('libtorrent', 'C++', '1', '0'),
- ('Vuze', 'Java', '1', '0'),
- ('Transmission', 'C', 'asa', '0'),
- ]:
- dba.insert_btclients_row(t)
+insert into btclients(name, language, dht, streaming)
+ values('Tribler', 'Python', 1, 1);
+insert into btclients(name, language, dht, streaming)
+ values('libtorrent', 'C++', 1, 0);
+insert into btclients(name, language, dht, streaming)
+ values('Vuze', 'Java', 1, 1);
+insert into btclients(name, language, dht, streaming)
+ values('Transmission', 'C', 1, 0);
+insert into btclients(name, language, dht, streaming)
+ values('Aria', 'C', 1, 0);
+insert into btclients(name, language, dht, streaming)
+ values('Mainline', 'Python', 1, 0);
- for t in [('1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'),
- ('3', '4', 'Linux', '2.6.26', '512', '1500', '141.85.224.209', '40400', '512', '64', '2455125.03174769'),
- ]:
- dba.insert_client_sessions_row(t)
+-- .genfkey --exec
+"""
+
+class AccessTest(unittest.TestCase):
+ """
+ Test case for Access class.
+ """
- for t in [('1', '2455128.10', '222', '0', '213', '56', '200', '300', '121.324'),
- ('6', '2455128.10', '222', '0', '213', '56', '200', '300', '121.324'),
- ]:
- dba.insert_status_messages_row(t)
+ database = "test.db"
- for t in [('1', '2455128.121295811', '0', '127.0.0.1', '1345', '0', '3', '4', '13', '777'),
- ('4', '2455128.121295811', '1', '127.0.0.1', '1345', '0', '3', '4', '13', '777'),
- ]:
- dba.insert_verbose_messages_row(t)
+ def setUp(self):
+ conn = sqlite3.connect(self.database)
+ cursor = conn.cursor()
+ cursor.executescript(sql_script)
+ cursor.close()
+ conn.close()
- dba.get_status()
+ def tearDown(self):
+ os.remove(self.database)
+ pass
- dba.select_btclient_by_name("Tribler")
+ def test_run(self):
+ conn = sqlite3.connect(self.database)
+ cursor = conn.cursor()
+ cursor.execute("""select * from btclients""");
+ for row in cursor:
+ print row
+ cursor.close()
+ conn.close()
- dba.disconnect()
+#
+# if len(sys.argv) != 2:
+# print "Usage: python DatabaseAccess dbfile"
+# sys.exit(2)
+#
+# dba = DatabaseAccess(sys.argv[1])
+#
+# dba.connect()
+#
+# for t in [('DarkKnight', '123000', 'experiment', 'TVTorrents'),
+# ('Fedora', '1024', 'experiment', 'local'),
+# ('Pulp Fiction', '102400', 'streaming', 'isohunt'),
+# ('Karaoke', 'anaaremere', 'streaming', 'local'),
+# ]:
+# dba.insert_swarms_row(t)
+#
+# for t in [('Tribler', 'Python', '1', '1'),
+# ('libtorrent', 'C++', '1', '0'),
+# ('Vuze', 'Java', '1', '0'),
+# ('Transmission', 'C', 'asa', '0'),
+# ]:
+# dba.insert_btclients_row(t)
+#
+# for t in [('1', '2', 'Linux', '2.6.26', '512', '1500', '141.85.224.205', '50500', '512', '64', '2455125.02409722'),
+# ('3', '4', 'Linux', '2.6.26', '512', '1500', '141.85.224.209', '40400', '512', '64', '2455125.03174769'),
+# ]:
+# dba.insert_client_sessions_row(t)
+#
+# for t in [('1', '2455128.10', '222', '0', '213', '56', '200', '300', '121.324'),
+# ('6', '2455128.10', '222', '0', '213', '56', '200', '300', '121.324'),
+# ]:
+# dba.insert_status_messages_row(t)
+#
+# for t in [('1', '2455128.121295811', '0', '127.0.0.1', '1345', '0', '3', '4', '13', '777'),
+# ('4', '2455128.121295811', '1', '127.0.0.1', '1345', '0', '3', '4', '13', '777'),
+# ]:
+# dba.insert_verbose_messages_row(t)
+#
+# dba.get_status()
+#
+# dba.select_btclient_by_name("Tribler")
+#
+# dba.disconnect()
if __name__ == "__main__":