Skip to content

Commit

Permalink
test: unit test for python script pipeline renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
makkus committed Dec 18, 2023
1 parent 702a632 commit 23041e8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
73 changes: 67 additions & 6 deletions src/kiara/interfaces/python_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,16 @@ def assemble_filter_pipeline_config(
def retrieve_renderer_infos(
self, source_type: Union[str, None] = None, target_type: Union[str, None] = None
) -> RendererInfos:
"""Retrieve information about the available renderers.
Arguments:
source_type: the type of the item to render (optional filter)
target_type: the type/profile of the rendered result (optional filter)
Returns:
a wrapper object containing the items as dictionary with renderer alias as key, and [kiara.interfaces.python_api.models.info.RendererInfo] as value
"""

if not source_type and not target_type:
renderers = self.context.render_registry.registered_renderers
Expand Down Expand Up @@ -1811,6 +1821,27 @@ def render(
target_type: str,
render_config: Union[Mapping[str, Any], None] = None,
) -> Any:
"""Render an internal instance of a supported source type into one of the supported target types.
To find out the supported source/target combinations, you can use the kiara cli:
```
kiara render list-renderers
```
or, for a filtered list:
````
kiara render --source-type pipeline list-renderers
```
What Python types are actually supported for the 'item' argument depends on the source_type of the renderer you are calling, for example if that is a pipeline, most of the ways to specify a pipeline would be supported (operation_id, pipeline file, etc.). This might need more documentation, let me know what exactly is needed in a support ticket and I'll add that information.
Arguments:
item: the item to render
source_type: the type of the item to render
target_type: the type/profile of the rendered result
render_config: optional configuration, depends on the renderer that is called
"""

registry = self.context.render_registry
result = registry.render(
Expand All @@ -1831,6 +1862,8 @@ def assemble_render_pipeline(
"""
Create a manifest describing a transformation that renders a value of the specified data type in the target format.
NOTE: this is a preliminary endpoint, don't use in anger yet.
If a list is provided as value for 'target_format', all items are tried until a 'render_value' operation is found that matches
the value type of the source value, and the provided target format.
Expand Down Expand Up @@ -2100,6 +2133,8 @@ def render_value(
"""
Render a value in the specified target format.
NOTE: this is a preliminary endpoint, don't use in anger yet.
If a list is provided as value for 'target_format', all items are tried until a 'render_value' operation is found that matches
the value type of the source value, and the provided target format.
Expand Down Expand Up @@ -2209,17 +2244,26 @@ def render_value(
# all of the workflow-related methods are provisional experiments, so don't rely on them to be availale long term

def list_workflow_ids(self) -> List[uuid.UUID]:
"""List all available workflow ids."""
"""List all available workflow ids.
NOTE: this is a provisional endpoint, don't use in anger yet
"""
return list(self.context.workflow_registry.all_workflow_ids)

def list_workflow_alias_names(self) -> List[str]:
""" "List all available workflow aliases."""
""" "List all available workflow aliases.
NOTE: this is a provisional endpoint, don't use in anger yet
"""
return list(self.context.workflow_registry.workflow_aliases.keys())

def get_workflow(
self, workflow: Union[str, uuid.UUID], create_if_necessary: bool = True
) -> Workflow:
"""Retrieve the workflow instance with the specified id or alias."""
"""Retrieve the workflow instance with the specified id or alias.
NOTE: this is a provisional endpoint, don't use in anger yet
"""
no_such_alias: bool = False
workflow_id: Union[uuid.UUID, None] = None
workflow_alias: Union[str, None] = None
Expand Down Expand Up @@ -2267,6 +2311,10 @@ def get_workflow(
def retrieve_workflow_info(
self, workflow: Union[str, uuid.UUID, Workflow]
) -> WorkflowInfo:
"""Retrieve information about the specified workflow.
NOTE: this is a provisional endpoint, don't use in anger yet
"""

if isinstance(workflow, Workflow):
_workflow: Workflow = workflow
Expand Down Expand Up @@ -2299,7 +2347,10 @@ def list_workflows(self, **matcher_params) -> "WorkflowsMap":
return WorkflowsMap(root={str(k): v for k, v in workflows.items()})

def list_workflow_aliases(self, **matcher_params) -> "WorkflowsMap":
"""List all available workflow sessions that have an alias, indexed by alias."""
"""List all available workflow sessions that have an alias, indexed by alias.
NOTE: this is a provisional endpoint, don't use in anger yet
"""

from kiara.interfaces.python_api.models.doc import WorkflowsMap

Expand Down Expand Up @@ -2329,7 +2380,10 @@ def list_workflow_aliases(self, **matcher_params) -> "WorkflowsMap":
return WorkflowsMap(root=result)

def retrieve_workflows_info(self, **matcher_params: Any) -> WorkflowGroupInfo:
"""Get a map info instances for all available workflows, indexed by (stringified) workflow-id."""
"""Get a map info instances for all available workflows, indexed by (stringified) workflow-id.
NOTE: this is a provisional endpoint, don't use in anger yet
"""
workflows = self.list_workflows(**matcher_params)

workflow_infos = WorkflowGroupInfo.create_from_workflows(
Expand All @@ -2342,7 +2396,10 @@ def retrieve_workflows_info(self, **matcher_params: Any) -> WorkflowGroupInfo:
def retrieve_workflow_aliases_info(
self, **matcher_params: Any
) -> WorkflowGroupInfo:
"""Get a map info instances for all available workflows, indexed by alias."""
"""Get a map info instances for all available workflows, indexed by alias.
NOTE: this is a provisional endpoint, don't use in anger yet
"""
workflows = self.list_workflow_aliases(**matcher_params)
workflow_infos = WorkflowGroupInfo.create_from_workflows(
*workflows.values(),
Expand All @@ -2360,6 +2417,10 @@ def create_workflow(
save: bool = False,
force_alias: bool = False,
) -> Workflow:
"""Create a workflow instance.
NOTE: this is a provisional endpoint, don't use in anger yet
"""

if workflow_alias is not None:
try:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_rendering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from kiara.api import KiaraAPI

# Copyright (c) 2021, University of Luxembourg / DHARPA project
# Copyright (c) 2021, Markus Binsteiner
#
# Mozilla Public License, version 2.0 (see LICENSE or https://www.mozilla.org/en-US/MPL/2.0/)


def test_render_python_script(api: KiaraAPI):

render_config = {"inputs": {"a": True, "b": False}}

rendered = api.render(
"logic.xor",
source_type="pipeline",
target_type="python_script",
render_config=render_config,
)

compile(rendered, "<string>", "exec")

local_vars = {}
exec(rendered, {}, local_vars) # noqa
assert local_vars["pipeline_result_y"].data is True

0 comments on commit 23041e8

Please sign in to comment.