3
0

test_SQLQueries.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import unittest, sqlite3
  2. from definitions import ROOT_DIR
  3. import Core.Controller as Ctrl
  4. pcap = ROOT_DIR + "/../resources/test/reference_1998.pcap"
  5. controller = Ctrl.Controller(pcap_file_path=pcap, do_extra_tests=False, non_verbose=False)
  6. controller.load_pcap_statistics(flag_write_file=False, flag_recalculate_stats=True, flag_print_statistics=False)
  7. class UnitTestPyparsing(unittest.TestCase):
  8. def test_apostrophe(self):
  9. query = "Select ipAddress from ip_Statistics where pktsSent = '5'"
  10. query2 = "Select ipAddress from ip_Statistics where pktsSent = 5"
  11. self.assertEqual(controller.statistics.stats_db.process_db_query(query),
  12. controller.statistics.stats_db.process_db_query(query2))
  13. def test_paranthesis(self):
  14. query = "Select (ipAddress) from (ip_Statistics) where (pktsSent) = (2 + (3))"
  15. self.assertEqual("72.247.178.67", controller.statistics.stats_db.process_db_query(query))
  16. def test_noresult(self):
  17. query = "Select ipAddress from ip_statistics where ipaddress = 'abc'"
  18. self.assertEqual([], controller.statistics.stats_db.process_db_query(query))
  19. def test_severaloperator(self):
  20. query1 = "Select ipAddress from ip_Statistics where pktsSent = '5'"
  21. query2 = "Select ipAddress from ip_Statistics where pktsSent < '5'"
  22. query3 = "Select ipAddress from ip_Statistics where pktsSent <= '5'"
  23. query4 = "Select ipAddress from ip_Statistics where pktsSent > '356'"
  24. query5 = "Select ipAddress from ip_Statistics where pktsSent >= '356'"
  25. self.assertEqual("72.247.178.67", controller.statistics.stats_db.process_db_query(query1))
  26. self.assertEqual("72.247.178.113", controller.statistics.stats_db.process_db_query(query2))
  27. self.assertEqual(["72.247.178.67", "72.247.178.113"], controller.statistics.stats_db.process_db_query(query3))
  28. self.assertEqual("10.0.2.15", controller.statistics.stats_db.process_db_query(query4))
  29. self.assertEqual(["10.0.2.15", "172.217.23.174"], controller.statistics.stats_db.process_db_query(query5))
  30. # too long query (stackoverflow)
  31. with self.assertRaises(sqlite3.OperationalError):
  32. query = "Select ipAddress from ip_statistics where pktsSent = "
  33. i = 0
  34. while (i < 15):
  35. query += "(Select pktsSent from ip_statistics where pktsSent ="
  36. i += 1
  37. query += "5"
  38. while (i > 0):
  39. query += ")"
  40. i -= 1
  41. controller.statistics.stats_db.process_db_query(query)
  42. # compare of tables with different dimension
  43. with self.assertRaises(sqlite3.OperationalError):
  44. controller.statistics.stats_db.process_db_query('Select ipAddress from ip_Statistics where pktsSent'
  45. '= (Select * from ip_Statistics)')