From 84bf9e2d278cc62139460cacdf9d40f15377a7d0 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Thu, 16 Jun 2022 19:03:11 -0300 Subject: [PATCH 01/16] feat: add to upper function for strings and bytes --- docs/usage/model_config.md | 3 +++ pydantic/config.py | 1 + pydantic/validators.py | 14 ++++++++++++++ tests/test_types.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/docs/usage/model_config.md b/docs/usage/model_config.md index a3f55fec90..dfc66a2f68 100644 --- a/docs/usage/model_config.md +++ b/docs/usage/model_config.md @@ -28,6 +28,9 @@ _(This script is complete, it should run "as is")_ **`anystr_lower`** : whether to make all characters lowercase for str & byte types (default: `False`) +**`anystr_upper`** +: whether to make all characters uppercase for str & byte types (default: `False`) + **`min_anystr_length`** : the min length for str & byte types (default: `0`) diff --git a/pydantic/config.py b/pydantic/config.py index ef4b3c008f..e19beb5914 100644 --- a/pydantic/config.py +++ b/pydantic/config.py @@ -39,6 +39,7 @@ class Extra(str, Enum): class BaseConfig: title: Optional[str] = None anystr_lower: bool = False + anystr_upper: bool = False anystr_strip_whitespace: bool = False min_anystr_length: int = 0 max_anystr_length: Optional[int] = None diff --git a/pydantic/validators.py b/pydantic/validators.py index d4783d97b1..3df05d7e9b 100644 --- a/pydantic/validators.py +++ b/pydantic/validators.py @@ -206,6 +206,10 @@ def anystr_strip_whitespace(v: 'StrBytes') -> 'StrBytes': return v.strip() +def anystr_upper(v: 'StrBytes') -> 'StrBytes': + return v.upper() + + def anystr_lower(v: 'StrBytes') -> 'StrBytes': return v.lower() @@ -488,6 +492,14 @@ def constr_strip_whitespace(v: 'StrBytes', field: 'ModelField', config: 'BaseCon return v +def constr_upper(v: 'StrBytes', field: 'ModelField', config: 'BaseConfig') -> 'StrBytes': + upper = field.type_.to_upper or config.anystr_upper + if upper: + v = v.upper() + + return v + + def constr_lower(v: 'StrBytes', field: 'ModelField', config: 'BaseConfig') -> 'StrBytes': lower = field.type_.to_lower or config.anystr_lower if lower: @@ -614,6 +626,7 @@ def check(self, config: Type['BaseConfig']) -> bool: [ str_validator, IfConfig(anystr_strip_whitespace, 'anystr_strip_whitespace'), + IfConfig(anystr_upper, 'anystr_upper'), IfConfig(anystr_lower, 'anystr_lower'), IfConfig(anystr_length_validator, 'min_anystr_length', 'max_anystr_length'), ], @@ -623,6 +636,7 @@ def check(self, config: Type['BaseConfig']) -> bool: [ bytes_validator, IfConfig(anystr_strip_whitespace, 'anystr_strip_whitespace'), + IfConfig(anystr_upper, 'anystr_upper'), IfConfig(anystr_lower, 'anystr_lower'), IfConfig(anystr_length_validator, 'min_anystr_length', 'max_anystr_length'), ], diff --git a/tests/test_types.py b/tests/test_types.py index 8e5481719c..c04de26870 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1811,6 +1811,37 @@ class Config: assert m.bytes_check == b' 456 ' +def test_anystr_upper_enabled(): + class Model(BaseModel): + str_check: str + bytes_check: bytes + + class Config: + anystr_upper = True + + m = Model(str_check='ABCDefG', bytes_check=b'abCD1Fg') + + assert m.str_check == 'ABCDEFG' + assert m.bytes_check == b'ABCD1FG' + + +def test_anystr_upper_disabled(): + class Model(BaseModel): + str_check: str + bytes_check: bytes + + class Config: + anystr_upper = False + + str_check_value = 'ABCDefG' + bytes_check_value = b'abCD1Fg' + + m = Model(str_check=str_check_value, bytes_check=bytes_check_value) + + assert m.str_check == str_check_value + assert m.bytes_check == bytes_check_value + + def test_anystr_lower_enabled(): class Model(BaseModel): str_check: str From 6d91cd31c35f2b9e42daf37f055cda49b1760891 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Thu, 16 Jun 2022 19:07:53 -0300 Subject: [PATCH 02/16] docs(changes): add message for change --- changes/4165-satheler.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/4165-satheler.md diff --git a/changes/4165-satheler.md b/changes/4165-satheler.md new file mode 100644 index 0000000000..82ba93abdf --- /dev/null +++ b/changes/4165-satheler.md @@ -0,0 +1 @@ +Add `Config.anystr_upper` and `to_upper` kwarg to constr and conbytes. From 2f98c8f52e2c17ce736b8dff2cf7832f5c4ac002 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Thu, 16 Jun 2022 20:50:17 -0300 Subject: [PATCH 03/16] fix: add constr upper on types --- pydantic/types.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pydantic/types.py b/pydantic/types.py index 2d0cc18f8d..5d9a645a65 100644 --- a/pydantic/types.py +++ b/pydantic/types.py @@ -35,6 +35,7 @@ constr_length_validator, constr_lower, constr_strip_whitespace, + constr_upper, decimal_validator, float_validator, frozenset_validator, @@ -341,6 +342,7 @@ def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None: def __get_validators__(cls) -> 'CallableGenerator': yield strict_bytes_validator if cls.strict else bytes_validator yield constr_strip_whitespace + yield constr_upper yield constr_lower yield constr_length_validator @@ -397,6 +399,7 @@ def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None: def __get_validators__(cls) -> 'CallableGenerator': yield strict_str_validator if cls.strict else str_validator yield constr_strip_whitespace + yield constr_upper yield constr_lower yield constr_length_validator yield cls.validate From d4debfdefd23224e5725a7ceb4a233ff0ea0acd8 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Thu, 16 Jun 2022 20:57:01 -0300 Subject: [PATCH 04/16] fix: add constr upper on types --- pydantic/types.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pydantic/types.py b/pydantic/types.py index 5d9a645a65..7caa0feaf6 100644 --- a/pydantic/types.py +++ b/pydantic/types.py @@ -329,6 +329,7 @@ class StrictFloat(ConstrainedFloat): class ConstrainedBytes(bytes): strip_whitespace = False + to_upper = False to_lower = False min_length: OptionalInt = None max_length: OptionalInt = None @@ -350,6 +351,7 @@ def __get_validators__(cls) -> 'CallableGenerator': def conbytes( *, strip_whitespace: bool = False, + to_upper: bool = False, to_lower: bool = False, min_length: int = None, max_length: int = None, @@ -358,6 +360,7 @@ def conbytes( # use kwargs then define conf in a dict to aid with IDE type hinting namespace = dict( strip_whitespace=strip_whitespace, + to_upper=to_upper, to_lower=to_lower, min_length=min_length, max_length=max_length, @@ -379,6 +382,7 @@ class StrictBytes(ConstrainedBytes): class ConstrainedStr(str): strip_whitespace = False + to_upper = False to_lower = False min_length: OptionalInt = None max_length: OptionalInt = None @@ -419,6 +423,7 @@ def validate(cls, value: Union[str]) -> Union[str]: def constr( *, strip_whitespace: bool = False, + to_upper: bool = False, to_lower: bool = False, strict: bool = False, min_length: int = None, @@ -429,6 +434,7 @@ def constr( # use kwargs then define conf in a dict to aid with IDE type hinting namespace = dict( strip_whitespace=strip_whitespace, + to_upper=to_upper, to_lower=to_lower, strict=strict, min_length=min_length, From f652b7a139a9fe1ca6efaec4e619e012a478babb Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Thu, 16 Jun 2022 21:11:06 -0300 Subject: [PATCH 05/16] feat: add examples and doc usage --- docs/examples/types_constrained.py | 2 ++ docs/usage/types.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/examples/types_constrained.py b/docs/examples/types_constrained.py index eb258c3f92..fd4e7ca635 100644 --- a/docs/examples/types_constrained.py +++ b/docs/examples/types_constrained.py @@ -22,10 +22,12 @@ class Model(BaseModel): + upper_bytes: conbytes(to_upper=True) lower_bytes: conbytes(to_lower=True) short_bytes: conbytes(min_length=2, max_length=10) strip_bytes: conbytes(strip_whitespace=True) + upper_str: constr(to_upper=True) lower_str: constr(to_lower=True) short_str: constr(min_length=2, max_length=10) regex_str: constr(regex=r'^apple (pie|tart|sandwich)$') diff --git a/docs/usage/types.md b/docs/usage/types.md index 86a437ba6e..677b63302f 100644 --- a/docs/usage/types.md +++ b/docs/usage/types.md @@ -894,6 +894,7 @@ The following arguments are available when using the `condecimal` type function The following arguments are available when using the `constr` type function - `strip_whitespace: bool = False`: removes leading and trailing whitespace +- `to_upper: bool = False`: turns all characters to uppercase - `to_lower: bool = False`: turns all characters to lowercase - `strict: bool = False`: controls type coercion - `min_length: int = None`: minimum length of the string @@ -905,6 +906,7 @@ The following arguments are available when using the `constr` type function The following arguments are available when using the `conbytes` type function - `strip_whitespace: bool = False`: removes leading and trailing whitespace +- `to_upper: bool = False`: turns all characters to uppercase - `to_lower: bool = False`: turns all characters to lowercase - `min_length: int = None`: minimum length of the byte string - `max_length: int = None`: maximum length of the byte string From 4efc9bc4e5a0822c43ce67496652479c9cb47d04 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Thu, 16 Jun 2022 21:11:39 -0300 Subject: [PATCH 06/16] test: add test to upper for types --- tests/test_types.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_types.py b/tests/test_types.py index c04de26870..be95c34790 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -113,6 +113,22 @@ def test_constrained_bytes_too_long(): ] +def test_constrained_bytes_upper_enabled(): + class Model(BaseModel): + v: conbytes(to_upper=True) + + m = Model(v=b'abcd') + assert m.v == b'ABCD' + + +def test_constrained_bytes_upper_disabled(): + class Model(BaseModel): + v: conbytes(to_upper=False) + + m = Model(v=b'aBcD') + assert m.v == b'aBcD' + + def test_constrained_bytes_lower_enabled(): class Model(BaseModel): v: conbytes(to_lower=True) @@ -699,6 +715,22 @@ def test_constrained_str_too_long(): ] +def test_constrained_str_upper_enabled(): + class Model(BaseModel): + v: constr(to_upper=True) + + m = Model(v='abcd') + assert m.v == 'ABCD' + + +def test_constrained_str_upper_disabled(): + class Model(BaseModel): + v: constr(to_upper=False) + + m = Model(v='aBcD') + assert m.v == 'aBcD' + + def test_constrained_str_lower_enabled(): class Model(BaseModel): v: constr(to_lower=True) From 582e73897560951bffe4448cec0fece4b181075e Mon Sep 17 00:00:00 2001 From: Gustavo Satheler Date: Sat, 16 Jul 2022 14:18:13 -0300 Subject: [PATCH 07/16] chore: apply suggestions from code review Co-authored-by: Hasan Ramezani --- tests/test_types.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index c774a24c5f..aece47a6fd 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -113,22 +113,19 @@ def test_constrained_bytes_too_long(): ] -def test_constrained_bytes_upper_enabled(): - class Model(BaseModel): - v: conbytes(to_upper=True) - - m = Model(v=b'abcd') - assert m.v == b'ABCD' - - -def test_constrained_bytes_upper_disabled(): +@pytest.mark.parametrize( + 'to_upper, value, result', + [ + (True, b'abcd', b'ABCD'), + (True, b'aBcD', b'aBcD'), + ], +) +def test_constrained_bytes_upper(to_upper, value, result): class Model(BaseModel): - v: conbytes(to_upper=False) - - m = Model(v=b'aBcD') - assert m.v == b'aBcD' - + v: conbytes(to_upper=to_upper) + m = Model(v=value) + assert m.v == result def test_constrained_bytes_lower_enabled(): class Model(BaseModel): v: conbytes(to_lower=True) From 8aa6032d5cbaf5665338116ec17462111a9e92e5 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 16 Jul 2022 14:16:12 -0300 Subject: [PATCH 08/16] chore(docs): reorder `anystr_upper` to under `anystr_lower` --- docs/usage/model_config.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/usage/model_config.md b/docs/usage/model_config.md index 1b78da50f9..a166495b3f 100644 --- a/docs/usage/model_config.md +++ b/docs/usage/model_config.md @@ -25,12 +25,12 @@ _(This script is complete, it should run "as is")_ **`anystr_strip_whitespace`** : whether to strip leading and trailing whitespace for str & byte types (default: `False`) -**`anystr_lower`** -: whether to make all characters lowercase for str & byte types (default: `False`) - **`anystr_upper`** : whether to make all characters uppercase for str & byte types (default: `False`) +**`anystr_lower`** +: whether to make all characters lowercase for str & byte types (default: `False`) + **`min_anystr_length`** : the min length for str & byte types (default: `0`) From f103a1a9b2939ca3456bd3ad4aa5c9e31bc82a71 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 16 Jul 2022 14:22:07 -0300 Subject: [PATCH 09/16] fix(test): adjust parametrizes to constrained bytes upper --- tests/test_types.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_types.py b/tests/test_types.py index aece47a6fd..cd395c13a8 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -117,7 +117,7 @@ def test_constrained_bytes_too_long(): 'to_upper, value, result', [ (True, b'abcd', b'ABCD'), - (True, b'aBcD', b'aBcD'), + (False, b'aBcD', b'aBcD'), ], ) def test_constrained_bytes_upper(to_upper, value, result): @@ -126,6 +126,8 @@ class Model(BaseModel): m = Model(v=value) assert m.v == result + + def test_constrained_bytes_lower_enabled(): class Model(BaseModel): v: conbytes(to_lower=True) From ccb42d3e192c9f983d53b1bb93e2e0655d436bf4 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 23 Jul 2022 19:58:46 -0300 Subject: [PATCH 10/16] refactor: use pytest parametrize for unify test constrained str upper --- tests/test_types.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index cd395c13a8..6118e0e1dc 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -714,20 +714,19 @@ def test_constrained_str_too_long(): ] -def test_constrained_str_upper_enabled(): - class Model(BaseModel): - v: constr(to_upper=True) - - m = Model(v='abcd') - assert m.v == 'ABCD' - - -def test_constrained_str_upper_disabled(): +@pytest.mark.parametrize( + 'to_upper, value, result', + [ + (True, 'abcd', 'ABCD'), + (False, 'aBcD', 'aBcD'), + ], +) +def test_constrained_str_upper(to_upper, value, result): class Model(BaseModel): - v: constr(to_upper=False) + v: constr(to_upper=to_upper) - m = Model(v='aBcD') - assert m.v == 'aBcD' + m = Model(v=value) + assert m.v == result def test_constrained_str_lower_enabled(): From f7bdca9c74306bc28ad6605d9c4047b705421c35 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 23 Jul 2022 20:00:32 -0300 Subject: [PATCH 11/16] refactor: use pytest parametrize for unify test constrained str lower --- tests/test_types.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index 6118e0e1dc..af54496774 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -729,20 +729,19 @@ class Model(BaseModel): assert m.v == result -def test_constrained_str_lower_enabled(): - class Model(BaseModel): - v: constr(to_lower=True) - - m = Model(v='ABCD') - assert m.v == 'abcd' - - -def test_constrained_str_lower_disabled(): +@pytest.mark.parametrize( + 'to_lower, value, result', + [ + (True, 'ABCD', 'abcd'), + (False, 'ABCD', 'ABCD'), + ], +) +def test_constrained_str_lower(to_lower, value, result): class Model(BaseModel): - v: constr(to_lower=False) + v: constr(to_lower=to_lower) - m = Model(v='ABCD') - assert m.v == 'ABCD' + m = Model(v=value) + assert m.v == result def test_constrained_str_max_length_0(): From 8a68576a37dbcaa200ffab5e1b1c823ca23b6947 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 23 Jul 2022 20:07:52 -0300 Subject: [PATCH 12/16] refactor(test): use pytest parametrize for unify test any str upper --- tests/test_types.py | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index af54496774..59f61dd1ec 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1839,35 +1839,22 @@ class Config: assert m.bytes_check == b' 456 ' -def test_anystr_upper_enabled(): - class Model(BaseModel): - str_check: str - bytes_check: bytes - - class Config: - anystr_upper = True - - m = Model(str_check='ABCDefG', bytes_check=b'abCD1Fg') - - assert m.str_check == 'ABCDEFG' - assert m.bytes_check == b'ABCD1FG' - - -def test_anystr_upper_disabled(): +@pytest.mark.parametrize( + 'anystr_upper_input, str_check, bytes_check, result_str_check, result_bytes_check', + [(True, 'ABCDefG', b'abCD1Fg', 'ABCDEFG', b'ABCD1FG'), (False, 'ABCDefG', b'abCD1Fg', 'ABCDefG', b'abCD1Fg')], +) +def test_anystr_upper(anystr_upper_input, str_check, bytes_check, result_str_check, result_bytes_check): class Model(BaseModel): str_check: str bytes_check: bytes class Config: - anystr_upper = False - - str_check_value = 'ABCDefG' - bytes_check_value = b'abCD1Fg' + anystr_upper = anystr_upper_input - m = Model(str_check=str_check_value, bytes_check=bytes_check_value) + m = Model(str_check=str_check, bytes_check=bytes_check) - assert m.str_check == str_check_value - assert m.bytes_check == bytes_check_value + assert m.str_check == result_str_check + assert m.bytes_check == result_bytes_check def test_anystr_lower_enabled(): From 7fcf6a5e5da50cfd96430b1baab01115755a8f22 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 23 Jul 2022 20:11:17 -0300 Subject: [PATCH 13/16] refactor(test): use pytest parametrize for unify test any str lower --- tests/test_types.py | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index 59f61dd1ec..754f6f22cf 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1857,32 +1857,22 @@ class Config: assert m.bytes_check == result_bytes_check -def test_anystr_lower_enabled(): - class Model(BaseModel): - str_check: str - bytes_check: bytes - - class Config: - anystr_lower = True - - m = Model(str_check='ABCDefG', bytes_check=b'abCD1Fg') - - assert m.str_check == 'abcdefg' - assert m.bytes_check == b'abcd1fg' - - -def test_anystr_lower_disabled(): +@pytest.mark.parametrize( + 'anystr_lower_input, str_check, bytes_check, result_str_check, result_bytes_check', + [(True, 'ABCDefG', b'abCD1Fg', 'abcdefg', b'abcd1fg'), (False, 'ABCDefG', b'abCD1Fg', 'ABCDefG', b'abCD1Fg')], +) +def test_anystr_lower(anystr_lower_input, str_check, bytes_check, result_str_check, result_bytes_check): class Model(BaseModel): str_check: str bytes_check: bytes class Config: - anystr_lower = False + anystr_lower = anystr_lower_input - m = Model(str_check='ABCDefG', bytes_check=b'abCD1Fg') + m = Model(str_check=str_check, bytes_check=bytes_check) - assert m.str_check == 'ABCDefG' - assert m.bytes_check == b'abCD1Fg' + assert m.str_check == result_str_check + assert m.bytes_check == result_bytes_check @pytest.mark.parametrize( From 403d60edd9a1f39b45122846b885ecc6c168889f Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 23 Jul 2022 20:24:22 -0300 Subject: [PATCH 14/16] refactor(test): use pytest parametrize for unify test constrained bytes lower --- tests/test_types.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index 754f6f22cf..e7eee42444 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -128,20 +128,19 @@ class Model(BaseModel): assert m.v == result -def test_constrained_bytes_lower_enabled(): - class Model(BaseModel): - v: conbytes(to_lower=True) - - m = Model(v=b'ABCD') - assert m.v == b'abcd' - - -def test_constrained_bytes_lower_disabled(): +@pytest.mark.parametrize( + 'to_lower, value, result', + [ + (True, b'ABCD', b'abcd'), + (False, b'ABCD', b'ABCD'), + ], +) +def test_constrained_bytes_lower(to_lower, value, result): class Model(BaseModel): - v: conbytes(to_lower=False) + v: conbytes(to_lower=to_lower) - m = Model(v=b'ABCD') - assert m.v == b'ABCD' + m = Model(v=value) + assert m.v == result def test_constrained_bytes_strict_true(): From 28f86dae8b135c84532a97a47a1a45beb062f3a5 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 23 Jul 2022 20:32:04 -0300 Subject: [PATCH 15/16] refactor(test): use pytest parametrize for unify test any str strip whitespace --- tests/test_types.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index e7eee42444..3266e1c338 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1812,30 +1812,24 @@ def test_uuid_validation(): ] -def test_anystr_strip_whitespace_enabled(): - class Model(BaseModel): - str_check: str - bytes_check: bytes - - class Config: - anystr_strip_whitespace = True - - m = Model(str_check=' 123 ', bytes_check=b' 456 ') - assert m.str_check == '123' - assert m.bytes_check == b'456' - - -def test_anystr_strip_whitespace_disabled(): +@pytest.mark.parametrize( + 'enabled, str_check, bytes_check, result_str_check, result_bytes_check', + [ + (True, ' 123 ', b' 456 ', '123', b'456'), + (False, ' 123 ', b' 456 ', ' 123 ', b' 456 '), + ], +) +def test_anystr_strip_whitespace(enabled, str_check, bytes_check, result_str_check, result_bytes_check): class Model(BaseModel): str_check: str bytes_check: bytes class Config: - anystr_strip_whitespace = False + anystr_strip_whitespace = enabled - m = Model(str_check=' 123 ', bytes_check=b' 456 ') - assert m.str_check == ' 123 ' - assert m.bytes_check == b' 456 ' + m = Model(str_check=str_check, bytes_check=bytes_check) + assert m.str_check == result_str_check + assert m.bytes_check == result_bytes_check @pytest.mark.parametrize( From 2748bd54dc6a8278a861d72bd1269d63e6d03848 Mon Sep 17 00:00:00 2001 From: Gustavo Bittencourt Satheler Date: Sat, 23 Jul 2022 20:33:47 -0300 Subject: [PATCH 16/16] refactor(test): change test signatures to improve readability --- tests/test_types.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index 3266e1c338..acf722b463 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -1833,16 +1833,16 @@ class Config: @pytest.mark.parametrize( - 'anystr_upper_input, str_check, bytes_check, result_str_check, result_bytes_check', + 'enabled, str_check, bytes_check, result_str_check, result_bytes_check', [(True, 'ABCDefG', b'abCD1Fg', 'ABCDEFG', b'ABCD1FG'), (False, 'ABCDefG', b'abCD1Fg', 'ABCDefG', b'abCD1Fg')], ) -def test_anystr_upper(anystr_upper_input, str_check, bytes_check, result_str_check, result_bytes_check): +def test_anystr_upper(enabled, str_check, bytes_check, result_str_check, result_bytes_check): class Model(BaseModel): str_check: str bytes_check: bytes class Config: - anystr_upper = anystr_upper_input + anystr_upper = enabled m = Model(str_check=str_check, bytes_check=bytes_check) @@ -1851,16 +1851,16 @@ class Config: @pytest.mark.parametrize( - 'anystr_lower_input, str_check, bytes_check, result_str_check, result_bytes_check', + 'enabled, str_check, bytes_check, result_str_check, result_bytes_check', [(True, 'ABCDefG', b'abCD1Fg', 'abcdefg', b'abcd1fg'), (False, 'ABCDefG', b'abCD1Fg', 'ABCDefG', b'abCD1Fg')], ) -def test_anystr_lower(anystr_lower_input, str_check, bytes_check, result_str_check, result_bytes_check): +def test_anystr_lower(enabled, str_check, bytes_check, result_str_check, result_bytes_check): class Model(BaseModel): str_check: str bytes_check: bytes class Config: - anystr_lower = anystr_lower_input + anystr_lower = enabled m = Model(str_check=str_check, bytes_check=bytes_check)