Skip to content

Commit

Permalink
feat: add tests for AliasGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroMiola committed Jun 15, 2024
1 parent 493c0fd commit 3c2d72f
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 2 deletions.
54 changes: 53 additions & 1 deletion tests/aliasing/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from pydantic import AliasChoices, BaseModel, ConfigDict, Field
from pydantic import AliasChoices, AliasGenerator, BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel, to_pascal


@pytest.fixture
Expand Down Expand Up @@ -89,3 +90,54 @@ class ModelWithValidationAliasPopByName(BaseModel):
first_name: str = Field(validation_alias="firstName")

return ModelWithValidationAliasPopByName


@pytest.fixture
def model_with_alias_generator_and_unset_priority():
class ModelWithAliasGeneratorAndUnsetPriority(BaseModel):
model_config = ConfigDict(
alias_generator=AliasGenerator(
alias=to_camel,
validation_alias=lambda x: x.upper(),
serialization_alias=to_pascal,
)
)
first_name_pa: str = Field(alias="f_name_pa")
first_name_va: str = Field(validation_alias="f_name_va")
first_name_sa: str = Field(serialization_alias="f_name_sa")

return ModelWithAliasGeneratorAndUnsetPriority


@pytest.fixture
def model_with_alias_generator_and_priority_1():
class ModelWithAliasGeneratorAndAliasPriority1(BaseModel):
model_config = ConfigDict(
alias_generator=AliasGenerator(
alias=to_camel,
validation_alias=lambda x: x.upper(),
serialization_alias=to_pascal,
)
)
first_name_pa: str = Field(alias="f_name_pa", alias_priority=1)
first_name_va: str = Field(validation_alias="f_name_va", alias_priority=1)
first_name_sa: str = Field(serialization_alias="f_name_sa", alias_priority=1)

return ModelWithAliasGeneratorAndAliasPriority1


@pytest.fixture
def model_with_alias_generator_and_priority_2():
class ModelWithAliasGeneratorAndAliasPriority2(BaseModel):
model_config = ConfigDict(
alias_generator=AliasGenerator(
alias=to_camel,
validation_alias=lambda x: x.upper(),
serialization_alias=to_pascal,
)
)
first_name_pa: str = Field(alias="f_name_pa", alias_priority=2)
first_name_va: str = Field(validation_alias="f_name_va", alias_priority=2)
first_name_sa: str = Field(serialization_alias="f_name_sa", alias_priority=2)

return ModelWithAliasGeneratorAndAliasPriority2
274 changes: 273 additions & 1 deletion tests/aliasing/test_combination_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from contextlib import nullcontext as does_not_raise

import pytest
from pydantic import ValidationError
from pydantic import AliasGenerator, ValidationError
from pydantic.alias_generators import to_camel, to_pascal


class TestPlainAndSerializationAlias:
Expand Down Expand Up @@ -139,6 +140,10 @@ def test_should_class_attribute_have_field_name(
assert hasattr(model, arg_name) is expected
assert (arg_name in dict(model)) is expected

def test_alias_priority(self, model_with_plain_and_serialization_alias):
model = model_with_plain_and_serialization_alias(firstName="Mickey")
assert model.model_fields["first_name"].alias_priority == 2


class TestPlainAndValidationAlias:
@pytest.mark.parametrize(
Expand Down Expand Up @@ -272,6 +277,10 @@ def test_should_class_attribute_have_field_name(
assert hasattr(model, arg_name) is expected
assert (arg_name in dict(model)) is expected

def test_alias_priority(self, model_with_plain_and_validation_alias):
model = model_with_plain_and_validation_alias(firstName="Mickey")
assert model.model_fields["first_name"].alias_priority == 2


class TestPlainAndSerializationAndValidationAlias:
@pytest.mark.parametrize(
Expand Down Expand Up @@ -390,3 +399,266 @@ def test_should_class_attribute_have_field_name(
model = model_with_plain_and_serialization_and_validation_alias(firstName="Mickey")
assert hasattr(model, arg_name) is expected
assert (arg_name in dict(model)) is expected

def test_alias_priority(self, model_with_plain_and_serialization_and_validation_alias):
model = model_with_plain_and_serialization_and_validation_alias(firstName="Mickey")
assert model.model_fields["first_name"].alias_priority == 2


class TestAliasGeneratorUnsetAliasPriority:
@pytest.mark.parametrize(
"field_name, plain_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_pa",
"f_name_pa",
AliasGenerator(alias=to_camel("first_name_pa")).alias,
AliasGenerator(validation_alias="first_name_pa".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_pa")).serialization_alias,
),
],
)
def test_should_plain_alias_with_unset_priority_override_generated_aliases(
self,
model_with_alias_generator_and_unset_priority,
field_name: str,
plain_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].alias == plain_alias
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].validation_alias == plain_alias
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].serialization_alias == plain_alias

assert model_with_alias_generator_and_unset_priority.model_fields[field_name].alias != gen_plain_alias
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].validation_alias != gen_val_alias
assert (
model_with_alias_generator_and_unset_priority.model_fields[field_name].serialization_alias != gen_ser_alias
)

@pytest.mark.parametrize(
"field_name, val_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_va",
"f_name_va",
AliasGenerator(alias=to_camel("first_name_va")).alias,
AliasGenerator(validation_alias="first_name_va".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_va")).serialization_alias,
),
],
)
def test_should_val_alias_with_unset_priority_override_generated_val_alias(
self,
model_with_alias_generator_and_unset_priority,
field_name: str,
val_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].alias == gen_plain_alias
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].validation_alias == val_alias
assert (
model_with_alias_generator_and_unset_priority.model_fields[field_name].serialization_alias == gen_ser_alias
)

assert model_with_alias_generator_and_unset_priority.model_fields[field_name].validation_alias != gen_val_alias

