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

Add possibility to override config #1

Merged
merged 2 commits into from
May 5, 2020
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
6 changes: 4 additions & 2 deletions pydantic_sqlalchemy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class OrmConfig(BaseConfig):
orm_mode = True


def sqlalchemy_to_pydantic(db_model: Type) -> Type[BaseModel]:
def sqlalchemy_to_pydantic(
db_model: Type, *, config: Type = OrmConfig
) -> Type[BaseModel]:
mapper = inspect(db_model)
fields = {}
for attr in mapper.attrs:
Expand All @@ -23,6 +25,6 @@ def sqlalchemy_to_pydantic(db_model: Type) -> Type[BaseModel]:
default = ...
fields[name] = (python_type, default)
pydantic_model = create_model(
db_model.__name__, __config__=OrmConfig, **fields # type: ignore
db_model.__name__, __config__=config, **fields # type: ignore
)
return pydantic_model
48 changes: 39 additions & 9 deletions tests/test_pydantic_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ class Address(Base):
user = relationship("User", back_populates="addresses")


PydanticUser = sqlalchemy_to_pydantic(User)
PydanticAddress = sqlalchemy_to_pydantic(Address)


class PydanticUserWithAddresses(PydanticUser):
addresses: List[PydanticAddress] = []


Base.metadata.create_all(engine)


Expand All @@ -56,7 +48,13 @@ class PydanticUserWithAddresses(PydanticUser):
db.commit()


def test_pydantic_sqlalchemy():
def test_defaults() -> None:
PydanticUser = sqlalchemy_to_pydantic(User)
PydanticAddress = sqlalchemy_to_pydantic(Address)

class PydanticUserWithAddresses(PydanticUser):
addresses: List[PydanticAddress] = []

user = db.query(User).first()
pydantic_user = PydanticUser.from_orm(user)
data = pydantic_user.dict()
Expand All @@ -78,3 +76,35 @@ def test_pydantic_sqlalchemy():
{"email_address": "eddy@example.com", "id": 2, "user_id": 1},
],
}


def test_config() -> None:
class Config:
orm_mode = True
allow_population_by_field_name = True

@classmethod
def alias_generator(cls, string: str) -> str:
pascal_case = "".join(word.capitalize() for word in string.split("_"))
camel_case = pascal_case[0].lower() + pascal_case[1:]
return camel_case

PydanticUser = sqlalchemy_to_pydantic(User)
PydanticAddress = sqlalchemy_to_pydantic(Address, config=Config)

class PydanticUserWithAddresses(PydanticUser):
addresses: List[PydanticAddress] = []

user = db.query(User).first()
pydantic_user_with_addresses = PydanticUserWithAddresses.from_orm(user)
data = pydantic_user_with_addresses.dict(by_alias=True)
assert data == {
"fullname": "Ed Jones",
"id": 1,
"name": "ed",
"nickname": "edsnickname",
"addresses": [
{"emailAddress": "ed@example.com", "id": 1, "userId": 1},
{"emailAddress": "eddy@example.com", "id": 2, "userId": 1},
],
}