Skip to content

Commit

Permalink
added bytes type for Pydantic (#31)
Browse files Browse the repository at this point in the history
* added bytes type for Pydantic

* Testing a byte type field for a pydantic model

* removed autogenerated models.py

* W292 no newline at end of file

* W292 no newline at end of file

Co-authored-by: typhoon <[email protected]>
  • Loading branch information
vortex14 and typhoon authored Apr 21, 2022
1 parent a523933 commit 7a2a960
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions omymodels/models/pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
numeric_types,
boolean_types,
datetime_types,
binary_types,
json_types,
text_types,
)
Expand All @@ -21,6 +22,7 @@
datetime_types: "datetime.datetime",
json_types: "Json",
text_types: "str",
binary_types: "bytes"
}

types_mapping = populate_types_mapping(mapper)
Expand Down
7 changes: 7 additions & 0 deletions omymodels/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
"date",
)

binary_types = (
"BINARY",
"VARBINARY",
"binary",
"varbinary",
)

json_types = ("union[dict, list]", "json", "union")

integer_types = ("integer", "int", "serial")
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/generator/test_pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,23 @@ class Material(BaseModel):
updated_at: Optional[datetime.datetime]
"""
assert expected == result


def test_pydantic_with_bytes():
expected = """from pydantic import BaseModel
class User(BaseModel):
avatar: bytes
"""

ddl = """
CREATE TABLE "User" (
"avatar" BINARY NOT NULL
);
"""
result = create_models(ddl, models_type="pydantic")

assert expected == result["code"]

0 comments on commit 7a2a960

Please sign in to comment.