Files
ha_xiaomi_home/test/conftest.py

173 lines
4.6 KiB
Python
Raw Normal View History

2024-12-13 16:51:09 +08:00
# -*- coding: utf-8 -*-
"""Pytest fixtures."""
import logging
import random
2024-12-13 16:51:09 +08:00
import shutil
import pytest
from os import path, makedirs
from uuid import uuid4
2024-12-13 16:51:09 +08:00
TEST_ROOT_PATH: str = path.dirname(path.abspath(__file__))
2024-12-16 09:37:06 +08:00
TEST_FILES_PATH: str = path.join(TEST_ROOT_PATH, 'miot')
2024-12-13 21:07:43 +08:00
TEST_CACHE_PATH: str = path.join(TEST_ROOT_PATH, 'test_cache')
TEST_OAUTH2_REDIRECT_URL: str = 'http://homeassistant.local:8123'
2024-12-13 18:08:10 +08:00
TEST_LANG: str = 'zh-Hans'
TEST_UID: str = '123456789'
TEST_CLOUD_SERVER: str = 'cn'
2024-12-13 16:51:09 +08:00
DOMAIN_CLOUD_CACHE: str = 'cloud_cache'
_LOGGER = logging.getLogger(__name__)
@pytest.fixture(scope='session', autouse=True)
def set_logger():
logger = logging.getLogger()
logger.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
_LOGGER.info('set logger, %s', logger)
2024-12-13 16:51:09 +08:00
@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_i18n.py',
'miot_lan.py',
'miot_mdns.py',
'miot_mips.py',
2024-12-13 16:51:09 +08:00
'miot_network.py',
'miot_spec.py',
'miot_storage.py']
2024-12-16 09:37:06 +08:00
makedirs(TEST_CACHE_PATH, exist_ok=True)
makedirs(TEST_FILES_PATH, exist_ok=True)
2024-12-13 16:51:09 +08:00
for file_name in file_list:
shutil.copyfile(
path.join(
TEST_ROOT_PATH, '../custom_components/xiaomi_home/miot',
file_name),
2024-12-16 09:37:06 +08:00
path.join(TEST_FILES_PATH, file_name))
_LOGGER.info('\nloaded test py files, %s', file_list)
2024-12-13 16:51:09 +08:00
# Copy spec files to test folder
shutil.copytree(
src=path.join(
TEST_ROOT_PATH, '../custom_components/xiaomi_home/miot/specs'),
2024-12-16 09:37:06 +08:00
dst=path.join(TEST_FILES_PATH, 'specs'),
2024-12-13 16:51:09 +08:00
dirs_exist_ok=True)
_LOGGER.info('loaded spec test folder, specs')
# Copy lan files to test folder
shutil.copytree(
src=path.join(
TEST_ROOT_PATH, '../custom_components/xiaomi_home/miot/lan'),
dst=path.join(TEST_FILES_PATH, 'lan'),
dirs_exist_ok=True)
_LOGGER.info('loaded lan test folder, lan')
2024-12-13 16:51:09 +08:00
# Copy i18n files to test folder
shutil.copytree(
src=path.join(
TEST_ROOT_PATH, '../custom_components/xiaomi_home/miot/i18n'),
2024-12-16 09:37:06 +08:00
dst=path.join(TEST_FILES_PATH, 'i18n'),
2024-12-13 16:51:09 +08:00
dirs_exist_ok=True)
_LOGGER.info('loaded i18n test folder, i18n')
2024-12-13 21:07:43 +08:00
2024-12-13 16:51:09 +08:00
yield
2024-12-13 21:07:43 +08:00
# NOTICE: All test files and data (tokens, device information, etc.) will
# be deleted after the test is completed. For some test cases that
# require caching data, you can comment out the following code.
2024-12-16 09:37:06 +08:00
if path.exists(TEST_FILES_PATH):
shutil.rmtree(TEST_FILES_PATH)
print('\nremoved test files, ', TEST_FILES_PATH)
2024-12-13 21:07:43 +08:00
2024-12-16 09:37:06 +08:00
if path.exists(TEST_CACHE_PATH):
shutil.rmtree(TEST_CACHE_PATH)
print('removed test cache, ', TEST_CACHE_PATH)
2024-12-13 18:08:10 +08:00
@pytest.fixture(scope='session')
def test_root_path() -> str:
return TEST_ROOT_PATH
@pytest.fixture(scope='session')
def test_cache_path() -> str:
2024-12-13 21:07:43 +08:00
makedirs(TEST_CACHE_PATH, exist_ok=True)
return TEST_CACHE_PATH
2024-12-13 18:08:10 +08:00
@pytest.fixture(scope='session')
def test_oauth2_redirect_url() -> str:
return TEST_OAUTH2_REDIRECT_URL
2024-12-13 18:08:10 +08:00
@pytest.fixture(scope='session')
def test_lang() -> str:
return TEST_LANG
@pytest.fixture(scope='session')
def test_uid() -> str:
return TEST_UID
@pytest.fixture(scope='session')
def test_random_did() -> str:
# Gen random did
return str(random.getrandbits(64))
@pytest.fixture(scope='session')
def test_uuid() -> str:
# Gen uuid
return uuid4().hex
2024-12-13 18:08:10 +08:00
@pytest.fixture(scope='session')
def test_cloud_server() -> str:
return TEST_CLOUD_SERVER
@pytest.fixture(scope='session')
def test_domain_cloud_cache() -> str:
return DOMAIN_CLOUD_CACHE
@pytest.fixture(scope='session')
def test_name_oauth2_info() -> str:
return f'{TEST_CLOUD_SERVER}_oauth2_info'
@pytest.fixture(scope='session')
def test_name_uid() -> str:
return f'{TEST_CLOUD_SERVER}_uid'
@pytest.fixture(scope='session')
def test_name_uuid() -> str:
return f'{TEST_CLOUD_SERVER}_uuid'
@pytest.fixture(scope='session')
def test_name_rd_did() -> str:
return f'{TEST_CLOUD_SERVER}_rd_did'
@pytest.fixture(scope='session')
def test_name_homes() -> str:
return f'{TEST_CLOUD_SERVER}_homes'
@pytest.fixture(scope='session')
def test_name_devices() -> str:
return f'{TEST_CLOUD_SERVER}_devices'