|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from semantic_kernel import Kernel |
| 6 | +from semantic_kernel.core_skills import MathSkill |
| 7 | +from semantic_kernel.orchestration.context_variables import ContextVariables |
| 8 | + |
| 9 | + |
| 10 | +def test_can_be_instantiated(): |
| 11 | + skill = MathSkill() |
| 12 | + assert skill is not None |
| 13 | + |
| 14 | + |
| 15 | +def test_can_be_imported(): |
| 16 | + kernel = Kernel() |
| 17 | + assert kernel.import_skill(MathSkill(), "math") |
| 18 | + assert kernel.skills.has_native_function("math", "add") |
| 19 | + assert kernel.skills.has_native_function("math", "subtract") |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("initial_Value, amount, expectedResult", [ |
| 23 | + ("10", "10", "20"), |
| 24 | + ("0", "10", "10"), |
| 25 | + ("0", "-10", "-10"), |
| 26 | + ("10", "0", "10"), |
| 27 | + ("-1", "10", "9"), |
| 28 | + ("-10", "10", "0"), |
| 29 | + ("-192", "13", "-179"), |
| 30 | + ("-192", "-13", "-205") |
| 31 | +]) |
| 32 | +def test_add_when_valid_parameters_should_succeed(initial_Value, amount, expectedResult): |
| 33 | + # Arrange |
| 34 | + context = ContextVariables() |
| 35 | + context["Amount"] = amount |
| 36 | + skill = MathSkill() |
| 37 | + |
| 38 | + # Act |
| 39 | + result = skill.add(initial_Value, context) |
| 40 | + |
| 41 | + # Assert |
| 42 | + assert result == expectedResult |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize("initial_Value, amount, expectedResult", [ |
| 46 | + ("10", "10", "0"), |
| 47 | + ("0", "10", "-10"), |
| 48 | + ("10", "0", "10"), |
| 49 | + ("100", "-10", "110"), |
| 50 | + ("100", "102", "-2"), |
| 51 | + ("-1", "10", "-11"), |
| 52 | + ("-10", "10", "-20"), |
| 53 | + ("-192", "13", "-205") |
| 54 | +]) |
| 55 | +def test_subtract_when_valid_parameters_should_succeed(initial_Value, amount, expectedResult): |
| 56 | + # Arrange |
| 57 | + context = ContextVariables() |
| 58 | + context["Amount"] = amount |
| 59 | + skill = MathSkill() |
| 60 | + |
| 61 | + # Act |
| 62 | + result = skill.subtract(initial_Value, context) |
| 63 | + |
| 64 | + # Assert |
| 65 | + assert result == expectedResult |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("initial_Value", [ |
| 69 | + "$0", |
| 70 | + "one hundred", |
| 71 | + "20..,,2,1", |
| 72 | + ".2,2.1", |
| 73 | + "0.1.0", |
| 74 | + "00-099", |
| 75 | + "¹²¹", |
| 76 | + "2²", |
| 77 | + "zero", |
| 78 | + "-100 units", |
| 79 | + "1 banana" |
| 80 | +]) |
| 81 | +def test_add_when_invalid_initial_value_should_throw(initial_Value): |
| 82 | + # Arrange |
| 83 | + context = ContextVariables() |
| 84 | + context["Amount"] = "1" |
| 85 | + skill = MathSkill() |
| 86 | + |
| 87 | + # Act |
| 88 | + with pytest.raises(ValueError) as exception: |
| 89 | + result = skill.add(initial_Value, context) |
| 90 | + |
| 91 | + # Assert |
| 92 | + assert str( |
| 93 | + exception.value) == f"Initial value provided is not in numeric format: {initial_Value}" |
| 94 | + assert exception.type == ValueError |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize('amount', [ |
| 98 | + "$0", |
| 99 | + "one hundred", |
| 100 | + "20..,,2,1", |
| 101 | + ".2,2.1", |
| 102 | + "0.1.0", |
| 103 | + "00-099", |
| 104 | + "¹²¹", |
| 105 | + "2²", |
| 106 | + "zero", |
| 107 | + "-100 units", |
| 108 | + "1 banana", |
| 109 | +]) |
| 110 | +def test_add_when_invalid_amount_should_throw(amount): |
| 111 | + # Arrange |
| 112 | + context = ContextVariables() |
| 113 | + context["Amount"] = amount |
| 114 | + skill = MathSkill() |
| 115 | + |
| 116 | + # Act / Assert |
| 117 | + with pytest.raises(ValueError) as exception: |
| 118 | + result = skill.add("1", context) |
| 119 | + |
| 120 | + assert str( |
| 121 | + exception.value) == f"Context amount provided is not in numeric format: {amount}" |
| 122 | + assert exception.type == ValueError |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize("initial_value", [ |
| 126 | + "$0", |
| 127 | + "one hundred", |
| 128 | + "20..,,2,1", |
| 129 | + ".2,2.1", |
| 130 | + "0.1.0", |
| 131 | + "00-099", |
| 132 | + "¹²¹", |
| 133 | + "2²", |
| 134 | + "zero", |
| 135 | + "-100 units", |
| 136 | + "1 banana", |
| 137 | +]) |
| 138 | +def test_subtract_when_invalid_initial_value_should_throw(initial_value): |
| 139 | + # Arrange |
| 140 | + context = ContextVariables() |
| 141 | + context["Amount"] = "1" |
| 142 | + skill = MathSkill() |
| 143 | + |
| 144 | + # Act / Assert |
| 145 | + with pytest.raises(ValueError) as exception: |
| 146 | + result = skill.subtract(initial_value, context) |
| 147 | + |
| 148 | + # Assert |
| 149 | + assert str( |
| 150 | + exception.value) == f"Initial value provided is not in numeric format: {initial_value}" |
| 151 | + assert exception.type == ValueError |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.parametrize("amount", [ |
| 155 | + "$0", |
| 156 | + "one hundred", |
| 157 | + "20..,,2,1", |
| 158 | + ".2,2.1", |
| 159 | + "0.1.0", |
| 160 | + "00-099", |
| 161 | + "¹²¹", |
| 162 | + "2²", |
| 163 | + "zero", |
| 164 | + "-100 units", |
| 165 | + "1 banana", |
| 166 | +]) |
| 167 | +def test_subtract_when_invalid_amount_should_throw(amount): |
| 168 | + # Arrange |
| 169 | + context = ContextVariables() |
| 170 | + context["Amount"] = amount |
| 171 | + skill = MathSkill() |
| 172 | + |
| 173 | + # Act / Assert |
| 174 | + with pytest.raises(ValueError) as exception: |
| 175 | + result = skill.subtract("1", context) |
| 176 | + |
| 177 | + # Assert |
| 178 | + assert str( |
| 179 | + exception.value) == f"Context amount provided is not in numeric format: {amount}" |
| 180 | + assert exception.type == ValueError |
0 commit comments