check-patroni/tests/tools.py

28 lines
770 B
Python
Raw Normal View History

import json
2021-08-11 19:09:14 +02:00
import pathlib
from pytest_mock import MockerFixture
from typing import Any
2021-08-11 19:09:14 +02:00
from check_patroni.types import APIError, PatroniResource
2021-08-11 19:09:14 +02:00
here = pathlib.Path(__file__).parent
def getjson(name: str) -> Any:
2021-08-11 19:09:14 +02:00
path = here / "json" / f"{name}.json"
2021-08-13 10:59:44 +02:00
if not path.exists():
raise Exception(f"path does not exist : {path}")
2021-08-13 10:59:44 +02:00
2021-08-11 19:09:14 +02:00
with path.open() as f:
return json.load(f)
2021-08-11 19:09:14 +02:00
def my_mock(mocker: MockerFixture, json_file: str, status: int) -> None:
def mock_rest_api(self: PatroniResource, service: str) -> Any:
if status != 200:
raise APIError("Test en erreur pour status code 200")
return getjson(json_file)
2021-08-11 19:09:14 +02:00
2021-08-13 10:59:44 +02:00
mocker.resetall()
2021-08-11 19:09:14 +02:00
mocker.patch("check_patroni.types.PatroniResource.rest_api", mock_rest_api)