|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from nemoguardrails.actions import action |
| 20 | +from nemoguardrails.actions.output_mapping import ( |
| 21 | + default_output_mapping, |
| 22 | + should_block_output, |
| 23 | +) |
| 24 | + |
| 25 | +# Tests for default_output_mapping |
| 26 | + |
| 27 | + |
| 28 | +def test_default_output_mapping_boolean_true(): |
| 29 | + # For booleans, the mapping returns the negation (block if result is False). |
| 30 | + # If result is True, not True == False, so output is not blocked. |
| 31 | + assert default_output_mapping(True) is False |
| 32 | + |
| 33 | + |
| 34 | +def test_default_output_mapping_boolean_false(): |
| 35 | + # If result is False, then not False == True, so it is blocked. |
| 36 | + assert default_output_mapping(False) is True |
| 37 | + |
| 38 | + |
| 39 | +def test_default_output_mapping_numeric_below_threshold(): |
| 40 | + # For numeric values, block if the value is less than 0.5. |
| 41 | + assert default_output_mapping(0.4) is True |
| 42 | + |
| 43 | + |
| 44 | +def test_default_output_mapping_numeric_above_threshold(): |
| 45 | + # For numeric values greater than or equal to 0.5, do not block. |
| 46 | + assert default_output_mapping(0.5) is False |
| 47 | + assert default_output_mapping(0.6) is False |
| 48 | + |
| 49 | + |
| 50 | +def test_default_output_mapping_non_numeric_non_boolean(): |
| 51 | + # For other types (e.g., strings), default mapping returns False (allowed). |
| 52 | + assert default_output_mapping("anything") is False |
| 53 | + |
| 54 | + |
| 55 | +# Tests for should_block_output |
| 56 | + |
| 57 | + |
| 58 | +# Create a dummy action function with an attached mapping in its metadata. |
| 59 | +def dummy_action_output_mapping(val): |
| 60 | + # For testing, block if the value equals "block", otherwise do not block. |
| 61 | + return val == "block" |
| 62 | + |
| 63 | + |
| 64 | +@action(output_mapping=dummy_action_output_mapping) |
| 65 | +def dummy_action(result): |
| 66 | + return result |
| 67 | + |
| 68 | + |
| 69 | +def test_should_block_output_with_tuple_result_and_mapping(): |
| 70 | + # Test should_block_output when the result is a tuple and the dummy mapping is used. |
| 71 | + # When the first element equals "block", we expect True. |
| 72 | + result = ("block",) |
| 73 | + assert should_block_output(result, dummy_action) is True |
| 74 | + |
| 75 | + # When the result is not "block", we expect False. |
| 76 | + result = ("allow",) |
| 77 | + assert should_block_output(result, dummy_action) is False |
| 78 | + |
| 79 | + |
| 80 | +def test_should_block_output_with_non_tuple_result_and_mapping(): |
| 81 | + # Test should_block_output when the result is not a tuple. |
| 82 | + # The function should wrap it into a tuple. |
| 83 | + result = "block" |
| 84 | + assert should_block_output(result, dummy_action) is True |
| 85 | + |
| 86 | + result = "allow" |
| 87 | + assert should_block_output(result, dummy_action) is False |
| 88 | + |
| 89 | + |
| 90 | +def test_should_block_output_without_action_meta(): |
| 91 | + # Test should_block_output when the action function does not have an "action_meta" attribute. |
| 92 | + # In this case, default_output_mapping should be used. |
| 93 | + def action_without_meta(res): |
| 94 | + return res |
| 95 | + |
| 96 | + # Ensure there is no action_meta attribute. |
| 97 | + if hasattr(action_without_meta, "action_meta"): |
| 98 | + del action_without_meta.action_meta |
| 99 | + |
| 100 | + # Test with a boolean: default_output_mapping for True is False and for False is True. |
| 101 | + assert should_block_output(True, action_without_meta) is False |
| 102 | + assert should_block_output(False, action_without_meta) is True |
| 103 | + |
| 104 | + # Test with a numeric value: block if < 0.5. |
| 105 | + assert should_block_output(0.4, action_without_meta) is True |
| 106 | + assert should_block_output(0.6, action_without_meta) is False |
0 commit comments