Skip to content

Commit

Permalink
test: check lang_integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
topsworld committed Dec 19, 2024
1 parent c4a981a commit dabf277
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions test/check_rule_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from os import listdir, path
from typing import Optional
import pytest
import yaml

SOURCE_DIR: str = path.dirname(path.abspath(__file__))

Expand All @@ -20,6 +21,18 @@ def load_json_file(file_path: str) -> Optional[dict]:
return None


def load_yaml_file(file_path: str) -> Optional[dict]:
try:
with open(file_path, 'r', encoding='utf-8') as file:
return yaml.safe_load(file)
except FileNotFoundError:
print(file_path, 'is not found.')
return None
except yaml.YAMLError:
print(file_path, 'is not a valid YAML file.')
return None


def dict_str_str(d: dict) -> bool:
"""restricted format: dict[str, str]"""
if not isinstance(d, dict):
Expand Down Expand Up @@ -86,6 +99,30 @@ def bool_trans(d: dict) -> bool:
return True


def compare_dict_structure(dict1: dict, dict2: dict) -> bool:
if not isinstance(dict1, dict) or not isinstance(dict2, dict):
print('invalid type')
return False
if dict1.keys() != dict2.keys():
print('inconsistent key values, ', dict1.keys(), dict2.keys())
return False
for key in dict1:
if isinstance(dict1[key], dict) and isinstance(dict2[key], dict):
if not compare_dict_structure(dict1[key], dict2[key]):
print('inconsistent key values, dict, ', key)
return False
elif isinstance(dict1[key], list) and isinstance(dict2[key], list):
if not all(
isinstance(i, type(j))
for i, j in zip(dict1[key], dict2[key])):
print('inconsistent key values, list, ', key)
return False
elif not isinstance(dict1[key], type(dict2[key])):
print('inconsistent key values, type, ', key)
return False
return True


@pytest.mark.github
def test_bool_trans():
data: dict = load_json_file(
Expand Down Expand Up @@ -125,3 +162,62 @@ def test_miot_i18n():
data: dict = load_json_file(file_path)
assert data
assert nested_3_dict_str_str(data)


@pytest.mark.github
def test_translations():
i18n_path: str = path.join(
SOURCE_DIR, '../custom_components/xiaomi_home/translations')
for file_name in listdir(i18n_path):
file_path: str = path.join(i18n_path, file_name)
data: dict = load_json_file(file_path)
assert data
assert dict_str_dict(data)


@pytest.mark.github
def test_miot_lang_integrity():
# pylint: disable=import-outside-toplevel
from miot.const import INTEGRATION_LANGUAGES
integration_lang_list: list[str] = [
f'{key}.json' for key in list(INTEGRATION_LANGUAGES.keys())]
translations_names: set[str] = set(listdir(
path.join(
SOURCE_DIR, '../custom_components/xiaomi_home/translations')))
assert len(translations_names) == len(integration_lang_list)
assert translations_names == set(integration_lang_list)
i18n_names: set[str] = set(listdir(
path.join(
SOURCE_DIR, '../custom_components/xiaomi_home/miot/i18n')))
assert len(i18n_names) == len(translations_names)
assert i18n_names == translations_names
# Check translation files structure
default_dict: dict = load_json_file(
path.join(
SOURCE_DIR,
'../custom_components/xiaomi_home/translations',
integration_lang_list[0]))
for name in list(integration_lang_list)[1:]:
compare_dict: dict = load_json_file(
path.join(
SOURCE_DIR,
'../custom_components/xiaomi_home/translations',
name))
if not compare_dict_structure(default_dict, compare_dict):
print('compare_dict_structure failed /translations, ', name)
assert False
# Check i18n files structure
default_dict = load_json_file(
path.join(
SOURCE_DIR,
'../custom_components/xiaomi_home/miot/i18n',
integration_lang_list[0]))
for name in list(integration_lang_list)[1:]:
compare_dict: dict = load_json_file(
path.join(
SOURCE_DIR,
'../custom_components/xiaomi_home/miot/i18n',
name))
if not compare_dict_structure(default_dict, compare_dict):
print('compare_dict_structure failed /miot/i18n, ', name)
assert False

0 comments on commit dabf277

Please sign in to comment.