Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remaining tests for alias #5

Merged
merged 5 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ repos:
hooks:
- id: ruff-lint
name: ruff (linter)
entry: just ruff
entry: poetry run ruff check .
language: system
types: [python]
- id: ruff-format
name: ruff (formatter)
entry: just ruff-format
entry: poetry run ruff format .
language: system
types: [python]
59 changes: 57 additions & 2 deletions 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, AliasPath, BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel, to_pascal


@pytest.fixture
Expand Down Expand Up @@ -37,7 +38,10 @@ class ModelWithValidationAliasChoices(BaseModel):
@pytest.fixture
def model_with_validation_alias_path():
class ModelWithValidationAliasPath(BaseModel):
pass
first_name: str = Field(validation_alias=AliasPath("names", 0))
last_name: str = Field(validation_alias=AliasPath("names", 1))

return ModelWithValidationAliasPath


@pytest.fixture
Expand Down Expand Up @@ -89,3 +93,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
Loading