Debian Codegraph @

recurring_ical_events/test/test_extend_classes.py

raw symbols SCIP
1 """This tests extneding and modifying the behaviour of recurring ical events."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING, Sequence
6
7 import pytest
8 from icalendar.cal import Component
9
10 from recurring_ical_events import (
11 of,
12 )
13 from recurring_ical_events.adapters.event import EventAdapter
14 from recurring_ical_events.adapters.journal import JournalAdapter
15 from recurring_ical_events.adapters.todo import TodoAdapter
16 from recurring_ical_events.occurrence import Occurrence
17 from recurring_ical_events.selection.all import AllKnownComponents
18 from recurring_ical_events.selection.base import SelectComponents
19 from recurring_ical_events.selection.name import ComponentsWithName
20 from recurring_ical_events.series import Series
21
22 if TYPE_CHECKING:
23 from icalendar.cal import Component
24
25
26 class SelectUID1(SelectComponents):
27 """Collect only one UID."""
28
29 def __init__(self, uid: str) -> None:
30 self.uid = uid
31
32 def collect_series_from(
33 self, source: Component, suppress_errors: tuple[Exception]
34 ) -> Sequence[Series]:
35 return [
36 series
37 for adapter in [EventAdapter, JournalAdapter, TodoAdapter]
38 for series in adapter.collect_series_from(source, suppress_errors)
39 if series.uid == self.uid
40 ]
41
42
43 class SelectUID2(AllKnownComponents):
44 def __init__(self, uid: str) -> None:
45 super().__init__()
46 self.uid = uid
47
48 def collect_series_from(
49 self, source: Component, suppress_errors: tuple[Exception]
50 ) -> Sequence[Series]:
51 return [
52 series
53 for series in super().collect_series_from(source, suppress_errors)
54 if series.uid == self.uid
55 ]
56
57
58 class SelectUID3(SelectComponents):
59 def __init__(self, uid: str) -> None:
60 self.uid = uid
61
62 def collect_series_from(
63 self,
64 source: Component,
65 suppress_errors: tuple[Exception], # noqa: ARG002
66 ) -> Sequence[Series]:
67 components: list[Component] = []
68 for component in source.walk("VEVENT"):
69 if component.get("UID") == self.uid:
70 components.append(EventAdapter(component)) # noqa: PERF401
71 print(components)
72 return [Series(components)] if components else []
73
74
75 @pytest.mark.parametrize("collector", [SelectUID1, SelectUID2, SelectUID3])
76 def test_collect_only_one_uid(calendars, collector):
77 """Test that only one UID is used."""
78 uid = "[email protected]"
79 query = of(calendars.raw.machbar_16_feb_2019, components=[collector(uid)])
80 assert query.count() == 1
81
82
83 class MyOccurrence(Occurrence):
84 """An occurrence that modifies the component."""
85
86 def as_component(self, keep_recurrence_attributes: bool) -> Component: # noqa: FBT001
87 """Return a shallow copy of the source component and modify some attributes."""
88 component = super().as_component(keep_recurrence_attributes)
89 component["X-MY-ATTRIBUTE"] = "my occurrence"
90 return component
91
92
93 def test_added_attributes(calendars):
94 """Test that attributes are added."""
95 query = of(
96 calendars.raw.one_event,
97 components=[AllKnownComponents(occurrence=MyOccurrence)],
98 )
99 event = next(query.all())
100 assert event["X-MY-ATTRIBUTE"] == "my occurrence"
101
102
103 @pytest.mark.parametrize(
104 ("calendar", "count", "collector"),
105 [
106 # all
107 ("one_event", 1, AllKnownComponents()),
108 ("issue_97_simple_todo", 1, AllKnownComponents()),
109 ("issue_97_simple_journal", 1, AllKnownComponents()),
110 # events
111 ("one_event", 1, ComponentsWithName("VEVENT")),
112 ("issue_97_simple_todo", 0, ComponentsWithName("VEVENT")),
113 ("issue_97_simple_journal", 0, ComponentsWithName("VEVENT")),
114 # todos
115 ("one_event", 0, ComponentsWithName("VTODO")),
116 ("issue_97_simple_todo", 1, ComponentsWithName("VTODO")),
117 ("issue_97_simple_journal", 0, ComponentsWithName("VTODO")),
118 # journals
119 ("one_event", 0, ComponentsWithName("VJOURNAL")),
120 ("issue_97_simple_todo", 0, ComponentsWithName("VJOURNAL")),
121 ("issue_97_simple_journal", 1, ComponentsWithName("VJOURNAL")),
122 ],
123 )
124 def test_we_collect_all_components(
125 calendars, calendar, count, collector: SelectComponents
126 ):
127 """Check that the calendars have the right amount of series collected."""
128 series = collector.collect_series_from(calendars.raw[calendar], [])
129 print(series)
130 assert len(series) == count