mirror of
https://github.com/andrewthetechie/py-healthchecks.io.git
synced 2026-04-23 15:25:35 +02:00
100% coverage of checks schemas
This commit is contained in:
1
tests/__init__.py
Normal file
1
tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Test suite for the healthchecks_io package."""
|
||||
74
tests/conftest.py
Normal file
74
tests/conftest.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
from typing import Dict, Union
|
||||
|
||||
from healthchecks_io.schemas import checks
|
||||
|
||||
@pytest.fixture
|
||||
def fake_check_api_result() -> Dict[str, Union[str, int]]:
|
||||
yield {
|
||||
"name": "Test Check",
|
||||
"slug": "Test Check",
|
||||
"tags": "test check",
|
||||
"desc": "Test Check",
|
||||
"grace": 43200,
|
||||
"n_pings": 76,
|
||||
"status": "up",
|
||||
"last_ping": "2021-12-03T12:30:16+00:00",
|
||||
"next_ping": "2021-12-06T12:30:16+00:00",
|
||||
"manual_resume": False,
|
||||
"methods": "",
|
||||
"ping_url": "testhc.io/ping/8f57a84b-86c2-4246-8923-02f83d17604a",
|
||||
"update_url": "testhc.io/api/v1/checks/8f57a84b-86c2-4246-8923-02f83d17604a",
|
||||
"pause_url": "testhc.io/api/v1/checks/8f57a84b-86c2-4246-8923-02f83d17604a/pause",
|
||||
"channels": "*",
|
||||
"timeout": 259200
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def fake_check_ro_api_result() -> Dict[str, Union[str, int]]:
|
||||
yield {
|
||||
"name": "Test Check",
|
||||
"slug": "Test Check",
|
||||
"tags": "Test Check",
|
||||
"desc": "Test Check",
|
||||
"grace": 600,
|
||||
"n_pings": 1,
|
||||
"status": "up",
|
||||
"last_ping": "2020-03-24T14:02:03+00:00",
|
||||
"next_ping": "2020-03-24T15:02:03+00:00",
|
||||
"manual_resume": False,
|
||||
"methods": "",
|
||||
"unique_key": "a6c7b0a8a66bed0df66abfdab3c77736861703ee",
|
||||
"timeout": 3600
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def fake_check() -> checks.Check:
|
||||
yield checks.Check(
|
||||
name="Test Check",
|
||||
slug="Test Check",
|
||||
tags="test check",
|
||||
desc="Test Check",
|
||||
grace=3600,
|
||||
n_pings=1,
|
||||
status="Test",
|
||||
last_ping=datetime.utcnow(),
|
||||
next_ping=datetime.utcnow(),
|
||||
manual_resume=False,
|
||||
ping_url="testurl.com/ping/test-uuid",
|
||||
update_url="testurl.com/api/v1/checks/test-uuid",
|
||||
pause_url="testurl.com/api/v1/checks/test-uuid/pause",
|
||||
channel="*",
|
||||
timeout=86400,
|
||||
uuid="test-uuid"
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def fake_ro_check(fake_check: checks.Check):
|
||||
fake_check.unique_key = "test-unique-key"
|
||||
fake_check.uuid = None
|
||||
fake_check.ping_url = None
|
||||
fake_check.update_url = None
|
||||
fake_check.pause_url = None
|
||||
yield fake_check
|
||||
84
tests/schemas/test_checks.py
Normal file
84
tests/schemas/test_checks.py
Normal file
@@ -0,0 +1,84 @@
|
||||
import pytest
|
||||
|
||||
from healthchecks_io.schemas import checks
|
||||
from pydantic import ValidationError
|
||||
|
||||
def test_check_from_api_result(fake_check_api_result, fake_check_ro_api_result):
|
||||
check = checks.Check.from_api_result(fake_check_api_result)
|
||||
assert check.name == fake_check_api_result['name']
|
||||
assert check.unique_key is None
|
||||
|
||||
ro_check = checks.Check.from_api_result(fake_check_ro_api_result)
|
||||
assert ro_check.name == fake_check_ro_api_result['name']
|
||||
assert ro_check.unique_key == fake_check_ro_api_result['unique_key']
|
||||
|
||||
|
||||
def test_check_validate_uuid(fake_check_api_result, fake_check_ro_api_result):
|
||||
check = checks.Check.from_api_result(fake_check_api_result)
|
||||
assert check.uuid == '8f57a84b-86c2-4246-8923-02f83d17604a'
|
||||
assert check.unique_key is None
|
||||
|
||||
ro_check = checks.Check.from_api_result(fake_check_ro_api_result)
|
||||
assert ro_check.uuid is None
|
||||
|
||||
def test_check_create_validators():
|
||||
check_create = checks.CheckCreate(
|
||||
name="Test",
|
||||
tags="",
|
||||
desc="Test",
|
||||
schedule="* * * * *",
|
||||
tz="UTC",
|
||||
methods="POST",
|
||||
unique=['name']
|
||||
)
|
||||
assert check_create.schedule == "* * * * *"
|
||||
|
||||
# test validate_schedule
|
||||
with pytest.raises(ValidationError):
|
||||
check_create = checks.CheckCreate(
|
||||
name="Test",
|
||||
tags="",
|
||||
desc="Test",
|
||||
schedule="no good"
|
||||
)
|
||||
|
||||
# test validate_tz
|
||||
with pytest.raises(ValidationError):
|
||||
check_create = checks.CheckCreate(
|
||||
name="Test",
|
||||
tags="",
|
||||
desc="Test",
|
||||
tz="no good"
|
||||
)
|
||||
|
||||
# test validate_methods
|
||||
with pytest.raises(ValidationError):
|
||||
check_create = checks.CheckCreate(
|
||||
name="Test",
|
||||
tags="",
|
||||
desc="Test",
|
||||
methods="no good"
|
||||
)
|
||||
|
||||
# test validate_unique
|
||||
with pytest.raises(ValidationError):
|
||||
check_create = checks.CheckCreate(
|
||||
name="Test",
|
||||
tags="",
|
||||
desc="Test",
|
||||
unique=["no good"]
|
||||
)
|
||||
|
||||
def test_check_pings_from_api():
|
||||
ping = {"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
|
||||
}
|
||||
this_ping = checks.CheckPings.from_api_result(ping)
|
||||
assert this_ping.type == ping['type']
|
||||
assert this_ping.duration == ping['duration']
|
||||
Reference in New Issue
Block a user