# pybond
[![Build](https://github.com/epgui/pybond/actions/workflows/build.yml/badge.svg)](https://github.com/epgui/pybond/actions/workflows/build.yml)
[![codecov](https://codecov.io/github/epgui/pybond/branch/main/graph/badge.svg?token=tkq655ROg3)](https://app.codecov.io/github/epgui/pybond)
`pybond` is a spying and stubbing library inspired heavily by the
[clojure `bond` library](https://github.com/circleci/bond/).
## Installation
pip
```bash
pip install pybond==0.2.2
```
requirements.txt
```python
pybond==0.2.2
```
pyproject.toml
```toml
pybond = "0.2.2"
```
## Example usage
Let's say you wanted to test the functions in this module:
```python
# /sample_code/my_module.py
from typing import Any
import sample_code.other_package as other_package
def foo(x: Any) -> None:
response = other_package.make_a_network_request(x)
other_package.write_to_disk(response)
return response
def bar(x: Any) -> None:
return foo(x)
```
With `pybond` you can easily spy on any given function or stub out functions
that perform IO:
```python
# /tests/test_my_module.py
from pybond import calls, called_with_args, spy, stub, times_called
import sample_code.other_package as other_package
import sample_code.my_module as my_module
from sample_code.my_module import bar
def test_foo_is_called():
with spy(my_module.foo):
assert times_called(my_module.foo, 0)
bar(42)
assert times_called(my_module.foo, 1)
bar(42)
bar(42)
assert times_called(my_module.foo, 3)
def test_bar_handles_response():
with stub(
(other_package.make_a_network_request, lambda x: {"result": x * 2}),
(other_package.write_to_disk, lambda _: None),
), spy(
my_module.foo,
):
assert times_called(my_module.foo, 0)
assert times_called(other_package.make_a_network_request, 0)
assert bar(21) == {"result": 42}
assert times_called(my_module.foo, 1)
assert times_called(other_package.make_a_network_request, 1)
assert called_with_args(my_module.foo, args=[21])
assert bar(20) == {"result": 40}
assert calls(my_module.foo) == [
{
"args": [21],
"kwargs": None,
"return": {"result": 42},
"error": None,
},
{
"args": [20],
"kwargs": None,
"return": {"result": 40},
"error": None,
},
]
assert calls(other_package.write_to_disk) == [
{
"args": [{"result": 42}],
"kwargs": None,
"return": None,
"error": None,
},
{
"args": [{"result": 40}],
"kwargs": None,
"return": None,
"error": None,
},
]
```
## License
Distributed under the
[Eclipse Public License](http://www.eclipse.org/legal/epl-v10.html).
Raw data
{
"_id": null,
"home_page": "https://github.com/epgui/pybond",
"name": "pybond",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "",
"keywords": "bond,monkeypatch,spy,stub,clojure,functional programming",
"author": "Guillaume Pelletier",
"author_email": "guigui.p@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/18/9f/17dc6bd9450c161c9b9051eac4cb0f1b037d50100dd33e249b832108cc92/pybond-0.2.2.tar.gz",
"platform": null,
"description": "# pybond\n\n[![Build](https://github.com/epgui/pybond/actions/workflows/build.yml/badge.svg)](https://github.com/epgui/pybond/actions/workflows/build.yml)\n[![codecov](https://codecov.io/github/epgui/pybond/branch/main/graph/badge.svg?token=tkq655ROg3)](https://app.codecov.io/github/epgui/pybond)\n\n`pybond` is a spying and stubbing library inspired heavily by the\n[clojure `bond` library](https://github.com/circleci/bond/).\n\n## Installation\n\npip\n\n```bash\npip install pybond==0.2.2\n```\n\nrequirements.txt\n\n```python\npybond==0.2.2\n```\n\npyproject.toml\n\n```toml\npybond = \"0.2.2\"\n```\n\n## Example usage\n\nLet's say you wanted to test the functions in this module:\n\n```python\n# /sample_code/my_module.py\nfrom typing import Any\n\nimport sample_code.other_package as other_package\n\n\ndef foo(x: Any) -> None:\n response = other_package.make_a_network_request(x)\n other_package.write_to_disk(response)\n return response\n\n\ndef bar(x: Any) -> None:\n return foo(x)\n```\n\nWith `pybond` you can easily spy on any given function or stub out functions\nthat perform IO:\n\n```python\n# /tests/test_my_module.py\nfrom pybond import calls, called_with_args, spy, stub, times_called\n\nimport sample_code.other_package as other_package\nimport sample_code.my_module as my_module\nfrom sample_code.my_module import bar\n\n\ndef test_foo_is_called():\n with spy(my_module.foo):\n assert times_called(my_module.foo, 0)\n bar(42)\n assert times_called(my_module.foo, 1)\n bar(42)\n bar(42)\n assert times_called(my_module.foo, 3)\n\n\ndef test_bar_handles_response():\n with stub(\n (other_package.make_a_network_request, lambda x: {\"result\": x * 2}),\n (other_package.write_to_disk, lambda _: None),\n ), spy(\n my_module.foo,\n ):\n assert times_called(my_module.foo, 0)\n assert times_called(other_package.make_a_network_request, 0)\n assert bar(21) == {\"result\": 42}\n assert times_called(my_module.foo, 1)\n assert times_called(other_package.make_a_network_request, 1)\n assert called_with_args(my_module.foo, args=[21])\n assert bar(20) == {\"result\": 40}\n assert calls(my_module.foo) == [\n {\n \"args\": [21],\n \"kwargs\": None,\n \"return\": {\"result\": 42},\n \"error\": None,\n },\n {\n \"args\": [20],\n \"kwargs\": None,\n \"return\": {\"result\": 40},\n \"error\": None,\n },\n ]\n assert calls(other_package.write_to_disk) == [\n {\n \"args\": [{\"result\": 42}],\n \"kwargs\": None,\n \"return\": None,\n \"error\": None,\n },\n {\n \"args\": [{\"result\": 40}],\n \"kwargs\": None,\n \"return\": None,\n \"error\": None,\n },\n ]\n```\n\n## License\n\nDistributed under the\n[Eclipse Public License](http://www.eclipse.org/legal/epl-v10.html).\n",
"bugtrack_url": null,
"license": "Eclipse Public License - v 1.0",
"summary": "pybond is a spying and stubbing library inspired by the clojure bond library.",
"version": "0.2.2",
"project_urls": {
"Homepage": "https://github.com/epgui/pybond",
"Repository": "https://github.com/epgui/pybond"
},
"split_keywords": [
"bond",
"monkeypatch",
"spy",
"stub",
"clojure",
"functional programming"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "adbb8b98ed990917e1bb9e82f4aa326a9d0d54477bebbbf39f486c73519fa726",
"md5": "d654583c3a30155a5ef3862772b9217f",
"sha256": "a2f75464596cff9b8ed7bb67f56624e9196039fe6d0bee39ce6b1b0d87b26cac"
},
"downloads": -1,
"filename": "pybond-0.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d654583c3a30155a5ef3862772b9217f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 6306,
"upload_time": "2024-03-03T15:20:49",
"upload_time_iso_8601": "2024-03-03T15:20:49.129805Z",
"url": "https://files.pythonhosted.org/packages/ad/bb/8b98ed990917e1bb9e82f4aa326a9d0d54477bebbbf39f486c73519fa726/pybond-0.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "189f17dc6bd9450c161c9b9051eac4cb0f1b037d50100dd33e249b832108cc92",
"md5": "c5316831ac9faa6144aee7c6fbdf1c83",
"sha256": "bc9275dcdb0ccba2bb48995df39fd702cd7d057ba77ab7f411d6ca35ee256bde"
},
"downloads": -1,
"filename": "pybond-0.2.2.tar.gz",
"has_sig": false,
"md5_digest": "c5316831ac9faa6144aee7c6fbdf1c83",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 5160,
"upload_time": "2024-03-03T15:20:50",
"upload_time_iso_8601": "2024-03-03T15:20:50.189294Z",
"url": "https://files.pythonhosted.org/packages/18/9f/17dc6bd9450c161c9b9051eac4cb0f1b037d50100dd33e249b832108cc92/pybond-0.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-03 15:20:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "epgui",
"github_project": "pybond",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pybond"
}