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

Make test cases easier to create and understand #76

Merged
merged 2 commits into from
Jul 11, 2023
Merged
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
152 changes: 74 additions & 78 deletions tests/integration/cases/config_to_model.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,85 @@
from ..case import Case
from ..file import File

config_to_model_before = """\
from pydantic import BaseModel


class A(BaseModel):
class Config:
orm_mode = True
validate_all = True


class BaseConfig:
orm_mode = True
validate_all = True


class B(BaseModel):
class Config(BaseConfig):
...
""".splitlines()

config_to_model_after = """\
from pydantic import ConfigDict, BaseModel


class A(BaseModel):
model_config = ConfigDict(from_attributes=True, validate_default=True)


class BaseConfig:
orm_mode = True
validate_all = True


class B(BaseModel):
# TODO[pydantic]: The `Config` class inherits from another class, please create the `model_config` manually.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
class Config(BaseConfig):
...
""".splitlines()

config_dict_and_settings_before = """\
from pydantic import BaseModel, BaseSettings


class Settings(BaseSettings):
sentry_dsn: str

class Config:
orm_mode = True


class A(BaseModel):
class Config:
orm_mode = True
""".splitlines()

config_dict_and_settings_after = """\
from pydantic import ConfigDict, BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
sentry_dsn: str
model_config = SettingsConfigDict(from_attributes=True)


class A(BaseModel):
model_config = ConfigDict(from_attributes=True)
""".splitlines()

cases = [
Case(
id="Replace Config class to model",
input=File(
"config_to_model.py",
content=[
"from pydantic import BaseModel",
"",
"",
"class A(BaseModel):",
" class Config:",
" orm_mode = True",
" validate_all = True",
"",
"",
"class BaseConfig:",
" orm_mode = True",
" validate_all = True",
"",
"",
"class B(BaseModel):",
" class Config(BaseConfig):",
" ...",
],
),
expected=File(
"config_to_model.py",
content=[
"from pydantic import ConfigDict, BaseModel",
"",
"",
"class A(BaseModel):",
" model_config = ConfigDict(from_attributes=True, validate_default=True)",
"",
"",
"class BaseConfig:",
" orm_mode = True",
" validate_all = True",
"",
"",
"class B(BaseModel):",
" # TODO[pydantic]: The `Config` class inherits from another class, please create the `model_config` manually.", # noqa: E501
" # Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.",
" class Config(BaseConfig):",
" ...",
],
),
input=File("config_to_model.py", content=config_to_model_before),
expected=File("config_to_model.py", content=config_to_model_after),
),
Case(
id="Replace Config class on BaseSettings",
input=File(
"config_dict_and_settings.py",
content=[
"from pydantic import BaseModel, BaseSettings",
"",
"",
"class Settings(BaseSettings):",
" sentry_dsn: str",
"",
" class Config:",
" orm_mode = True",
"",
"",
"class A(BaseModel):",
" class Config:",
" orm_mode = True",
],
),
expected=File(
"config_dict_and_settings.py",
content=[
"from pydantic import ConfigDict, BaseModel",
"from pydantic_settings import BaseSettings, SettingsConfigDict",
"",
"",
"class Settings(BaseSettings):",
" sentry_dsn: str",
" model_config = SettingsConfigDict(from_attributes=True)",
"",
"",
"class A(BaseModel):",
" model_config = ConfigDict(from_attributes=True)",
],
),
input=File("config_dict_and_settings.py", content=config_dict_and_settings_before),
expected=File("config_dict_and_settings.py", content=config_dict_and_settings_after),
),
]