moqpy


Namemoqpy JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/omlnaut/pymoq
SummaryExtending unittest-mock with moq-like validators
upload_time2023-03-19 09:59:10
maintainer
docs_urlNone
authoromlnaut
requires_python>=3.10
licenseApache Software License 2.0
keywords nbdev jupyter notebook python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            pymoq
================

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
from pymoq.all import *
```

Following the end-to-end
[tutorial](https://nbdev.fast.ai/Tutorials/tutorial.html) for nbdev.

Project homepage: [github](https://github.com/omlnaut/pymoq)

Full documentation: [documentation](https://omlnaut.github.io/pymoq/)

## Install

``` sh
pip install pymoq
```

## Quickstart

Suppose we have the following setup in a python backend.

``` python
from typing import Protocol

class IWeb(Protocol):
    "Interface for accessing internet resources"
    
    def get(self, url:str, page:int, verbose:bool=False) -> str:
        "Fetches the ressource at `url` and returns it in string representation"
```

``` python
class RessourceFetcher:
    base_url: str = "https://some_base.com/"
    
    def __init__(self, web: IWeb):
        self._web = web
    
    def check_ressource(self, ressource_name: str, page:int, verbose:bool=False) -> bool:
        url = self.base_url + ressource_name
        ressource = self._web.get(url, page, verbose)
        
        return ressource is not None
```

We want to test the `fetch_ressource` method of `RessourceFetcher`. More
specifically, we want to test that if the ressource is correctly
returned from the source, this method should return `True`, otherwise
`False`.

### Setting up the mock

``` python
mock = Mock(IWeb)
mock.get\
    .setup('https://some_base.com/ressource', int, False)\
    .returns(True)

fetcher = RessourceFetcher(mock)
```

If the call matches the siganture defined in the `setup` method, the
lambda in `returns` is called and its return value is returned:

``` python
assert fetcher.check_ressource('ressource', 1)
```

If any part of the signature does not match, `None` is returned:

``` python
assert not fetcher.check_ressource('other_ressource', 1) # wrong ressource name
assert not fetcher.check_ressource('ressource', "1") # wrong type of page argument
assert not fetcher.check_ressource('ressource', "1", verbose=True) # wrong value for verbose argument
```

### Verification

One might want to check how often a function mock was invoked with a
specific call signature. This can easily be done via the `.verify`
method:

``` python
mock = Mock(IWeb)
fetcher = RessourceFetcher(mock)

# setup
mock.get.setup(str, int, bool).returns(True)

# act
fetcher.check_ressource('ressource', 1)
fetcher.check_ressource('ressource', 2)
fetcher.check_ressource('ressource', 1, verbose=True)

# assert
mock.get.verify(str, int, bool).times(3)
mock.get.verify(str, int, bool).more_than(1)
mock.get.verify(str, int, bool).more_than_or_equal_to(3)
mock.get.verify(str, int, bool).less_than(4)
mock.get.verify(str, int, bool).less_than_or_equal_to(3)
mock.get.verify(str, str).never()

mock.get.verify(str, AnyInt('page', 2).less_than(2), bool).times(2)
```

### Setup sequences

``` python
mock = Mock(IWeb)
mock.get.setup('resource', int, bool).returns_sequence([1,2,3])

assert mock.get('resource', 1, True)==1
assert mock.get('resource', 2, False)==2
assert mock.get('resource', 3, True)==3

print(mock.get('ressource', 1, True))
```

    None

### Setup exceptions

``` python
class WebException(Exception):
    """Exception that describes web-access errors"""
```

``` python
mock = Mock(IWeb)
fetcher = RessourceFetcher(mock)

# setup failing web call
mock.get.setup('https://some_base.com/unavailable_ressource', int, bool).throws(WebException())

# act and assert exception
with pytest.raises(WebException):
    fetcher.check_ressource('unavailable_ressource', 1, True)
    
