Coverage for polar/models/product_benefit.py: 90%

19 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 17:15 +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 Benefit, Product 

11 

12 

13class ProductBenefit(RecordModel): 1ab

14 __tablename__ = "product_benefits" 1ab

15 __table_args__ = (UniqueConstraint("product_id", "order"),) 1ab

16 

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

18 Uuid, 

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

20 primary_key=True, 

21 ) 

22 benefit_id: Mapped[UUID] = mapped_column( 1ab

23 Uuid, 

24 ForeignKey("benefits.id", ondelete="cascade"), 

25 primary_key=True, 

26 ) 

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

28 

29 @declared_attr 1ab

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

31 return relationship("Product", lazy="raise", back_populates="product_benefits") 1ab

32 

33 @declared_attr 1ab

34 def benefit(cls) -> Mapped["Benefit"]: 1ab

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

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