more api methods

This commit is contained in:
Andrew Herrington
2021-12-10 22:42:03 -06:00
parent 15d1d41340
commit 7c3f263b5a
6 changed files with 284 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ from httpx import Response
from healthchecks_io.client import AsyncClient
from healthchecks_io.client.exceptions import HCAPIAuthError
from healthchecks_io.client.exceptions import HCAPIError, CheckNotFoundError
from healthchecks_io.client.exceptions import HCAPIError, CheckNotFoundError, BadAPIRequestError
@pytest.mark.asyncio
@@ -98,3 +98,101 @@ async def test_check_get_404(respx_mock, test_async_client):
)
with pytest.raises(CheckNotFoundError):
await test_async_client.get_check("test")
@pytest.mark.asyncio
@pytest.mark.respx
async def test_pause_check_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/test/pause")
respx_mock.post(checks_url).mock(
return_value=Response(status_code=200, json=fake_check_api_result)
)
check = await test_async_client.pause_check(check_id="test")
assert check.name == fake_check_api_result["name"]
@pytest.mark.asyncio
@pytest.mark.respx
async def test_check_pause_404(respx_mock, test_async_client):
assert test_async_client._client is not None
checks_url = urljoin(test_async_client._api_url, "checks/test/pause")
respx_mock.post(checks_url).mock(
return_value=Response(status_code=404)
)
with pytest.raises(CheckNotFoundError):
await test_async_client.pause_check("test")
@pytest.mark.asyncio
@pytest.mark.respx
async def test_delete_check_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/test")
respx_mock.delete(checks_url).mock(
return_value=Response(status_code=200, json=fake_check_api_result)
)
check = await test_async_client.delete_check(check_id="test")
assert check.name == fake_check_api_result["name"]
@pytest.mark.asyncio
@pytest.mark.respx
async def test_delete_pause404(respx_mock, test_async_client):
assert test_async_client._client is not None
checks_url = urljoin(test_async_client._api_url, "checks/test")
respx_mock.delete(checks_url).mock(
return_value=Response(status_code=404)
)
with pytest.raises(CheckNotFoundError):
await test_async_client.delete_check("test")
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_check_pings_200(fake_check_pings_api_result, respx_mock, test_async_client):
assert test_async_client._client is not None
checks_url = urljoin(test_async_client._api_url, "checks/test/pings/")
respx_mock.get(checks_url).mock(
return_value=Response(status_code=200, json={"pings": fake_check_pings_api_result})
)
pings = await test_async_client.get_check_pings("test")
assert len(pings) == len(fake_check_pings_api_result)
assert pings[0].type == fake_check_pings_api_result[0]['type']
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_check_flips_200(fake_check_flips_api_result, respx_mock, test_async_client):
assert test_async_client._client is not None
checks_url = urljoin(test_async_client._api_url, "checks/test/flips/")
respx_mock.get(checks_url).mock(
return_value=Response(status_code=200, json=fake_check_flips_api_result)
)
flips = await test_async_client.get_check_flips("test")
assert len(flips) == len(fake_check_flips_api_result)
assert flips[0].up == fake_check_flips_api_result[0]['up']
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_check_flips_params_200(fake_check_flips_api_result, respx_mock, test_async_client):
assert test_async_client._client is not None
checks_url = urljoin(test_async_client._api_url, "checks/test/flips/?seconds=1&start=1&end=1")
respx_mock.get(checks_url).mock(
return_value=Response(status_code=200, json=fake_check_flips_api_result)
)
flips = await test_async_client.get_check_flips("test", seconds=1, start=1, end=1)
assert len(flips) == len(fake_check_flips_api_result)
assert flips[0].up == fake_check_flips_api_result[0]['up']
@pytest.mark.asyncio
@pytest.mark.respx
async def test_get_check_flips_400(fake_check_flips_api_result, respx_mock, test_async_client):
assert test_async_client._client is not None
checks_url = urljoin(test_async_client._api_url, "checks/test/flips/")
respx_mock.get(checks_url).mock(
return_value=Response(status_code=400)
)
with pytest.raises(BadAPIRequestError):
await test_async_client.get_check_flips("test")

View File

@@ -86,3 +86,63 @@ 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")
@pytest.fixture
def fake_check_pings_api_result():
return [
{
"type": "success",
"date": "2020-06-09T14:51:06.113073+00:00",
"n": 4,
"scheme": "http",
"remote_addr": "192.0.2.0",
"method": "GET",
"ua": "curl/7.68.0",
"duration": 2.896736
},
{
"type": "start",
"date": "2020-06-09T14:51:03.216337+00:00",
"n": 3,
"scheme": "http",
"remote_addr": "192.0.2.0",
"method": "GET",
"ua": "curl/7.68.0"
},
{
"type": "success",
"date": "2020-06-09T14:50:59.633577+00:00",
"n": 2,
"scheme": "http",
"remote_addr": "192.0.2.0",
"method": "GET",
"ua": "curl/7.68.0",
"duration": 2.997976
},
{
"type": "start",
"date": "2020-06-09T14:50:56.635601+00:00",
"n": 1,
"scheme": "http",
"remote_addr": "192.0.2.0",
"method": "GET",
"ua": "curl/7.68.0"
}
]
@pytest.fixture
def fake_check_flips_api_result():
return [
{
"timestamp": "2020-03-23T10:18:23+00:00",
"up": 1
},
{
"timestamp": "2020-03-23T10:17:15+00:00",
"up": 0
},
{
"timestamp": "2020-03-23T10:16:18+00:00",
"up": 1
}
]