|
| 1 | +# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +from haystack.utils import Secret |
| 5 | + |
| 6 | +from haystack_integrations.prompts.github.repo_forker_prompt import REPO_FORKER_PROMPT, REPO_FORKER_SCHEMA |
| 7 | +from haystack_integrations.tools.github.repo_forker_tool import GitHubRepoForkerTool |
| 8 | +from haystack_integrations.tools.github.utils import message_handler |
| 9 | + |
| 10 | + |
| 11 | +class TestGitHubRepoForkerTool: |
| 12 | + def test_init(self, monkeypatch): |
| 13 | + monkeypatch.setenv("GITHUB_TOKEN", "test-token") |
| 14 | + |
| 15 | + tool = GitHubRepoForkerTool() |
| 16 | + assert tool.name == "repo_forker" |
| 17 | + assert tool.description == REPO_FORKER_PROMPT |
| 18 | + assert tool.parameters == REPO_FORKER_SCHEMA |
| 19 | + assert tool.github_token == Secret.from_env_var("GITHUB_TOKEN") |
| 20 | + assert tool.raise_on_failure is True |
| 21 | + assert tool.outputs_to_string is None |
| 22 | + assert tool.inputs_from_state is None |
| 23 | + assert tool.outputs_to_state is None |
| 24 | + |
| 25 | + def test_from_dict(self, monkeypatch): |
| 26 | + monkeypatch.setenv("GITHUB_TOKEN", "test-token") |
| 27 | + tool_dict = { |
| 28 | + "type": "haystack_integrations.tools.github.repo_forker_tool.GitHubRepoForkerTool", |
| 29 | + "data": { |
| 30 | + "name": "repo_forker", |
| 31 | + "description": REPO_FORKER_PROMPT, |
| 32 | + "parameters": REPO_FORKER_SCHEMA, |
| 33 | + "github_token": {"env_vars": ["GITHUB_TOKEN"], "strict": True, "type": "env_var"}, |
| 34 | + "raise_on_failure": True, |
| 35 | + "outputs_to_string": None, |
| 36 | + "inputs_from_state": None, |
| 37 | + "outputs_to_state": None, |
| 38 | + }, |
| 39 | + } |
| 40 | + tool = GitHubRepoForkerTool.from_dict(tool_dict) |
| 41 | + assert tool.name == "repo_forker" |
| 42 | + assert tool.description == REPO_FORKER_PROMPT |
| 43 | + assert tool.parameters == REPO_FORKER_SCHEMA |
| 44 | + assert tool.github_token == Secret.from_env_var("GITHUB_TOKEN") |
| 45 | + assert tool.raise_on_failure is True |
| 46 | + assert tool.outputs_to_string is None |
| 47 | + assert tool.inputs_from_state is None |
| 48 | + assert tool.outputs_to_state is None |
| 49 | + |
| 50 | + def test_to_dict(self, monkeypatch): |
| 51 | + monkeypatch.setenv("GITHUB_TOKEN", "test-token") |
| 52 | + tool = GitHubRepoForkerTool() |
| 53 | + tool_dict = tool.to_dict() |
| 54 | + assert tool_dict["type"] == "haystack_integrations.tools.github.repo_forker_tool.GitHubRepoForkerTool" |
| 55 | + assert tool_dict["data"]["name"] == "repo_forker" |
| 56 | + assert tool_dict["data"]["description"] == REPO_FORKER_PROMPT |
| 57 | + assert tool_dict["data"]["parameters"] == REPO_FORKER_SCHEMA |
| 58 | + assert tool_dict["data"]["github_token"] == { |
| 59 | + "env_vars": ["GITHUB_TOKEN"], |
| 60 | + "strict": True, |
| 61 | + "type": "env_var", |
| 62 | + } |
| 63 | + assert tool_dict["data"]["raise_on_failure"] is True |
| 64 | + assert tool_dict["data"]["outputs_to_string"] is None |
| 65 | + assert tool_dict["data"]["inputs_from_state"] is None |
| 66 | + assert tool_dict["data"]["outputs_to_state"] is None |
| 67 | + |
| 68 | + def test_to_dict_with_extra_params(self, monkeypatch): |
| 69 | + monkeypatch.setenv("GITHUB_TOKEN", "test-token") |
| 70 | + tool = GitHubRepoForkerTool( |
| 71 | + github_token=Secret.from_env_var("GITHUB_TOKEN"), |
| 72 | + raise_on_failure=False, |
| 73 | + outputs_to_string={"source": "docs", "handler": message_handler}, |
| 74 | + inputs_from_state={"repository": "repo"}, |
| 75 | + outputs_to_state={"documents": {"source": "docs", "handler": message_handler}}, |
| 76 | + ) |
| 77 | + tool_dict = tool.to_dict() |
| 78 | + assert tool_dict["type"] == "haystack_integrations.tools.github.repo_forker_tool.GitHubRepoForkerTool" |
| 79 | + assert tool_dict["data"]["name"] == "repo_forker" |
| 80 | + assert tool_dict["data"]["description"] == REPO_FORKER_PROMPT |
| 81 | + assert tool_dict["data"]["parameters"] == REPO_FORKER_SCHEMA |
| 82 | + assert tool_dict["data"]["github_token"] == { |
| 83 | + "env_vars": ["GITHUB_TOKEN"], |
| 84 | + "strict": True, |
| 85 | + "type": "env_var", |
| 86 | + } |
| 87 | + assert tool_dict["data"]["raise_on_failure"] is False |
| 88 | + assert ( |
| 89 | + tool_dict["data"]["outputs_to_string"]["handler"] |
| 90 | + == "haystack_integrations.tools.github.utils.message_handler" |
| 91 | + ) |
| 92 | + assert tool_dict["data"]["inputs_from_state"] == {"repository": "repo"} |
| 93 | + assert tool_dict["data"]["outputs_to_state"]["documents"]["source"] == "docs" |
| 94 | + assert ( |
| 95 | + tool_dict["data"]["outputs_to_state"]["documents"]["handler"] |
| 96 | + == "haystack_integrations.tools.github.utils.message_handler" |
| 97 | + ) |
| 98 | + |
| 99 | + def test_from_dict_with_extra_params(self, monkeypatch): |
| 100 | + monkeypatch.setenv("GITHUB_TOKEN", "test-token") |
| 101 | + tool_dict = { |
| 102 | + "type": "haystack_integrations.tools.github.repo_forker_tool.GitHubRepoForkerTool", |
| 103 | + "data": { |
| 104 | + "name": "repo_forker", |
| 105 | + "description": REPO_FORKER_PROMPT, |
| 106 | + "parameters": REPO_FORKER_SCHEMA, |
| 107 | + "github_token": {"env_vars": ["GITHUB_TOKEN"], "strict": True, "type": "env_var"}, |
| 108 | + "raise_on_failure": False, |
| 109 | + "outputs_to_string": {"handler": "haystack_integrations.tools.github.utils.message_handler"}, |
| 110 | + "inputs_from_state": {"repository": "repo"}, |
| 111 | + "outputs_to_state": { |
| 112 | + "documents": { |
| 113 | + "source": "docs", |
| 114 | + "handler": "haystack_integrations.tools.github.utils.message_handler", |
| 115 | + } |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + tool = GitHubRepoForkerTool.from_dict(tool_dict) |
| 120 | + assert tool.name == "repo_forker" |
| 121 | + assert tool.description == REPO_FORKER_PROMPT |
| 122 | + assert tool.parameters == REPO_FORKER_SCHEMA |
| 123 | + assert tool.github_token == Secret.from_env_var("GITHUB_TOKEN") |
| 124 | + assert tool.raise_on_failure is False |
| 125 | + assert tool.outputs_to_string["handler"] == message_handler |
| 126 | + assert tool.inputs_from_state == {"repository": "repo"} |
| 127 | + assert tool.outputs_to_state["documents"]["source"] == "docs" |
| 128 | + assert tool.outputs_to_state["documents"]["handler"] == message_handler |
0 commit comments