fix action Action commit Created package file minor updates Add binary_sensor Updated package file Updated package file Fix BinarySensor Class name Add more descriptions Updated package file Move stuff Updated package file add binary_sensor to resources.json Updated package file Add configuration Updated package file Remove switch Adds CONTRIBUTING Fix broken link Remove refrences Fix links fix broken actions Update actions Updated package file Updated package file Black format Fix config issues Updated package file Fix icon for binary_sensor Add switch platform Update README.md (#1) * Update README.md * Update README.md Update CONTRIBUTING.md (#2) Update __init__.py (#3) * Update __init__.py * Set enabled default to `True` Update const.py (#4) cleanup badges/add style (#5) * shield that supports styling * badge cleanup * Change discord ID Correct typo Create manifest.json (#12) Fixes https://github.com/custom-components/blueprint/issues/10 Move manifest to correct dir Fixes #9 by renaming the file Updates, updates, more updates, worst commit messge ever! revert Update main.workflow Delete resources.json Create example.yaml Update README.md Summer update 😎 Adds info about devcontainer Adds pylint Add translation example remove stalebot Adds integrationhelper adds requirements.txt Adds "tabnine.tabnine-vscode" to devcontainer Add support for config_flow configuration Add config_flow "docs" Update README.md use https for pip install ha@dev (#15) Typo on README.md (#18) Fix various typos in comments (#19) Add hassfest action (#22) * Add hassfest action * Fix manifest issue Spring cleaning ☀️ (#23) * Spring cleaning * Actions * Fix branches * Changes for config_flow Show how to only allow one instance Adds HACS validation action (#24) Update postCreateCommand Update tasks Minor updates (#26) * link correction in README.md and info.md * Add READMME.md for .devcontainer * Add automation.yaml file in the configuration * Complete CONTRIBUTING.md Improve README for container dev and library update. (#27) Feature/setup cfg (#28) * Add setup.cfg * Run black and isort. * Add blueprint to first party. * Make const import consistent. Move translation files Fixes #32 Use CoordinatorEntity (#33) Co-authored-by: Joakim Sørensen <hi@ludeeus.dev> Fixed typos (#34) Fix directory name for translations and link to the documentation (#36) Fix info.md (#40) Use debian based devcontainer (#44) Remove sampleclient (#45) Rename [blueprint|Blueprint] -> [integration_blueprint|Integration blueprint] (#47) Change HACS action (#39) Fix wrong path for link (#48) For an unknown reason the link was pointing to a one of my repository. Probably a too quick copy/paste. Add french translation and strings improvements (#49) Reusing work done on strings.json done in #37 Add example tests (#50) Prepare and explain how to step by step debugging (#51) Add version Fix testing by bumping pytest-homeassistant-custom-component (#54) Fix typo in api.py (#55) passeword -> password Fix a typo in the readme (#56) Update .gitignore to include .idea (#57) Update .gitignore to include .idea for those using Py Charm Add iot_class to manifest Use `enable_custom_integrations` fixture by default (#58) Fix typo (#59) retain user input after an error (#52) Update README.md Closes #61 remove async_timeout.timeout loop arg (#65) Correct name "Blueprint" ->"Integration blueprint" (#64) change entity.py to use extra_state_attributes (#66)
87 lines
3.4 KiB
Python
87 lines
3.4 KiB
Python
"""Tests for integration_blueprint api."""
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from custom_components.integration_blueprint.api import IntegrationBlueprintApiClient
|
|
|
|
|
|
async def test_api(hass, aioclient_mock, caplog):
|
|
"""Test API calls."""
|
|
|
|
# To test the api submodule, we first create an instance of our API client
|
|
api = IntegrationBlueprintApiClient("test", "test", async_get_clientsession(hass))
|
|
|
|
# Use aioclient_mock which is provided by `pytest_homeassistant_custom_components`
|
|
# to mock responses to aiohttp requests. In this case we are telling the mock to
|
|
# return {"test": "test"} when a `GET` call is made to the specified URL. We then
|
|
# call `async_get_data` which will make that `GET` request.
|
|
aioclient_mock.get(
|
|
"https://jsonplaceholder.typicode.com/posts/1", json={"test": "test"}
|
|
)
|
|
assert await api.async_get_data() == {"test": "test"}
|
|
|
|
# We do the same for `async_set_title`. Note the difference in the mock call
|
|
# between the previous step and this one. We use `patch` here instead of `get`
|
|
# because we know that `async_set_title` calls `api_wrapper` with `patch` as the
|
|
# first parameter
|
|
aioclient_mock.patch("https://jsonplaceholder.typicode.com/posts/1")
|
|
assert await api.async_set_title("test") is None
|
|
|
|
# In order to get 100% coverage, we need to test `api_wrapper` to test the code
|
|
# that isn't already called by `async_get_data` and `async_set_title`. Because the
|
|
# only logic that lives inside `api_wrapper` that is not being handled by a third
|
|
# party library (aiohttp) is the exception handling, we also want to simulate
|
|
# raising the exceptions to ensure that the function handles them as expected.
|
|
# The caplog fixture allows access to log messages in tests. This is particularly
|
|
# useful during exception handling testing since often the only action as part of
|
|
# exception handling is a logging statement
|
|
caplog.clear()
|
|
aioclient_mock.put(
|
|
"https://jsonplaceholder.typicode.com/posts/1", exc=asyncio.TimeoutError
|
|
)
|
|
assert (
|
|
await api.api_wrapper("put", "https://jsonplaceholder.typicode.com/posts/1")
|
|
is None
|
|
)
|
|
assert (
|
|
len(caplog.record_tuples) == 1
|
|
and "Timeout error fetching information from" in caplog.record_tuples[0][2]
|
|
)
|
|
|
|
caplog.clear()
|
|
aioclient_mock.post(
|
|
"https://jsonplaceholder.typicode.com/posts/1", exc=aiohttp.ClientError
|
|
)
|
|
assert (
|
|
await api.api_wrapper("post", "https://jsonplaceholder.typicode.com/posts/1")
|
|
is None
|
|
)
|
|
assert (
|
|
len(caplog.record_tuples) == 1
|
|
and "Error fetching information from" in caplog.record_tuples[0][2]
|
|
)
|
|
|
|
caplog.clear()
|
|
aioclient_mock.post("https://jsonplaceholder.typicode.com/posts/2", exc=Exception)
|
|
assert (
|
|
await api.api_wrapper("post", "https://jsonplaceholder.typicode.com/posts/2")
|
|
is None
|
|
)
|
|
assert (
|
|
len(caplog.record_tuples) == 1
|
|
and "Something really wrong happened!" in caplog.record_tuples[0][2]
|
|
)
|
|
|
|
caplog.clear()
|
|
aioclient_mock.post("https://jsonplaceholder.typicode.com/posts/3", exc=TypeError)
|
|
assert (
|
|
await api.api_wrapper("post", "https://jsonplaceholder.typicode.com/posts/3")
|
|
is None
|
|
)
|
|
assert (
|
|
len(caplog.record_tuples) == 1
|
|
and "Error parsing information from" in caplog.record_tuples[0][2]
|
|
)
|