| 216 | |
| 217 | |
| 218 | def test_int_validation(): |
| 219 | class Model(BaseModel): |
| 220 | a: int |
| 221 | |
| 222 | with pytest.raises(ValidationError) as exc_info: |
| 223 | Model(a='snap') |
| 224 | assert exc_info.value.errors(include_url=False) == [ |
| 225 | { |
| 226 | 'type': 'int_parsing', |
| 227 | 'loc': ('a',), |
| 228 | 'msg': 'Input should be a valid integer, unable to parse string as an integer', |
| 229 | 'input': 'snap', |
| 230 | } |
| 231 | ] |
| 232 | assert Model(a=3).a == 3 |
| 233 | assert Model(a=True).a == 1 |
| 234 | assert Model(a=False).a == 0 |
| 235 | with pytest.raises(ValidationError) as exc_info: |
| 236 | Model(a=4.5) |
| 237 | assert exc_info.value.errors(include_url=False) == [ |
| 238 | { |
| 239 | 'type': 'int_from_float', |
| 240 | 'loc': ('a',), |
| 241 | 'msg': 'Input should be a valid integer, got a number with a fractional part', |
| 242 | 'input': 4.5, |
| 243 | } |
| 244 | ] |
| 245 | |
| 246 | # Doesn't raise ValidationError for number > (2 ^ 63) - 1 |
| 247 | assert Model(a=(2**63) + 100).a == (2**63) + 100 |
| 248 | |
| 249 | |
| 250 | @pytest.mark.parametrize('value', [2.2250738585072011e308, float('nan'), float('inf')]) |