test_health_exporter_unittest.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python3
  2. # Copyright 2022 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests for test_health_exporter."""
  6. import datetime as dt
  7. import json
  8. import pathlib
  9. import tempfile
  10. import unittest
  11. from java_test_utils import JavaTestHealth
  12. import test_health_exporter
  13. from test_health_extractor import GitRepoInfo, TestHealthInfo
  14. _TEST_TYPE = 'JAVA'
  15. _JAVA_PACKAGE = 'org.chromium.foo_pkg'
  16. _GIT_HEAD_HASH = 'fcd260583cb6d2c739b91f53363fcf0ad3eb3216'
  17. _GIT_HEAD_TIME = '2022-01-18T22:45:48.000000+00:00'
  18. _GIT_REPO_INFO = GitRepoInfo(
  19. git_head=_GIT_HEAD_HASH,
  20. git_head_time=dt.datetime.fromisoformat(_GIT_HEAD_TIME))
  21. _JAVA_TEST_NAME = 'FooTest'
  22. _JAVA_TEST_FILENAME = _JAVA_TEST_NAME + '.java'
  23. _JAVA_TEST_DIR = 'javatests/org/chromium/foo_pkg'
  24. _JAVA_TEST_HEALTH = JavaTestHealth(java_package=_JAVA_PACKAGE,
  25. disabled_tests_count=1,
  26. disable_if_tests_count=2,
  27. flaky_tests_count=3)
  28. _TEST_HEALTH_INFO = TestHealthInfo(_JAVA_TEST_NAME,
  29. test_dir=pathlib.Path(_JAVA_TEST_DIR),
  30. test_filename=_JAVA_TEST_FILENAME,
  31. java_test_health=_JAVA_TEST_HEALTH,
  32. git_repo_info=_GIT_REPO_INFO)
  33. _TEST_HEALTH_JSON_DICT = dict(test_name=_JAVA_TEST_NAME,
  34. test_path=_JAVA_TEST_DIR,
  35. test_filename=_JAVA_TEST_FILENAME,
  36. test_type=_TEST_TYPE,
  37. java_package=_JAVA_PACKAGE,
  38. disabled_tests_count=1,
  39. disable_if_tests_count=2,
  40. flaky_tests_count=3,
  41. git_head_hash=_GIT_HEAD_HASH,
  42. git_head_timestamp=_GIT_HEAD_TIME)
  43. class ToJsonFile(unittest.TestCase):
  44. """Tests for the to_json_file function."""
  45. def test_to_json_file_single_java_test(self):
  46. with tempfile.TemporaryDirectory() as tmpdir:
  47. json_file = pathlib.Path(tmpdir) / 'test_output'
  48. test_health_exporter.to_json_file([_TEST_HEALTH_INFO], json_file)
  49. with open(json_file) as output_file:
  50. json_lines = output_file.readlines()
  51. self.assertEqual(1, len(json_lines))
  52. result = json.loads(json_lines[0])
  53. self.assertDictEqual(_TEST_HEALTH_JSON_DICT, result)
  54. def test_to_json_file_multiple_java_tests(self):
  55. with tempfile.TemporaryDirectory() as tmpdir:
  56. json_file = pathlib.Path(tmpdir) / 'test_output'
  57. test_health_exporter.to_json_file(
  58. [_TEST_HEALTH_INFO, _TEST_HEALTH_INFO, _TEST_HEALTH_INFO],
  59. json_file)
  60. with open(json_file) as output_file:
  61. json_lines = output_file.readlines()
  62. self.assertEqual(3, len(json_lines))
  63. result1 = json.loads(json_lines[0])
  64. result2 = json.loads(json_lines[1])
  65. result3 = json.loads(json_lines[2])
  66. self.assertDictEqual(_TEST_HEALTH_JSON_DICT, result1)
  67. self.assertDictEqual(_TEST_HEALTH_JSON_DICT, result2)
  68. self.assertDictEqual(_TEST_HEALTH_JSON_DICT, result3)
  69. def test_to_json_file_java_package_omitted(self):
  70. java_test_health = JavaTestHealth(java_package=None,
  71. disabled_tests_count=1,
  72. disable_if_tests_count=2,
  73. flaky_tests_count=3)
  74. test_health_info = TestHealthInfo(
  75. _JAVA_TEST_NAME,
  76. test_dir=pathlib.Path(_JAVA_TEST_DIR),
  77. test_filename=_JAVA_TEST_FILENAME,
  78. java_test_health=java_test_health,
  79. git_repo_info=_GIT_REPO_INFO)
  80. test_health_json_dict = _TEST_HEALTH_JSON_DICT.copy()
  81. del test_health_json_dict['java_package']
  82. with tempfile.TemporaryDirectory() as tmpdir:
  83. json_file = pathlib.Path(tmpdir) / 'test_output'
  84. test_health_exporter.to_json_file([test_health_info], json_file)
  85. with open(json_file) as output_file:
  86. json_lines = output_file.readlines()
  87. self.assertEqual(1, len(json_lines))
  88. result = json.loads(json_lines[0])
  89. self.assertDictEqual(test_health_json_dict, result)
  90. if __name__ == '__main__':
  91. unittest.main()