Skip to content

Commit

Permalink
Merge pull request #546 from 3rd-Son/testrefactor
Browse files Browse the repository at this point in the history
added fixture and skipifs to the community tests
  • Loading branch information
cobycloud authored Sep 26, 2024
2 parents b42d7be + 95a89b4 commit fc9a7df
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 335 deletions.
4 changes: 3 additions & 1 deletion pkgs/community/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@
"pygithub",
"pacmap",
"tf-keras",
"pinecone",
"pinecone[grpc]",
"neo4j",
"tiktoken",
"redis",
"textstat",
]
},
classifiers=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def test_agent_exec(shuttleai_tool_model):
tool = AdditionTool()
toolkit.add_tool(tool)

agent = ToolAgent(llm=llm, conversation=conversation, toolkit=toolkit)
agent = ToolAgent(
llm=shuttleai_tool_model, conversation=conversation, toolkit=toolkit
)
result = agent.exec("Add 512+671")
assert type(result) == str
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import pytest
from swarmauri.metrics.concrete.TokenCountEstimatorMetric import TokenCountEstimatorMetric as Metric
from swarmauri_community.metrics.concrete.TokenCountEstimatorMetric import (
TokenCountEstimatorMetric as Metric,
)


@pytest.mark.unit
def test_ubc_resource():
def test():
assert Metric().resource == 'Metric'
assert Metric().resource == "Metric"

test()


@pytest.mark.unit
def test_ubc_type():
metric = Metric()
assert metric.type == 'TokenCountEstimatorMetric'
assert metric.type == "TokenCountEstimatorMetric"


@pytest.mark.unit
def test_serialization():
Expand All @@ -20,13 +26,18 @@ def test_serialization():

@pytest.mark.unit
def test_metric_value():
def test():
assert Metric().calculate("Lorem ipsum odor amet, consectetuer adipiscing elit.") == 11
test()
def test():
assert (
Metric().calculate("Lorem ipsum odor amet, consectetuer adipiscing elit.")
== 11
)

test()


@pytest.mark.unit
def test_metric_unit():
def test():
assert Metric().unit == "tokens"
test()
def test():
assert Metric().unit == "tokens"

test()
69 changes: 29 additions & 40 deletions pkgs/community/tests/unit/tools/GithubIssueTool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,42 @@
load_dotenv()


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_ubc_resource():
# Fixture for retrieving GitHub token and skipping tests if not available
@pytest.fixture(scope="module")
def github_token():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert tool.resource == "Tool"
if not token:
pytest.skip("Skipping due to GITHUBTOOL_TEST_TOKEN not set")
return token


# Fixture for initializing the GithubIssueTool
@pytest.fixture(scope="module")
def github_issue_tool(github_token):
return Tool(token=github_token)


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_ubc_type():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
assert Tool(token=token).type == "GithubIssueTool"
def test_ubc_resource(github_issue_tool):
assert github_issue_tool.resource == "Tool"


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_initialization():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert type(tool.id) == str
def test_ubc_type(github_issue_tool):
assert github_issue_tool.type == "GithubIssueTool"


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_serialization():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert tool.id == Tool.model_validate_json(tool.model_dump_json()).id
def test_initialization(github_issue_tool):
assert type(github_issue_tool.id) == str


@pytest.mark.unit
def test_serialization(github_issue_tool):
assert (
github_issue_tool.id
== Tool.model_validate_json(github_issue_tool.model_dump_json()).id
)


@pytest.mark.parametrize(
Expand All @@ -73,16 +68,10 @@ def test_serialization():
("invalid_action", {}, None),
],
)
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
@pytest.mark.unit
@patch("swarmauri_community.tools.concrete.GithubIssueTool.Github")
def test_call(mock_github, action, kwargs, method_called):
def test_call(mock_github, github_issue_tool, action, kwargs, method_called):
expected_keys = {action}
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)

mock_github.return_value = MagicMock()

Expand All @@ -92,7 +81,7 @@ def test_call(mock_github, action, kwargs, method_called):
method_called,
return_value="performed a test action successfully",
) as mock_method:
result = tool(action=action, **kwargs)
result = github_issue_tool(action=action, **kwargs)

mock_method.assert_called_once_with(**kwargs)

Expand All @@ -108,4 +97,4 @@ def test_call(mock_github, action, kwargs, method_called):
assert result == {f"{action}": "performed a test action successfully"}
else:
with pytest.raises(ValueError, match=f"Action '{action}' is not supported."):
tool(action=action, **kwargs)
github_issue_tool(action=action, **kwargs)
69 changes: 29 additions & 40 deletions pkgs/community/tests/unit/tools/GithubPRTool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,42 @@
load_dotenv()


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_ubc_resource():
# Fixture for retrieving GitHub token and skipping tests if not available
@pytest.fixture(scope="module")
def github_token():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert tool.resource == "Tool"
if not token:
pytest.skip("Skipping due to GITHUBTOOL_TEST_TOKEN not set")
return token


