wip
This commit is contained in:
parent
864321a45b
commit
53d878c75a
4 changed files with 64 additions and 5 deletions
|
|
@ -5,7 +5,7 @@ import httpx
|
|||
from pydantic import BaseModel
|
||||
from sqlmodel import Field, Relationship, SQLModel, Session, select
|
||||
|
||||
from learn_sql_model.config import config, get_session
|
||||
from learn_sql_model.config import config, get_config
|
||||
from learn_sql_model.models.pet import Pet
|
||||
|
||||
|
||||
|
|
@ -55,10 +55,12 @@ class HeroRead(HeroBase):
|
|||
where=None,
|
||||
offset=0,
|
||||
limit=None,
|
||||
session: Session = get_session,
|
||||
session: Session = None,
|
||||
) -> Hero:
|
||||
# with config.database.session as session:
|
||||
|
||||
if session is None:
|
||||
session = get_config().database.session
|
||||
statement = select(Hero)
|
||||
if where != "None" and where is not None:
|
||||
from sqlmodel import text
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import httpx
|
|||
from pydantic import BaseModel
|
||||
from sqlmodel import Field, Relationship, SQLModel, Session, select
|
||||
|
||||
from learn_sql_model.config import config, get_session
|
||||
from learn_sql_model.config import config, get_config
|
||||
from learn_sql_model.models.pet import Pet
|
||||
|
||||
|
||||
|
|
@ -48,9 +48,11 @@ class {{modelname}}Read({{modelname}}Base):
|
|||
where=None,
|
||||
offset=0,
|
||||
limit=None,
|
||||
session: Session = get_session,
|
||||
session: Session = None,
|
||||
) -> {{modelname}}:
|
||||
# with config.database.session as session:
|
||||
|
||||
if session is None:
|
||||
session = get_config().database.session
|
||||
|
||||
statement = select({{modelname}})
|
||||
if where != "None" and where is not None:
|
||||
|
|
|
|||
|
|
@ -205,3 +205,30 @@ def test_cli_get_404(mocker):
|
|||
assert result.exception.status_code == 404
|
||||
assert result.exception.detail == "{{modelname}} not found"
|
||||
|
||||
|
||||
def test_cli_list(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()}}_1 = {{modelname}}Factory().build(name="Steelman", age=25)
|
||||
{{modelname.lower()}}_2 = {{modelname}}Factory().build(name="Hunk", age=52)
|
||||
|
||||
with config.database.session as session:
|
||||
session.add({{modelname.lower()}}_1)
|
||||
session.add({{modelname.lower()}}_2)
|
||||
session.commit()
|
||||
session.refresh({{modelname.lower()}}_1)
|
||||
session.refresh({{modelname.lower()}}_2)
|
||||
result = runner.invoke({{modelname.lower()}}_app, ["list"])
|
||||
assert result.exit_code == 0
|
||||
assert f"name='{{{modelname.lower()}}_1.name}'" in result.stdout
|
||||
assert f"secret_name='{{{modelname.lower()}}_1.secret_name}'" in result.stdout
|
||||
assert f"name='{{{modelname.lower()}}_2.name}'" in result.stdout
|
||||
assert f"secret_name='{{{modelname.lower()}}_2.secret_name}'" in result.stdout
|
||||
|
|
|
|||
|
|
@ -205,3 +205,31 @@ def test_cli_get_404(mocker):
|
|||
result = runner.invoke(hero_app, ["get", "--hero-id", "999"])
|
||||
assert result.exception.status_code == 404
|
||||
assert result.exception.detail == "Hero not found"
|
||||
|
||||
|
||||
def test_cli_list(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)
|
||||
|
||||
hero_1 = HeroFactory().build(name="Steelman", age=25)
|
||||
hero_2 = HeroFactory().build(name="Hunk", age=52)
|
||||
|
||||
with config.database.session as session:
|
||||
session.add(hero_1)
|
||||
session.add(hero_2)
|
||||
session.commit()
|
||||
session.refresh(hero_1)
|
||||
session.refresh(hero_2)
|
||||
result = runner.invoke(hero_app, ["list"])
|
||||
assert result.exit_code == 0
|
||||
assert f"name='{hero_1.name}'" in result.stdout
|
||||
assert f"secret_name='{hero_1.secret_name}'" in result.stdout
|
||||
assert f"name='{hero_2.name}'" in result.stdout
|
||||
assert f"secret_name='{hero_2.secret_name}'" in result.stdout
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue