Coverage for polar/models/checkout_product.py: 92%

22 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 16:17 +0000

1from typing import TYPE_CHECKING 1ab

2from uuid import UUID 1ab

3 

4from sqlalchemy import ForeignKey, Integer, UniqueConstraint, Uuid 1ab

5from sqlalchemy.orm import Mapped, declared_attr, mapped_column, relationship 1ab

6 

7from polar.kit.db.models import RecordModel 1ab

8 

9if TYPE_CHECKING: 9 ↛ 10line 9 didn't jump to line 10 because the condition on line 9 was never true1ab

10 from polar.models import Checkout, Product, ProductPrice 

11 

12 

13class CheckoutProduct(RecordModel): 1ab

14 __tablename__ = "checkout_products" 1ab

15 __table_args__ = ( 1ab

16 UniqueConstraint("checkout_id", "product_id"), 

17 UniqueConstraint("checkout_id", "order"), 

18 ) 

19 

20 checkout_id: Mapped[UUID] = mapped_column( 1ab

21 Uuid, ForeignKey("checkouts.id", ondelete="cascade") 

22 ) 

23 product_id: Mapped[UUID] = mapped_column( 1ab

24 Uuid, ForeignKey("products.id", ondelete="cascade") 

25 ) 

26 order: Mapped[int] = mapped_column(Integer, nullable=False) 1ab

27 

28 @declared_attr 1ab

29 def checkout(cls) -> Mapped["Checkout"]: 1ab

30 return relationship("Checkout", back_populates="checkout_products") 1ab

31 

32 @declared_attr 1ab

33 def product(cls) -> Mapped["Product"]: 1ab

34 # This is an association table, so eager loading makes sense 

35 return relationship("Product", lazy="joined") 1ab

36 

37 @declared_attr 1ab

38 def ad_hoc_prices(cls) -> Mapped[list["ProductPrice"]]: 1ab

39 return relationship( 1ab

40 "ProductPrice", lazy="selectin", back_populates="checkout_product" 

41 )