|
| 1 | +import math |
| 2 | + |
| 3 | +import pytest |
| 4 | +from collections import OrderedDict |
| 5 | +from unittest.mock import patch, MagicMock |
| 6 | + |
| 7 | +mock_nodes = MagicMock() |
| 8 | +mock_nodes.MAX_RESOLUTION = 16384 |
| 9 | +mock_server = MagicMock() |
| 10 | + |
| 11 | +with patch.dict("sys.modules", {"nodes": mock_nodes, "server": mock_server}): |
| 12 | + from comfy_extras.nodes_math import MathExpressionNode |
| 13 | + |
| 14 | + |
| 15 | +class TestMathExpressionExecute: |
| 16 | + @staticmethod |
| 17 | + def _exec(expression: str, **kwargs) -> object: |
| 18 | + values = OrderedDict(kwargs) |
| 19 | + return MathExpressionNode.execute(expression, values) |
| 20 | + |
| 21 | + def test_addition(self): |
| 22 | + result = self._exec("a + b", a=3, b=4) |
| 23 | + assert result[0] == 7.0 |
| 24 | + assert result[1] == 7 |
| 25 | + |
| 26 | + def test_subtraction(self): |
| 27 | + result = self._exec("a - b", a=10, b=3) |
| 28 | + assert result[0] == 7.0 |
| 29 | + assert result[1] == 7 |
| 30 | + |
| 31 | + def test_multiplication(self): |
| 32 | + result = self._exec("a * b", a=3, b=5) |
| 33 | + assert result[0] == 15.0 |
| 34 | + assert result[1] == 15 |
| 35 | + |
| 36 | + def test_division(self): |
| 37 | + result = self._exec("a / b", a=10, b=4) |
| 38 | + assert result[0] == 2.5 |
| 39 | + assert result[1] == 2 |
| 40 | + |
| 41 | + def test_single_input(self): |
| 42 | + result = self._exec("a * 2", a=5) |
| 43 | + assert result[0] == 10.0 |
| 44 | + assert result[1] == 10 |
| 45 | + |
| 46 | + def test_three_inputs(self): |
| 47 | + result = self._exec("a + b + c", a=1, b=2, c=3) |
| 48 | + assert result[0] == 6.0 |
| 49 | + assert result[1] == 6 |
| 50 | + |
| 51 | + def test_float_inputs(self): |
| 52 | + result = self._exec("a + b", a=1.5, b=2.5) |
| 53 | + assert result[0] == 4.0 |
| 54 | + assert result[1] == 4 |
| 55 | + |
| 56 | + def test_mixed_int_float_inputs(self): |
| 57 | + result = self._exec("a * b", a=1024, b=1.5) |
| 58 | + assert result[0] == 1536.0 |
| 59 | + assert result[1] == 1536 |
| 60 | + |
| 61 | + def test_mixed_resolution_scale(self): |
| 62 | + result = self._exec("a * b", a=512, b=0.75) |
| 63 | + assert result[0] == 384.0 |
| 64 | + assert result[1] == 384 |
| 65 | + |
| 66 | + def test_sum_values_array(self): |
| 67 | + result = self._exec("sum(values)", a=1, b=2, c=3) |
| 68 | + assert result[0] == 6.0 |
| 69 | + |
| 70 | + def test_sum_variadic(self): |
| 71 | + result = self._exec("sum(a, b, c)", a=1, b=2, c=3) |
| 72 | + assert result[0] == 6.0 |
| 73 | + |
| 74 | + def test_min_values(self): |
| 75 | + result = self._exec("min(values)", a=5, b=2, c=8) |
| 76 | + assert result[0] == 2.0 |
| 77 | + |
| 78 | + def test_max_values(self): |
| 79 | + result = self._exec("max(values)", a=5, b=2, c=8) |
| 80 | + assert result[0] == 8.0 |
| 81 | + |
| 82 | + def test_abs_function(self): |
| 83 | + result = self._exec("abs(a)", a=-7) |
| 84 | + assert result[0] == 7.0 |
| 85 | + assert result[1] == 7 |
| 86 | + |
| 87 | + def test_sqrt(self): |
| 88 | + result = self._exec("sqrt(a)", a=16) |
| 89 | + assert result[0] == 4.0 |
| 90 | + assert result[1] == 4 |
| 91 | + |
| 92 | + def test_ceil(self): |
| 93 | + result = self._exec("ceil(a)", a=2.3) |
| 94 | + assert result[0] == 3.0 |
| 95 | + assert result[1] == 3 |
| 96 | + |
| 97 | + def test_floor(self): |
| 98 | + result = self._exec("floor(a)", a=2.7) |
| 99 | + assert result[0] == 2.0 |
| 100 | + assert result[1] == 2 |
| 101 | + |
| 102 | + def test_sin(self): |
| 103 | + result = self._exec("sin(a)", a=0) |
| 104 | + assert result[0] == 0.0 |
| 105 | + |
| 106 | + def test_log10(self): |
| 107 | + result = self._exec("log10(a)", a=100) |
| 108 | + assert result[0] == 2.0 |
| 109 | + assert result[1] == 2 |
| 110 | + |
| 111 | + def test_float_output_type(self): |
| 112 | + result = self._exec("a + b", a=1, b=2) |
| 113 | + assert isinstance(result[0], float) |
| 114 | + |
| 115 | + def test_int_output_type(self): |
| 116 | + result = self._exec("a + b", a=1, b=2) |
| 117 | + assert isinstance(result[1], int) |
| 118 | + |
| 119 | + def test_non_numeric_result_raises(self): |
| 120 | + with pytest.raises(ValueError, match="must evaluate to a numeric result"): |
| 121 | + self._exec("'hello'", a=42) |
| 122 | + |
| 123 | + def test_undefined_function_raises(self): |
| 124 | + with pytest.raises(Exception, match="not defined"): |
| 125 | + self._exec("str(a)", a=42) |
| 126 | + |
| 127 | + def test_boolean_result_raises(self): |
| 128 | + with pytest.raises(ValueError, match="got bool"): |
| 129 | + self._exec("a > b", a=5, b=3) |
| 130 | + |
| 131 | + def test_empty_expression_raises(self): |
| 132 | + with pytest.raises(ValueError, match="Expression cannot be empty"): |
| 133 | + self._exec("", a=1) |
| 134 | + |
| 135 | + def test_whitespace_only_expression_raises(self): |
| 136 | + with pytest.raises(ValueError, match="Expression cannot be empty"): |
| 137 | + self._exec(" ", a=1) |
| 138 | + |
| 139 | + # --- Missing function coverage (round, pow, log, log2, cos, tan) --- |
| 140 | + |
| 141 | + def test_round(self): |
| 142 | + result = self._exec("round(a)", a=2.7) |
| 143 | + assert result[0] == 3.0 |
| 144 | + assert result[1] == 3 |
| 145 | + |
| 146 | + def test_round_with_ndigits(self): |
| 147 | + result = self._exec("round(a, 2)", a=3.14159) |
| 148 | + assert result[0] == pytest.approx(3.14) |
| 149 | + |
| 150 | + def test_pow(self): |
| 151 | + result = self._exec("pow(a, b)", a=2, b=10) |
| 152 | + assert result[0] == 1024.0 |
| 153 | + assert result[1] == 1024 |
| 154 | + |
| 155 | + def test_log(self): |
| 156 | + result = self._exec("log(a)", a=math.e) |
| 157 | + assert result[0] == pytest.approx(1.0) |
| 158 | + |
| 159 | + def test_log2(self): |
| 160 | + result = self._exec("log2(a)", a=8) |
| 161 | + assert result[0] == pytest.approx(3.0) |
| 162 | + |
| 163 | + def test_cos(self): |
| 164 | + result = self._exec("cos(a)", a=0) |
| 165 | + assert result[0] == 1.0 |
| 166 | + |
| 167 | + def test_tan(self): |
| 168 | + result = self._exec("tan(a)", a=0) |
| 169 | + assert result[0] == 0.0 |
| 170 | + |
| 171 | + # --- int/float converter functions --- |
| 172 | + |
| 173 | + def test_int_converter(self): |
| 174 | + result = self._exec("int(a / b)", a=7, b=2) |
| 175 | + assert result[1] == 3 |
| 176 | + |
| 177 | + def test_float_converter(self): |
| 178 | + result = self._exec("float(a)", a=5) |
| 179 | + assert result[0] == 5.0 |
| 180 | + |
| 181 | + # --- Error path tests --- |
| 182 | + |
| 183 | + def test_division_by_zero_raises(self): |
| 184 | + with pytest.raises(ZeroDivisionError): |
| 185 | + self._exec("a / b", a=1, b=0) |
| 186 | + |
| 187 | + def test_sqrt_negative_raises(self): |
| 188 | + with pytest.raises(ValueError, match="math domain error"): |
| 189 | + self._exec("sqrt(a)", a=-1) |
| 190 | + |
| 191 | + def test_overflow_inf_raises(self): |
| 192 | + with pytest.raises(ValueError, match="non-finite result"): |
| 193 | + self._exec("a * b", a=1e308, b=10) |
| 194 | + |
| 195 | + def test_pow_huge_exponent_raises(self): |
| 196 | + with pytest.raises(ValueError, match="Exponent .* exceeds maximum"): |
| 197 | + self._exec("pow(a, b)", a=10, b=10000000) |
0 commit comments