Coverage for src/copytrading_engine/shared/notification_models.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Immutable values shared by notification presenters, persistence, and delivery.""" 

2 

3from collections.abc import Mapping 

4from dataclasses import dataclass 

5from datetime import datetime 

6from types import MappingProxyType 

7 

8 

9@dataclass(frozen=True, slots=True) 

10class NotificationPayload: 

11 labels: Mapping[str, str] 

12 annotations: Mapping[str, str] 

13 starts_at: datetime 

14 

15 def __post_init__(self) -> None: 

16 object.__setattr__(self, "labels", _freeze_strings(self.labels, "labels")) 

17 object.__setattr__(self, "annotations", _freeze_strings(self.annotations, "annotations")) 

18 if not isinstance(self.starts_at, datetime): 

19 raise TypeError("Notification start time must be a datetime") 

20 if self.starts_at.tzinfo is None or self.starts_at.utcoffset() is None: 

21 raise ValueError("Notification start time must be timezone-aware") 

22 

23 

24@dataclass(frozen=True, slots=True) 

25class QueuedNotification: 

26 key: str 

27 payload: NotificationPayload 

28 

29 def __post_init__(self) -> None: 

30 if not self.key: 

31 raise ValueError("Notification key cannot be empty") 

32 if not isinstance(self.payload, NotificationPayload): 

33 raise TypeError("Queued notification payload must be a NotificationPayload") 

34 

35 

36@dataclass(frozen=True, slots=True) 

37class NotificationIntent: 

38 key: str 

39 stream_id: str 

40 payload: NotificationPayload 

41 

42 def __post_init__(self) -> None: 

43 if not self.key or not self.stream_id: 

44 raise ValueError("Notification key and stream ID cannot be empty") 

45 if not isinstance(self.payload, NotificationPayload): 

46 raise TypeError("Notification intent payload must be a NotificationPayload") 

47 

48 

49def _freeze_strings(values: Mapping[str, str], name: str) -> Mapping[str, str]: 

50 if not isinstance(values, Mapping): 

51 raise TypeError(f"Notification {name} must be a mapping") 

52 copied: dict[str, str] = {} 

53 for key, value in values.items(): 

54 if not isinstance(key, str) or not isinstance(value, str): 

55 raise TypeError(f"Notification {name} keys and values must be strings") 

56 copied[key] = value 

57 return MappingProxyType(copied)