From 53d878c75a52b829ce3b3f3e510b5c46c083d52e Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Fri, 9 Jun 2023 16:36:23 -0500 Subject: [PATCH] wip --- learn_sql_model/models/hero.py | 6 ++-- .../models/{{modelname.lower()}}.py.jinja | 8 ++++-- .../tests/{{modelname.lower()}}.py.jinja | 27 ++++++++++++++++++ tests/test_hero.py | 28 +++++++++++++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/learn_sql_model/models/hero.py b/learn_sql_model/models/hero.py index f349905..7bbef32 100644 --- a/learn_sql_model/models/hero.py +++ b/learn_sql_model/models/hero.py @@ -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 diff --git a/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja b/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja index 992c33c..6fed597 100644 --- a/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja +++ b/templates/model/learn_sql_model/models/{{modelname.lower()}}.py.jinja @@ -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: diff --git a/templates/model/tests/{{modelname.lower()}}.py.jinja b/templates/model/tests/{{modelname.lower()}}.py.jinja index c7a4d1b..39da70f 100644 --- a/templates/model/tests/{{modelname.lower()}}.py.jinja +++ b/templates/model/tests/{{modelname.lower()}}.py.jinja @@ -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 diff --git a/tests/test_hero.py b/tests/test_hero.py index d65c665..139a3af 100644 --- a/tests/test_hero.py +++ b/tests/test_hero.py @@ -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