Skip to content

Commit c628558

Browse files
refactor: address code review feedback
1 parent 16f602f commit c628558

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

tests/conftest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
CALENDARS_DIR = HERE / "calendars"
1414

1515

16+
1617
def merge_func(calendars: list[str]) -> icalendar.Calendar:
1718
"""Use the main merge function to merge the calendars."""
1819
icalendars = []
@@ -23,7 +24,7 @@ def merge_func(calendars: list[str]) -> icalendar.Calendar:
2324
if not calendar.endswith(".ics"):
2425
calendar += ".ics"
2526
calendar_path = CALENDARS_DIR / calendar
26-
icalendars.extend(calendars_from_ical(calendar_path.read_bytes()))
27+
icalendars.append(ICSCalendars().get_calendar(calendar_path.read_bytes()))
2728
return merge_calendars(icalendars)
2829

2930

@@ -52,7 +53,9 @@ class ICSCalendars:
5253
def get_calendar(self, content):
5354
"""Return the calendar given the content."""
5455
cals = calendars_from_ical(content)
55-
return cals[0] if len(cals) == 1 else merge_calendars(cals)
56+
cal = cals[0]
57+
cal.stream = cals
58+
return cal
5659

5760
def __getitem__(self, name):
5861
"""Return the calendar from the calendars directory."""

tests/test_multiple_vcalendars.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
"""Tests for ICS files containing multiple VCALENDAR components."""
22

3-
from pathlib import Path
3+
from typing import TYPE_CHECKING
44

5-
from mergecal import calendars_from_ical, merge_calendars
5+
import pytest
66

7-
CALENDARS_DIR = Path(__file__).parent / "calendars"
8-
TWO_VCALENDARS_DATA = (CALENDARS_DIR / "two_vcalendars.ics").read_bytes()
7+
from mergecal import merge_calendars
98

9+
if TYPE_CHECKING:
10+
from icalendar import Calendar
1011

11-
def test_calendars_from_ical_returns_two_calendars() -> None:
12+
13+
@pytest.fixture
14+
def two_vcalendars_cals(calendars) -> list["Calendar"]:
15+
"""Fixture providing parsed calendars from two_vcalendars.ics file."""
16+
return calendars.two_vcalendars.stream
17+
18+
19+
def test_calendars_from_ical_returns_two_calendars(
20+
two_vcalendars_cals: list["Calendar"],
21+
) -> None:
1222
"""Parse an ICS file with two VCALENDAR blocks and get two Calendar objects."""
13-
cals = calendars_from_ical(TWO_VCALENDARS_DATA)
14-
assert len(cals) == 2
23+
assert len(two_vcalendars_cals) == 2
1524

1625

17-
def test_merge_file_with_two_vcalendars_yields_both_events() -> None:
26+
def test_merge_file_with_two_vcalendars_yields_both_events(
27+
two_vcalendars_cals: list["Calendar"],
28+
) -> None:
1829
"""Merging calendars from a multi-VCALENDAR file produces all events."""
19-
cals = calendars_from_ical(TWO_VCALENDARS_DATA)
20-
merged = merge_calendars(cals)
30+
merged = merge_calendars(two_vcalendars_cals)
2131
summaries = {str(e.get("summary")) for e in merged.walk("VEVENT")}
2232
assert summaries == {"Event A", "Event B"}
2333

2434

25-
def test_merging_multi_vcalendar_file_with_itself_deduplicates() -> None:
35+
def test_merging_multi_vcalendar_file_with_itself_deduplicates(
36+
two_vcalendars_cals: list["Calendar"],
37+
) -> None:
2638
"""Merging a multi-VCALENDAR file with itself produces no duplicate events."""
27-
cals = calendars_from_ical(TWO_VCALENDARS_DATA)
39+
cals = two_vcalendars_cals
2840
merged_once = merge_calendars(cals)
2941
merged_twice = merge_calendars(cals + cals)
3042
assert merged_once == merged_twice

0 commit comments

Comments
 (0)