Skip to content

Commit c469879

Browse files
authored
Merge pull request #91 from jwillemsen/jwi-tests
First step for unit tests, enable gzip compression on data
2 parents 0a5c916 + f778ebe commit c469879

File tree

7 files changed

+179
-0
lines changed

7 files changed

+179
-0
lines changed

.github/workflows/tests.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Run unit tests with pytest
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: "0 1 * * SUN"
8+
workflow_dispatch:
9+
10+
jobs:
11+
tests:
12+
runs-on: "ubuntu-latest"
13+
name: Run tests
14+
steps:
15+
- name: Check out code from GitHub
16+
uses: "actions/checkout@v4"
17+
- name: Setup Python
18+
uses: "actions/setup-python@v5"
19+
with:
20+
python-version: "3.11"
21+
- name: Install requirements
22+
run: |
23+
pip install --constraint=.github/workflows/constraints.txt pip
24+
pip install -r requirements_test.txt
25+
- name: Tests suite
26+
run: |
27+
pytest \
28+
--timeout=9 \
29+
--durations=10 \
30+
-n auto \
31+
-p no:sugar \
32+
tests

custom_components/daikin_onecta/daikin_api.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async def doBearerRequest(self, resourceUrl, options=None):
6464
resourceUrl = "https://api.onecta.daikineurope.com" + resourceUrl
6565

6666
headers = {
67+
"Accept-Encoding": "gzip",
6768
"Authorization": "Bearer " + token,
6869
"Content-Type": "application/json",
6970
}

requirements_dev.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pre-commit==3.6.2
2+
black==24.2.0
3+
flake8==7.0.0
4+
reorder-python-imports==3.12.0
5+
homeassistant
6+
pytest

requirements_test.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-r requirements_dev.txt
2+
pytest-homeassistant-custom-component
3+
anyio
4+
pytest-asyncio
5+
pytest-tornasync
6+
pytest-trio
7+
pytest-twisted
8+
aiohttp_cors
9+
twisted
10+
service_identity

setup.cfg

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[flake8]
2+
exclude = .venv,.git,.tox,docs,venv,bin,lib,deps,build
3+
doctests = True
4+
# To work with Black
5+
max-line-length = 88
6+
# E501: line too long
7+
# W503: Line break occurred before a binary operator
8+
# E203: Whitespace before ':'
9+
# D202 No blank lines allowed after function docstring
10+
# W504 line break after binary operator
11+
ignore =
12+
E501,
13+
W503,
14+
E203,
15+
D202,
16+
W504
17+
18+
[isort]
19+
# https://github.com/timothycrosley/isort
20+
# https://github.com/timothycrosley/isort/wiki/isort-Settings
21+
# splits long import on multiple lines indented by 4 spaces
22+
multi_line_output = 3
23+
include_trailing_comma=True
24+
force_grid_wrap=0
25+
use_parentheses=True
26+
line_length=88
27+
indent = " "
28+
# by default isort don't check module indexes
29+
not_skip = __init__.py
30+
# will group `import x` and `from x import` of the same module.
31+
force_sort_within_sections = true
32+
sections = FUTURE,STDLIB,INBETWEENS,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
33+
default_section = THIRDPARTY
34+
known_first_party = custom_components.daikin_onecta, tests
35+
combine_as_imports = true
36+
37+
[tool:pytest]
38+
addopts = -qq --cov=custom_components.daikin_onecta
39+
console_output_style = count
40+
asyncio_mode = auto
41+
42+
[coverage:run]
43+
branch = False
44+
45+
[coverage:report]
46+
show_missing = true
47+
fail_under = 0

tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for the daikin_onecta integration."""

tests/test_config_flow.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""Test the daikin_onecta config flow."""
2+
3+
from unittest.mock import patch
4+
5+
import pytest
6+
7+
from homeassistant import config_entries
8+
from homeassistant.components.application_credentials import (
9+
ClientCredential,
10+
async_import_client_credential,
11+
)
12+
from custom_components.daikin_onecta.const import (
13+
DOMAIN,
14+
OAUTH2_AUTHORIZE,
15+
OAUTH2_TOKEN,
16+
)
17+
from custom_components.daikin_onecta.device import DaikinOnectaDevice
18+
from homeassistant.core import HomeAssistant
19+
from homeassistant.helpers import config_entry_oauth2_flow
20+
from homeassistant.setup import async_setup_component
21+
22+
CLIENT_ID = "1234"
23+
CLIENT_SECRET = "5678"
24+
25+
26+
@pytest.fixture(autouse=True)
27+
async def setup_credentials(hass: HomeAssistant) -> None:
28+
"""Fixture to setup credentials."""
29+
assert await async_setup_component(hass, "application_credentials", {})
30+
# await async_import_client_credential(
31+
# hass,
32+
# DOMAIN,
33+
# ClientCredential(CLIENT_ID, CLIENT_SECRET),
34+
# DOMAIN,
35+
# )
36+
37+
38+
async def test_full_flow(
39+
hass: HomeAssistant,
40+
hass_client_no_auth,
41+
aioclient_mock,
42+
current_request_with_host,
43+
setup_credentials,
44+
) -> None:
45+
"""Check full flow."""
46+
# assert await async_setup_component(hass, "daikin_onecta", {})
47+
#
48+
# await async_import_client_credential(hass, DOMAIN, ClientCredential(CLIENT_ID, CLIENT_SECRET))
49+
#
50+
# result = await hass.config_entries.flow.async_init("daikin_onecta", context={"source": config_entries.SOURCE_USER})
51+
# state = config_entry_oauth2_flow._encode_jwt(
52+
# hass,
53+
# {
54+
# "flow_id": result["flow_id"],
55+
# "redirect_uri": "https://example.com/auth/external/callback",
56+
# },
57+
# )
58+
#
59+
# assert result["url"] == (
60+
# f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" "&redirect_uri=https://example.com/auth/external/callback" f"&state={state}"
61+
# )
62+
#
63+
# client = await hass_client_no_auth()
64+
# resp = await client.get(f"/auth/external/callback?code=abcd&state={state}")
65+
# assert resp.status == 200
66+
# assert resp.headers["content-type"] == "text/html; charset=utf-8"
67+
#
68+
# aioclient_mock.post(
69+
# OAUTH2_TOKEN,
70+
# json={
71+
# "refresh_token": "mock-refresh-token",
72+
# "access_token": "mock-access-token",
73+
# "type": "Bearer",
74+
# "expires_in": 60,
75+
# },
76+
# )
77+
#
78+
# with patch("homeassistant.components.daikin_onecta.async_setup_entry", return_value=True) as mock_setup:
79+
# await hass.config_entries.flow.async_configure(result["flow_id"])
80+
#
81+
# assert len(hass.config_entries.async_entries(DOMAIN)) == 1
82+
# assert len(mock_setup.mock_calls) == 1

0 commit comments

Comments
 (0)