Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """Tests for ``composio.experimental.usage``.""" |
| 2 | |
| 3 | from unittest.mock import Mock |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from composio import exceptions |
| 8 | from composio.client.types import usage_retrieve_summary_response |
| 9 | from composio.core.models.experimental import ExperimentalAPI, ExperimentalUsage |
| 10 | |
| 11 | |
| 12 | @pytest.fixture |
| 13 | def mock_client() -> Mock: |
| 14 | client = Mock() |
| 15 | client.project = Mock() |
| 16 | client.project.usage = Mock() |
| 17 | return client |
| 18 | |
| 19 | |
| 20 | @pytest.fixture |
| 21 | def usage(mock_client: Mock) -> ExperimentalUsage: |
| 22 | return ExperimentalUsage(client=mock_client) |
| 23 | |
| 24 | |
| 25 | class TestExperimentalUsage: |
| 26 | def test_mounted_on_experimental_api(self, mock_client): |
| 27 | experimental = ExperimentalAPI(client=mock_client) |
| 28 | |
| 29 | assert isinstance(experimental.usage, ExperimentalUsage) |
| 30 | assert experimental.usage._client is mock_client |
| 31 | |
| 32 | def test_summary_passes_params_through(self, usage, mock_client): |
| 33 | mock_client.project.usage.retrieve_summary.return_value = ( |
| 34 | usage_retrieve_summary_response.UsageRetrieveSummaryResponse.model_validate( |
| 35 | {"entities": {}, "instant_charge": "0.025"} |
| 36 | ) |
| 37 | ) |
| 38 | |
| 39 | result = usage.summary( |
| 40 | from_=1.0, |
| 41 | to=2.0, |
| 42 | entity_types=["tool_execution"], |
| 43 | filters={"toolkit_slug": "github"}, |
| 44 | ) |
| 45 | |
| 46 | assert result.entities == {} |
| 47 | assert result.instant_charge == "0.025" |
| 48 | mock_client.project.usage.retrieve_summary.assert_called_once_with( |
| 49 | from_=1.0, |
| 50 | to=2.0, |
| 51 | entity_types=["tool_execution"], |
| 52 | filters={"toolkit_slug": "github"}, |
| 53 | ) |
| 54 | |
| 55 | def test_summary_without_params(self, usage, mock_client): |
| 56 | mock_client.project.usage.retrieve_summary.return_value = ( |
| 57 | usage_retrieve_summary_response.UsageRetrieveSummaryResponse.model_validate( |
| 58 | {"entities": {}, "instant_charge": "0"} |
| 59 | ) |
| 60 | ) |
| 61 | |
| 62 | result = usage.summary() |
| 63 | |
| 64 | mock_client.project.usage.retrieve_summary.assert_called_once_with() |
| 65 | assert result.instant_charge == "0" |
| 66 | |
| 67 | def test_breakdown_passes_entity_type_and_params(self, usage, mock_client): |
| 68 | mock_client.project.usage.retrieve.return_value = "breakdown" |
| 69 | |
| 70 | result = usage.breakdown( |
| 71 | "tool_execution", |
| 72 | group_by="toolkit_slug", |
| 73 | order_by="total_quantity", |
| 74 | order_direction="desc", |
| 75 | limit=5, |
| 76 | ) |
| 77 | |
| 78 | assert result == "breakdown" |
| 79 | mock_client.project.usage.retrieve.assert_called_once_with( |
| 80 | "tool_execution", |
| 81 | group_by="toolkit_slug", |
| 82 | order_by="total_quantity", |
| 83 | order_direction="desc", |
| 84 | limit=5, |
| 85 | ) |
| 86 | |
| 87 | def test_requires_client(self): |
| 88 | usage = ExperimentalUsage(client=None) |
| 89 | |
| 90 | with pytest.raises(exceptions.ValidationError): |
| 91 | usage.summary() |
| 92 | with pytest.raises(exceptions.ValidationError): |
| 93 | usage.breakdown("tool_execution") |