|
1 | 1 | import re
|
2 | 2 | from decimal import Decimal
|
3 |
| -from typing import Any, Dict |
| 3 | +from typing import Any, Dict, Union |
4 | 4 |
|
5 | 5 | import pytest
|
6 | 6 |
|
@@ -134,12 +134,28 @@ def test_unicode_error():
|
134 | 134 | ]
|
135 | 135 |
|
136 | 136 |
|
137 |
| -def test_str_constrained(): |
138 |
| - v = SchemaValidator({'type': 'str', 'max_length': 5}) |
139 |
| - assert v.validate_python('test') == 'test' |
140 |
| - |
141 |
| - with pytest.raises(ValidationError, match='String should have at most 5 characters'): |
142 |
| - v.validate_python('test long') |
| 137 | +@pytest.mark.parametrize( |
| 138 | + ('data', 'max_length', 'error'), |
| 139 | + [ |
| 140 | + pytest.param('test', 5, None, id='short string'), |
| 141 | + pytest.param('test long', 5, 'String should have at most 5 characters', id='long string'), |
| 142 | + pytest.param('␛⯋℃▤', 5, None, id='short string with unicode characters'), |
| 143 | + pytest.param( |
| 144 | + '␛⯋℃▤⩥⠫⳼⣪⨺✒⧐♳⩚⏭⏣⍥┙⧃Ⰴ┽⏏♜', |
| 145 | + 5, |
| 146 | + 'String should have at most 5 characters', |
| 147 | + id='long string with unicode characters', |
| 148 | + ), |
| 149 | + pytest.param('а' * 25, 32, None, id='a lot of `а`s'), |
| 150 | + ], |
| 151 | +) |
| 152 | +def test_str_constrained(data: str, max_length: int, error: Union[re.Pattern, None]): |
| 153 | + v = SchemaValidator({'type': 'str', 'max_length': max_length}) |
| 154 | + if error is None: |
| 155 | + assert v.validate_python(data) == data |
| 156 | + else: |
| 157 | + with pytest.raises(ValidationError, match=error): |
| 158 | + v.validate_python(data) |
143 | 159 |
|
144 | 160 |
|
145 | 161 | def test_str_constrained_config():
|
|
0 commit comments