Coverage for tests/test_validator.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-28 15:17 +0000

1import pytest 

2from inline_snapshot import snapshot 

3 

4from prisma import types, validate 

5from prisma._compat import PYDANTIC_V2 

6 

7 

8class Foo: 

9 pass 

10 

11 

12if PYDANTIC_V2: 

13 from pydantic.v1 import ValidationError 

14else: 

15 from pydantic import ValidationError # type: ignore[assignment] 

16 

17 

18def test_valid() -> None: 

19 """Basic usage with correct data""" 

20 validated = validate(types.IntFilter, {'equals': 1}) 

21 assert validated == {'equals': 1} 

22 

23 validated = validate(types.IntFilter, {'equals': '1'}) 

24 assert validated == {'equals': 1} 

25 

26 

27def test_disallows_non_typeddict_type() -> None: 

28 """Validating against non TypedDict types throws an error""" 

29 with pytest.raises(TypeError) as exc: 

30 validate(Foo, None) 

31 

32 assert str(exc.value) == snapshot( 

33 "Only TypedDict types are supported, got: <class 'tests.test_validator.Foo'> instead." 

34 ) 

35 

36 with pytest.raises(TypeError) as exc: 

37 validate(dict, None) 

38 

39 assert str(exc.value) == snapshot("Only TypedDict types are supported, got: <class 'dict'> instead.") 

40 

41 

42def test_non_dictionary_values() -> None: 

43 """Validating a non-dictionary value throws an error""" 

44 with pytest.raises(ValidationError) as exc: 

45 validate(types.UserCreateInput, None) 

46 

47 assert str(exc.value) == snapshot( 

48 """\ 

491 validation error for UserCreateInput 

50__root__ 

51 UserCreateInput expected dict not NoneType (type=type_error)\ 

52""" 

53 ) 

54 

55 

56def test_recursive() -> None: 

57 """Validating recursive types works as expected""" 

58 with pytest.raises(ValidationError) as exc: 

59 validate(types.FloatFilter, {'not': {'not': {'not': 'a'}}}) 

60 

61 assert str(exc.value) == snapshot( 

62 """\ 

634 validation errors for FloatFilter 

64not 

65 value is not a valid float (type=type_error.float) 

66not -> not 

67 value is not a valid float (type=type_error.float) 

68not -> not -> not 

69 value is not a valid float (type=type_error.float) 

70not -> not -> not -> __root__ 

71 FloatFilterRecursive3 expected dict not str (type=type_error)\ 

72""" 

73 ) 

74 

75 validated = validate(types.FloatFilter, {'not': {'not': {'not': '193.4'}}}) 

76 assert validated == {'not': {'not': {'not': 193.4}}} 

77 

78 

79def test_missing_values() -> None: 

80 """TypedDict with required keys is correctly validated""" 

81 with pytest.raises(ValidationError) as exc: 

82 validate(types.PostCreateInput, {}) 

83 

84 assert str(exc.value) == snapshot( 

85 """\ 

862 validation errors for PostCreateInput 

87title 

88 field required (type=value_error.missing) 

89published 

90 field required (type=value_error.missing)\ 

91""" 

92 ) 

93 

94 

95def test_optional_values() -> None: 

96 """Fields that can be None are still included in the validated data""" 

97 validated = validate(types.PostCreateInput, dict(title='My Title', published=True)) 

98 assert validated == {'title': 'My Title', 'published': True} 

99 

100 validated = validate( 

101 type=types.PostCreateInput, 

102 data=dict(title='My Title', published=True, desc=None), 

103 ) 

104 assert validated == { 

105 'title': 'My Title', 

106 'published': True, 

107 'desc': None, 

108 } 

109 

110 

111def test_disallows_extra_values() -> None: 

112 """Fields that are not present in the TypedDict are not allowed""" 

113 with pytest.raises(ValidationError) as exc: 

114 validate(types.PostCreateInput, {'foo': 'bar'}) 

115 

116 assert str(exc.value) == snapshot( 

117 """\ 

1183 validation errors for PostCreateInput 

119title 

120 field required (type=value_error.missing) 

121published 

122 field required (type=value_error.missing) 

123foo 

124 extra fields not permitted (type=value_error.extra)\ 

125""" 

126 )