This commit is contained in:
Waylon Walker 2023-06-09 16:36:23 -05:00
parent 864321a45b
commit 53d878c75a
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
4 changed files with 64 additions and 5 deletions

View file

@ -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:

View file

@ -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