This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(tests): tests for endpoint_decorator.py * chore(tests): tests for http.py * chore(tests): linting * chore(tests): tests for init_plugin.py * chore(tests): tests cleaning up various missing.
- Loading branch information
1 parent
a7ff981
commit b1cc226
Showing
9 changed files
with
228 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Tests for endpoint_decorator.py.""" | ||
# pylint: disable=too-few-public-methods | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from starlite_saqlalchemy import endpoint_decorator | ||
|
||
|
||
def test_endpoint_decorator() -> None: | ||
"""Test for basic functionality.""" | ||
|
||
@endpoint_decorator.endpoint(base_url="/something") | ||
class Endpoint: | ||
"""Endpoints for something.""" | ||
|
||
root = "" | ||
nested = "/somewhere" | ||
|
||
assert Endpoint.root == "/something/" | ||
assert Endpoint.nested == "/something/somewhere" | ||
|
||
|
||
def test_endpoint_decorator_raises_if_no_base_url() -> None: | ||
"""Test raising behavior when no base_url provided.""" | ||
with pytest.raises(RuntimeError): | ||
|
||
@endpoint_decorator.endpoint | ||
class Endpoint: # pylint: disable=unused-variable | ||
"""whoops.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Tests for http.py.""" | ||
# pylint: disable=protected-access | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from unittest.mock import AsyncMock, MagicMock | ||
|
||
import httpx | ||
import pytest | ||
|
||
from starlite_saqlalchemy import http | ||
|
||
if TYPE_CHECKING: | ||
|
||
from pytest import MonkeyPatch | ||
|
||
|
||
async def test_client_request(monkeypatch: MonkeyPatch) -> None: | ||
"""Tests logic of request() method.""" | ||
response_mock = MagicMock() | ||
request_mock = AsyncMock(return_value=response_mock) | ||
monkeypatch.setattr(http.Client._client, "request", request_mock) | ||
res = await http.Client().request("with", "args", and_some="kwargs") | ||
request_mock.assert_called_once_with("with", "args", and_some="kwargs") | ||
response_mock.raise_for_status.assert_called_once() | ||
assert res is response_mock | ||
|
||
|
||
async def test_client_raises_client_exception(monkeypatch: MonkeyPatch) -> None: | ||
"""Tests that we convert httpx exceptions into ClientException.""" | ||
exc = httpx.HTTPError("a message") | ||
req = AsyncMock(side_effect=exc) | ||
req.url = "http://whatever.com" | ||
exc.request = req | ||
monkeypatch.setattr(http.Client._client, "request", req) | ||
with pytest.raises(http.ClientException): | ||
await http.Client().request() | ||
|
||
|
||
def test_client_json() -> None: | ||
"""Tests the json() and unwrap_json() passthrough.""" | ||
resp = MagicMock() | ||
resp.json.return_value = {"data": "data"} | ||
assert http.Client().json(resp) == {"data": "data"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"""Tests for init_plugin.py.""" | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from starlite import Starlite | ||
from starlite.cache import SimpleCacheBackend | ||
|
||
from starlite_saqlalchemy import init_plugin | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
|
||
from pytest import MonkeyPatch | ||
|
||
|
||
def test_config_switches() -> None: | ||
"""Tests that the app produced with all config switches off is as we | ||
expect.""" | ||
config = init_plugin.PluginConfig( | ||
do_after_exception=False, | ||
do_cache=False, | ||
do_compression=False, | ||
# pyright reckons this parameter doesn't exist, I beg to differ | ||
do_collection_dependencies=False, # pyright:ignore | ||
do_exception_handlers=False, | ||
do_health_check=False, | ||
do_logging=False, | ||
do_openapi=False, | ||
do_response_class=False, | ||
do_sentry=False, | ||
do_sqlalchemy_plugin=False, | ||
do_worker=False, | ||
) | ||
app = Starlite( | ||
route_handlers=[], | ||
openapi_config=None, | ||
on_app_init=[init_plugin.ConfigureApp(config=config)], | ||
) | ||
assert app.compression_config is None | ||
assert app.logging_config is None | ||
assert app.openapi_config is None | ||
assert app.response_class is None | ||
assert isinstance(app.cache.backend, SimpleCacheBackend) | ||
# client.close and redis.close go in there unconditionally atm | ||
assert len(app.on_shutdown) == 2 | ||
assert not app.after_exception | ||
assert not app.dependencies | ||
assert not app.exception_handlers | ||
assert not app.on_startup | ||
assert not app.plugins | ||
assert not app.routes | ||
|
||
|
||
def test_do_worker_but_not_logging(monkeypatch: MonkeyPatch) -> None: | ||
"""Tests branch where we can have the worker enabled, but logging | ||
disabled.""" | ||
mock = MagicMock() | ||
monkeypatch.setattr(init_plugin, "create_worker_instance", mock) | ||
config = init_plugin.PluginConfig(do_logging=False) | ||
Starlite(route_handlers=[], on_app_init=[init_plugin.ConfigureApp(config=config)]) | ||
mock.assert_called_once() | ||
call = mock.mock_calls[0] | ||
assert "before_process" not in call.kwargs | ||
assert "after_process" not in call.kwargs | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("in_", "out"), | ||
[ | ||
(["something"], ["something"]), | ||
("something", ["something"]), | ||
([], []), | ||
(None, []), | ||
], | ||
) | ||
def test_ensure_list(in_: Any, out: Any) -> None: | ||
"""Test _ensure_list() functionality.""" | ||
# pylint: disable=protected-access | ||
assert init_plugin.ConfigureApp._ensure_list(in_) == out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters