test_utils.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit-Tests für trixy_core.utils.
  4. """
  5. import io
  6. import sys
  7. from unittest.mock import patch
  8. import pytest
  9. from trixy_core.utils.debug import (
  10. pinfo, pdebug, perror, pwarn,
  11. set_debug_mode, is_debug_mode,
  12. Colors, _format_message
  13. )
  14. from trixy_core.utils.version import (
  15. VERSION, VERSION_STRING, MAJOR, MINOR, REVISION,
  16. PROTOCOL_VERSION, PROTOCOL_VERSION_STRING,
  17. PROTOCOL_MAJOR, PROTOCOL_MINOR
  18. )
  19. class TestDebugFunctions:
  20. """Tests für Debug-Ausgabefunktionen."""
  21. def test_set_debug_mode_enables(self):
  22. """Testet das Aktivieren des Debug-Modus."""
  23. set_debug_mode(True)
  24. assert is_debug_mode() is True
  25. def test_set_debug_mode_disables(self):
  26. """Testet das Deaktivieren des Debug-Modus."""
  27. set_debug_mode(False)
  28. assert is_debug_mode() is False
  29. def test_pinfo_outputs_to_stdout_in_debug_mode(self):
  30. """Testet pinfo-Ausgabe im Debug-Modus."""
  31. set_debug_mode(True)
  32. with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
  33. pinfo("Test message")
  34. output = mock_stdout.getvalue()
  35. assert "[INFO]" in output
  36. assert "Test message" in output
  37. def test_pdebug_outputs_to_stdout_in_debug_mode(self):
  38. """Testet pdebug-Ausgabe im Debug-Modus."""
  39. set_debug_mode(True)
  40. with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
  41. pdebug("Debug message")
  42. output = mock_stdout.getvalue()
  43. assert "[DEBUG]" in output
  44. assert "Debug message" in output
  45. def test_perror_outputs_to_stderr_in_debug_mode(self):
  46. """Testet perror-Ausgabe im Debug-Modus."""
  47. set_debug_mode(True)
  48. with patch("sys.stderr", new_callable=io.StringIO) as mock_stderr:
  49. perror("Error message")
  50. output = mock_stderr.getvalue()
  51. assert "[ERROR]" in output
  52. assert "Error message" in output
  53. def test_pwarn_outputs_to_stdout_in_debug_mode(self):
  54. """Testet pwarn-Ausgabe im Debug-Modus."""
  55. set_debug_mode(True)
  56. with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
  57. pwarn("Warning message")
  58. output = mock_stdout.getvalue()
  59. assert "[WARN]" in output
  60. assert "Warning message" in output
  61. def test_format_message_with_args(self):
  62. """Testet Nachrichtenformatierung mit Argumenten."""
  63. formatted = _format_message("INFO", Colors.GREEN, "Value: %s", "test")
  64. assert "Value: test" in formatted
  65. assert "[INFO]" in formatted
  66. def test_format_message_without_args(self):
  67. """Testet Nachrichtenformatierung ohne Argumente."""
  68. formatted = _format_message("DEBUG", Colors.CYAN, "Simple message")
  69. assert "Simple message" in formatted
  70. def test_pinfo_with_format_args(self):
  71. """Testet pinfo mit Format-Argumenten."""
  72. set_debug_mode(True)
  73. with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
  74. pinfo("Count: %d, Name: %s", 42, "test")
  75. output = mock_stdout.getvalue()
  76. assert "Count: 42" in output
  77. assert "Name: test" in output
  78. class TestColors:
  79. """Tests für ANSI-Farbcodes."""
  80. def test_reset_code(self):
  81. """Testet den Reset-Code."""
  82. assert Colors.RESET == "\033[0m"
  83. def test_bold_code(self):
  84. """Testet den Bold-Code."""
  85. assert Colors.BOLD == "\033[1m"
  86. def test_color_codes_are_strings(self):
  87. """Testet, dass alle Farbcodes Strings sind."""
  88. assert isinstance(Colors.RED, str)
  89. assert isinstance(Colors.GREEN, str)
  90. assert isinstance(Colors.YELLOW, str)
  91. assert isinstance(Colors.BLUE, str)
  92. assert isinstance(Colors.CYAN, str)
  93. def test_bright_colors_exist(self):
  94. """Testet, dass helle Farbvarianten existieren."""
  95. assert hasattr(Colors, "BRIGHT_RED")
  96. assert hasattr(Colors, "BRIGHT_GREEN")
  97. assert hasattr(Colors, "BRIGHT_YELLOW")
  98. class TestVersion:
  99. """Tests für Versionskonstanten."""
  100. def test_version_tuple_format(self):
  101. """Testet das Version-Tuple-Format."""
  102. assert isinstance(VERSION, tuple)
  103. assert len(VERSION) == 3
  104. assert VERSION == (MAJOR, MINOR, REVISION)
  105. def test_version_string_format(self):
  106. """Testet das Version-String-Format."""
  107. assert isinstance(VERSION_STRING, str)
  108. assert VERSION_STRING == f"{MAJOR}.{MINOR}.{REVISION}"
  109. def test_version_components_are_integers(self):
  110. """Testet, dass Versionskomponenten Integer sind."""
  111. assert isinstance(MAJOR, int)
  112. assert isinstance(MINOR, int)
  113. assert isinstance(REVISION, int)
  114. def test_protocol_version_tuple(self):
  115. """Testet das Protokollversions-Tuple."""
  116. assert isinstance(PROTOCOL_VERSION, tuple)
  117. assert len(PROTOCOL_VERSION) == 2
  118. assert PROTOCOL_VERSION == (PROTOCOL_MAJOR, PROTOCOL_MINOR)
  119. def test_protocol_version_string(self):
  120. """Testet den Protokollversions-String."""
  121. assert isinstance(PROTOCOL_VERSION_STRING, str)
  122. assert PROTOCOL_VERSION_STRING == f"{PROTOCOL_MAJOR}.{PROTOCOL_MINOR}"
  123. def test_version_is_positive(self):
  124. """Testet, dass Versionsnummern nicht negativ sind."""
  125. assert MAJOR >= 0
  126. assert MINOR >= 0
  127. assert REVISION >= 0
  128. assert PROTOCOL_MAJOR >= 0
  129. assert PROTOCOL_MINOR >= 0