Testing Helpers

Helpers for testing an extension’s tools, without installing the extension.

An extension’s tests call its tools directly, with the settings they need, and without the database or an LLM:

from pydantic import SecretStr

from smarter.apps.llmtool.extension.testing import call_tool, tool_context
from weather.tools import GetWeather

class TestGetWeather(unittest.TestCase):
    def test_unknown_place(self):
        context = tool_context("weather", google_maps_api_key=SecretStr("test-key"))
        with patch("weather.services.googlemaps.Client") as client:
            client.return_value.geocode.return_value = []
            result = call_tool(GetWeather, {"location": "Atlantis"}, context)
        self.assertIn("Could not find Atlantis", result["error"])

Run an extension repository’s tests in a Smarter development container with manage.py test_extension <path>, which puts the repository on sys.path, so that its tests import its package by name.

class smarter.apps.llmtool.extension.testing.TestSettings(**data)[source]

Bases: BaseModel

The base class of the settings that tool_context() builds.

model_config = {'arbitrary_types_allowed': True, 'frozen': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

smarter.apps.llmtool.extension.testing.call_tool(tool, arguments, context=None)[source]

Call a tool as the platform does: validate its arguments, run it, and turn its errors into results.

Unlike the platform, it runs the tool in the calling thread, without a timeout, and lets unexpected exceptions propagate, so that a test sees them.

Parameters:
Return type:

TypeAliasType

Returns:

The tool’s result, or {"error": ...} for invalid arguments or an ExtensionToolError.

smarter.apps.llmtool.extension.testing.tool_context(slug='test', version='0.0.0', **settings)[source]

Return the context of a tool call, with the given settings.

Parameters:
  • slug (str) – The extension’s slug, which names its logger and switches.

  • version (str) – The extension’s version.

  • settings (str | int | float | bool | SecretStr | None) – The settings, by name, as the tool reads them, e.g. api_key=SecretStr("x").

Return type:

ExtensionContext

Returns:

A context without an account, user or LLMClient.