diff --git a/application/tests/web_main_test.py b/application/tests/web_main_test.py index dfdfbf0dc..fb0fab914 100644 --- a/application/tests/web_main_test.py +++ b/application/tests/web_main_test.py @@ -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: @@ -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", @@ -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", @@ -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", @@ -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`", diff --git a/application/web/web_main.py b/application/web/web_main.py index c69b5a45c..41ba1e64b 100644 --- a/application/web/web_main.py +++ b/application/web/web_main.py @@ -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) @@ -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")})