Coverage for tests/serializers/msgspec/test_serializer.py: 100%

51 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-13 05:19 +0000

1import msgspec 1abcdefgh

2import pytest 1abcdefgh

3 

4from fast_depends import Depends, Provider, inject 1abcdefgh

5from fast_depends.exceptions import ValidationError 1abcdefgh

6from fast_depends.msgspec import MsgSpecSerializer 1abcdefgh

7 

8 

9class TestNativeErrors: 1abcdefgh

10 """`use_fastdepends_errors=False` keeps the original `msgspec` errors.""" 

11 

12 serializer = MsgSpecSerializer(use_fastdepends_errors=False) 1abcdefgh

13 

14 def test_arguments_are_casted(self) -> None: 1abcdefgh

15 @inject(serializer_cls=self.serializer, dependency_provider=Provider()) 1abcdefgh

16 def func(a: int, b: float): 1abcdefgh

17 return a, b 1abcdefgh

18 

19 assert func("1", b="2.5") == (1, 2.5) 1abcdefgh

20 

21 def test_response_is_casted(self) -> None: 1abcdefgh

22 @inject( 1abcdefgh

23 serializer_cls=self.serializer, 

24 dependency_provider=Provider(), 

25 cast_result=True, 

26 ) 

27 def func(a: int) -> float: 1abcdefgh

28 return a 1abcdefgh

29 

30 result = func("1") 1abcdefgh

31 assert isinstance(result, float) 1abcdefgh

32 assert result == 1.0 1abcdefgh

33 

34 def test_argument_error_is_not_wrapped(self) -> None: 1abcdefgh

35 @inject(serializer_cls=self.serializer, dependency_provider=Provider()) 1abcdefgh

36 def func(a: int): 1abcdefgh

37 raise AssertionError("unreachable") 

38 

39 with pytest.raises(msgspec.ValidationError): 1abcdefgh

40 func("not-an-int") 1abcdefgh

41 

42 def test_response_error_is_not_wrapped(self) -> None: 1abcdefgh

43 @inject( 1abcdefgh

44 serializer_cls=self.serializer, 

45 dependency_provider=Provider(), 

46 cast_result=True, 

47 ) 

48 def func() -> int: 1abcdefgh

49 return "not-an-int" 1abcdefgh

50 

51 with pytest.raises(msgspec.ValidationError): 1abcdefgh

52 func() 1abcdefgh

53 

54 

55class TestFastDependsErrors: 1abcdefgh

56 """`use_fastdepends_errors=True` (the default) wraps them instead.""" 

57 

58 serializer = MsgSpecSerializer(use_fastdepends_errors=True) 1abcdefgh

59 

60 def test_argument_error_is_wrapped(self) -> None: 1abcdefgh

61 @inject(serializer_cls=self.serializer, dependency_provider=Provider()) 1abcdefgh

62 def func(a: int): 1abcdefgh

63 raise AssertionError("unreachable") 

64 

65 with pytest.raises(ValidationError): 1abcdefgh

66 func("not-an-int") 1abcdefgh

67 

68 def test_response_error_is_wrapped(self) -> None: 1abcdefgh

69 @inject( 1abcdefgh

70 serializer_cls=self.serializer, 

71 dependency_provider=Provider(), 

72 cast_result=True, 

73 ) 

74 def func() -> int: 1abcdefgh

75 return "not-an-int" 1abcdefgh

76 

77 with pytest.raises(ValidationError): 1abcdefgh

78 func() 1abcdefgh

79 

80 

81@pytest.mark.parametrize( 1abcdefgh

82 "serializer", 

83 ( 

84 pytest.param(MsgSpecSerializer(use_fastdepends_errors=True), id="wrapped"), 

85 pytest.param(MsgSpecSerializer(use_fastdepends_errors=False), id="native"), 

86 ), 

87) 

88def test_field_alias(serializer: MsgSpecSerializer) -> None: 1abcdefgh

89 def dep(nested: int = msgspec.field(name="nestedAlias")) -> int: 1abcdefgh

90 return nested 1abcdefgh

91 

92 @inject(serializer_cls=serializer, dependency_provider=Provider()) 1abcdefgh

93 def func( 1abcdefgh

94 a: int = msgspec.field(name="aliasedA"), 

95 d: int = Depends(dep), 

96 ) -> tuple[int, int]: 

97 return a, d 1abcdefgh

98 

99 assert func(aliasedA="1", nestedAlias="2") == (1, 2) 1abcdefgh