|
25 | 25 | import rest_framework |
26 | 26 | from rest_framework import exceptions, serializers |
27 | 27 | from rest_framework.fields import ( |
28 | | - BuiltinSignatureError, DjangoImageField, SkipField, empty, |
| 28 | + AlphabeticFieldValidator, AlphanumericFieldValidator, BuiltinSignatureError, CustomLengthValidator, DjangoImageField, SkipField, empty, |
29 | 29 | is_simple_callable |
30 | 30 | ) |
31 | 31 | from tests.models import UUIDForeignKeyTarget |
@@ -1061,6 +1061,112 @@ class TestFilePathField(FieldValues): |
1061 | 1061 | ) |
1062 | 1062 |
|
1063 | 1063 |
|
| 1064 | +class TestAlphabeticField: |
| 1065 | + valid_inputs = { |
| 1066 | + 'John Doe': 'John Doe', |
| 1067 | + 'Alice': 'Alice', |
| 1068 | + 'Bob Marley': 'Bob Marley', |
| 1069 | + } |
| 1070 | + invalid_inputs = { |
| 1071 | + 'John123': ['This field must contain only alphabetic characters and spaces.'], |
| 1072 | + 'Alice!': ['This field must contain only alphabetic characters and spaces.'], |
| 1073 | + '': ['This field must contain only alphabetic characters and spaces.'], |
| 1074 | + } |
| 1075 | + non_string_inputs = [ |
| 1076 | + 123, # Integer |
| 1077 | + 45.67, # Float |
| 1078 | + None, # NoneType |
| 1079 | + [], # Empty list |
| 1080 | + {}, # Empty dict |
| 1081 | + set() # Empty set |
| 1082 | + ] |
| 1083 | + |
| 1084 | + def test_valid_inputs(self): |
| 1085 | + validator = AlphabeticFieldValidator() |
| 1086 | + for value in self.valid_inputs.keys(): |
| 1087 | + validator(value) |
| 1088 | + |
| 1089 | + def test_invalid_inputs(self): |
| 1090 | + validator = AlphabeticFieldValidator() |
| 1091 | + for value, expected_errors in self.invalid_inputs.items(): |
| 1092 | + with pytest.raises(ValueError) as excinfo: |
| 1093 | + validator(value) |
| 1094 | + assert str(excinfo.value) == expected_errors[0] |
| 1095 | + |
| 1096 | + def test_non_string_inputs(self): |
| 1097 | + validator = AlphabeticFieldValidator() |
| 1098 | + for value in self.non_string_inputs: |
| 1099 | + with pytest.raises(ValueError) as excinfo: |
| 1100 | + validator(value) |
| 1101 | + assert str(excinfo.value) == "This field must be a string." |
| 1102 | + |
| 1103 | + |
| 1104 | +class TestAlphanumericField: |
| 1105 | + valid_inputs = { |
| 1106 | + 'John123': 'John123', |
| 1107 | + 'Alice007': 'Alice007', |
| 1108 | + 'Bob1990': 'Bob1990', |
| 1109 | + } |
| 1110 | + invalid_inputs = { |
| 1111 | + 'John!': ['This field must contain only alphanumeric characters (letters and numbers).'], |
| 1112 | + 'Alice 007': ['This field must contain only alphanumeric characters (letters and numbers).'], |
| 1113 | + '': ['This field must contain only alphanumeric characters (letters and numbers).'], |
| 1114 | + } |
| 1115 | + non_string_inputs = [ |
| 1116 | + 123, # Integer |
| 1117 | + 45.67, # Float |
| 1118 | + None, # NoneType |
| 1119 | + [], # Empty list |
| 1120 | + {}, # Empty dict |
| 1121 | + set() # Empty set |
| 1122 | + ] |
| 1123 | + |
| 1124 | + def test_valid_inputs(self): |
| 1125 | + validator = AlphanumericFieldValidator() |
| 1126 | + for value in self.valid_inputs.keys(): |
| 1127 | + validator(value) |
| 1128 | + |
| 1129 | + def test_invalid_inputs(self): |
| 1130 | + validator = AlphanumericFieldValidator() |
| 1131 | + for value, expected_errors in self.invalid_inputs.items(): |
| 1132 | + with pytest.raises(ValueError) as excinfo: |
| 1133 | + validator(value) |
| 1134 | + assert str(excinfo.value) == expected_errors[0] |
| 1135 | + |
| 1136 | + def test_non_string_inputs(self): |
| 1137 | + validator = AlphanumericFieldValidator() |
| 1138 | + for value in self.non_string_inputs: |
| 1139 | + with pytest.raises(ValueError) as excinfo: |
| 1140 | + validator(value) |
| 1141 | + assert str(excinfo.value) == "This field must be a string." |
| 1142 | + |
| 1143 | +class TestCustomLengthField: |
| 1144 | + """ |
| 1145 | + Valid and invalid values for `CustomLengthValidator`. |
| 1146 | + """ |
| 1147 | + valid_inputs = { |
| 1148 | + 'abc': 'abc', # 3 characters |
| 1149 | + 'abcdefghij': 'abcdefghij', # 10 characters |
| 1150 | + } |
| 1151 | + invalid_inputs = { |
| 1152 | + 'ab': ['This field must be at least 3 characters long.'], # Too short |
| 1153 | + 'abcdefghijk': ['This field must be no more than 10 characters long.'], # Too long |
| 1154 | + } |
| 1155 | + field = str |
| 1156 | + |
| 1157 | + def test_valid_inputs(self): |
| 1158 | + validator = CustomLengthValidator(min_length=3, max_length=10) |
| 1159 | + for value in self.valid_inputs.keys(): |
| 1160 | + validator(value) |
| 1161 | + |
| 1162 | + def test_invalid_inputs(self): |
| 1163 | + validator = CustomLengthValidator(min_length=3, max_length=10) |
| 1164 | + for value, expected_errors in self.invalid_inputs.items(): |
| 1165 | + with pytest.raises(ValueError) as excinfo: |
| 1166 | + validator(value) |
| 1167 | + assert str(excinfo.value) == expected_errors[0] |
| 1168 | + |
| 1169 | + |
1064 | 1170 | # Number types... |
1065 | 1171 |
|
1066 | 1172 | class TestIntegerField(FieldValues): |
|
0 commit comments