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

22 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 12:42 +0000

1from typing import TYPE_CHECKING 

2from uuid import UUID 

3 

4from sqlalchemy import ForeignKey, Integer, UniqueConstraint, Uuid 

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

6 

7from polar.kit.db.models import RecordModel 

8 

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

10 from polar.models import Checkout, Product, ProductPrice 

11 

12 

13class CheckoutProduct(RecordModel): 

14 __tablename__ = "checkout_products" 

15 __table_args__ = ( 

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

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

18 ) 

19 

20 checkout_id: Mapped[UUID] = mapped_column( 

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

22 ) 

23 product_id: Mapped[UUID] = mapped_column( 

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

25 ) 

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

27 

28 @declared_attr 

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

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

31 

32 @declared_attr 

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

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

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

36 

37 @declared_attr 

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

39 return relationship( 

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

41 )