Coverage for dibbler / queries / adjust_stock.py: 20%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-25 14:26 +0000

1from datetime import datetime 

2 

3from sqlalchemy.orm import Session 

4 

5from dibbler.models import Product, Transaction, User 

6 

7 

8def adjust_stock( 

9 sql_session: Session, 

10 user: User, 

11 product: Product, 

12 product_count: int, 

13 time: datetime | None = None, 

14 message: str | None = None, 

15) -> Transaction: 

16 if user.id is None: 

17 raise ValueError("User must be persisted in the database.") 

18 

19 if product.id is None: 

20 raise ValueError("Product must be persisted in the database.") 

21 

22 if product_count == 0: 

23 raise ValueError("Product count must be non-zero.") 

24 

25 # TODO: it should not be possible to reduce stock below zero. 

26 # 

27 # TODO: verify time is not behind last transaction's time 

28 

29 transaction = Transaction.adjust_stock( 

30 user_id=user.id, 

31 product_id=product.id, 

32 product_count=product_count, 

33 time=time, 

34 message=message, 

35 ) 

36 

37 sql_session.add(transaction) 

38 sql_session.commit() 

39 

40 return transaction