class TestTaskUpdateCase(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) self.sql_db.update_task(task_id=1, task_status='Complete', report_id='report1') def test_get_updated_task(self): expected_response = { 'Task': { 'task_id': 1, 'task_status': 'Complete', 'report_id': 'report1' } } 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_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 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)