Coverage for polar/models/discount_product.py: 89%

17 statements  

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

1from typing import TYPE_CHECKING 1ab

2from uuid import UUID 1ab

3 

4from sqlalchemy import ForeignKey, 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 Discount, Product 

11 

12 

13class DiscountProduct(RecordModel): 1ab

14 __tablename__ = "discount_products" 1ab

15 

16 discount_id: Mapped[UUID] = mapped_column( 1ab

17 Uuid, 

18 ForeignKey("discounts.id", ondelete="cascade"), 

19 primary_key=True, 

20 ) 

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

22 Uuid, 

23 ForeignKey("products.id", ondelete="cascade"), 

24 primary_key=True, 

25 ) 

26 

27 @declared_attr 1ab

28 def discount(cls) -> Mapped["Discount"]: 1ab

29 return relationship( 1ab

30 "Discount", lazy="raise", back_populates="discount_products" 

31 ) 

32 

33 @declared_attr 1ab

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

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

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