fix all the tests

This commit is contained in:
Waylon Walker 2023-06-21 10:29:50 -05:00
parent 7db07c7d35
commit a7e6f2c4e5
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
9 changed files with 128 additions and 137 deletions

View file

@ -1,8 +1,8 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import SQLModel, Session
from sqlmodel import Session, select
from learn_sql_model.api.websocket_connection_manager import manager
from learn_sql_model.config import get_config, get_session
from learn_sql_model.config import get_session
from learn_sql_model.models.hero import Hero, HeroCreate, HeroRead, HeroUpdate, Heros
hero_router = APIRouter()
@ -10,7 +10,8 @@ hero_router = APIRouter()
@hero_router.on_event("startup")
def on_startup() -> None:
SQLModel.metadata.create_all(get_config().database.engine)
# SQLModel.metadata.create_all(get_config().database.engine)
...
@hero_router.get("/hero/{hero_id}")
@ -32,7 +33,7 @@ async def post_hero(
session: Session = Depends(get_session),
hero: HeroCreate,
) -> HeroRead:
"read all the heros"
"create a hero"
db_hero = Hero.from_orm(hero)
session.add(db_hero)
session.commit()
@ -47,7 +48,7 @@ async def patch_hero(
session: Session = Depends(get_session),
hero: HeroUpdate,
) -> HeroRead:
"read all the heros"
"update a hero"
db_hero = session.get(Hero, hero.id)
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
@ -66,7 +67,7 @@ async def delete_hero(
session: Session = Depends(get_session),
hero_id: int,
):
"read all the heros"
"delete a hero"
hero = session.get(Hero, hero_id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
@ -84,4 +85,4 @@ async def get_heros(
"get all heros"
statement = select(Hero)
heros = session.exec(statement).all()
return Heros(heros=heros)
return Heros(__root__=heros)

View file

@ -33,7 +33,6 @@ def hero():
@hero_app.command()
@engorgio(typer=True)
def get(
hero_id: Optional[int] = typer.Argument(default=None),
) -> Union[Hero, List[Hero]]:
@ -44,16 +43,11 @@ def get(
@hero_app.command()
@engorgio(typer=True)
def list(
where: Optional[str] = None,
offset: int = 0,
limit: Optional[int] = None,
) -> Union[Hero, List[Hero]]:
def list() -> Union[Hero, List[Hero]]:
"list many heros"
heros = Heros.list(where=where, offset=offset, limit=limit)
Console().print(hero)
return hero
heros = Heros.list()
Console().print(heros)
return heros
@hero_app.command()
@ -94,7 +88,6 @@ def delete(
@hero_app.command()
@engorgio(typer=True)
def populate(
n: int = 10,
) -> Hero:

View file

@ -4,7 +4,7 @@ from typing import TYPE_CHECKING
from fastapi import Depends
from pydantic import BaseModel, BaseSettings, validator
from sqlalchemy import create_engine
from sqlmodel import SQLModel, Session
from sqlmodel import Session
from learn_sql_model.standard_config import load
@ -71,7 +71,8 @@ class Config(BaseSettings):
return get_database(config=self)
def init(self) -> None:
SQLModel.metadata.create_all(self.database.engine)
# SQLModel.metadata.create_all(self.database.engine)
...
def get_database(config: Config = None) -> Database:
@ -88,7 +89,8 @@ def get_config(overrides: dict = {}) -> Config:
def get_session() -> "Session":
config = get_config()
with Session(config.database.engine) as session:
engine = create_engine(config.database_url)
with Session(engine) as session:
yield session

View file

@ -1,23 +1,25 @@
from learn_sql_model.console import console
from learn_sql_model.optional import _optional_import_
pygame = _optional_import_("pygame", group="game")
class Light:
def __init__(self, game):
self.game = game
def render(self):
mx, my = pygame.mouse.get_pos()
v = pygame.math.Vector2(mx - self.game.player.hero.x, my - self.game.player.hero.y)
v = pygame.math.Vector2(
mx - self.game.player.hero.x, my - self.game.player.hero.y
)
v.scale_to_length(1000)
for r in range(0, 360):
_v = v.rotate(r)
pygame.draw.line(
self.game.screen,
(255,250,205),
(255, 250, 205),
(self.game.player.hero.x, self.game.player.hero.y),
(self.game.player.hero.x + _v.x, self.game.player.hero.y + _v.y),
50
)
50,
)

View file

@ -2,7 +2,8 @@ from typing import Callable, Tuple
from pydantic import BaseModel
from learn_sql_model.optional import _optional_import_
pygame = _optional_import_('pygame', group='game')
pygame = _optional_import_("pygame", group="game")
screen_sizes = [
@ -128,7 +129,6 @@ class Menu:
class Hamburger:
def __init__(self, game):
self.game = game
self.hamburger_width = 50
self.bar_height = self.hamburger_width / 4

View file

@ -49,7 +49,7 @@ class Player:
def quit(self):
try:
HeroDelete(id=self.hero.id).delete()
except:
except RuntimeError:
pass
def handle_events(self):
@ -153,13 +153,12 @@ class Player:
self.pos = pygame.math.Vector2(self.hero.x, self.hero.y)
if self.game.map.point_check_collision(self.pos.x, self.pos.y):
start_pos = pygame.math.Vector2(self.x_last, self.y_last)
end_pos = pygame.math.Vector2(self.hero.x, self.hero.y)
movement_vector = end_pos - start_pos
try:
movement_direction = movement_vector.normalize()
except:
except ZeroDivisionError:
end_pos = pygame.math.Vector2(self.hero.x + 128, self.hero.y + 128)
movement_vector = end_pos - start_pos
movement_direction = movement_vector.normalize()

View file

@ -1,6 +1,5 @@
from typing import Optional
from fastapi import HTTPException
import httpx
from pydantic import BaseModel
from sqlmodel import Field, Relationship, SQLModel
@ -51,11 +50,11 @@ class HeroRead(HeroBase):
r = httpx.get(f"{config.api_client.url}/hero/{id}")
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
return hero
return HeroRead.parse_obj(r.json())
class Heros(BaseModel):
heros: list[Hero]
__root__: list[Hero]
@classmethod
def list(
@ -64,7 +63,7 @@ class Heros(BaseModel):
r = httpx.get(f"{config.api_client.url}/heros/")
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
return Heros.parse_obj(r.json())
return Heros.parse_obj({"__root__": r.json()})
class HeroUpdate(SQLModel):
@ -76,8 +75,8 @@ class HeroUpdate(SQLModel):
secret_name: Optional[str] = None
age: Optional[int] = None
shoe_size: Optional[int] = None
x: int
y: int
x: Optional[int]
y: Optional[int]
pet_id: Optional[int] = Field(default=None, foreign_key="pet.id")
pet: Optional[Pet] = Relationship(back_populates="hero")