@pytest.mark.parametrize(
"field_name, ser_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_sa",
"f_name_sa",
AliasGenerator(alias=to_camel("first_name_sa")).alias,
AliasGenerator(validation_alias="first_name_sa".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_sa")).serialization_alias,
),
],
)
def test_should_ser_alias_with_unset_priority_override_generated_ser_alias(
self,
model_with_alias_generator_and_unset_priority,
field_name: str,
ser_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].alias == gen_plain_alias
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].validation_alias == gen_val_alias
assert model_with_alias_generator_and_unset_priority.model_fields[field_name].serialization_alias == ser_alias

assert (
model_with_alias_generator_and_unset_priority.model_fields[field_name].serialization_alias != gen_ser_alias
)


class TestAliasGeneratorAliasPriority1:
@pytest.mark.parametrize(
"field_name, plain_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_pa",
"f_name_pa",
AliasGenerator(alias=to_camel("first_name_pa")).alias,
AliasGenerator(validation_alias="first_name_pa".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_pa")).serialization_alias,
),
],
)
def test_should_plain_alias_with_low_priority_be_overriden_by_generated_plain_alias(
self,
model_with_alias_generator_and_priority_1,
field_name: str,
plain_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_priority_1.model_fields[field_name].alias == gen_plain_alias
assert model_with_alias_generator_and_priority_1.model_fields[field_name].validation_alias == gen_val_alias
assert model_with_alias_generator_and_priority_1.model_fields[field_name].serialization_alias == gen_ser_alias

assert model_with_alias_generator_and_priority_1.model_fields[field_name].alias != plain_alias

@pytest.mark.parametrize(
"field_name, val_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_va",
"f_name_va",
AliasGenerator(alias=to_camel("first_name_va")).alias,
AliasGenerator(validation_alias="first_name_va".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_va")).serialization_alias,
),
],
)
def test_should_val_alias_with_low_priority_be_overriden_by_generated_val_alias(
self,
model_with_alias_generator_and_priority_1,
field_name: str,
val_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_priority_1.model_fields[field_name].alias == gen_plain_alias
assert model_with_alias_generator_and_priority_1.model_fields[field_name].validation_alias == gen_val_alias
assert model_with_alias_generator_and_priority_1.model_fields[field_name].serialization_alias == gen_ser_alias

assert model_with_alias_generator_and_priority_1.model_fields[field_name].validation_alias != val_alias

@pytest.mark.parametrize(
"field_name, ser_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_sa",
"f_name_sa",
AliasGenerator(alias=to_camel("first_name_sa")).alias,
AliasGenerator(validation_alias="first_name_sa".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_sa")).serialization_alias,
),
],
)
def test_should_ser_alias_with_low_priority_be_overridden_by_generated_ser_alias(
self,
model_with_alias_generator_and_priority_1,
field_name: str,
ser_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_priority_1.model_fields[field_name].alias == gen_plain_alias
assert model_with_alias_generator_and_priority_1.model_fields[field_name].validation_alias == gen_val_alias
assert model_with_alias_generator_and_priority_1.model_fields[field_name].serialization_alias == gen_ser_alias

assert model_with_alias_generator_and_priority_1.model_fields[field_name].serialization_alias != ser_alias


class TestAliasGeneratorAliasPriority2:
@pytest.mark.parametrize(
"field_name, plain_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_pa",
"f_name_pa",
AliasGenerator(alias=to_camel("first_name_pa")).alias,
AliasGenerator(validation_alias="first_name_pa".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_pa")).serialization_alias,
),
],
)
def test_should_plain_alias_with_high_priority_override_generated_aliases(
self,
model_with_alias_generator_and_priority_2,
field_name: str,
plain_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_priority_2.model_fields[field_name].alias == plain_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].validation_alias == plain_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].serialization_alias == plain_alias

assert model_with_alias_generator_and_priority_2.model_fields[field_name].alias != gen_plain_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].validation_alias != gen_val_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].serialization_alias != gen_ser_alias

@pytest.mark.parametrize(
"field_name, val_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_va",
"f_name_va",
AliasGenerator(alias=to_camel("first_name_va")).alias,
AliasGenerator(validation_alias="first_name_va".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_va")).serialization_alias,
),
],
)
def test_should_val_alias_with_high_priority_override_generated_val_alias(
self,
model_with_alias_generator_and_priority_2,
field_name: str,
val_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_priority_2.model_fields[field_name].alias == gen_plain_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].validation_alias == val_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].serialization_alias == gen_ser_alias

assert model_with_alias_generator_and_priority_2.model_fields[field_name].validation_alias != gen_val_alias

@pytest.mark.parametrize(
"field_name, ser_alias, gen_plain_alias, gen_val_alias, gen_ser_alias",
[
(
"first_name_sa",
"f_name_sa",
AliasGenerator(alias=to_camel("first_name_sa")).alias,
AliasGenerator(validation_alias="first_name_sa".upper()).validation_alias,
AliasGenerator(serialization_alias=to_pascal("first_name_sa")).serialization_alias,
),
],
)
def test_should_ser_alias_with_high_priority_override_generated_ser_alias(
self,
model_with_alias_generator_and_priority_2,
field_name: str,
ser_alias: str,
gen_plain_alias: str,
gen_val_alias: str,
gen_ser_alias: str,
):
assert model_with_alias_generator_and_priority_2.model_fields[field_name].alias == gen_plain_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].validation_alias == gen_val_alias
assert model_with_alias_generator_and_priority_2.model_fields[field_name].serialization_alias == ser_alias

assert model_with_alias_generator_and_priority_2.model_fields[field_name].serialization_alias != gen_ser_alias

0 comments on commit 3c2d72f

Please sign in to comment.