Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarreau committed Jan 28, 2025
1 parent 230db07 commit 3c8efd4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/agent_toolkit/tests/resources/actions/test_resources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import copy
import importlib
import json
import sys
Expand Down Expand Up @@ -980,3 +981,56 @@ def execute(ctx, result_builder):
response = self.loop.run_until_complete(self.action_resource.execute(request))
self.assertEqual(response.headers["headerOne"], "valueOne")
self.assertEqual(response.headers["headerOne"], "valueOne")

def test_execute_should_get_all_form_fields_included_hidden(self):

self.decorated_collection_book.add_action(
"test_action_global_hidden_fields",
{
"scope": ActionsScope.GLOBAL,
"execute": lambda ctx, rb: rb.success(ctx.form_values.get("hidden_field")),
"form": [
{
"type": "String",
"label": "hidden_field",
"if_": lambda ctx: False,
}
],
},
)
body_params = copy.deepcopy(self.body_params)
body_params["data"]["attributes"]["values"] = {"hidden_field": "hidden_value"}
request = ActionRequest(
method=RequestMethod.POST,
action_name="test_action_global_hidden_fields",
collection=self.decorated_collection_book,
body=body_params,
query={
"timezone": "Europe/Paris",
"collection_name": "Book",
"action_name": 0,
"slug": "test_action_global_hidden_fields",
},
headers={},
user=self.mocked_caller,
client_ip="127.0.0.1",
)
with patch.object(
self.decorated_collection_book,
"get_form",
new_callable=AsyncMock,
wraps=self.decorated_collection_book.get_form,
) as spy_get_form:
response = self.loop.run_until_complete(self.action_resource.execute(request))
spy_get_form.assert_awaited_once_with(
request.user,
"test_action_global_hidden_fields",
{"hidden_field": "hidden_value"},
ANY,
{"include_hidden_fields": True},
)
self.assertEqual(response.status, 200)
self.assertEqual(
response.body,
'{"success": "hidden_value"}',
)
Original file line number Diff line number Diff line change
Expand Up @@ -819,3 +819,43 @@ def _search_fn(context, search_value):
}
],
)

def test_get_form_should_return_hidden_fields_when_asked(self):
if_fn = Mock(return_value=False)
test_action: ActionDict = {
"scope": ActionsScope.SINGLE,
"execute": lambda ctx, result_builder: result_builder.success("ok"),
"form": [
{
"label": "name",
"type": ActionFieldType.STRING,
"if_": if_fn,
},
],
}
self.product_collection.add_action("action_test", test_action)

result = self.loop.run_until_complete(
self.product_collection.get_form(
self.mocked_caller, "action_test", {"name": "name"}, None, {"include_hidden_fields": True}
)
)
self.assertEqual(
result,
[
{
"label": "name",
"id": "name",
"type": ActionFieldType.STRING,
"description": "",
"is_read_only": False,
"is_required": False,
"value": "name",
"default_value": None,
"collection_name": None,
"enum_values": None,
"watch_changes": False,
}
],
)
if_fn.assert_not_called()

0 comments on commit 3c8efd4

Please sign in to comment.