Skip to content

Commit

Permalink
Add export and validation handler tests (#2876)
Browse files Browse the repository at this point in the history
Co-authored-by: Michonster <83566627+Michonster@users.noreply.github.com>
Co-authored-by: Aarnav Patel <shadowluigi27@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 12, 2022
1 parent dff6169 commit ae59094
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions elyra/pipeline/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def post(self, *args, **kwargs):
pipeline_export_path = payload["export_path"]
pipeline_overwrite = payload["overwrite"]

response = await PipelineValidationManager.instance().validate(pipeline=pipeline_definition)
response = await PipelineValidationManager.instance().validate(pipeline_definition)
self.log.debug(f"Validation checks completed. Results as follows: {response.to_json()}")

if not response.has_fatal:
Expand Down Expand Up @@ -283,7 +283,7 @@ async def post(self):
pipeline_definition = self.get_json_body()
self.log.debug(f"Pipeline payload: {pipeline_definition}")

response = await PipelineValidationManager.instance().validate(pipeline=pipeline_definition)
response = await PipelineValidationManager.instance().validate(pipeline_definition)
json_msg = response.to_json()

self.set_status(200)
Expand Down
55 changes: 55 additions & 0 deletions elyra/tests/pipeline/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
from elyra.tests.pipeline import resources
from elyra.tests.util.handlers_utils import expected_http_error

from elyra.pipeline.processor import PipelineProcessorManager
from elyra.pipeline.validation import PipelineValidationManager
from elyra.pipeline.validation import ValidationResponse
from elyra.pipeline.parser import PipelineParser
from elyra.pipeline.validation import ValidationSeverity

try:
import importlib.resources as pkg_resources
Expand All @@ -42,6 +47,13 @@
TEST_CATALOG_NAME = "test_handlers_catalog"


def _async_return(result):
# Helper function to return an arbitrary value when mocking awaits
f = asyncio.Future()
f.set_result(result)
return f


def _get_resource_path(filename):
resource_path = os.path.join(os.path.dirname(__file__), "resources", "components", filename)
resource_path = os.path.normpath(resource_path)
Expand Down Expand Up @@ -223,3 +235,46 @@ async def test_get_pipeline_properties_definition(jp_fetch):
{"id": "elyra_mounted_volumes"},
{"id": "elyra_kubernetes_pod_annotations"},
]


async def test_pipeline_success(jp_fetch, monkeypatch):
request_body = {"pipeline": "body", "export_format": "py", "export_path": "test.py", "overwrite": True}

# Create a response that will trigger the valid code path
validation_response = ValidationResponse()

monkeypatch.setattr(PipelineValidationManager, "validate", lambda x, y: _async_return(validation_response))
monkeypatch.setattr(PipelineParser, "parse", lambda x, y: "Dummy_Data")
monkeypatch.setattr(PipelineProcessorManager, "export", lambda x, y, z, aa, bb: _async_return("test.py"))

json_body = json.dumps(request_body)

http_response = await jp_fetch("elyra", "pipeline", "export", body=json_body, method="POST")

assert http_response.code == 201


async def test_pipeline_failure(jp_fetch, monkeypatch):
request_body = {"pipeline": "body", "export_format": "py", "export_path": "test.py", "overwrite": True}

# Create a response that will trigger the fatal code path
bad_validation_response = ValidationResponse()
bad_validation_response.add_message(severity=ValidationSeverity.Error, message_type="invalidJSON", message="issue")

monkeypatch.setattr(PipelineValidationManager, "validate", lambda x, y: _async_return(bad_validation_response))

json_body = json.dumps(request_body)

# Will raise HTTP error so we need to catch with pytest
with pytest.raises(HTTPClientError):
await jp_fetch("elyra", "pipeline", "export", body=json_body, method="POST")


async def test_validation_handler(jp_fetch, monkeypatch):
request_body = {"pipeline": "body", "export_format": "py", "export_path": "test.py", "overwrite": True}

monkeypatch.setattr(PipelineValidationManager, "validate", lambda x, y: _async_return(ValidationResponse()))
json_body = json.dumps(request_body)
http_response = await jp_fetch("elyra", "pipeline", "validate", body=json_body, method="POST")

assert http_response.code == 200

0 comments on commit ae59094

Please sign in to comment.