Coverage for docs_src/sql_databases/sql_app_py39/crud.py: 100%
23 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from sqlalchemy.orm import Session 1abcd
3from . import models, schemas 1abcd
6def get_user(db: Session, user_id: int): 1abcd
7 return db.query(models.User).filter(models.User.id == user_id).first() 1abcd
10def get_user_by_email(db: Session, email: str): 1abcd
11 return db.query(models.User).filter(models.User.email == email).first() 1abcd
14def get_users(db: Session, skip: int = 0, limit: int = 100): 1abcd
15 return db.query(models.User).offset(skip).limit(limit).all() 1abcd
18def create_user(db: Session, user: schemas.UserCreate): 1abcd
19 fake_hashed_password = user.password + "notreallyhashed" 1abcd
20 db_user = models.User(email=user.email, hashed_password=fake_hashed_password) 1abcd
21 db.add(db_user) 1abcd
22 db.commit() 1abcd
23 db.refresh(db_user) 1abcd
24 return db_user 1abcd
27def get_items(db: Session, skip: int = 0, limit: int = 100): 1abcd
28 return db.query(models.Item).offset(skip).limit(limit).all() 1abcd
31def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int): 1abcd
32 db_item = models.Item(**item.dict(), owner_id=user_id) 1abcd
33 db.add(db_item) 1abcd
34 db.commit() 1abcd
35 db.refresh(db_item) 1abcd
36 return db_item 1abcd