test_translations.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. Tests for translation files.
  3. """
  4. from fnmatch import fnmatch
  5. from os import walk
  6. from os.path import join, dirname
  7. import pytest
  8. from unittest import TestCase
  9. from homeassistant.util.json import load_json
  10. import custom_components.tuya_local as root
  11. from custom_components.tuya_local.helpers.device_config import (
  12. available_configs,
  13. TuyaDeviceConfig,
  14. )
  15. def get_translations():
  16. translations = join(dirname(root.__file__), "translations")
  17. for (path, dirs, files) in walk(translations):
  18. for file in files:
  19. if fnmatch(file, "*.json"):
  20. yield load_json(join(path, file))
  21. english = None
  22. def get_english():
  23. global english
  24. if english is None:
  25. translations = join(dirname(root.__file__), "translations", "en.json")
  26. json = load_json(translations)
  27. english = json["config"]["step"]["choose_entities"]["data"]
  28. return english
  29. def get_devices():
  30. for device in available_configs():
  31. yield TuyaDeviceConfig(device)
  32. @pytest.mark.parametrize("translations", get_translations())
  33. def test_config_and_options_match(translations):
  34. config = translations["config"]["step"]["choose_entities"]["data"]
  35. options = translations["options"]["step"]["user"]["data"]
  36. # remove expected differences
  37. config.pop("name", None)
  38. options.pop("host", None)
  39. options.pop("local_key", None)
  40. test = TestCase()
  41. test.maxDiff = None
  42. test.assertDictEqual(config, options)
  43. def subtest_entity_covered(entity):
  44. strings = get_english()
  45. TestCase().assertIn(
  46. entity.config_id,
  47. strings,
  48. f"{entity._device.config}: {entity.config_id} is missing a translation",
  49. )
  50. @pytest.mark.parametrize("device", get_devices())
  51. def test_device_covered(device):
  52. entity = device.primary_entity
  53. if entity.deprecated:
  54. subtest_entity_covered(entity)
  55. for entity in device.secondary_entities():
  56. if entity.deprecated:
  57. subtest_entity_covered(entity)