|
| 1 | +from datetime import datetime |
| 2 | +import unittest |
| 3 | +from contextlib import redirect_stdout |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from dm_job_utilities.dm_log import DmLog |
| 8 | + |
| 9 | +class StdoutRedirectionContext(): |
| 10 | + class ListIO(): |
| 11 | + def __init__(self): |
| 12 | + # Container for messages sent to stdout. |
| 13 | + self.output = [] |
| 14 | + def write(self, s): |
| 15 | + # Filter empty strings or naked newline characters. |
| 16 | + if s in ("\n", ""): |
| 17 | + return |
| 18 | + self.output.append(s) |
| 19 | + |
| 20 | + def __enter__(self): |
| 21 | + self._buf = self.ListIO() |
| 22 | + self._ctx = redirect_stdout(self._buf) |
| 23 | + self._ctx.__enter__() |
| 24 | + return self._buf |
| 25 | + |
| 26 | + def __exit__(self, exc_type, exc_value, exc_traceback): |
| 27 | + self._ctx.__exit__(exc_type, exc_value, exc_traceback) |
| 28 | + |
| 29 | + |
| 30 | +class StdoutTestCase(unittest.TestCase): |
| 31 | + def assertStdout(self): |
| 32 | + return StdoutRedirectionContext() |
| 33 | + |
| 34 | + |
| 35 | +class TestDmLogMethods(StdoutTestCase): |
| 36 | + |
| 37 | + def test_dmlog_emit_event(self): |
| 38 | + with self.assertStdout() as cm: |
| 39 | + DmLog.emit_event("Test message") |
| 40 | + self.assertEqual(len(cm.output), 1) |
| 41 | + self.assertIn("# INFO -EVENT- Test message", cm.output[0]) |
| 42 | + |
| 43 | + def test_dmlog_emit_fatal_event(self): |
| 44 | + with pytest.raises(SystemExit) as pytest_wrapped_e: |
| 45 | + with self.assertStdout() as cm: |
| 46 | + DmLog.emit_fatal_event("Bang!") |
| 47 | + assert pytest_wrapped_e.type == SystemExit |
| 48 | + assert pytest_wrapped_e.value.code == 1 |
| 49 | + self.assertEqual(len(cm.output), 1) |
| 50 | + self.assertIn("# CRITICAL -EVENT-", cm.output[0]) |
| 51 | + self.assertIn("Bang!", cm.output[0]) |
| 52 | + # Lines must start with an ISO-8601 a date |
| 53 | + date_str = cm.output[0].split()[0] |
| 54 | + date_time = datetime.fromisoformat(date_str) |
| 55 | + self.assertIsNotNone(date_time) |
| 56 | + |
| 57 | + def test_dmlog_emit_cost_incremental(self): |
| 58 | + DmLog.reset_cost_sequence_number() |
| 59 | + with self.assertStdout() as cm: |
| 60 | + DmLog.emit_cost(4.0) |
| 61 | + DmLog.emit_cost(8.0) |
| 62 | + self.assertEqual(len(cm.output), 2) |
| 63 | + self.assertIn("# INFO -COST- 4.0 1", cm.output[0]) |
| 64 | + self.assertIn("# INFO -COST- 8.0 2", cm.output[1]) |
| 65 | + # Lines must start with an ISO-8601 a date |
| 66 | + date_str = cm.output[0].split()[0] |
| 67 | + date_time = datetime.fromisoformat(date_str) |
| 68 | + self.assertIsNotNone(date_time) |
| 69 | + |
| 70 | + def test_dmlog_emit_cost_non_incremental(self): |
| 71 | + DmLog.reset_cost_sequence_number() |
| 72 | + with self.assertStdout() as cm: |
| 73 | + DmLog.emit_cost(4.0, incremental=True) |
| 74 | + DmLog.emit_cost(4.0, incremental=True) |
| 75 | + self.assertEqual(len(cm.output), 2) |
| 76 | + self.assertIn("# INFO -COST- +4.0 1", cm.output[0]) |
| 77 | + self.assertIn("# INFO -COST- +4.0 2", cm.output[1]) |
| 78 | + # Lines must start with an ISO-8601 a date |
| 79 | + date_str = cm.output[0].split()[0] |
| 80 | + date_time = datetime.fromisoformat(date_str) |
| 81 | + self.assertIsNotNone(date_time) |
0 commit comments