Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .cz.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool]
[tool.commitizen]
name = "cz_conventional_commits"
version = "22.0.0"
tag_format = "$version"
version_files = [
'pyproject.toml:version',
'aiohttpretty/__init__.py:__version__',
]
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

* text=auto
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: format
format:
black --line-length 79 aiohttpretty tests
isort --profile black --line-length 79 aiohttpretty tests

.PHONY: test
test:
pytest tests
41 changes: 20 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,31 @@ Purge the registry and the list of intercepted calls.

Checks to see if the given uri was called during the test. By default, will verify that the query params match up. Setting `check_params` to `False` will strip params from the *registered* uri, not the passed-in uri.

### `.open(clear: bool = True)`
Runs `.clear()` if `clear`, `.activate()` on enter and `.deactivate()` on exit.

### `.async_decorate(func)`
Decorator to run `with aiohttpretty.open()` before running coroutine function

### `.decorate(func)`
Same as `.async_decorate()` but for normal functions

## Other

### pytest marker

To simplify usage of `aiohttpretty` in tests, you can make a pytest marker that will automatically activate/deactivate `aiohttpretty` for the scope of a test. To do so, add the following to your `conftest.py`:
To simplify usage of `aiohttpretty` in tests, it already ships with a pytest plugin. To use, add `@pytest.mark.aiohttpretty` to your test:

```
import aiohttpretty
```python
import pytest

def pytest_configure(config):
config.addinivalue_line(
'markers',
'aiohttpretty: mark tests to activate aiohttpretty'
)

def pytest_runtest_setup(item):
marker = item.get_marker('aiohttpretty')
if marker is not None:
aiohttpretty.clear()
aiohttpretty.activate()

def pytest_runtest_teardown(item, nextitem):
marker = item.get_marker('aiohttpretty')
if marker is not None:
aiohttpretty.deactivate()
```
@pytest.mark.aiohttpretty
async def my_test():
# inside test function aiohttpretty is clean and active
assert thing() == other

Then add `@pytest.mark.aiohttpretty` before `async def test_foo`.
@pytes.mark.aiohttpretty
def my_other_test(test_client):
# aiohttpretty also supports sync functions in case of testing web applications
assert test_client.get('/my-route') == other
```
219 changes: 0 additions & 219 deletions aiohttpretty.py

This file was deleted.

4 changes: 4 additions & 0 deletions aiohttpretty/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .main import aiohttpretty

__all__ = ['aiohttpretty']
__version__ = '22.0.0'
21 changes: 21 additions & 0 deletions aiohttpretty/exc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class AioHttPrettyError(Exception):
"""Base class for all AioHttPrettyErrors"""


class NoUrlMatching(AioHttPrettyError, KeyError):
"""No url matches received url with given params and method"""

def __str__(self) -> str:
return Exception.__str__(self)


class ExhaustedAllResponses(AioHttPrettyError, IndexError):
"""No response left for given url"""


class InvalidBody(AioHttPrettyError, TypeError):
"""Received invalid body type"""


class InvalidResponses(AioHttPrettyError, ValueError):
"""Cannot specify params in responses"""
49 changes: 49 additions & 0 deletions aiohttpretty/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
import typing

from aiohttpretty import types

DEFAULT_ENCODING = 'utf-8'


def encode_string(string: str, encoding: str = DEFAULT_ENCODING):
return string.encode(encoding)


def is_stream_like(value: typing.Any):
return hasattr(value, 'read') and asyncio.iscoroutinefunction(value.read)


class StreamLike(typing.Protocol):
async def read(self, n: int) -> bytes:
...

@property
def size(self) -> int:
...


def wrap_content_stream(content: typing.Union[str, bytes, StreamLike]):
if isinstance(content, str):
content = encode_string(content)

if isinstance(content, bytes):
return types.MockStream(content)

return content


def build_raw_headers(headers: typing.Mapping[str, str]):
"""Convert a dict of headers to a tuple of tuples. Mimics the format of ClientResponse.
"""
return tuple(
(encode_string(key), encode_string(value))
for key, value in headers.items()
)


def compare_mapping(
first: typing.Mapping[str, typing.Any],
second: typing.Mapping[str, typing.Any],
):
return all(second.get(key) == value for key, value in first.items())
Loading