diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4471c71..251359d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -23,4 +23,8 @@ jobs: - name: Check rule format with pytest run: | - pytest -v -s ./test/check_rule_format.py + pytest -v -s -m github ./test/check_rule_format.py + + - name: Unit test with pytest + run: | + pytest -v -s -m github ./test/ diff --git a/custom_components/xiaomi_home/miot/miot_ev.py b/custom_components/xiaomi_home/miot/miot_ev.py index 5d3958e..be4e684 100644 --- a/custom_components/xiaomi_home/miot/miot_ev.py +++ b/custom_components/xiaomi_home/miot/miot_ev.py @@ -240,6 +240,9 @@ class MIoTEventLoop: if fd is None: raise MIoTEvError('invalid params') + if not self._poll_fd: + raise MIoTEvError('event loop not started') + fd_key: str = str(id(fd)) fd_handler = self._fd_handlers.get(fd_key, None) diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..9b4cc3a --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +miot +test_cache \ No newline at end of file diff --git a/test/check_rule_format.py b/test/check_rule_format.py index ca05966..db2d829 100644 --- a/test/check_rule_format.py +++ b/test/check_rule_format.py @@ -3,6 +3,7 @@ import json from os import listdir, path from typing import Optional +import pytest SOURCE_DIR: str = path.dirname(path.abspath(__file__)) @@ -85,6 +86,7 @@ def bool_trans(d: dict) -> bool: return True +@pytest.mark.github def test_bool_trans(): data: dict = load_json_file( path.join( @@ -94,6 +96,7 @@ def test_bool_trans(): assert bool_trans(data) +@pytest.mark.github def test_spec_filter(): data: dict = load_json_file( path.join( @@ -103,6 +106,7 @@ def test_spec_filter(): assert spec_filter(data) +@pytest.mark.github def test_multi_lang(): data: dict = load_json_file( path.join( @@ -112,6 +116,7 @@ def test_multi_lang(): assert nested_3_dict_str_str(data) +@pytest.mark.github def test_miot_i18n(): i18n_path: str = path.join( SOURCE_DIR, '../custom_components/xiaomi_home/miot/i18n') diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..c60e277 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +"""Pytest fixtures.""" +import shutil +import pytest +from os import path, makedirs + +TEST_ROOT_PATH: str = path.dirname(path.abspath(__file__)) + + +@pytest.fixture(scope='session') +def test_lang() -> str: + return 'zh-Hans' + + +@pytest.fixture(scope='session') +def test_root_path() -> str: + return TEST_ROOT_PATH + + +@pytest.fixture(scope='session') +def test_cache_path() -> str: + cache_path: str = path.join(TEST_ROOT_PATH, 'test_cache') + makedirs(cache_path, exist_ok=True) + return cache_path + + +@pytest.fixture(scope='session', autouse=True) +def load_py_file(): + # Copy py file to test folder + file_list = [ + 'common.py', + 'const.py', + 'miot_cloud.py', + 'miot_error.py', + 'miot_ev.py', + 'miot_i18n.py', + 'miot_lan.py', + 'miot_mdns.py', + 'miot_network.py', + 'miot_spec.py', + 'miot_storage.py'] + makedirs(path.join(TEST_ROOT_PATH, 'miot'), exist_ok=True) + for file_name in file_list: + shutil.copyfile( + path.join( + TEST_ROOT_PATH, '../custom_components/xiaomi_home/miot', + file_name), + path.join(TEST_ROOT_PATH, 'miot', file_name)) + print('\nloaded test py file, %s' % file_list) + # Copy spec files to test folder + shutil.copytree( + src=path.join( + TEST_ROOT_PATH, '../custom_components/xiaomi_home/miot/specs'), + dst=path.join(TEST_ROOT_PATH, 'miot/specs'), + dirs_exist_ok=True) + print('\nloaded spec test folder, miot/specs') + # Copy i18n files to test folder + shutil.copytree( + src=path.join( + TEST_ROOT_PATH, '../custom_components/xiaomi_home/miot/i18n'), + dst=path.join(TEST_ROOT_PATH, 'miot/i18n'), + dirs_exist_ok=True) + print('\nloaded i18n test folder, miot/i18n') + yield + # shutil.rmtree(path.join(TEST_ROOT_PATH, 'miot')) + # print('\nremoved test file, %s', file_list) + # shutil.rmtree(path.join(TEST_ROOT_PATH, 'miot/specs')) + # print('\nremoved test folder, %s', 'miot/specs') + # shutil.rmtree(path.join(TEST_ROOT_PATH, 'miot/i18n')) + # print('\nremoved test folder, %s', 'miot/i18n') diff --git a/test/pytest.ini b/test/pytest.ini new file mode 100644 index 0000000..098fdc0 --- /dev/null +++ b/test/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +markers: + github: tests for github actions \ No newline at end of file