Skip to content

Commit

Permalink
Fixed failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Habush committed Mar 31, 2019
1 parent 08a180f commit 6f868ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion tests/test_api_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ def test_status_error(self, client):

self.assertEqual(resonse.status_code, 404)

@patch("zipfile.ZipFile")
@patch("webserver.apimain.send_file")
@patch("webserver.apimain.zip_dir")
@patch("pymongo.MongoClient")
def test_result_api(self, client, make_archive, send_file):
def test_result_api(self, client, make_archive, send_file, zip_file):
temp_session = Session("abcd", "", "", "", "5abcd")
temp_session.status = 2
mock_db = mongomock.MongoClient().db
Expand Down
29 changes: 19 additions & 10 deletions tests/test_scan_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@

class TestScanSession(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.session = {
def test_get_expired_session(self):
session = {
"id": "abcd", "moses_options": "", "crossval_options": "",
"dataset": "", "mnemonic": "abcdr4e", "target_feature": "case",
"status": 2, "end_time": time.time(), "progress": 100, "message": "",
"start_time": 0, "expired": False
}

def test_get_expired_session(self):
mock_db = mongomock.MongoClient().db
mock_db.sessions.insert_one(self.session)
mock_db.sessions.insert_one(session)
time.sleep(3) # to simulate session being run

sessions = list(get_expired_sessions(mock_db, 3))
Expand All @@ -33,9 +30,15 @@ def test_get_expired_session(self):
@patch("pymongo.MongoClient")
@patch("task.task_runner.delete_expired_sessions")
def test_scan_expired_sessions(self, mock_task, client):
session = {
"id": "abcd", "moses_options": "", "crossval_options": "",
"dataset": "", "mnemonic": "abcdr4e", "target_feature": "case",
"status": 2, "end_time": time.time(), "progress": 100, "message": "",
"start_time": 0, "expired": False
}
mock_db = mongomock.MongoClient().db
client().__getitem__.return_value = mock_db
mock_db["sessions"].insert_one(self.session)
mock_db["sessions"].insert_one(session)

mock_task().apply_async.return_value = None

Expand All @@ -49,18 +52,24 @@ def test_scan_expired_sessions(self, mock_task, client):
@patch("shutil.rmtree")
@patch("os.remove")
def test_delete_expired_sessions(self, remove, rmtree, client):
session = {
"id": "abcd", "moses_options": "", "crossval_options": "",
"dataset": "", "mnemonic": "abcdr4e", "target_feature": "case",
"status": 2, "end_time": time.time(), "progress": 100, "message": "",
"start_time": 0, "expired": False
}
mock_db = mongomock.MongoClient().db
client().__getitem__.return_value = mock_db

mock_session = MagicMock()
mock_session.mnemonic = self.session['mnemonic']
mock_session.mnemonic = session['mnemonic']
mock_session.update.return_value = None

celery.conf.update(CELERY_ALWAYS_EAGER=True)
delete_expired_sessions([mock_session])

zip_file = os.path.join(DATASET_DIR, f"session_{self.session['mnemonic']}.zip")
sess_dir = os.path.join(DATASET_DIR, f"session_{self.session['mnemonic']}")
zip_file = os.path.join(DATASET_DIR, f"session_{session['mnemonic']}.zip")
sess_dir = os.path.join(DATASET_DIR, f"session_{session['mnemonic']}")

remove.assert_called_with(zip_file)
rmtree.assert_called_with(sess_dir)

0 comments on commit 6f868ed

Please sign in to comment.