Skip to content

Commit 5dfd223

Browse files
committed
Add example of a plugin action that uses the plugin context.
1 parent 906f241 commit 5dfd223

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

tests/test_description.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
from collections.abc import Sequence
55

6-
from cmem_plugin_base.dataintegration.context import ExecutionContext
6+
from cmem_plugin_base.dataintegration.context import ExecutionContext, PluginContext
77
from cmem_plugin_base.dataintegration.description import Plugin, PluginAction
88
from cmem_plugin_base.dataintegration.entity import Entities
99
from cmem_plugin_base.dataintegration.plugins import TransformPlugin, WorkflowPlugin
@@ -71,8 +71,15 @@ def test__actions(self) -> None:
7171
label="My Workflow Plugin",
7272
actions=[
7373
PluginAction(
74-
name="get_name", label="Get name", description="Returns the supplied name"
75-
)
74+
name="get_name",
75+
label="Get name",
76+
description="Returns the supplied name",
77+
),
78+
PluginAction(
79+
name="get_project",
80+
label="Get project",
81+
description="Returns the current project.",
82+
),
7683
],
7784
)
7885
class MyWorkflowPlugin(WorkflowPlugin):
@@ -87,19 +94,25 @@ def execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> Enti
8794
def get_name(self) -> str:
8895
return self.name
8996

97+
def get_project(self, context: PluginContext) -> str:
98+
return context.project_id
99+
90100
# Get plugin description
91101
plugin = Plugin.plugins[0]
92102

93-
# Check action
94-
assert len(plugin.actions) == 1
103+
# There should be two actions
104+
assert len(plugin.actions) == 2
105+
106+
# Check first action
95107
action = plugin.actions[0]
96108
assert action.name == "get_name"
97109
assert action.label == "Get name"
98110
assert action.description == "Returns the supplied name"
99111

100-
# Call action on a plugin instance
112+
# Call actions on a plugin instance
101113
plugin_instance = MyWorkflowPlugin("My Name")
102-
action.execute(plugin_instance, TestPluginContext())
114+
assert action.execute(plugin_instance, TestPluginContext()) == "My Name"
115+
assert action.execute(plugin_instance, TestPluginContext(project_id="movies")) == "movies"
103116

104117

105118
if __name__ == "__main__":

0 commit comments

Comments
 (0)