|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from tinypipeline import pipeline, step |
| 6 | + |
| 7 | + |
| 8 | +def test_pipeline_completion(): |
| 9 | + """ |
| 10 | + Test that the pipeline runs all the steps in the correct order. |
| 11 | + """ |
| 12 | + mock_step_1 = Mock() |
| 13 | + mock_step_2 = Mock() |
| 14 | + mock_step_3 = Mock() |
| 15 | + |
| 16 | + mock_step_1.return_value = "step 1" |
| 17 | + mock_step_2.return_value = "step 2" |
| 18 | + mock_step_3.return_value = "step 3" |
| 19 | + |
| 20 | + @pipeline( |
| 21 | + name="test_pipeline", |
| 22 | + description="Test pipeline", |
| 23 | + version="1.0.0", |
| 24 | + ) |
| 25 | + def test_pipeline(): |
| 26 | + step_one = step( |
| 27 | + callable=mock_step_1, |
| 28 | + name="step_one", |
| 29 | + description="Step one", |
| 30 | + version="1.0.0", |
| 31 | + ) |
| 32 | + |
| 33 | + step_two = step( |
| 34 | + callable=mock_step_2, |
| 35 | + name="step_two", |
| 36 | + description="Step two", |
| 37 | + version="1.0.0", |
| 38 | + ) |
| 39 | + |
| 40 | + step_three = step( |
| 41 | + callable=mock_step_3, |
| 42 | + name="step_three", |
| 43 | + description="Step three", |
| 44 | + version="1.0.0", |
| 45 | + ) |
| 46 | + |
| 47 | + return [step_one, step_two, step_three] |
| 48 | + |
| 49 | + pipe = test_pipeline() |
| 50 | + pipe.run() |
| 51 | + |
| 52 | + assert len(pipe.steps) == 3 |
| 53 | + mock_step_1.assert_called_once() |
| 54 | + mock_step_2.assert_called_once() |
| 55 | + mock_step_3.assert_called_once() |
| 56 | + |
| 57 | + |
| 58 | +def test_pipeline_failure_no_function_passed(): |
| 59 | + """ |
| 60 | + Test that the pipeline fails with a TypeError if no function is passed |
| 61 | + to the method. |
| 62 | +
|
| 63 | + Also check that the error message is correct. |
| 64 | + """ |
| 65 | + data = "" |
| 66 | + with pytest.raises(TypeError) as context: |
| 67 | + pipe = pipeline( |
| 68 | + name="test_pipeline", |
| 69 | + description="Test pipeline", |
| 70 | + version="1.0.0", |
| 71 | + ) |
| 72 | + pipe(data)().run() |
| 73 | + |
| 74 | + assert ( |
| 75 | + f"The pipeline decorator only accepts functions. Passed {type(data)}" |
| 76 | + == str(context.value) |
| 77 | + ) |
| 78 | + |
| 79 | +def test_pipeline_failure_invalid_step(): |
| 80 | + """ |
| 81 | + Test that the pipeline fails with a TypeError if there is no instance of Step, |
| 82 | + in a list of pipeline steps. |
| 83 | +
|
| 84 | + Also check that the error message is correct. |
| 85 | + """ |
| 86 | + |
| 87 | + @pipeline( |
| 88 | + name="test_pipeline", |
| 89 | + description="Test pipeline", |
| 90 | + version="1.0.0", |
| 91 | + ) |
| 92 | + def test_pipeline(): |
| 93 | + return ["step_one"] |
| 94 | + |
| 95 | + with pytest.raises(TypeError) as context: |
| 96 | + pipe = test_pipeline() |
| 97 | + pipe.run() |
| 98 | + |
| 99 | + assert ( |
| 100 | + "Not a valid step. Consider using the step() method to create steps for your pipeline." |
| 101 | + == str(context.value) |
| 102 | + ) |
| 103 | + |
| 104 | +def test_pipeline_failure_no_steps_list(): |
| 105 | + """ |
| 106 | + Test that the pipeline fails with a TypeError if there is no list of steps. |
| 107 | +
|
| 108 | + Also check that the error message is correct. |
| 109 | + """ |
| 110 | + |
| 111 | + @pipeline( |
| 112 | + name="test_pipeline", |
| 113 | + description="Test pipeline", |
| 114 | + version="1.0.0", |
| 115 | + ) |
| 116 | + def test_pipeline(): |
| 117 | + return "step_one" |
| 118 | + |
| 119 | + with pytest.raises(TypeError) as context: |
| 120 | + pipe = test_pipeline() |
| 121 | + pipe.run() |
| 122 | + |
| 123 | + assert "Pipeline steps must be in a list." == str(context.value) |
| 124 | + |
| 125 | +def test_pipeline_failure_no_steps_to_run(): |
| 126 | + """ |
| 127 | + Test that the pipeline fails with an Exception if there are no steps to run. |
| 128 | +
|
| 129 | + Also check that the error message is correct. |
| 130 | + """ |
| 131 | + |
| 132 | + @pipeline( |
| 133 | + name="test_pipeline", |
| 134 | + description="Test pipeline", |
| 135 | + version="1.0.0", |
| 136 | + ) |
| 137 | + def test_pipeline(): |
| 138 | + return [] |
| 139 | + |
| 140 | + with pytest.raises(Exception) as context: |
| 141 | + pipe = test_pipeline() |
| 142 | + pipe.run() |
| 143 | + |
| 144 | + assert f"Pipeline {pipe.name} has no steps to run." == str(context.value) |
| 145 | + |
| 146 | +def test_pipeline_failure_exception_in_step(): |
| 147 | + """ |
| 148 | + Test that the pipeline fails with an Exception if there is an exception in one of the steps. |
| 149 | +
|
| 150 | + Also check that the error message is correct. |
| 151 | + """ |
| 152 | + mock_step_1 = Mock() |
| 153 | + mock_step_2 = Mock() |
| 154 | + mock_step_3 = Mock() |
| 155 | + |
| 156 | + mock_step_1.return_value = "step 1" |
| 157 | + mock_step_2.side_effect = Exception("Something went wrong") |
| 158 | + mock_step_3.return_value = "step 3" |
| 159 | + |
| 160 | + @pipeline( |
| 161 | + name="test_pipeline", |
| 162 | + description="Test pipeline", |
| 163 | + version="1.0.0", |
| 164 | + ) |
| 165 | + def test_pipeline(): |
| 166 | + step_one = step( |
| 167 | + callable=mock_step_1, |
| 168 | + name="step_one", |
| 169 | + description="Step one", |
| 170 | + version="1.0.0", |
| 171 | + ) |
| 172 | + |
| 173 | + step_two = step( |
| 174 | + callable=mock_step_2, |
| 175 | + name="step_two", |
| 176 | + description="Step two", |
| 177 | + version="1.0.0", |
| 178 | + ) |
| 179 | + |
| 180 | + step_three = step( |
| 181 | + callable=mock_step_3, |
| 182 | + name="step_three", |
| 183 | + description="Step three", |
| 184 | + version="1.0.0", |
| 185 | + ) |
| 186 | + |
| 187 | + return [step_one, step_two, step_three] |
| 188 | + |
| 189 | + with pytest.raises(Exception) as context: |
| 190 | + pipe = test_pipeline() |
| 191 | + pipe.run() |
| 192 | + |
| 193 | + assert str(mock_step_2.side_effect) == str(context.value) |
0 commit comments