Coverage for docs_src/sql_databases/sql_app_py310/crud.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from sqlalchemy.orm import Session 1abc

2 

3from . import models, schemas 1abc

4 

5 

6def get_user(db: Session, user_id: int): 1abc

7 return db.query(models.User).filter(models.User.id == user_id).first() 1abc

8 

9 

10def get_user_by_email(db: Session, email: str): 1abc

11 return db.query(models.User).filter(models.User.email == email).first() 1abc

12 

13 

14def get_users(db: Session, skip: int = 0, limit: int = 100): 1abc

15 return db.query(models.User).offset(skip).limit(limit).all() 1abc

16 

17 

18def create_user(db: Session, user: schemas.UserCreate): 1abc

19 fake_hashed_password = user.password + "notreallyhashed" 1abc

20 db_user = models.User(email=user.email, hashed_password=fake_hashed_password) 1abc

21 db.add(db_user) 1abc

22 db.commit() 1abc

23 db.refresh(db_user) 1abc

24 return db_user 1abc

25 

26 

27def get_items(db: Session, skip: int = 0, limit: int = 100): 1abc

28 return db.query(models.Item).offset(skip).limit(limit).all() 1abc

29 

30 

31def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int): 1abc

32 db_item = models.Item(**item.dict(), owner_id=user_id) 1abc

33 db.add(db_item) 1abc

34 db.commit() 1abc

35 db.refresh(db_item) 1abc

36 return db_item 1abc