Skip to content

Commit

Permalink
SQLModel does NOT checks pydantic typehint validators
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Jan 2, 2024
1 parent 902c3ea commit a7024a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions sc_crawler/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
def crawl():
create_db_and_tables()
with Session(engine) as session:
# fill lookup tables? might not be needed due to autofill of downstream
# TODO check country
session.add(aws)
session.commit()

Expand Down
18 changes: 16 additions & 2 deletions sc_crawler/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
PrivateAttr,
computed_field,
)

# TODO SQLModel does NOT actually do pydantic validations
# https://github.com/tiangolo/sqlmodel/issues/52
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, JSON, Column


Expand Down Expand Up @@ -57,15 +60,15 @@ class Vendor(SQLModel, table=True):
Vendor(identifier='aws'...
""" # noqa: E501

id: str = Field(default=None, primary_key=True)
id: str = Field(primary_key=True)
name: str
# TODO HttpUrl not supported by SQLModel
# TODO upload to cdn.sparecores.com
logo: Optional[str] = None
# TODO HttpUrl not supported by SQLModel
homepage: str

country: str = Field(default=None, foreign_key="country.id")
country: str = Field(foreign_key="country.id")
state: Optional[str] = None
city: Optional[str] = None
address_line: Optional[str] = None
Expand All @@ -90,8 +93,19 @@ class Vendor(SQLModel, table=True):
def __init__(self, **kwargs):
super().__init__(**kwargs)
try:
# TODO SQLModel does not validates pydantic typing
if not self.id:
raise ValueError("No vendor id provided")
if not self.name:
raise ValueError("No vendor name provided")
if not self.homepage:
raise ValueError("No vendor homepage provided")
if not self.country:
raise ValueError("No vendor country provided")
vendor_module = __name__.split(".")[0] + ".vendors." + self.id
self._methods = import_module(vendor_module)
except ValueError as exc:
raise exc
except Exception as exc:
raise NotImplementedError("Unsupported vendor") from exc

Expand Down
19 changes: 10 additions & 9 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import pytest
from pydantic import ValidationError
from sc_crawler import Vendor, Location
from sc_crawler.schemas import Vendor, Country


def test_bad_vendor_definition():
with pytest.raises(ValidationError):
# TODO ValidationError once SQLModel supports pydantic typehint validation
with pytest.raises(ValueError):
Vendor()
Vendor(identifier="foobar")
Vendor(identifier="foobar", name="foobar")
Vendor(identifier="foobar", name="foobar", homepage="https://foobar")
Vendor(id="foobar")
Vendor(id="foobar", name="foobar")
Vendor(id="foobar", name="foobar", homepage="https://foobar")
Vendor(
identifier="foobar",
id="foobar",
name="foobar",
homepage="https://foobar",
location=Location(country="US", city="Los Angeles"),
country=Country(id="US"),
)
with pytest.raises(NotImplementedError):
Vendor(
identifier="foobar",
id="foobar",
name="foobar",
homepage="https://foobar",
location=Location(country="US", city="Los Angeles"),
country=Country(id="US"),
founding_year=2042,
)

Expand Down

0 comments on commit a7024a4

Please sign in to comment.