From 0ca6ac72a3675cdf9f5f3ffe442e8296f18ea0f1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 10 Jul 2023 20:04:11 +0200 Subject: [PATCH 1/2] Make test cases easier to create and understand --- tests/integration/cases/config_to_model.py | 134 +++++++++++---------- 1 file changed, 68 insertions(+), 66 deletions(-) diff --git a/tests/integration/cases/config_to_model.py b/tests/integration/cases/config_to_model.py index e345818..a3bbe13 100644 --- a/tests/integration/cases/config_to_model.py +++ b/tests/integration/cases/config_to_model.py @@ -1,3 +1,5 @@ +import textwrap + from ..case import Case from ..file import File @@ -6,84 +8,84 @@ 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):", - " ...", - ], + content=textwrap.dedent("""\ + 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(), ), 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):", - " ...", - ], + content=textwrap.dedent("""\ + 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(), ), ), 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", - ], + content=textwrap.dedent("""\ + 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(), ), 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)", - ], + content=textwrap.dedent("""\ + 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(), ), ), ] From 81b205a646ff8d1efbcb9fbed9d4fb3085b12b8d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 11 Jul 2023 17:08:49 +0200 Subject: [PATCH 2/2] before and after --- tests/integration/cases/config_to_model.py | 154 ++++++++++----------- 1 file changed, 74 insertions(+), 80 deletions(-) diff --git a/tests/integration/cases/config_to_model.py b/tests/integration/cases/config_to_model.py index a3bbe13..21aea31 100644 --- a/tests/integration/cases/config_to_model.py +++ b/tests/integration/cases/config_to_model.py @@ -1,91 +1,85 @@ -import textwrap - 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=textwrap.dedent("""\ - 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(), - ), - expected=File( - "config_to_model.py", - content=textwrap.dedent("""\ - 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(), - ), + 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=textwrap.dedent("""\ - 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(), - ), - expected=File( - "config_dict_and_settings.py", - content=textwrap.dedent("""\ - 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(), - ), + 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), ), ]