test_py_logging_util.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import pytest
  7. import unittest.mock as mock
  8. from pytest_mock import MockFixture
  9. import ly_test_tools._internal.log.py_logging_util as py_logging_util
  10. pytestmark = pytest.mark.SUITE_smoke
  11. class TestTerminateLogging(object):
  12. def test_TerminateLogging_HandlersUninitialized_NoHandlersRemoved(self, mocker):
  13. # type: (MockFixture) -> None
  14. mock_getLogger = mocker.patch("logging.getLogger") # type: MagicMock
  15. mock_removeHandler = mock_getLogger.return_value.removeHandler # type: MagicMock
  16. py_logging_util.terminate_logging()
  17. mock_removeHandler.assert_not_called()
  18. def test_TerminateLogging_HandlersInitialized_HandlersRemoved(self, mocker):
  19. # type: (MockFixture) -> None
  20. mock_getLogger = mocker.patch("logging.getLogger") # type: MagicMock
  21. mock_removeHandler = mock_getLogger.return_value.removeHandler # type: MagicMock
  22. mock_stream_handler = "Mock Stream Handler"
  23. mock_info_file_handler = "Mock Info File Handler"
  24. mock_debug_file_handler = "Mock Debug File Handler"
  25. py_logging_util._stream_handler = mock_stream_handler
  26. py_logging_util._info_file_handler = mock_info_file_handler
  27. py_logging_util._debug_file_handler = mock_debug_file_handler
  28. py_logging_util.terminate_logging()
  29. calls = [
  30. mock.call(mock_stream_handler),
  31. mock.call(mock_info_file_handler),
  32. mock.call(mock_debug_file_handler),
  33. ]
  34. mock_removeHandler.assert_has_calls(calls)
  35. @mock.patch("logging.FileHandler", mock.MagicMock())
  36. class TestInitializeLogging(object):
  37. @mock.patch("logging.getLogger", scope='function')
  38. @mock.patch("logging.StreamHandler", mock.MagicMock())
  39. def test_InitializeLogging_AddHandlerCalled_CalledThrice(self, mock_get_logger):
  40. dummy_log_path = "dummy_log_path"
  41. dummy_info_path = "dummy_info_path"
  42. py_logging_util._stream_handler = None
  43. py_logging_util._info_file_handler = None
  44. py_logging_util._debug_file_handler = None
  45. mock_add_handler = mock_get_logger.return_value.addHandler
  46. py_logging_util.initialize_logging(dummy_info_path, dummy_log_path)
  47. assert mock_add_handler.call_count == 3
  48. py_logging_util.terminate_logging()
  49. @mock.patch("logging.getLogger", scope='function')
  50. def test_InitializeLogging_CheckLoggerCalled_LoggerCalledOnce(self, mock_get_logger):
  51. dummy_log_path = "dummy_path"
  52. dummy_info_path = "dummy_path"
  53. py_logging_util.initialize_logging(dummy_info_path, dummy_log_path)
  54. mock_get_logger.assert_called_once()
  55. @mock.patch("logging.getLogger", scope='function')
  56. def test_InitializeLogging_SetLogLevelValidArgs_ValidArgsPassed(self, mock_get_logger):
  57. dummy_log_path = "dummy_path"
  58. dummy_info_path = "dummy_path"
  59. mock_setLevel = mock_get_logger.return_value.setLevel
  60. py_logging_util.initialize_logging(dummy_info_path, dummy_log_path)
  61. mock_setLevel.assert_called_with(10) # logging.DEBUG = 10
  62. def test_InitializeLogging_CheckHandlerInitialized_HandlerNotNone(self):
  63. dummy_log_path = "dummy_path"
  64. dummy_info_path = "dummy_path"
  65. py_logging_util.initialize_logging(dummy_info_path,dummy_log_path)
  66. assert py_logging_util._debug_file_handler is not None
  67. assert py_logging_util._info_file_handler is not None
  68. assert py_logging_util._stream_handler is not None
  69. @mock.patch("logging.StreamHandler.setFormatter", scope='function')
  70. @mock.patch('logging.getLogger', mock.MagicMock())
  71. def test_InitializeLogging_CheckFormatting_HandlerFormattingIsCorrect(self, mock_stream_handler_formatter):
  72. dummy_log_path = "dummy_path"
  73. dummy_info_path = "dummy_path"
  74. py_logging_util._stream_handler = None
  75. py_logging_util._info_file_handler = None
  76. py_logging_util._debug_file_handler = None
  77. py_logging_util.initialize_logging(dummy_info_path, dummy_log_path)
  78. #example of formatted string : 7024.00016785 - DEBUG - [MainThread] - ly_test_tools.launchers.platforms.win.launcher - Initialized Windows Launcher
  79. format_string = "%(relativeCreated)s - %(levelname)s - [%(threadName)s] - %(name)s - %(message)s"
  80. assert mock_stream_handler_formatter.call_args[0][0]._fmt == format_string