test_Notifier.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import io
  2. import re
  3. import time
  4. import pytest
  5. from panda3d import core
  6. from direct.directnotify import Logger, Notifier
  7. NOTIFIER_NAME = 'Test notifier'
  8. DEBUG_LOG = 'Debug log'
  9. INFO_LOG = 'Info log'
  10. WARNING_LOG = 'Warning log'
  11. ERROR_LOG = 'Error log'
  12. @pytest.fixture
  13. def log_io():
  14. return io.StringIO()
  15. @pytest.fixture
  16. def notifier(log_io):
  17. logger = Logger.Logger()
  18. logger.setTimeStamp(False)
  19. logger._Logger__logFile = log_io
  20. notifier = Notifier.Notifier(NOTIFIER_NAME, logger)
  21. notifier.setLogging(True)
  22. return notifier
  23. def test_setServerDelta():
  24. notifier = Notifier.Notifier(NOTIFIER_NAME)
  25. notifier.setServerDelta(4.2, time.timezone)
  26. assert Notifier.Notifier.serverDelta == 4
  27. Notifier.Notifier.serverDelta = 0
  28. def test_logging(notifier, log_io):
  29. notifier.setLogging(False)
  30. assert not notifier.getLogging()
  31. notifier.info(INFO_LOG)
  32. assert log_io.getvalue() == ''
  33. notifier.setLogging(True)
  34. assert notifier.getLogging()
  35. notifier.info(INFO_LOG)
  36. assert log_io.getvalue() == f':{NOTIFIER_NAME}: {INFO_LOG}\n'
  37. @pytest.mark.parametrize('severity', (core.NS_debug, core.NS_info, core.NS_warning, core.NS_error))
  38. def test_severity(severity, notifier, log_io):
  39. notifier.setSeverity(severity)
  40. assert notifier.getSeverity() == severity
  41. with pytest.raises(Notifier.NotifierException):
  42. notifier.error(ERROR_LOG)
  43. warning_return = notifier.warning(WARNING_LOG)
  44. info_return = notifier.info(INFO_LOG)
  45. debug_return = notifier.debug(DEBUG_LOG)
  46. assert warning_return and info_return and debug_return
  47. expected_logs = [
  48. f'{Notifier.NotifierException}: {NOTIFIER_NAME}(error): {ERROR_LOG}',
  49. f':{NOTIFIER_NAME}(warning): {WARNING_LOG}',
  50. f':{NOTIFIER_NAME}: {INFO_LOG}',
  51. f':{NOTIFIER_NAME}(debug): {DEBUG_LOG}',
  52. ]
  53. del expected_logs[6-severity:]
  54. assert log_io.getvalue() == '\n'.join(expected_logs) + '\n'
  55. def test_custom_exception(notifier):
  56. class CustomException(Exception):
  57. pass
  58. with pytest.raises(CustomException):
  59. notifier.error(ERROR_LOG, CustomException)
  60. def test_debugCall(notifier, log_io):
  61. notifier.setDebug(False)
  62. return_value = notifier.debugCall(DEBUG_LOG)
  63. assert return_value
  64. assert log_io.getvalue() == ''
  65. notifier.setDebug(True)
  66. notifier.debugCall(DEBUG_LOG)
  67. pattern = rf':\d\d:\d\d:\d\d:{NOTIFIER_NAME} "{DEBUG_LOG}" test_debugCall\(.*\)\n'
  68. assert re.match(pattern, log_io.getvalue())