Home / Class/ ToolsUnitTests Class — langchain Architecture

ToolsUnitTests Class — langchain Architecture

Architecture documentation for the ToolsUnitTests class in tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963["ToolsUnitTests"]
  98288f9f_1f36_11e6_3122_c061243aebd9["ToolsTests"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|extends| 98288f9f_1f36_11e6_3122_c061243aebd9
  5ebe56ae_0ac8_cb13_b5a9_ee567b924009["BaseTool"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|extends| 5ebe56ae_0ac8_cb13_b5a9_ee567b924009
  c7851a8c_0dc1_c211_50a5_ab8ccadfe0bb["tools.py"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|defined in| c7851a8c_0dc1_c211_50a5_ab8ccadfe0bb
  951b903c_b272_7120_36f9_37b92fd82c7d["init_from_env_params()"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|method| 951b903c_b272_7120_36f9_37b92fd82c7d
  7e854dd8_fecc_4444_df3a_a7c72dfa80ee["test_init()"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|method| 7e854dd8_fecc_4444_df3a_a7c72dfa80ee
  8093dbd7_2060_9f45_332f_1059b0e15ca5["test_init_from_env()"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|method| 8093dbd7_2060_9f45_332f_1059b0e15ca5
  ac680efc_2caa_2efb_a1e9_e42a386367a9["test_has_name()"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|method| ac680efc_2caa_2efb_a1e9_e42a386367a9
  4b50ec01_c835_7192_3424_f94b0d2d76a3["test_has_input_schema()"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|method| 4b50ec01_c835_7192_3424_f94b0d2d76a3
  0f0b99d8_694c_4716_5f8b_436bf4223501["test_input_schema_matches_invoke_params()"]
  e67cb5df_cffe_42a9_d7fc_a74ca46e0963 -->|method| 0f0b99d8_694c_4716_5f8b_436bf4223501

Relationship Graph

Source Code

libs/standard-tests/langchain_tests/unit_tests/tools.py lines 58–125

class ToolsUnitTests(ToolsTests):
    """Base class for tools unit tests."""

    @property
    def init_from_env_params(
        self,
    ) -> tuple[dict[str, str], dict[str, Any], dict[str, Any]]:
        """Init from env params.

        Return env vars, init args, and expected instance attrs for initializing
        from env vars.
        """
        return {}, {}, {}

    def test_init(self) -> None:
        """Test init.

        Test that the tool can be initialized with `tool_constructor` and
        `tool_constructor_params`. If this fails, check that the
        keyword args defined in `tool_constructor_params` are valid.
        """
        if isinstance(self.tool_constructor, BaseTool):
            tool = self.tool_constructor
        else:
            tool = self.tool_constructor(**self.tool_constructor_params)
        assert tool is not None

    def test_init_from_env(self) -> None:
        """Test that the tool can be initialized from environment variables."""
        env_params, tools_params, expected_attrs = self.init_from_env_params
        if env_params:
            with mock.patch.dict(os.environ, env_params):
                tool = self.tool_constructor(**tools_params)  # type: ignore[operator]
            assert tool is not None
            for k, expected in expected_attrs.items():
                actual = getattr(tool, k)
                if isinstance(actual, SecretStr):
                    actual = actual.get_secret_value()
                assert actual == expected

    def test_has_name(self, tool: BaseTool) -> None:
        """Tests that the tool has a name attribute to pass to chat models.

        If this fails, add a `name` parameter to your tool.
        """
        assert tool.name

    def test_has_input_schema(self, tool: BaseTool) -> None:
        """Tests that the tool has an input schema.

        If this fails, add an `args_schema` to your tool.

        See [this guide](https://docs.langchain.com/oss/python/contributing/implement-langchain#tools)
        and see how `CalculatorInput` is configured in the
        `CustomCalculatorTool.args_schema` attribute
        """
        assert tool.get_input_schema()

    def test_input_schema_matches_invoke_params(self, tool: BaseTool) -> None:
        """Tests that the provided example params match the declared input schema.

        If this fails, update the `tool_invoke_params_example` attribute to match
        the input schema (`args_schema`) of the tool.
        """
        # This will be a Pydantic object
        input_schema = tool.get_input_schema()

        assert input_schema(**self.tool_invoke_params_example)

Frequently Asked Questions

What is the ToolsUnitTests class?
ToolsUnitTests is a class in the langchain codebase, defined in libs/standard-tests/langchain_tests/unit_tests/tools.py.
Where is ToolsUnitTests defined?
ToolsUnitTests is defined in libs/standard-tests/langchain_tests/unit_tests/tools.py at line 58.
What does ToolsUnitTests extend?
ToolsUnitTests extends ToolsTests, BaseTool.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free