# Fixture for initializing the GithubPRTool
@pytest.fixture(scope="module")
def github_pr_tool(github_token):
return Tool(token=github_token)


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_ubc_type():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
assert Tool(token=token).type == "GithubPRTool"
def test_ubc_resource(github_pr_tool):
assert github_pr_tool.resource == "Tool"


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_initialization():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert type(tool.id) == str
def test_ubc_type(github_pr_tool):
assert github_pr_tool.type == "GithubPRTool"


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_serialization():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert tool.id == Tool.model_validate_json(tool.model_dump_json()).id
def test_initialization(github_pr_tool):
assert type(github_pr_tool.id) == str


@pytest.mark.unit
def test_serialization(github_pr_tool):
assert (
github_pr_tool.id
== Tool.model_validate_json(github_pr_tool.model_dump_json()).id
)


@pytest.mark.parametrize(
Expand All @@ -66,16 +61,10 @@ def test_serialization():
("invalid_action", {}, None),
],
)
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
@pytest.mark.unit
@patch("swarmauri_community.tools.concrete.GithubPRTool.Github")
def test_call(mock_github, action, kwargs, method_called):
def test_call(mock_github, github_pr_tool, action, kwargs, method_called):
expected_keys = {action}
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)

mock_github.return_value = MagicMock()

Expand All @@ -85,7 +74,7 @@ def test_call(mock_github, action, kwargs, method_called):
method_called,
return_value="performed a test action successfully",
) as mock_method:
result = tool(action=action, **kwargs)
result = github_pr_tool(action=action, **kwargs)

mock_method.assert_called_once_with(**kwargs)

Expand All @@ -101,4 +90,4 @@ def test_call(mock_github, action, kwargs, method_called):
assert result == {f"{action}": "performed a test action successfully"}
else:
with pytest.raises(ValueError, match=f"Action '{action}' is not supported."):
tool(action=action, **kwargs)
github_pr_tool(action=action, **kwargs)
69 changes: 29 additions & 40 deletions pkgs/community/tests/unit/tools/GithubRepoTool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,42 @@
load_dotenv()


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_ubc_resource():
# Fixture for retrieving GitHub token and skipping tests if not available
@pytest.fixture(scope="module")
def github_token():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert tool.resource == "Tool"
if not token:
pytest.skip("Skipping due to GITHUBTOOL_TEST_TOKEN not set")
return token


# Fixture for initializing the GithubRepoTool
@pytest.fixture(scope="module")
def github_repo_tool(github_token):
return Tool(token=github_token)


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_ubc_type():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
assert Tool(token=token).type == "GithubRepoTool"
def test_ubc_resource(github_repo_tool):
assert github_repo_tool.resource == "Tool"


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_initialization():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert type(tool.id) == str
def test_ubc_type(github_repo_tool):
assert github_repo_tool.type == "GithubRepoTool"


@pytest.mark.unit
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
def test_serialization():
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)
assert tool.id == Tool.model_validate_json(tool.model_dump_json()).id
def test_initialization(github_repo_tool):
assert type(github_repo_tool.id) == str


@pytest.mark.unit
def test_serialization(github_repo_tool):
assert (
github_repo_tool.id
== Tool.model_validate_json(github_repo_tool.model_dump_json()).id
)


@pytest.mark.parametrize(
Expand All @@ -66,16 +61,10 @@ def test_serialization():
("invalid_action", {}, None),
],
)
@pytest.mark.skipif(
not os.getenv("GITHUBTOOL_TEST_TOKEN"),
reason="Skipping due to environment variable not set",
)
@pytest.mark.unit
@patch("swarmauri_community.tools.concrete.GithubRepoTool.Github")
def test_call(mock_github, action, kwargs, method_called):
def test_call(mock_github, github_repo_tool, action, kwargs, method_called):
expected_keys = {action}
token = os.getenv("GITHUBTOOL_TEST_TOKEN")
tool = Tool(token=token)

mock_github.return_value = MagicMock()

Expand All @@ -85,7 +74,7 @@ def test_call(mock_github, action, kwargs, method_called):
method_called,
return_value="performed a test action successfully",
) as mock_method:
result = tool(action=action, **kwargs)
result = github_repo_tool(action=action, **kwargs)

mock_method.assert_called_once_with(**kwargs)

Expand All @@ -101,4 +90,4 @@ def test_call(mock_github, action, kwargs, method_called):
assert result == {f"{action}": "performed a test action successfully"}
else:
with pytest.raises(ValueError, match=f"Action '{action}' is not supported."):
tool(action=action, **kwargs)
github_repo_tool(action=action, **kwargs)
Loading

0 comments on commit fc9a7df

Please sign in to comment.