Coverage for polar/models/customer_meter.py: 85%
31 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 17:15 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 17:15 +0000
1from decimal import Decimal 1ab
2from typing import TYPE_CHECKING 1ab
3from uuid import UUID 1ab
5from sqlalchemy import BigInteger, ForeignKey, Numeric, UniqueConstraint, Uuid 1ab
6from sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy 1ab
7from sqlalchemy.orm import Mapped, declared_attr, mapped_column, relationship 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 .customer import Customer
13 from .event import Event
14 from .meter import Meter
15 from .organization import Organization
18class CustomerMeter(RecordModel): 1ab
19 __tablename__ = "customer_meters" 1ab
20 __table_args__ = (UniqueConstraint("customer_id", "meter_id"),) 1ab
22 customer_id: Mapped[UUID] = mapped_column( 1ab
23 Uuid, ForeignKey("customers.id", ondelete="cascade")
24 )
25 meter_id: Mapped[UUID] = mapped_column( 1ab
26 Uuid, ForeignKey("meters.id", ondelete="cascade"), index=True
27 )
28 last_balanced_event_id: Mapped[UUID | None] = mapped_column( 1ab
29 Uuid, ForeignKey("events.id"), nullable=True, index=True, default=None
30 )
31 consumed_units: Mapped[Decimal] = mapped_column( 1ab
32 Numeric, nullable=False, default=0, index=True
33 )
34 credited_units: Mapped[int] = mapped_column( 1ab
35 BigInteger, nullable=False, default=0, index=True
36 )
37 balance: Mapped[Decimal] = mapped_column( 1ab
38 Numeric, nullable=False, default=Decimal(0), index=True
39 )
41 @declared_attr 1ab
42 def customer(cls) -> Mapped["Customer"]: 1ab
43 return relationship("Customer", lazy="raise_on_sql") 1ab
45 @declared_attr 1ab
46 def meter(cls) -> Mapped["Meter"]: 1ab
47 return relationship("Meter", lazy="raise_on_sql") 1ab
49 @declared_attr 1ab
50 def last_balanced_event(cls) -> Mapped["Event | None"]: 1ab
51 return relationship("Event", lazy="raise_on_sql") 1ab
53 organization: AssociationProxy["Organization"] = association_proxy( 1ab
54 "customer", "organization"
55 )