add some client work

This commit is contained in:
Andrew Herrington
2021-12-10 18:05:49 -06:00
parent 90233c568e
commit 2bfece1e56
14 changed files with 428 additions and 19 deletions

View File

@@ -0,0 +1,76 @@
from urllib.parse import urljoin
import pytest
import respx
from httpx import AsyncClient as HTTPXAsyncClient
from httpx import Response
from healthchecks_io.client import AsyncClient
from healthchecks_io.client.exceptions import HCAPIAuthError
from healthchecks_io.client.exceptions import HCAPIError
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_checks_200(fake_check_api_result, respx_mock, test_async_client):
assert test_async_client._client is not None
checks_url = urljoin(test_async_client._api_url, "checks/")
respx_mock.get(checks_url).mock(
return_value=Response(status_code=200, json={"checks": [fake_check_api_result]})
)
checks = await test_async_client.get_checks()
assert len(checks) == 1
assert checks[0].name == fake_check_api_result["name"]
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_checks_pass_in_client(fake_check_api_result, respx_mock):
httpx_client = HTTPXAsyncClient()
test_async_client = AsyncClient(
api_key="test", api_url="http://localhost/api/", client=httpx_client
)
checks_url = urljoin(test_async_client._api_url, "checks/")
respx_mock.get(checks_url).mock(
return_value=Response(status_code=200, json={"checks": [fake_check_api_result]})
)
checks = await test_async_client.get_checks()
assert len(checks) == 1
assert checks[0].name == fake_check_api_result["name"]
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_checks_exceptions(
fake_check_api_result, respx_mock, test_async_client
):
checks_url = urljoin(test_async_client._api_url, "checks/")
# test exceptions
respx_mock.get(checks_url).mock(return_value=Response(status_code=401))
with pytest.raises(HCAPIAuthError):
await test_async_client.get_checks()
respx_mock.get(checks_url).mock(return_value=Response(status_code=500))
with pytest.raises(HCAPIError):
await test_async_client.get_checks()
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_checks_tags(fake_check_api_result, respx_mock, test_async_client):
"""Test get_checks with tags"""
checks_url = urljoin(test_async_client._api_url, "checks/")
respx_mock.get(f"{checks_url}?tag=test&tag=test2").mock(
return_value=Response(status_code=200, json={"checks": [fake_check_api_result]})
)
checks = await test_async_client.get_checks(tags=["test", "test2"])
assert len(checks) == 1
assert checks[0].name == fake_check_api_result["name"]
@pytest.mark.asyncio
def test_finalizer_closes(test_async_client):
"""Tests our finalizer works to close the method"""
assert not test_async_client.is_closed
test_async_client._finalizer_method()
assert test_async_client.is_closed

View File

@@ -4,6 +4,7 @@ from typing import Union
import pytest
from healthchecks_io.client import AsyncClient
from healthchecks_io.schemas import checks
@@ -78,3 +79,10 @@ def fake_ro_check(fake_check: checks.Check):
fake_check.update_url = None
fake_check.pause_url = None
yield fake_check
@pytest.fixture
def test_async_client():
"""An AsyncClient for testing, set to a nonsense url so we aren't pinging healtchecks."""
yield AsyncClient(api_key="test", api_url="https://localhost/api")

View File

@@ -1,14 +1,15 @@
from healthchecks_io.schemas.badges import Badges
def test_badge_from_api_result():
badges_dict = {
"svg": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M-2/backup.svg",
"svg3": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M/backup.svg",
"json": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M-2/backup.json",
"json3": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M/backup.json",
"shields": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M-2/backup.shields",
"shields3": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M/backup.shields"
"svg": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M-2/backup.svg",
"svg3": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M/backup.svg",
"json": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M-2/backup.json",
"json3": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M/backup.json",
"shields": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M-2/backup.shields",
"shields3": "https://healthchecks.io/badge/67541b37-8b9c-4d17-b952-690eae/LOegDs5M/backup.shields",
}
this_badge = Badges.from_api_result(badges_dict)
assert this_badge.svg == badges_dict['svg']
assert this_badge.json_url == badges_dict['json']
assert this_badge.svg == badges_dict["svg"]
assert this_badge.json_url == badges_dict["json"]

View File

@@ -1,11 +1,12 @@
from healthchecks_io.schemas.integrations import Integration
def test_badge_from_api_result():
int_dict = {
"id": "4ec5a071-2d08-4baa-898a-eb4eb3cd6941",
"name": "My Work Email",
"kind": "email"
int_dict = {
"id": "4ec5a071-2d08-4baa-898a-eb4eb3cd6941",
"name": "My Work Email",
"kind": "email",
}
this_integration = Integration.from_api_result(int_dict)
assert this_integration.id == int_dict['id']
assert this_integration.name == int_dict['name']
assert this_integration.id == int_dict["id"]
assert this_integration.name == int_dict["name"]