Add base model for HASS API JSON objects representation

This commit is contained in:
Vadim Titov
2021-04-03 18:35:22 +01:00
parent 205b031892
commit 494718f934
4 changed files with 39 additions and 1 deletions

View File

@@ -47,7 +47,7 @@ class BaseClient(AuthenticatedClient):
url=self._get_url(endpoint),
headers=self._headers,
timeout=self._timeout,
params={**(params or {}), **kwargs} or None
params={**(params or {}), **kwargs} or None,
)
)

1
tests/client/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""Tests for client subpackage."""

1
tests/models/__init__.py Normal file
View File

@@ -0,0 +1 @@
"""Tests for models subpackage."""

36
tests/models/test_base.py Normal file
View File

@@ -0,0 +1,36 @@
from unittest.mock import patch
import pytest
from hassapi.models import base
class DummyModel(base.Model):
pass
class DummyModelList(base.ModelList):
def __init__(self):
super().__init__(("a", "b"))
@patch("hassapi.models.base.asdict")
@patch("hassapi.models.base._repr_dict", return_value="dummy field repr")
def test_model_repr(mock_repr_dict, mock_asdict):
assert repr(DummyModel()) == "DummyModel with fields:\ndummy field repr"
def test_model_list_repr():
assert repr(DummyModelList()) == "DummyModelList with items: [\n- 'a'\n\n\n- 'b'\n\n\n]"
def test_repr_dict():
input_dict = {"a": "b", "c": {"d": "e", "f": {"g": "h"}}}
expected_lines = (
" a: b",
" c: ",
" d: e",
" f: ",
" g: h",
)
assert base._repr_dict(input_dict) == "\n".join(expected_lines)