|
| 1 | +import pytest |
| 2 | + |
| 3 | +from moorcheh_sdk.exceptions import InvalidInputError |
| 4 | +from moorcheh_sdk.utils.decorators import required_args |
| 5 | + |
| 6 | + |
| 7 | +class TestClass: |
| 8 | + @required_args(["a", "b"], types={"a": int, "b": str}) |
| 9 | + def method(self, a, b, c=None): |
| 10 | + return a, b, c |
| 11 | + |
| 12 | + |
| 13 | +def test_required_args_success(): |
| 14 | + """Test that the decorator allows valid arguments.""" |
| 15 | + obj = TestClass() |
| 16 | + assert obj.method(1, "test") == (1, "test", None) |
| 17 | + assert obj.method(a=1, b="test", c="optional") == (1, "test", "optional") |
| 18 | + |
| 19 | + |
| 20 | +def test_required_args_missing_arg(): |
| 21 | + """Test that missing required arguments raise TypeError (caught and re-raised as InvalidInputError).""" |
| 22 | + obj = TestClass() |
| 23 | + # If an argument is missing from the call, python's bind will raise TypeError |
| 24 | + with pytest.raises(InvalidInputError): |
| 25 | + obj.method(a=1) |
| 26 | + |
| 27 | + |
| 28 | +def test_required_args_none_value(): |
| 29 | + """Test that None values for required arguments raise InvalidInputError.""" |
| 30 | + obj = TestClass() |
| 31 | + with pytest.raises(InvalidInputError, match="Argument 'a' cannot be None."): |
| 32 | + obj.method(a=None, b="test") |
| 33 | + |
| 34 | + with pytest.raises(InvalidInputError, match="Argument 'b' cannot be None."): |
| 35 | + obj.method(a=1, b=None) |
| 36 | + |
| 37 | + |
| 38 | +def test_required_args_empty_value(): |
| 39 | + """Test that empty values for string/list arguments raise InvalidInputError.""" |
| 40 | + obj = TestClass() |
| 41 | + with pytest.raises(InvalidInputError, match="Argument 'b' cannot be empty."): |
| 42 | + obj.method(a=1, b="") |
| 43 | + |
| 44 | + |
| 45 | +def test_required_args_wrong_type(): |
| 46 | + """Test that arguments with wrong types raise InvalidInputError.""" |
| 47 | + obj = TestClass() |
| 48 | + with pytest.raises( |
| 49 | + InvalidInputError, match="Argument 'a' must be of type <class 'int'>." |
| 50 | + ): |
| 51 | + obj.method(a="not an int", b="test") |
| 52 | + |
| 53 | + with pytest.raises( |
| 54 | + InvalidInputError, match="Argument 'b' must be of type <class 'str'>." |
| 55 | + ): |
| 56 | + obj.method(a=1, b=123) |
| 57 | + |
| 58 | + |
| 59 | +def test_required_args_allow_zero_false(): |
| 60 | + """Test that 0 and False are not considered empty.""" |
| 61 | + |
| 62 | + @required_args(["num", "flag"], types={"num": int, "flag": bool}) |
| 63 | + def func(num, flag): |
| 64 | + return num, flag |
| 65 | + |
| 66 | + assert func(0, False) == (0, False) |
| 67 | + |
| 68 | + |
| 69 | +def test_required_args_list_validation(): |
| 70 | + """Test validation for list types.""" |
| 71 | + |
| 72 | + @required_args(["items"], types={"items": list}) |
| 73 | + def func(items): |
| 74 | + return items |
| 75 | + |
| 76 | + assert func([1, 2]) == [1, 2] |
| 77 | + |
| 78 | + with pytest.raises(InvalidInputError, match="Argument 'items' cannot be empty."): |
| 79 | + func([]) |
| 80 | + |
| 81 | + with pytest.raises( |
| 82 | + InvalidInputError, match="Argument 'items' must be of type <class 'list'>." |
| 83 | + ): |
| 84 | + func("not a list") |
0 commit comments