Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
northdpole committed Oct 24, 2023
1 parent 186a4cb commit e387199
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
23 changes: 13 additions & 10 deletions application/tests/web_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class MockJob:
def id(self):
return "ABC"

def get_status(self):
return rq.job.JobStatus.STARTED

class TestMain(unittest.TestCase):
def tearDown(self) -> None:
Expand Down Expand Up @@ -575,10 +577,12 @@ def test_smartlink(self) -> None:
self.assertEqual(404, response.status_code)

@patch.object(redis, "from_url")
def test_gap_analysis_from_cache_full_response(self, redis_conn_mock) -> None:
@patch.object(db, "Node_collection")
def test_gap_analysis_from_cache_full_response(self,db_mock, redis_conn_mock) -> None:
expected = {"result": "hello"}
redis_conn_mock.return_value.exists.return_value = True
redis_conn_mock.return_value.get.return_value = json.dumps(expected)
db_mock.return_value.get_gap_analysis_result.return_value = json.dumps(expected)
with self.app.test_client() as client:
response = client.get(
"/rest/v1/map_analysis?standard=aaa&standard=bbb",
Expand All @@ -587,14 +591,16 @@ def test_gap_analysis_from_cache_full_response(self, redis_conn_mock) -> None:
self.assertEqual(200, response.status_code)
self.assertEqual(expected, json.loads(response.data))

@patch.object(rq.job.Job, "fetch")
@patch.object(rq.Queue, "enqueue_call")
@patch.object(redis, "from_url")
def test_gap_analysis_from_cache_job_id(
self, redis_conn_mock, enqueue_call_mock
self, redis_conn_mock, enqueue_call_mock, fetch_mock
) -> None:
expected = {"job_id": "hello"}
redis_conn_mock.return_value.exists.return_value = True
redis_conn_mock.return_value.get.return_value = json.dumps(expected)
fetch_mock.return_value = MockJob()
with self.app.test_client() as client:
response = client.get(
"/rest/v1/map_analysis?standard=aaa&standard=bbb",
Expand All @@ -611,8 +617,9 @@ def test_gap_analysis_create_job_id(
self, redis_conn_mock, enqueue_call_mock, db_mock
) -> None:
expected = {"job_id": "ABC"}
redis_conn_mock.return_value.exists.return_value = False
redis_conn_mock.return_value.get.return_value = None
enqueue_call_mock.return_value = MockJob()
db_mock.return_value.get_gap_analysis_result.return_value = None
with self.app.test_client() as client:
response = client.get(
"/rest/v1/map_analysis?standard=aaa&standard=bbb",
Expand Down Expand Up @@ -684,14 +691,10 @@ def test_gap_analysis_weak_links_no_cache(self) -> None:
)
self.assertEqual(404, response.status_code)

@patch.object(redis, "from_url")
def test_gap_analysis_weak_links_response(self, redis_conn_mock) -> None:
@patch.object(db, "Node_collection")
def test_gap_analysis_weak_links_response(self, db_mock) -> None:
expected = {"result": "hello"}
collection = db.Node_collection()
standards = ["aaa", "bbb"]
key = "ccc"
cache_key = make_cache_key(standards=standards, key=key)
collection.add_gap_analysis_result(cache_key=cache_key, ga_object=expected)
db_mock.return_value.get_gap_analysis_result.return_value = json.dumps(expected)
with self.app.test_client() as client:
response = client.get(
"/rest/v1/map_analysis_weak_links?standard=aaa&standard=bbb&key=ccc`",
Expand Down
6 changes: 3 additions & 3 deletions application/web/web_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def gap_analysis() -> Any:
result = database.get_gap_analysis_result(standards_hash)
if result:
gap_analysis_dict = json.loads(result)
if gap_analysis_dict.get("results"):
if gap_analysis_dict.get("result"):
return jsonify({"result": gap_analysis_dict.get("result")})

gap_analysis_results = conn.get(standards_hash)
Expand All @@ -242,8 +242,8 @@ def gap_analysis() -> Any:
abort(404, "No such job")
if (
res.get_status() != job.JobStatus.FAILED
and res.get_status() == job.JobStatus.STOPPED
and res.get_status() == job.JobStatus.CANCELED
and res.get_status() != job.JobStatus.STOPPED
and res.get_status() != job.JobStatus.CANCELED
):
logger.info("gap analysis job id already exists, returning early")
return jsonify({"job_id": gap_analysis_dict.get("job_id")})
Expand Down

0 comments on commit e387199

Please sign in to comment.