import sys import unittest from fpsweeper.connector import mysql from fpsweeper import config from fpsweeper.logic import sweeper class TestSweeperMissingTuids(unittest.TestCase): def exit_if_not_test_environment(self): """For safety, only run tests in test environment pointed to sqlite Exit immediately if not in test environment or not pointed to sqlite """ if config.environment != config.TEST_ENVIRONMENT: sys.exit("Environment must be set to {}".format(config.TEST_ENVIRONMENT)) elif 'sqlite' not in self.test_db_session.bind.url.drivername: sys.exit("Tests must point to sqlite database") def setUp(self): """Create test db tables with some simple fixture data """ self.test_db_session = mysql.fpc_session() self.exit_if_not_test_environment() self.test_db_session.execute("CREATE TABLE track_fp(tuid INT NOT NULL)") self.populate_test_tables() def populate_test_tables(self): """Insert some simple fixture data into the fingerprint_capture test table track_fp will contain tuids 1, 2, 3 """ self.exit_if_not_test_environment() for i in range(1, 4): self.test_db_session.execute("INSERT INTO track_fp(tuid) VALUES ({})".format(i)) def tearDown(self): """Tear down the test db tables """ self.exit_if_not_test_environment() self.test_db_session.execute("DROP TABLE track_fp") def test_empty_expected_tuids(self): """Assert that passing in empty set of expected_tuids returns another empty set. """ expected_tuids = frozenset([]) missing_tuids = sweeper.get_missing_tuids(self.test_db_session, expected_tuids) self.assertEqual(len(missing_tuids), 0) def test_no_missing_tuids(self): """Assert that passing in set of tuids that are all in fp db returns an empty set.""" expected_uids = frozenset([1, 2, 3]) missing_tuids = sweeper.get_missing_tuids(self.test_db_session, expected_uids) self.assertEqual(len(missing_tuids), 0) def test_some_missing_tuids(self): """Assert that passing in set of tuids where some of the tuids are missing in the fp db returns the set of missing uids""" expected_uids = frozenset([1, 2, 3, 4, 5, 6, 7, 8, 9]) missing_tuids = sweeper.get_missing_tuids(self.test_db_session, expected_uids) self.assertEqual(missing_tuids, frozenset([4, 5, 6, 7, 8, 9]))