Skip to content

Use MockColonyClient for toolkit tests, accept client= injection#26

Merged
jackparnell merged 3 commits intomainfrom
feature/mock-client-and-injection
Apr 12, 2026
Merged

Use MockColonyClient for toolkit tests, accept client= injection#26
jackparnell merged 3 commits intomainfrom
feature/mock-client-and-injection

Conversation

@ColonistOne
Copy link
Copy Markdown
Collaborator

Summary

Adopt the new colony_sdk.testing.MockColonyClient (shipped in colony-sdk 1.7.0) for toolkit tests, replacing the existing unittest.mock.patch boilerplate. Also adds a client= constructor parameter to ColonyToolkit / AsyncColonyToolkit so users (and tests) can inject any client — perfect for testing, but also useful for sharing a client across toolkits or supplying a pre-configured one with custom retry / hooks / typed mode / etc.

Fully backward compatible — existing ColonyToolkit(api_key=...) calls still work unchanged.

Changes

src/langchain_colony/toolkit.py

  • ColonyToolkit.__init__ adds client: Any = None kwarg. When set, api_key / base_url / retry are ignored.
  • AsyncColonyToolkit.__init__ does the same.
  • api_key is now optional (must supply either it or client); raises ValueError if both are missing.

tests/test_toolkit.py

  • New _toolkit_with(responses) helper returns (toolkit, mock_client) for one-line setup.
  • Migrated all 30+ tool-invocation tests from with patch(...) to MockColonyClient(responses=...) injected via client=.
  • Replaced mock.method.assert_called_once_with(...) with mock.calls[-1] == ("method", {...}).
  • Net -40 lines while keeping all 81 tests.

pyproject.toml

  • Bumped colony-sdk>=1.5.0>=1.7.0 (and colony-sdk[async]>=1.5.0>=1.7.0) for MockColonyClient.

Why this matters

  • Less code to maintain: future framework integrations can use the same shared mock instead of hand-rolling
  • Easier client injection for users: users wanting a pre-configured client (custom retry, hooks, typed mode, proxies, caching) can now pass one in
  • Pattern for other framework repos: this PR is a template for migrating crewai-colony, openai-agents-colony, smolagents-colony, pydantic-ai-colony

Test plan

  • 270 tests pass (81 in test_toolkit.py + 189 elsewhere)
  • No tests modified outside test_toolkit.py
  • ruff check clean
  • ruff format clean
  • CI matrix (Python 3.10/3.11/3.12/3.13)

🤖 Generated with Claude Code

ColonyToolkit and AsyncColonyToolkit now accept a pre-built client via
client=, alongside the existing api_key= constructor. Backward compatible:
old api_key= calls still work unchanged.

Migrated tests/test_toolkit.py from unittest.mock.patch to
colony_sdk.testing.MockColonyClient injected via the new client=
parameter. The test file shrinks from 1009 → 968 lines while preserving
the same 81 tests, and gains:

- No more import-time patching (`with patch("langchain_colony.toolkit.ColonyClient") as MockClient: ...`)
- Real instances with structured `client.calls` recording instead of MagicMock attribute juggling
- A small `_toolkit_with(responses)` helper that returns (toolkit, mock_client) for one-line setup

Bumped colony-sdk constraint from >=1.5.0 → >=1.7.0 for MockColonyClient.

All 270 tests pass, lint/format/typecheck clean.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 12, 2026

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment

Thanks for integrating Codecov - We've got you covered ☂️

ColonistOne and others added 2 commits April 12, 2026 09:52
The previous PR commit added a client= constructor parameter to both
ColonyToolkit and AsyncColonyToolkit but didn't cover the new code
paths in tests. Codecov flagged the coverage drop on the patch.

This commit adds 7 tests covering:
- ColonyToolkit accepts injected MockColonyClient
- ColonyToolkit injected client overrides api_key
- ColonyToolkit raises ValueError when neither api_key nor client provided
- ColonyToolkit api_key path still constructs a real ColonyClient
- AsyncColonyToolkit accepts injected MockColonyClient
- AsyncColonyToolkit injected client overrides api_key
- AsyncColonyToolkit raises ValueError when neither api_key nor client provided

src/langchain_colony/toolkit.py now at 100% coverage (was 95%).
277 tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add .coverage / htmlcov/ / coverage.xml to .gitignore so future
coverage runs don't leak binary state into the repo.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jackparnell jackparnell merged commit 0a4f433 into main Apr 12, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants