Skip to content
Snippets Groups Projects
Commit 50c27377 authored by Janet Goldstein's avatar Janet Goldstein
Browse files

WS-651: addresses issues found in MR505

parent 00cca147
No related branches found
No related tags found
1 merge request!505WS-651: catches and saves RH CARTA event
""" Unit tests for Capability Service """
from unittest.mock import patch
# pylint: disable=C0301, E0401, R0201
import pytest
from workspaces.capability.schema import CapabilityExecution, CapabilityVersion
from workspaces.capability.schema import CapabilityExecution
from workspaces.capability.services.capability_info import CapabilityInfo
from workspaces.capability.services.capability_service import CapabilityService
from workspaces.workflow.schema import WorkflowRequest
pytest_plugins = ["testing.utils.conftest"]
@pytest.mark.usefixtures("mock_capability_service")
class TestCapabilityService:
"""Tests for CapabilityService methods"""
@pytest.mark.skip("Broken due to queue/messenger rework")
def test_on_ingestion_complete(
self,
......@@ -45,18 +46,19 @@ class TestCapabilityService:
self,
mock_capability_service: CapabilityService,
mock_capability_info: CapabilityInfo,
mock_capability_execution: CapabilityExecution,
):
"""
Are we catching the "carta-ready" message?
Are we catching the "carta-ready" message and saving the metadata
to the capability request version?
:param mock_capability_service: stand-in for capability service
:param mock_capability_info: stand-in for capability info
:param mock_capability_execution: stand-in for capability execution
:return:
"""
wf_request_id = -1
fake_request = WorkflowRequest(workflow_name="carta", workflow_request_id=wf_request_id)
fake_cr_version = CapabilityVersion(capability_request_id=wf_request_id)
carta_url = "decartes_image_carta_url"
fake_carta_ready_msg = {
"service": "capability",
......@@ -64,21 +66,18 @@ class TestCapabilityService:
"carta_url": carta_url,
"subject": "Your CARTA Session is ready!",
"type": "carta-ready",
"wf_request_id": wf_request_id,
}
save_entity_old_call_count = mock_capability_info.save_entity.call_count
with patch(
"workspaces.capability.services.capability_info.CapabilityInfo.lookup_capability_request",
return_value=fake_request,
"workspaces.capability.services.capability_info.CapabilityInfo.lookup_execution_by_workflow_request_id",
return_value=mock_capability_execution,
):
with patch(
"workspaces.capability.services.capability_info.CapabilityInfo.lookup_capability_request_version",
return_value=fake_cr_version,
) as mock_cr_version:
mock_capability_service.on_carta_ready(**fake_carta_ready_msg)
assert mock_capability_info.save_entity.call_count == save_entity_old_call_count + 1
mock_cr_version.assert_called()
(request,) = mock_capability_info.save_entity.call_args.args
assert request.carta_url == carta_url
mock_capability_service.on_carta_ready(wf_request_id, **fake_carta_ready_msg)
assert mock_capability_info.save_entity.call_count == save_entity_old_call_count + 1
(request_version,) = mock_capability_info.save_entity.call_args.args
assert request_version.version_number > 0
assert request_version.workflow_metadata["carta_url"] == carta_url
......@@ -196,16 +196,6 @@ class CapabilityInfo(CapabilityInfoIF):
"""
return self.session.query(CapabilityRequest).filter_by(id=request_id).first()
def lookup_capability_request_version(self, request_id: int) -> CapabilityVersion:
"""
Finds the capability request version with the provided request ID
:param request_id: capability request ID of interest
:return:
"""
return self.session.query(CapabilityVersion).filter_by(capability_request_id=request_id).first()
def lookup_capability_execution(self, execution_id: int) -> CapabilityExecution:
"""
Finds the capability execution with the provided request ID
......
......@@ -162,7 +162,7 @@ class CapabilityService(CapabilityServiceIF):
self.capability_info.save_entity(request)
@on_message(type="carta-ready")
def on_carta_ready(self, **message: Dict[str, str]):
def on_carta_ready(self, wf_request_id: int, **message: Dict[str, str]):
"""
Catch the RH-flavored event and save it to the capability request version metadata
......@@ -170,14 +170,13 @@ class CapabilityService(CapabilityServiceIF):
:return:
"""
logger.info(f"RECEIVED CARTA READY MESSAGE: {message}")
wf_request_id = int(message["wf_request_id"])
request = self.capability_info.lookup_capability_request(wf_request_id)
request_version = self.capability_info.lookup_capability_request_version(wf_request_id)
request.version_number = request_version
request.carta_url = message["carta_url"]
execution = self.capability_info.lookup_execution_by_workflow_request_id(wf_request_id)
request = execution.capability_request
request_version = request.current_version
request_version.workflow_metadata = {"carta_url": message["carta_url"]}
self.capability_info.save_entity(request)
self.capability_info.save_entity(request_version)
class CapabilityLauncher:
......
......@@ -169,9 +169,6 @@ class CapabilityInfoIF(QueueReporterIF, metaclass=ABCMeta):
def lookup_capability_request(self, request_id) -> CapabilityRequestIF:
raise NotImplementedError
@abstractmethod
def lookup_capability_request_version(self, request_id) -> CapabilityRequestIF:
raise NotImplementedError
@abstractmethod
def lookup_execution(self, execution_id: int) -> CapabilityExecutionIF:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment