|
| 1 | +import unittest |
| 2 | + |
| 3 | +from watergrid.context import DataContext |
| 4 | +from watergrid.metrics.ConsoleMetricsExporter import ConsoleMetricsExporter |
| 5 | +from watergrid.pipelines.pipeline import Pipeline |
| 6 | +from watergrid.steps import Step |
| 7 | + |
| 8 | + |
| 9 | +class MockStep(Step): |
| 10 | + def __init__(self, throw_exception=False): |
| 11 | + self.throw_exception = throw_exception |
| 12 | + super().__init__(self.__class__.__name__) |
| 13 | + |
| 14 | + def run(self, context: DataContext): |
| 15 | + if self.throw_exception: |
| 16 | + raise Exception("MockStep failed") |
| 17 | + |
| 18 | + |
| 19 | +class ConsoleMetricsExporterTestCase(unittest.TestCase): |
| 20 | + def test_outputs_pipeline_start(self): |
| 21 | + pipeline = Pipeline("test_pipeline") |
| 22 | + pipeline.add_metrics_exporter(ConsoleMetricsExporter()) |
| 23 | + with self.assertLogs() as captured: |
| 24 | + pipeline.run() |
| 25 | + self.assertIn("INFO:root:Starting pipeline: test_pipeline", captured.output) |
| 26 | + |
| 27 | + def test_outputs_pipeline_end(self): |
| 28 | + pipeline = Pipeline("test_pipeline") |
| 29 | + pipeline.add_metrics_exporter(ConsoleMetricsExporter()) |
| 30 | + with self.assertLogs() as captured: |
| 31 | + pipeline.run() |
| 32 | + self.assertIn("INFO:root:Ending pipeline", captured.output) |
| 33 | + |
| 34 | + def test_outputs_pipeline_end_with_error(self): |
| 35 | + pipeline = Pipeline("test_pipeline") |
| 36 | + pipeline.add_metrics_exporter(ConsoleMetricsExporter()) |
| 37 | + pipeline.add_step(MockStep(throw_exception=True)) |
| 38 | + with self.assertLogs() as captured: |
| 39 | + pipeline.run() |
| 40 | + self.assertIn("ERROR:root:Exception: MockStep failed", captured.output) |
| 41 | + |
| 42 | + def test_outputs_step_start(self): |
| 43 | + pipeline = Pipeline("test_pipeline") |
| 44 | + pipeline.add_metrics_exporter(ConsoleMetricsExporter()) |
| 45 | + pipeline.add_step(MockStep()) |
| 46 | + with self.assertLogs() as captured: |
| 47 | + pipeline.run() |
| 48 | + self.assertIn("INFO:root:Starting step: MockStep", captured.output) |
| 49 | + |
| 50 | + def test_outputs_step_end(self): |
| 51 | + pipeline = Pipeline("test_pipeline") |
| 52 | + pipeline.add_metrics_exporter(ConsoleMetricsExporter()) |
| 53 | + pipeline.add_step(MockStep()) |
| 54 | + with self.assertLogs() as captured: |
| 55 | + pipeline.run() |
| 56 | + self.assertIn("INFO:root:Ending step", captured.output) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + unittest.main() |
0 commit comments