|
1 | 1 | # Maintain all core related tests here |
2 | 2 | from pytest import mark |
| 3 | +import pytest |
3 | 4 |
|
4 | | -from tirith.core.core import final_evaluator |
| 5 | +from tirith.core.core import final_evaluator, generate_evaluator_result |
| 6 | +from tirith.providers.common import ProviderError |
| 7 | +from unittest.mock import patch, MagicMock |
5 | 8 |
|
6 | 9 |
|
7 | 10 | @mark.passing |
@@ -38,3 +41,113 @@ def test_final_evaluator_malicious_eval_should_err(): |
38 | 41 | "!skipped_check && passing_check || [].__class__.__base__", dict(skipped_check=None, passing_check=True) |
39 | 42 | ) |
40 | 43 | assert actual_result == (False, ["The following symbols are not allowed: __class__, __base__"]) |
| 44 | + |
| 45 | + |
| 46 | +class MockEvaluator: |
| 47 | + def evaluate(self, input_value, data): |
| 48 | + if input_value == "resource1": |
| 49 | + return {"passed": True, "message": "First resource passed"} |
| 50 | + else: |
| 51 | + return {"passed": False, "message": "Second resource failed"} |
| 52 | + |
| 53 | + |
| 54 | +@mark.passing |
| 55 | +def test_generate_evaluator_result_empty_inputs(): |
| 56 | + """Test that when a provider returns no inputs, the evaluation should fail.""" |
| 57 | + # Mock evaluator object |
| 58 | + evaluator_obj = { |
| 59 | + "id": "test_evaluator", |
| 60 | + "provider_args": {"operation_type": "attribute", "key": "value"}, |
| 61 | + "condition": {"type": "Equals", "value": True}, |
| 62 | + } |
| 63 | + |
| 64 | + # Mock the provider function to return empty list |
| 65 | + with patch("tirith.core.core.get_evaluator_inputs_from_provider_inputs", return_value=[]): |
| 66 | + result = generate_evaluator_result(evaluator_obj, {}, "test_provider") |
| 67 | + |
| 68 | + # Verify the result shows a failed evaluation with the correct message |
| 69 | + assert result["passed"] is False |
| 70 | + assert len(result["result"]) == 1 |
| 71 | + assert result["result"][0]["passed"] is False |
| 72 | + assert result["result"][0]["message"] == "Could not find input value" |
| 73 | + |
| 74 | + |
| 75 | +@mark.passing |
| 76 | +def test_generate_evaluator_result_provider_error_above_tolerance(): |
| 77 | + """Test that provider errors with severity higher than tolerance cause the evaluation to fail.""" |
| 78 | + # Mock evaluator object with error_tolerance = 1 |
| 79 | + evaluator_obj = { |
| 80 | + "id": "test_evaluator", |
| 81 | + "provider_args": {"operation_type": "attribute", "key": "value"}, |
| 82 | + "condition": {"type": "Equals", "value": True, "error_tolerance": 1}, |
| 83 | + } |
| 84 | + |
| 85 | + # Create a provider error with severity 2 (above tolerance) |
| 86 | + provider_error = {"value": ProviderError(severity_value=2), "err": "Resource not found"} |
| 87 | + |
| 88 | + # Mock the provider function to return the error |
| 89 | + with patch("tirith.core.core.get_evaluator_inputs_from_provider_inputs", return_value=[provider_error]): |
| 90 | + # Create a mapping for EVALUATORS_DICT.get to return a mock evaluator class |
| 91 | + mock_evaluator_dict = {"Equals": MockEvaluator} |
| 92 | + with patch("tirith.core.core.EVALUATORS_DICT", mock_evaluator_dict): |
| 93 | + result = generate_evaluator_result(evaluator_obj, {}, "test_provider") |
| 94 | + |
| 95 | + # Verify the result shows a failed evaluation |
| 96 | + assert result["passed"] is False |
| 97 | + assert len(result["result"]) == 1 |
| 98 | + assert result["result"][0]["passed"] is False |
| 99 | + assert result["result"][0]["message"] == "Resource not found" |
| 100 | + |
| 101 | + |
| 102 | +@mark.passing |
| 103 | +def test_generate_evaluator_result_provider_error_within_tolerance(): |
| 104 | + """Test that provider errors with severity within tolerance are skipped.""" |
| 105 | + # Mock evaluator object with error_tolerance = 2 |
| 106 | + evaluator_obj = { |
| 107 | + "id": "test_evaluator", |
| 108 | + "provider_args": {"operation_type": "attribute", "key": "value"}, |
| 109 | + "condition": {"type": "Equals", "value": True, "error_tolerance": 2}, |
| 110 | + } |
| 111 | + |
| 112 | + # Create a provider error with severity 1 (within tolerance) |
| 113 | + provider_error = {"value": ProviderError(severity_value=1), "err": "Minor issue"} |
| 114 | + |
| 115 | + # Mock the provider function to return the error |
| 116 | + with patch("tirith.core.core.get_evaluator_inputs_from_provider_inputs", return_value=[provider_error]): |
| 117 | + # Create a mapping for EVALUATORS_DICT.get to return a mock evaluator class |
| 118 | + mock_evaluator_dict = {"Equals": MockEvaluator} |
| 119 | + with patch("tirith.core.core.EVALUATORS_DICT", mock_evaluator_dict): |
| 120 | + result = generate_evaluator_result(evaluator_obj, {}, "test_provider") |
| 121 | + |
| 122 | + # Verify the result shows a skipped evaluation |
| 123 | + assert result["passed"] is None |
| 124 | + assert len(result["result"]) == 1 |
| 125 | + assert result["result"][0]["passed"] is None |
| 126 | + assert result["result"][0]["message"] == "Minor issue" |
| 127 | + |
| 128 | + |
| 129 | +@mark.passing |
| 130 | +def test_generate_evaluator_result_multiple_resources_one_failing(): |
| 131 | + """Test that when one resource fails, the entire evaluation fails.""" |
| 132 | + # Mock evaluator object |
| 133 | + evaluator_obj = { |
| 134 | + "id": "test_evaluator", |
| 135 | + "provider_args": {"operation_type": "attribute", "key": "value"}, |
| 136 | + "condition": {"type": "Equals", "value": "expected_value"}, |
| 137 | + } |
| 138 | + |
| 139 | + # Mock provider to return two resources |
| 140 | + with patch( |
| 141 | + "tirith.core.core.get_evaluator_inputs_from_provider_inputs", |
| 142 | + return_value=[{"value": "resource1"}, {"value": "resource2"}], |
| 143 | + ): |
| 144 | + # Create a mapping for EVALUATORS_DICT.get to return our mock evaluator class |
| 145 | + mock_evaluator_dict = {"Equals": MockEvaluator} |
| 146 | + with patch("tirith.core.core.EVALUATORS_DICT", mock_evaluator_dict): |
| 147 | + result = generate_evaluator_result(evaluator_obj, {}, "test_provider") |
| 148 | + |
| 149 | + # Verify the result shows a failed evaluation even though one resource passed |
| 150 | + assert result["passed"] is False |
| 151 | + assert len(result["result"]) == 2 |
| 152 | + assert result["result"][0]["passed"] is True |
| 153 | + assert result["result"][1]["passed"] is False |
0 commit comments