wip
This commit is contained in:
parent
1a0bf1adb9
commit
c3db85a209
21 changed files with 647 additions and 658 deletions
|
|
@ -18,3 +18,9 @@ def read_heroes(hero: Hero) -> list[Hero]:
|
|||
def read_heros() -> list[Hero]:
|
||||
"read all the heros"
|
||||
return Hero.get()
|
||||
|
||||
|
||||
@app.patch("/heros/")
|
||||
def update_heros() -> list[Hero]:
|
||||
"read all the heros"
|
||||
return Hero.get()
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
from typing import Annotated
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlmodel import SQLModel, Session
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlmodel import SQLModel
|
||||
|
||||
from learn_sql_model.api.user import oauth2_scheme
|
||||
from learn_sql_model.config import Config, get_config
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}
|
||||
from learn_sql_model.api.websocket_connection_manager import manager
|
||||
from learn_sql_model.config import get_config, get_session
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}, {{modelname}}Create, {{modelname}}Read, {{modelname}}Update
|
||||
|
||||
{{modelname.lower()}}_router = APIRouter()
|
||||
|
||||
|
|
@ -15,31 +13,74 @@ def on_startup() -> None:
|
|||
SQLModel.metadata.create_all(get_config().database.engine)
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/items/")
|
||||
async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
|
||||
return {"token": token}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}/{id}")
|
||||
def get_{{modelname.lower()}}(id: int, config: Config = Depends(get_config)) -> {{modelname}}:
|
||||
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}/{{{modelname.lower()}}_id}")
|
||||
async def get_{{modelname.lower()}}(
|
||||
*,
|
||||
session: Session = Depends(get_session),
|
||||
{{modelname.lower()}}_id: int,
|
||||
) -> {{modelname}}Read:
|
||||
"get one {{modelname.lower()}}"
|
||||
return {{modelname}}().get(id=id, config=config)
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/h/{id}")
|
||||
def get_h(id: int, config: Config = Depends(get_config)) -> {{modelname}}:
|
||||
"get one {{modelname.lower()}}"
|
||||
return {{modelname}}().get(id=id, config=config)
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.post("/{{modelname.lower()}}/")
|
||||
def post_{{modelname.lower()}}({{modelname.lower()}}: {{modelname}}, config: Config = Depends(get_config)) -> {{modelname.lower()}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
{{modelname.lower()}}.post(config=config)
|
||||
{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}_id)
|
||||
if not {{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.post("/{{modelname.lower()}}/")
|
||||
async def post_{{modelname.lower()}}(
|
||||
*,
|
||||
session: Session = Depends(get_session),
|
||||
{{modelname.lower()}}: {{modelname}}Create,
|
||||
) -> {{modelname}}Read:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
db_{{modelname.lower()}} = {{modelname}}.from_orm({{modelname.lower()}})
|
||||
session.add(db_{{modelname.lower()}})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
await manager.broadcast({{{modelname.lower()}}.json()}, id=1)
|
||||
return db_{{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.patch("/{{modelname.lower()}}/")
|
||||
async def patch_{{modelname.lower()}}(
|
||||
*,
|
||||
session: Session = Depends(get_session),
|
||||
{{modelname.lower()}}: {{modelname}}Update,
|
||||
) -> {{modelname}}Read:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
db_{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}.id)
|
||||
if not db_{{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
for key, value in {{modelname.lower()}}.dict(exclude_unset=True).items():
|
||||
setattr(db_{{modelname.lower()}}, key, value)
|
||||
session.add(db_{{modelname.lower()}})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
await manager.broadcast({{{modelname.lower()}}.json()}, id=1)
|
||||
return db_{{modelname.lower()}}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.delete("/{{modelname.lower()}}/{{{modelname.lower()}}_id}")
|
||||
async def delete_{{modelname.lower()}}(
|
||||
*,
|
||||
session: Session = Depends(get_session),
|
||||
{{modelname.lower()}}_id: int,
|
||||
):
|
||||
"read all the {{modelname.lower()}}s"
|
||||
{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}_id)
|
||||
if not {{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
session.delete({{modelname.lower()}})
|
||||
session.commit()
|
||||
await manager.broadcast(f"deleted {{modelname.lower()}} {{{modelname.lower()}}_id}", id=1)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@{{modelname.lower()}}_router.get("/{{modelname.lower()}}s/")
|
||||
def get_{{modelname.lower()}}s(config: Config = Depends(get_config)) -> list[{{modelname}}]:
|
||||
async def get_{{modelname.lower()}}s(
|
||||
*,
|
||||
session: Session = Depends(get_session),
|
||||
) -> list[{{modelname}}]:
|
||||
"get all {{modelname.lower()}}s"
|
||||
return {{modelname}}().get(config=config)
|
||||
return {{modelname}}Read.list(session=session)
|
||||
|
||||
|
|
|
|||
|
|
@ -5,9 +5,8 @@ from engorgio import engorgio
|
|||
from rich.console import Console
|
||||
import typer
|
||||
|
||||
from learn_sql_model.config import Config, get_config
|
||||
from learn_sql_model.config import get_config
|
||||
from learn_sql_model.factories.{{modelname.lower()}} import {{modelname}}Factory
|
||||
from learn_sql_model.factories.pet import PetFactory
|
||||
from learn_sql_model.models.{{modelname.lower()}} import (
|
||||
{{modelname}},
|
||||
{{modelname}}Create,
|
||||
|
|
@ -18,6 +17,8 @@ from learn_sql_model.models.{{modelname.lower()}} import (
|
|||
|
||||
{{modelname.lower()}}_app = typer.Typer()
|
||||
|
||||
config = get_config()
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.callback()
|
||||
def {{modelname.lower()}}():
|
||||
|
|
@ -27,12 +28,10 @@ def {{modelname.lower()}}():
|
|||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def get(
|
||||
id: Optional[int] = typer.Argument(default=None),
|
||||
config: Config = None,
|
||||
) -> Union[{{modelname}}, List[{{modelname.lower()}}]]:
|
||||
{{modelname.lower()}}_id: Optional[int] = typer.Argument(default=None),
|
||||
) -> Union[{{modelname}}, List[{{modelname}}]]:
|
||||
"get one {{modelname.lower()}}"
|
||||
config.init()
|
||||
{{modelname.lower()}} = {{modelname}}Read.get(id=id, config=config)
|
||||
{{modelname.lower()}} = {{modelname}}Read.get(id={{modelname.lower()}}_id)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
|
@ -41,12 +40,11 @@ def get(
|
|||
@engorgio(typer=True)
|
||||
def list(
|
||||
where: Optional[str] = None,
|
||||
config: Config = None,
|
||||
offset: int = 0,
|
||||
limit: Optional[int] = None,
|
||||
) -> Union[{{modelname}}, List[{{modelname.lower()}}]]:
|
||||
"get one {{modelname.lower()}}"
|
||||
{{modelname.lower()}} = {{modelname}}Read.list(config=config, where=where, offset=offset, limit=limit)
|
||||
) -> Union[{{modelname}}, List[{{modelname}}]]:
|
||||
"list many {{modelname.lower()}}s"
|
||||
{{modelname.lower()}} = {{modelname}}Read.list(where=where, offset=offset, limit=limit)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
|
||||
|
|
@ -55,53 +53,40 @@ def list(
|
|||
@engorgio(typer=True)
|
||||
def create(
|
||||
{{modelname.lower()}}: {{modelname}}Create,
|
||||
config: Config = None,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
# config.init()
|
||||
{{modelname.lower()}} = {{modelname.lower()}}.post(config=config)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
"create one {{modelname.lower()}}"
|
||||
{{modelname.lower()}}.post()
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def update(
|
||||
{{modelname.lower()}}: {{modelname}}Update,
|
||||
config: Config = None,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
{{modelname.lower()}} = {{modelname.lower()}}.update(config=config)
|
||||
Console().print({{modelname.lower()}})
|
||||
return {{modelname.lower()}}
|
||||
"update one {{modelname.lower()}}"
|
||||
{{modelname.lower()}}.update()
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def delete(
|
||||
{{modelname.lower()}}: {{modelname}}Delete,
|
||||
config: Config = None,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
# config.init()
|
||||
{{modelname.lower()}} = {{modelname.lower()}}.delete(config=config)
|
||||
return {{modelname.lower()}}
|
||||
"delete a {{modelname.lower()}} by id"
|
||||
{{modelname.lower()}}.delete()
|
||||
|
||||
|
||||
@{{modelname.lower()}}_app.command()
|
||||
@engorgio(typer=True)
|
||||
def populate(
|
||||
{{modelname.lower()}}: {{modelname}},
|
||||
n: int = 10,
|
||||
) -> {{modelname}}:
|
||||
"read all the {{modelname.lower()}}s"
|
||||
config = get_config()
|
||||
"Create n number of {{modelname.lower()}}s"
|
||||
if config.env == "prod":
|
||||
Console().print("populate is not supported in production")
|
||||
sys.exit(1)
|
||||
|
||||
for {{modelname.lower()}} in {{modelname}}Factory().batch(n):
|
||||
pet = PetFactory().build()
|
||||
{{modelname.lower()}}.pet = pet
|
||||
Console().print({{modelname.lower()}})
|
||||
{{modelname.lower()}}.post(config=config)
|
||||
{{modelname.lower()}} = {{modelname}}Create(**{{modelname.lower()}}.dict())
|
||||
{{modelname.lower()}}.post()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,43 +1,41 @@
|
|||
from typing import Optional
|
||||
|
||||
from fastapi import HTTPException
|
||||
from fastapi import Depends, HTTPException
|
||||
import httpx
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel import Field, Relationship, SQLModel, Session, select
|
||||
|
||||
from learn_sql_model.config import Config
|
||||
from learn_sql_model.config import config, get_session
|
||||
from learn_sql_model.models.pet import Pet
|
||||
|
||||
|
||||
class {{modelname}}Base(SQLModel, table=False):
|
||||
|
||||
|
||||
class {{modelname}}({{modelname.lower()}}Base, table=True):
|
||||
class {{modelname}}({{modelname}}Base, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
|
||||
|
||||
class {{modelname}}Create({{modelname.lower()}}Base):
|
||||
class {{modelname}}Create({{modelname}}Base):
|
||||
...
|
||||
|
||||
def post(self, config: Config) -> {{modelname}}:
|
||||
config.init()
|
||||
with Session(config.database.engine) as session:
|
||||
db_{{modelname.lower()}} = {{modelname}}.from_orm(self)
|
||||
session.add(db_{{modelname.lower()}})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
return db_{{modelname.lower()}}
|
||||
def post(self) -> {{modelname}}:
|
||||
r = httpx.post(
|
||||
f"{config.api_client.url}/{{modelname.lower()}}/",
|
||||
json=self.dict(),
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
||||
|
||||
|
||||
class {{modelname}}Read({{modelname.lower()}}Base):
|
||||
class {{modelname}}Read({{modelname}}Base):
|
||||
id: int
|
||||
|
||||
@classmethod
|
||||
def get(
|
||||
cls,
|
||||
config: Config,
|
||||
id: int,
|
||||
) -> {{modelname}}:
|
||||
|
||||
with config.database.session as session:
|
||||
{{modelname.lower()}} = session.get({{modelname}}, id)
|
||||
if not {{modelname.lower()}}:
|
||||
|
|
@ -47,53 +45,49 @@ class {{modelname}}Read({{modelname.lower()}}Base):
|
|||
@classmethod
|
||||
def list(
|
||||
self,
|
||||
config: Config,
|
||||
where=None,
|
||||
offset=0,
|
||||
limit=None,
|
||||
session: Session = get_session,
|
||||
) -> {{modelname}}:
|
||||
# with config.database.session as session:
|
||||
|
||||
with config.database.session as session:
|
||||
statement = select({{modelname}})
|
||||
if where != "None":
|
||||
from sqlmodel import text
|
||||
statement = select({{modelname}})
|
||||
if where != "None" and where is not None:
|
||||
from sqlmodel import text
|
||||
|
||||
statement = statement.where(text(where))
|
||||
statement = statement.offset(offset).limit(limit)
|
||||
{{modelname.lower()}}es = session.exec(statement).all()
|
||||
statement = statement.where(text(where))
|
||||
statement = statement.offset(offset).limit(limit)
|
||||
{{modelname.lower()}}es = session.exec(statement).all()
|
||||
return {{modelname.lower()}}es
|
||||
|
||||
|
||||
class {{modelname}}Update(SQLModel):
|
||||
# id is required to get the {{modelname.lower()}}
|
||||
# id is required to update the {{modelname.lower()}}
|
||||
id: int
|
||||
|
||||
# all other fields, must match the model, but with Optional default None
|
||||
|
||||
def update(self, config: Config) -> {{modelname}}:
|
||||
with Session(config.database.engine) as session:
|
||||
db_{{modelname.lower()}} = session.get({{modelname}}, self.id)
|
||||
if not db_{{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
{{modelname.lower()}}_data = self.dict(exclude_unset=True)
|
||||
for key, value in {{modelname.lower()}}_data.items():
|
||||
if value is not None:
|
||||
setattr(db_{{modelname.lower()}}, key, value)
|
||||
session.add(db_{{modelname.lower()}})
|
||||
session.commit()
|
||||
session.refresh(db_{{modelname.lower()}})
|
||||
return db_{{modelname.lower()}}
|
||||
pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
|
||||
pet: Optional[Pet] = Relationship(back_populates="{{modelname.lower()}}")
|
||||
|
||||
def update(self) -> {{modelname}}:
|
||||
r = httpx.patch(
|
||||
f"{config.api_client.url}/{{modelname.lower()}}/",
|
||||
json=self.dict(),
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
||||
|
||||
|
||||
class {{modelname}}Delete(BaseModel):
|
||||
id: int
|
||||
|
||||
def delete(self, config: Config) -> {{modelname}}:
|
||||
config.init()
|
||||
with Session(config.database.engine) as session:
|
||||
{{modelname.lower()}} = session.get({{modelname}}, self.id)
|
||||
if not {{modelname.lower()}}:
|
||||
raise HTTPException(status_code=404, detail="{{modelname}} not found")
|
||||
session.delete({{modelname.lower()}})
|
||||
session.commit()
|
||||
return {"ok": True}
|
||||
def delete(self) -> {{modelname}}:
|
||||
r = httpx.delete(
|
||||
f"{config.api_client.url}/{{modelname.lower()}}/{self.id}",
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f"{r.status_code}:\n {r.text}")
|
||||
return {"ok": True}
|
||||
|
||||
|
|
|
|||
207
templates/model/tests/{{modelname.lower()}}.py.jinja
Normal file
207
templates/model/tests/{{modelname.lower()}}.py.jinja
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
from fastapi.testclient import TestClient
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlmodel import SQLModel, Session, select
|
||||
from sqlmodel.pool import StaticPool
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from learn_sql_model.api.app import app
|
||||
from learn_sql_model.config import get_config, get_session
|
||||
from learn_sql_model.factories.{{modelname.lower()}} import {{modelname}}Factory
|
||||
from learn_sql_model.models.{{modelname.lower()}} import {{modelname}}
|
||||
|
||||
runner = CliRunner()
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture(name="session")
|
||||
def session_fixture():
|
||||
engine = create_engine(
|
||||
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||
)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def client_fixture(session: Session):
|
||||
def get_session_override():
|
||||
return session
|
||||
|
||||
app.dependency_overrides[get_session] = get_session_override
|
||||
|
||||
client = TestClient(app)
|
||||
yield client
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def test_api_post(client: TestClient):
|
||||
{{modelname.lower()}} = {{modelname}}Factory().build(name="Steelman", age=25)
|
||||
{{modelname.lower()}}_dict = {{modelname.lower()}}.dict()
|
||||
response = client.post("/{{modelname.lower()}}/", json={"{{modelname.lower()}}": {{modelname.lower()}}_dict})
|
||||
response_{{modelname.lower()}} = {{modelname}}.parse_obj(response.json())
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response_{{modelname.lower()}}.name == "Steelman"
|
||||
assert response_{{modelname.lower()}}.age == 25
|
||||
|
||||
|
||||
def test_api_read_{{modelname.lower()}}es(session: Session, client: TestClient):
|
||||
{{modelname.lower()}}_1 = {{modelname}}(name="Deadpond", secret_name="Dive Wilson")
|
||||
{{modelname.lower()}}_2 = {{modelname}}(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.add({{modelname.lower()}}_2)
|
||||
session.commit()
|
||||
|
||||
response = client.get("/{{modelname.lower()}}s/")
|
||||
data = response.json()
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
assert len(data) == 2
|
||||
assert data[0]["name"] == {{modelname.lower()}}_1.name
|
||||
assert data[0]["secret_name"] == {{modelname.lower()}}_1.secret_name
|
||||
assert data[0]["age"] == {{modelname.lower()}}_1.age
|
||||
assert data[0]["id"] == {{modelname.lower()}}_1.id
|
||||
assert data[1]["name"] == {{modelname.lower()}}_2.name
|
||||
assert data[1]["secret_name"] == {{modelname.lower()}}_2.secret_name
|
||||
assert data[1]["age"] == {{modelname.lower()}}_2.age
|
||||
assert data[1]["id"] == {{modelname.lower()}}_2.id
|
||||
|
||||
|
||||
def test_api_read_{{modelname.lower()}}(session: Session, client: TestClient):
|
||||
{{modelname.lower()}}_1 = {{modelname}}(name="Deadpond", secret_name="Dive Wilson")
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.commit()
|
||||
|
||||
response = client.get(f"/{{modelname.lower()}}/999")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_api_read_{{modelname.lower()}}_404(session: Session, client: TestClient):
|
||||
{{modelname.lower()}}_1 = {{modelname}}(name="Deadpond", secret_name="Dive Wilson")
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.commit()
|
||||
|
||||
response = client.get(f"/{{modelname.lower()}}/{{{modelname.lower()}}_1.id}")
|
||||
data = response.json()
|
||||
|
||||
assert response.status_code == 200
|
||||
assert data["name"] == {{modelname.lower()}}_1.name
|
||||
assert data["secret_name"] == {{modelname.lower()}}_1.secret_name
|
||||
assert data["age"] == {{modelname.lower()}}_1.age
|
||||
assert data["id"] == {{modelname.lower()}}_1.id
|
||||
|
||||
|
||||
def test_api_update_{{modelname.lower()}}(session: Session, client: TestClient):
|
||||
{{modelname.lower()}}_1 = {{modelname}}(name="Deadpond", secret_name="Dive Wilson")
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.commit()
|
||||
|
||||
response = client.patch(
|
||||
f"/{{modelname.lower()}}/", json={"{{modelname.lower()}}": {"name": "Deadpuddle", "id": {{modelname.lower()}}_1.id}}
|
||||
)
|
||||
data = response.json()
|
||||
|
||||
assert response.status_code == 200
|
||||
assert data["name"] == "Deadpuddle"
|
||||
assert data["secret_name"] == "Dive Wilson"
|
||||
assert data["age"] is None
|
||||
assert data["id"] == {{modelname.lower()}}_1.id
|
||||
|
||||
|
||||
def test_api_update_{{modelname.lower()}}_404(session: Session, client: TestClient):
|
||||
{{modelname.lower()}}_1 = {{modelname}}(name="Deadpond", secret_name="Dive Wilson")
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.commit()
|
||||
|
||||
response = client.patch(f"/{{modelname.lower()}}/", json={"{{modelname.lower()}}": {"name": "Deadpuddle", "id": 999}})
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_delete_{{modelname.lower()}}(session: Session, client: TestClient):
|
||||
{{modelname.lower()}}_1 = {{modelname}}(name="Deadpond", secret_name="Dive Wilson")
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.commit()
|
||||
|
||||
response = client.delete(f"/{{modelname.lower()}}/{{{modelname.lower()}}_1.id}")
|
||||
|
||||
{{modelname.lower()}}_in_db = session.get({{modelname}}, {{modelname.lower()}}_1.id)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
assert {{modelname.lower()}}_in_db is None
|
||||
|
||||
|
||||
def test_delete_{{modelname.lower()}}_404(session: Session, client: TestClient):
|
||||
{{modelname.lower()}}_1 = {{modelname}}(name="Deadpond", secret_name="Dive Wilson")
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.commit()
|
||||
|
||||
response = client.delete(f"/{{modelname.lower()}}/999")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_config_memory(mocker):
|
||||
mocker.patch(
|
||||
"learn_sql_model.config.Database.engine",
|
||||
new_callable=lambda: create_engine(
|
||||
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||
),
|
||||
)
|
||||
config = get_config()
|
||||
SQLModel.metadata.create_all(config.database.engine)
|
||||
{{modelname.lower()}} = {{modelname}}Factory().build(name="Steelman", age=25)
|
||||
with config.database.session as session:
|
||||
session.add({{modelname.lower()}})
|
||||
session.commit()
|
||||
{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}.id)
|
||||
{{modelname.lower()}}es = session.exec(select({{modelname}})).all()
|
||||
assert {{modelname.lower()}}.name == "Steelman"
|
||||
assert {{modelname.lower()}}.age == 25
|
||||
assert len({{modelname.lower()}}es) == 1
|
||||
|
||||
|
||||
def test_cli_get(mocker):
|
||||
mocker.patch(
|
||||
"learn_sql_model.config.Database.engine",
|
||||
new_callable=lambda: create_engine(
|
||||
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||
),
|
||||
)
|
||||
|
||||
config = get_config()
|
||||
SQLModel.metadata.create_all(config.database.engine)
|
||||
|
||||
{{modelname.lower()}} = {{modelname}}Factory().build(name="Steelman", age=25)
|
||||
with config.database.session as session:
|
||||
session.add({{modelname.lower()}})
|
||||
session.commit()
|
||||
{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}.id)
|
||||
result = runner.invoke({{modelname.lower()}}_app, ["get", "--{{modelname.lower()}}-id", "1"])
|
||||
assert result.exit_code == 0
|
||||
assert f"name='{{{modelname.lower()}}.name}'" in result.stdout
|
||||
assert f"secret_name='{{{modelname.lower()}}.secret_name}'" in result.stdout
|
||||
|
||||
|
||||
def test_cli_get_404(mocker):
|
||||
mocker.patch(
|
||||
"learn_sql_model.config.Database.engine",
|
||||
new_callable=lambda: create_engine(
|
||||
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||
),
|
||||
)
|
||||
|
||||
config = get_config()
|
||||
SQLModel.metadata.create_all(config.database.engine)
|
||||
|
||||
{{modelname.lower()}} = {{modelname}}Factory().build(name="Steelman", age=25)
|
||||
with config.database.session as session:
|
||||
session.add({{modelname.lower()}})
|
||||
session.commit()
|
||||
{{modelname.lower()}} = session.get({{modelname}}, {{modelname.lower()}}.id)
|
||||
result = runner.invoke({{modelname.lower()}}_app, ["get", "--{{modelname.lower()}}-id", "999"])
|
||||
assert result.exception.status_code == 404
|
||||
assert result.exception.detail == "{{modelname}} not found"
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue