test_SQLQueries.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import unittest
  2. import sqlite3
  3. import ID2TLib.TestLibrary as Lib
  4. import Core.Controller as Ctrl
  5. controller = Ctrl.Controller(pcap_file_path=Lib.test_pcap, do_extra_tests=False, non_verbose=True)
  6. controller.load_pcap_statistics(flag_write_file=False, flag_recalculate_stats=True, flag_print_statistics=False)
  7. class UnitTestSqlQueries(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_parenthesis(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' ORDER BY ipAddress DESC"
  23. query4 = "Select ipAddress from ip_Statistics where pktsSent > '356'"
  24. query5 = "Select ipAddress from ip_Statistics where pktsSent >= '356' ORDER BY ipAddress ASC"
  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. # compare of tables with different dimension
  31. with self.assertRaises(sqlite3.OperationalError):
  32. controller.statistics.stats_db.process_db_query('Select ipAddress from ip_Statistics where pktsSent'
  33. '= (Select * from ip_Statistics)')
  34. def test_is_query_standard_query(self):
  35. self.assertTrue(controller.statistics.is_query('SELECT * from ip_statistics'))