From 24d50af861a43ff34299ea1ce866e5008d1e8860 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 13 Dec 2023 14:38:59 -0800 Subject: [PATCH] [TESTS][PostHog] --- pyproject.toml | 2 +- swarms/__init__.py | 2 +- tests/telemetry/test_posthog_utils.py | 58 +++++++++++++++++++ .../{user_utils.py => test_user_utils.py} | 0 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/telemetry/test_posthog_utils.py rename tests/telemetry/{user_utils.py => test_user_utils.py} (100%) diff --git a/pyproject.toml b/pyproject.toml index 06e0b132..292a440c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "2.8.3" +version = "2.8.4" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/__init__.py b/swarms/__init__.py index fd7b75aa..9b866975 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -6,4 +6,4 @@ from swarms.agents import * # noqa: E402, F403 from swarms.swarms import * # noqa: E402, F403 from swarms.structs import * # noqa: E402, F403 from swarms.models import * # noqa: E402, F403 -from swarms.telemetry import * # noqa: E402, F403 \ No newline at end of file +from swarms.telemetry import * # noqa: E402, F403 diff --git a/tests/telemetry/test_posthog_utils.py b/tests/telemetry/test_posthog_utils.py new file mode 100644 index 00000000..1a35296a --- /dev/null +++ b/tests/telemetry/test_posthog_utils.py @@ -0,0 +1,58 @@ +from unittest.mock import Mock + +import pytest + +from swarms.telemetry.posthog_utils import ( + log_activity_posthog, + posthog, +) + + +# Mock Posthog client +@pytest.fixture +def mock_posthog(): + return Mock() + +# Mock environment variables +@pytest.fixture +def mock_env(monkeypatch): + monkeypatch.setenv("POSTHOG_API_KEY", "test_api_key") + monkeypatch.setenv("POSTHOG_HOST", "test_host") + +# Test the log_activity_posthog decorator +def test_log_activity_posthog(mock_posthog, mock_env): + event_name = "test_event" + event_properties = {"test_property": "test_value"} + + # Create a test function with the decorator + @log_activity_posthog(event_name, **event_properties) + def test_function(): + pass + + # Call the test function + test_function() + + # Check if the Posthog capture method was called with the expected arguments + mock_posthog.capture.assert_called_once_with( + "test_user_id", event_name, event_properties + ) + +# Test a scenario where environment variables are not set +def test_missing_env_variables(monkeypatch): + # Unset environment variables + monkeypatch.delenv("POSTHOG_API_KEY", raising=False) + monkeypatch.delenv("POSTHOG_HOST", raising=False) + + # Create a test function with the decorator + @log_activity_posthog("test_event", test_property="test_value") + def test_function(): + pass + + # Ensure that calling the test function does not raise errors + test_function() + +# Test the Posthog client initialization +def test_posthog_client_initialization(mock_env): + assert posthog.api_key == "test_api_key" + assert posthog.host == "test_host" + assert posthog.debug is True diff --git a/tests/telemetry/user_utils.py b/tests/telemetry/test_user_utils.py similarity index 100% rename from tests/telemetry/user_utils.py rename to tests/telemetry/test_user_utils.py