# does not raise exception if call signature does not match
fetcher.check_ressource('available_ressource', 1, True);
```

## Deep Dive

Refer to [General
Structure](https://omlnaut.github.io/pymoq/doc/general.html) for more
detail.

# Things to add

- publish on pypi, conda

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/omlnaut/pymoq",
    "name": "moqpy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "nbdev jupyter notebook python",
    "author": "omlnaut",
    "author_email": "oneironaut.oml@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/0e/47/c2ba17b6a83f1a5bfce3f29034d0b0efd18697d0076440e0e9ef5a67830f/moqpy-0.0.2.tar.gz",
    "platform": null,
    "description": "pymoq\n================\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n``` python\nfrom pymoq.all import *\n```\n\nFollowing the end-to-end\n[tutorial](https://nbdev.fast.ai/Tutorials/tutorial.html) for nbdev.\n\nProject homepage: [github](https://github.com/omlnaut/pymoq)\n\nFull documentation: [documentation](https://omlnaut.github.io/pymoq/)\n\n## Install\n\n``` sh\npip install pymoq\n```\n\n## Quickstart\n\nSuppose we have the following setup in a python backend.\n\n``` python\nfrom typing import Protocol\n\nclass IWeb(Protocol):\n    \"Interface for accessing internet resources\"\n    \n    def get(self, url:str, page:int, verbose:bool=False) -> str:\n        \"Fetches the ressource at `url` and returns it in string representation\"\n```\n\n``` python\nclass RessourceFetcher:\n    base_url: str = \"https://some_base.com/\"\n    \n    def __init__(self, web: IWeb):\n        self._web = web\n    \n    def check_ressource(self, ressource_name: str, page:int, verbose:bool=False) -> bool:\n        url = self.base_url + ressource_name\n        ressource = self._web.get(url, page, verbose)\n        \n        return ressource is not None\n```\n\nWe want to test the `fetch_ressource` method of `RessourceFetcher`. More\nspecifically, we want to test that if the ressource is correctly\nreturned from the source, this method should return `True`, otherwise\n`False`.\n\n### Setting up the mock\n\n``` python\nmock = Mock(IWeb)\nmock.get\\\n    .setup('https://some_base.com/ressource', int, False)\\\n    .returns(True)\n\nfetcher = RessourceFetcher(mock)\n```\n\nIf the call matches the siganture defined in the `setup` method, the\nlambda in `returns` is called and its return value is returned:\n\n``` python\nassert fetcher.check_ressource('ressource', 1)\n```\n\nIf any part of the signature does not match, `None` is returned:\n\n``` python\nassert not fetcher.check_ressource('other_ressource', 1) # wrong ressource name\nassert not fetcher.check_ressource('ressource', \"1\") # wrong type of page argument\nassert not fetcher.check_ressource('ressource', \"1\", verbose=True) # wrong value for verbose argument\n```\n\n### Verification\n\nOne might want to check how often a function mock was invoked with a\nspecific call signature. This can easily be done via the `.verify`\nmethod:\n\n``` python\nmock = Mock(IWeb)\nfetcher = RessourceFetcher(mock)\n\n# setup\nmock.get.setup(str, int, bool).returns(True)\n\n# act\nfetcher.check_ressource('ressource', 1)\nfetcher.check_ressource('ressource', 2)\nfetcher.check_ressource('ressource', 1, verbose=True)\n\n# assert\nmock.get.verify(str, int, bool).times(3)\nmock.get.verify(str, int, bool).more_than(1)\nmock.get.verify(str, int, bool).more_than_or_equal_to(3)\nmock.get.verify(str, int, bool).less_than(4)\nmock.get.verify(str, int, bool).less_than_or_equal_to(3)\nmock.get.verify(str, str).never()\n\nmock.get.verify(str, AnyInt('page', 2).less_than(2), bool).times(2)\n```\n\n### Setup sequences\n\n``` python\nmock = Mock(IWeb)\nmock.get.setup('resource', int, bool).returns_sequence([1,2,3])\n\nassert mock.get('resource', 1, True)==1\nassert mock.get('resource', 2, False)==2\nassert mock.get('resource', 3, True)==3\n\nprint(mock.get('ressource', 1, True))\n```\n\n    None\n\n### Setup exceptions\n\n``` python\nclass WebException(Exception):\n    \"\"\"Exception that describes web-access errors\"\"\"\n```\n\n``` python\nmock = Mock(IWeb)\nfetcher = RessourceFetcher(mock)\n\n# setup failing web call\nmock.get.setup('https://some_base.com/unavailable_ressource', int, bool).throws(WebException())\n\n# act and assert exception\nwith pytest.raises(WebException):\n    fetcher.check_ressource('unavailable_ressource', 1, True)\n    \n# does not raise exception if call signature does not match\nfetcher.check_ressource('available_ressource', 1, True);\n```\n\n## Deep Dive\n\nRefer to [General\nStructure](https://omlnaut.github.io/pymoq/doc/general.html) for more\ndetail.\n\n# Things to add\n\n- publish on pypi, conda\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "Extending unittest-mock with moq-like validators",
    "version": "0.0.2",
    "split_keywords": [
        "nbdev",
        "jupyter",
        "notebook",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7513808f8e370b53879f2611e749fa10504a0e429456146c35738a053d530dcc",
                "md5": "981f463d40f5c607a73bb795d5fd598d",
                "sha256": "5b116bd24ea290b3bb4f8fe4822fa2076051f6e5417d6ec43dc9f1d8635ea42d"
            },
            "downloads": -1,
            "filename": "moqpy-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "981f463d40f5c607a73bb795d5fd598d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 15744,
            "upload_time": "2023-03-19T09:59:08",
            "upload_time_iso_8601": "2023-03-19T09:59:08.382826Z",
            "url": "https://files.pythonhosted.org/packages/75/13/808f8e370b53879f2611e749fa10504a0e429456146c35738a053d530dcc/moqpy-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0e47c2ba17b6a83f1a5bfce3f29034d0b0efd18697d0076440e0e9ef5a67830f",
                "md5": "4fc83c0a3b73ee62fb3520659eedce47",
                "sha256": "5008297491bb70ae93dc61b1f77658625cdde23863edd45fd6f0397a8eb0e6f9"
            },
            "downloads": -1,
            "filename": "moqpy-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "4fc83c0a3b73ee62fb3520659eedce47",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 14359,
            "upload_time": "2023-03-19T09:59:10",
            "upload_time_iso_8601": "2023-03-19T09:59:10.698535Z",
            "url": "https://files.pythonhosted.org/packages/0e/47/c2ba17b6a83f1a5bfce3f29034d0b0efd18697d0076440e0e9ef5a67830f/moqpy-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-19 09:59:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "omlnaut",
    "github_project": "pymoq",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "moqpy"
}
        
Elapsed time: 0.04295s