Coverage for polar/models/subscription_meter.py: 80%
28 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 15:52 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 15:52 +0000
1from decimal import Decimal 1ab
2from typing import TYPE_CHECKING 1ab
3from uuid import UUID 1ab
5from sqlalchemy import ForeignKey, Numeric, UniqueConstraint, Uuid 1ab
6from sqlalchemy.orm import Mapped, declared_attr, mapped_column, relationship 1ab
7from sqlalchemy.sql.sqltypes import Integer 1ab
9from polar.kit.db.models.base import RecordModel 1ab
11if TYPE_CHECKING: 11 ↛ 12line 11 didn't jump to line 12 because the condition on line 11 was never true1ab
12 from .meter import Meter
13 from .subscription import Subscription
16class SubscriptionMeter(RecordModel): 1ab
17 __tablename__ = "subscription_meters" 1ab
18 __table_args__ = (UniqueConstraint("subscription_id", "meter_id"),) 1ab
20 consumed_units: Mapped[Decimal] = mapped_column(Numeric, nullable=False, default=0) 1ab
21 credited_units: Mapped[int] = mapped_column(Integer, nullable=False, default=0) 1ab
22 amount: Mapped[int] = mapped_column(Integer, nullable=False, default=0) 1ab
24 subscription_id: Mapped[UUID] = mapped_column( 1ab
25 Uuid, ForeignKey("subscriptions.id", ondelete="cascade"), nullable=False
26 )
27 meter_id: Mapped[UUID] = mapped_column( 1ab
28 Uuid, ForeignKey("meters.id", ondelete="cascade"), nullable=False
29 )
31 @declared_attr 1ab
32 def subscription(cls) -> Mapped["Subscription"]: 1ab
33 return relationship( 1ab
34 "Subscription",
35 lazy="raise_on_sql",
36 back_populates="meters",
37 # cascade="all, delete-orphan",
38 )
40 @declared_attr 1ab
41 def meter(cls) -> Mapped["Meter"]: 1ab
42 return relationship("Meter", lazy="raise_on_sql") 1ab
44 def reset(self) -> None: 1ab
45 self.consumed_units = Decimal(0)
46 self.credited_units = 0
47 self.amount = 0