test_SQLQueries.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. intervals=[], delete=True)
  8. class UnitTestSqlQueries(unittest.TestCase):
  9. def test_apostrophe(self):
  10. query = "Select ipAddress from ip_Statistics where pktsSent = '5'"
  11. query2 = "Select ipAddress from ip_Statistics where pktsSent = 5"
  12. self.assertEqual(controller.statistics.stats_db.process_db_query(query),
  13. controller.statistics.stats_db.process_db_query(query2))
  14. def test_parenthesis(self):
  15. query = "Select (ipAddress) from (ip_Statistics) where (pktsSent) = (2 + (3))"
  16. self.assertEqual("72.247.178.67", controller.statistics.stats_db.process_db_query(query))
  17. def test_noResult(self):
  18. query = "Select ipAddress from ip_statistics where ipaddress = 'abc'"
  19. self.assertEqual([], controller.statistics.stats_db.process_db_query(query))
  20. def test_severalOperator(self):
  21. query1 = "Select ipAddress from ip_Statistics where pktsSent = '5'"
  22. query2 = "Select ipAddress from ip_Statistics where pktsSent < '5'"
  23. query3 = "Select ipAddress from ip_Statistics where pktsSent <= '5' ORDER BY ipAddress DESC"
  24. query4 = "Select ipAddress from ip_Statistics where pktsSent > '356'"
  25. query5 = "Select ipAddress from ip_Statistics where pktsSent >= '356' ORDER BY ipAddress ASC"
  26. self.assertEqual("72.247.178.67", controller.statistics.stats_db.process_db_query(query1))
  27. self.assertEqual("72.247.178.113", controller.statistics.stats_db.process_db_query(query2))
  28. self.assertEqual(["72.247.178.67", "72.247.178.113"], controller.statistics.stats_db.process_db_query(query3))
  29. self.assertEqual("10.0.2.15", controller.statistics.stats_db.process_db_query(query4))
  30. self.assertEqual(["10.0.2.15", "172.217.23.174"], controller.statistics.stats_db.process_db_query(query5))
  31. # compare of tables with different dimension
  32. with self.assertRaises(sqlite3.OperationalError):
  33. controller.statistics.stats_db.process_db_query('Select ipAddress from ip_Statistics where pktsSent'
  34. '= (Select * from ip_Statistics)')
  35. def test_is_query_standard_query(self):
  36. self.assertTrue(controller.statistics.is_query('SELECT * from ip_statistics'))