test: check_rule_format.py use constant

This commit is contained in:
topsworld 2024-12-19 13:53:53 +08:00
parent b93d8631b8
commit 2d6387c30a

View File

@ -6,7 +6,9 @@ from typing import Optional
import pytest import pytest
import yaml import yaml
SOURCE_DIR: str = path.dirname(path.abspath(__file__)) SOURCE_PATH: str = path.dirname(path.abspath(__file__))
TRANS_RELATIVE_PATH: str = '../custom_components/xiaomi_home/translations'
MIOT_I18N_RELATIVE_PATH: str = '../custom_components/xiaomi_home/miot/i18n'
def load_json_file(file_path: str) -> Optional[dict]: def load_json_file(file_path: str) -> Optional[dict]:
@ -127,7 +129,7 @@ def compare_dict_structure(dict1: dict, dict2: dict) -> bool:
def test_bool_trans(): def test_bool_trans():
data: dict = load_json_file( data: dict = load_json_file(
path.join( path.join(
SOURCE_DIR, SOURCE_PATH,
'../custom_components/xiaomi_home/miot/specs/bool_trans.json')) '../custom_components/xiaomi_home/miot/specs/bool_trans.json'))
assert data assert data
assert bool_trans(data) assert bool_trans(data)
@ -137,7 +139,7 @@ def test_bool_trans():
def test_spec_filter(): def test_spec_filter():
data: dict = load_json_file( data: dict = load_json_file(
path.join( path.join(
SOURCE_DIR, SOURCE_PATH,
'../custom_components/xiaomi_home/miot/specs/spec_filter.json')) '../custom_components/xiaomi_home/miot/specs/spec_filter.json'))
assert data assert data
assert spec_filter(data) assert spec_filter(data)
@ -147,7 +149,7 @@ def test_spec_filter():
def test_multi_lang(): def test_multi_lang():
data: dict = load_json_file( data: dict = load_json_file(
path.join( path.join(
SOURCE_DIR, SOURCE_PATH,
'../custom_components/xiaomi_home/miot/specs/multi_lang.json')) '../custom_components/xiaomi_home/miot/specs/multi_lang.json'))
assert data assert data
assert nested_3_dict_str_str(data) assert nested_3_dict_str_str(data)
@ -155,8 +157,7 @@ def test_multi_lang():
@pytest.mark.github @pytest.mark.github
def test_miot_i18n(): def test_miot_i18n():
i18n_path: str = path.join( i18n_path: str = path.join(SOURCE_PATH, MIOT_I18N_RELATIVE_PATH)
SOURCE_DIR, '../custom_components/xiaomi_home/miot/i18n')
for file_name in listdir(i18n_path): for file_name in listdir(i18n_path):
file_path: str = path.join(i18n_path, file_name) file_path: str = path.join(i18n_path, file_name)
data: dict = load_json_file(file_path) data: dict = load_json_file(file_path)
@ -166,8 +167,7 @@ def test_miot_i18n():
@pytest.mark.github @pytest.mark.github
def test_translations(): def test_translations():
i18n_path: str = path.join( i18n_path: str = path.join(SOURCE_PATH, TRANS_RELATIVE_PATH)
SOURCE_DIR, '../custom_components/xiaomi_home/translations')
for file_name in listdir(i18n_path): for file_name in listdir(i18n_path):
file_path: str = path.join(i18n_path, file_name) file_path: str = path.join(i18n_path, file_name)
data: dict = load_json_file(file_path) data: dict = load_json_file(file_path)
@ -182,42 +182,29 @@ def test_miot_lang_integrity():
integration_lang_list: list[str] = [ integration_lang_list: list[str] = [
f'{key}.json' for key in list(INTEGRATION_LANGUAGES.keys())] f'{key}.json' for key in list(INTEGRATION_LANGUAGES.keys())]
translations_names: set[str] = set(listdir( translations_names: set[str] = set(listdir(
path.join( path.join(SOURCE_PATH, TRANS_RELATIVE_PATH)))
SOURCE_DIR, '../custom_components/xiaomi_home/translations')))
assert len(translations_names) == len(integration_lang_list) assert len(translations_names) == len(integration_lang_list)
assert translations_names == set(integration_lang_list) assert translations_names == set(integration_lang_list)
i18n_names: set[str] = set(listdir( i18n_names: set[str] = set(listdir(
path.join( path.join(SOURCE_PATH, MIOT_I18N_RELATIVE_PATH)))
SOURCE_DIR, '../custom_components/xiaomi_home/miot/i18n')))
assert len(i18n_names) == len(translations_names) assert len(i18n_names) == len(translations_names)
assert i18n_names == translations_names assert i18n_names == translations_names
# Check translation files structure # Check translation files structure
default_dict: dict = load_json_file( default_dict: dict = load_json_file(
path.join( path.join(SOURCE_PATH, TRANS_RELATIVE_PATH, integration_lang_list[0]))
SOURCE_DIR,
'../custom_components/xiaomi_home/translations',
integration_lang_list[0]))
for name in list(integration_lang_list)[1:]: for name in list(integration_lang_list)[1:]:
compare_dict: dict = load_json_file( compare_dict: dict = load_json_file(
path.join( path.join(SOURCE_PATH, TRANS_RELATIVE_PATH, name))
SOURCE_DIR,
'../custom_components/xiaomi_home/translations',
name))
if not compare_dict_structure(default_dict, compare_dict): if not compare_dict_structure(default_dict, compare_dict):
print('compare_dict_structure failed /translations, ', name) print('compare_dict_structure failed /translations, ', name)
assert False assert False
# Check i18n files structure # Check i18n files structure
default_dict = load_json_file( default_dict = load_json_file(
path.join( path.join(
SOURCE_DIR, SOURCE_PATH, MIOT_I18N_RELATIVE_PATH, integration_lang_list[0]))
'../custom_components/xiaomi_home/miot/i18n',
integration_lang_list[0]))
for name in list(integration_lang_list)[1:]: for name in list(integration_lang_list)[1:]:
compare_dict: dict = load_json_file( compare_dict: dict = load_json_file(
path.join( path.join(SOURCE_PATH, MIOT_I18N_RELATIVE_PATH, name))
SOURCE_DIR,
'../custom_components/xiaomi_home/miot/i18n',
name))
if not compare_dict_structure(default_dict, compare_dict): if not compare_dict_structure(default_dict, compare_dict):
print('compare_dict_structure failed /miot/i18n, ', name) print('compare_dict_structure failed /miot/i18n, ', name)
assert False assert False