|
| 1 | +import sys |
| 2 | +from io import StringIO |
| 3 | +from unittest.mock import Mock, PropertyMock |
| 4 | + |
1 | 5 | from pydantic import BaseModel, ValidationError |
2 | 6 |
|
3 | | -from rules_validation_api.app import refine_error |
| 7 | +from rules_validation_api.app import display_current_iteration, refine_error |
4 | 8 |
|
5 | 9 |
|
6 | 10 | def _raise_validation_error(model_cls, **kwargs) -> ValidationError: |
@@ -70,3 +74,50 @@ class Model(BaseModel): |
70 | 74 | expected_no_lines = 3 |
71 | 75 | assert len(lines) == expected_no_lines |
72 | 76 | assert lines[0].startswith("❌Validation Error:") |
| 77 | + |
| 78 | + |
| 79 | +def test_no_current_iteration(): |
| 80 | + # Arrange |
| 81 | + result = Mock() |
| 82 | + result.campaign_config = Mock() |
| 83 | + |
| 84 | + # iterations must be a list, not a Mock |
| 85 | + result.campaign_config.iterations = [] |
| 86 | + |
| 87 | + # current_iteration should raise StopIteration |
| 88 | + type(result.campaign_config).current_iteration = PropertyMock(side_effect=StopIteration) |
| 89 | + |
| 90 | + captured = StringIO() |
| 91 | + sys.stdout = captured |
| 92 | + |
| 93 | + # Act |
| 94 | + display_current_iteration(result) |
| 95 | + |
| 96 | + # Reset stdout |
| 97 | + sys.stdout = sys.__stdout__ |
| 98 | + |
| 99 | + # Assert |
| 100 | + assert "No active iteration could be determined" in captured.getvalue() |
| 101 | + |
| 102 | + |
| 103 | +def test_current_iteration_exists(): |
| 104 | + # Arrange |
| 105 | + mock_iteration = Mock() |
| 106 | + mock_iteration.iteration_number = 7 |
| 107 | + |
| 108 | + result = Mock() |
| 109 | + result.campaign_config = Mock() |
| 110 | + |
| 111 | + result.campaign_config.iterations = [mock_iteration] |
| 112 | + |
| 113 | + type(result.campaign_config).current_iteration = PropertyMock(return_value=mock_iteration) |
| 114 | + |
| 115 | + captured = StringIO() |
| 116 | + sys.stdout = captured |
| 117 | + |
| 118 | + display_current_iteration(result) |
| 119 | + |
| 120 | + sys.stdout = sys.__stdout__ |
| 121 | + |
| 122 | + assert "Current Iteration Number:" in captured.getvalue() |
| 123 | + assert "7" in captured.getvalue() |
0 commit comments