class TestTaskDeleteCase(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() self.app = api.app.test_client() # Replace the real production DB w/ a testing DB api.db = self.sql_db api.UPLOAD_FOLDER = TEST_UPLOAD_FOLDER if not os.path.isdir(api.UPLOAD_FOLDER): os.makedirs(api.UPLOAD_FOLDER) # populate the DB w/ a task post_file(self.app) def test_delete_task(self): expected_response = {'Message': 'Deleted'} resp = self.app.get('/api/v1/tasks/delete/1') self.assertEqual(resp.status_code, api.HTTP_OK) self.assertDictEqual(json.loads(resp.get_data().decode()), expected_response) def test_delete_nonexistent_task(self): expected_response = api.TASK_NOT_FOUND resp = self.app.get('/api/v1/tasks/delete/2') self.assertEqual(resp.status_code, api.HTTP_NOT_FOUND) self.assertDictEqual(json.loads(resp.get_data().decode()), expected_response) def tearDown(self): # Clean up Test DB and upload folder os.remove(TEST_DB_PATH) shutil.rmtree(TEST_UPLOAD_FOLDER)
class CeleryTestCase(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() # Replace the real production DB w/ a testing DB celery_worker.db = self.sql_db def tearDown(self): # Clean up Test DB and upload folder os.remove(TEST_DB_PATH)
class TestDBInit(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() def test_db_init(self): if DB_CONF['db_type'] == 'sqlite': self.assertIn('SQLite 3.x database', magic.from_file(TEST_DB_PATH)) def tearDown(self): if DB_CONF['db_type'] == 'sqlite': os.remove(TEST_DB_PATH)
class TestTaskCreateCase(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() self.app = api.app.test_client() # Replace the real production DB w/ a testing DB api.db = self.sql_db api.UPLOAD_FOLDER = TEST_UPLOAD_FOLDER if not os.path.isdir(api.UPLOAD_FOLDER): os.makedirs(api.UPLOAD_FOLDER) api.multiscanner_process = fake_multiscanner_process # populate the DB w/ a task post_file(self.app) def test_get_task(self): expected_response = { 'Task': { 'task_id': 1, 'task_status': 'Pending', 'report_id': None } } resp = self.app.get('/api/v1/tasks/list/1') self.assertEqual(resp.status_code, api.HTTP_OK) self.assertDictEqual(json.loads(resp.get_data().decode()), expected_response) def test_get_nonexistent_task(self): expected_response = api.TASK_NOT_FOUND resp = self.app.get('/api/v1/tasks/list/2') self.assertEqual(resp.status_code, api.HTTP_NOT_FOUND) self.assertDictEqual(json.loads(resp.get_data().decode()), expected_response) def test_get_task_list(self): expected_response = { 'Tasks': [{ 'task_id': 1, 'task_status': 'Pending', 'report_id': None }] } resp = self.app.get('/api/v1/tasks/list/') self.assertEqual(resp.status_code, api.HTTP_OK) self.assertDictEqual(json.loads(resp.get_data().decode()), expected_response) def tearDown(self): # Clean up Test DB and upload folder os.remove(TEST_DB_PATH) shutil.rmtree(TEST_UPLOAD_FOLDER)
class APITestCase(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() self.app = api.app.test_client() # Replace the real production DB w/ a testing DB api.db = self.sql_db if not os.path.isdir(TEST_UPLOAD_FOLDER): os.makedirs(TEST_UPLOAD_FOLDER) def tearDown(self): # Clean up Test DB and upload folder os.remove(TEST_DB_PATH) shutil.rmtree(TEST_UPLOAD_FOLDER)
class TestStressTest(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() for i in range(0, 1000): self.sql_db.add_task() i += 1 def test_get_all_tasks(self): for i in range(1, 1000): self.sql_db.get_task(task_id=i) def tearDown(self): if DB_CONF['db_type'] == 'sqlite': os.remove(TEST_DB_PATH) else: drop_db_table(self.sql_db.db_engine)
class TestTaskAdd(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() def test_add_task(self): task_id = 1 resp = self.sql_db.add_task(task_id=task_id, task_status='Pending', report_id=None) self.assertEqual(resp, task_id) def tearDown(self): if DB_CONF['db_type'] == 'sqlite': os.remove(TEST_DB_PATH) else: drop_db_table(self.sql_db._get_db_engine())
class TestTaskManipulation(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() self.sql_db.add_task(task_status='Pending', report_id=None) def test_add_second_task(self): resp = self.sql_db.add_task() self.assertEqual(resp, 2) def test_get_task(self): resp = self.sql_db.get_task(task_id=1) self.assertEqual(resp.task_id, 1) self.assertEqual(resp.task_status, 'Pending') self.assertEqual(resp.report_id, None) def test_update_task(self): resp = self.sql_db.update_task( task_id=1, task_status='Complete', report_id= '88d11f0ea5cc77a59b6e47deee859440f26d2d14440beb712dbac8550d35ef1f') self.assertDictEqual(resp, self.sql_db.get_task(1).to_dict()) self.assertDictEqual( resp, { 'task_id': 1, 'task_status': 'Complete', 'report_id': '88d11f0ea5cc77a59b6e47deee859440f26d2d14440beb712dbac8550d35ef1f' }) def test_delete_task(self): deleted = self.sql_db.delete_task(task_id=1) self.assertTrue(deleted) resp = self.sql_db.get_task(task_id=1) self.assertEqual(resp, None) def tearDown(self): if DB_CONF['db_type'] == 'sqlite': os.remove(TEST_DB_PATH) else: drop_db_table(self.sql_db._get_db_engine())
class TestTaskManipulation(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() self.sql_db.add_task(task_status='Pending', ) def test_add_second_task(self): resp = self.sql_db.add_task() self.assertEqual(resp, 2) def test_get_task(self): resp = self.sql_db.get_task(task_id=1) self.assertEqual(resp.task_id, 1) self.assertEqual(resp.task_status, 'Pending') self.assertEqual(resp.sample_id, None) self.assertEqual(resp.timestamp, None) def test_update_task(self): resp = self.sql_db.update_task( task_id=1, task_status='Complete', ) self.assertDictEqual(resp, self.sql_db.get_task(1).to_dict()) self.assertDictEqual( resp, { 'task_id': 1, 'sample_id': None, 'task_status': 'Complete', 'timestamp': None }) def test_delete_task(self): deleted = self.sql_db.delete_task(task_id=1) self.assertTrue(deleted) resp = self.sql_db.get_task(task_id=1) self.assertEqual(resp, None) def tearDown(self): if DB_CONF['db_type'] == 'sqlite': os.remove(TEST_DB_PATH) else: drop_db_table(self.sql_db.db_engine)
class TestGetAllTasks(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() for i in range(0, 3): self.sql_db.add_task() i += 1 def test_get_all_tasks(self): expected_response = [ {'task_id': i, 'sample_id': None, 'task_status': 'Pending', 'timestamp': None} for i in range(1, 4) ] resp = self.sql_db.get_all_tasks() for i in range(0, 3): self.assertDictEqual(expected_response[i], resp[i]) def tearDown(self): if DB_CONF['db_type'] == 'sqlite': os.remove(TEST_DB_PATH) else: drop_db_table(self.sql_db.db_engine)
class TestURLCase(unittest.TestCase): def setUp(self): self.sql_db = Database(config=DB_CONF) self.sql_db.init_db() self.app = api.app.test_client() # Replace the real production DB w/ a testing DB api.db = self.sql_db api.UPLOAD_FOLDER = TEST_UPLOAD_FOLDER if not os.path.isdir(api.UPLOAD_FOLDER): os.makedirs(api.UPLOAD_FOLDER) api.multiscanner_process = fake_multiscanner_process def test_index(self): expected_response = {'Message': 'True'} resp = self.app.get('/') self.assertEqual(resp.status_code, api.HTTP_OK) self.assertEqual(json.loads(resp.get_data().decode()), expected_response) def test_empty_db(self): expected_response = {'Tasks': []} resp = self.app.get('/api/v1/tasks/list/') self.assertEqual(resp.status_code, api.HTTP_OK) self.assertEqual(json.loads(resp.get_data().decode()), expected_response) def test_create_first_task(self): expected_response = {'Message': {'task_id': 1}} resp = post_file(self.app) self.assertEqual(resp.status_code, api.HTTP_CREATED) self.assertEqual(json.loads(resp.get_data().decode()), expected_response) def tearDown(self): # Clean up Test DB and upload folder os.remove(TEST_DB_PATH) shutil.rmtree(TEST_UPLOAD_FOLDER)