File: test_ical_export.py

package info (click to toggle)
python-calendra 7.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,600 kB
  • sloc: python: 16,840; makefile: 6
file content (149 lines) | stat: -rw-r--r-- 5,186 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import tempfile
from pathlib import Path

from ..core import Calendar
from ..exceptions import ICalExportRangeError, ICalExportTargetPathError

from . import CoreCalendarTest


class FakeCalendar(Calendar):
    """Fake Calendar"""


class IcalExportPeriodTest(CoreCalendarTest):
    cal_class = FakeCalendar

    def test_empty_range(self):
        # No period means 2000-2030
        result = self.cal._get_ical_period()
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2000)
        self.assertEqual(result[1], 2030)
        # No period means 2000-2030
        result = self.cal._get_ical_period(period=())
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2000)
        self.assertEqual(result[1], 2030)
        # No period means 2000-2030
        result = self.cal._get_ical_period(period=[])
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2000)
        self.assertEqual(result[1], 2030)

    def test_range_lists(self):
        # Period: 2010-2020
        result = self.cal._get_ical_period(period=[2010, 2020])
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2010)
        self.assertEqual(result[1], 2020)

        # Bad period: 2020-2010 => Swap terms
        result = self.cal._get_ical_period(period=[2020, 2010])
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2010)
        self.assertEqual(result[1], 2020)

        # Too long of a list => take the extremes
        result = self.cal._get_ical_period(period=[2010, 2015, 2020])
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2010)
        self.assertEqual(result[1], 2020)

        # Assume that the range is only one year
        result = self.cal._get_ical_period(period=[2020, 2020])
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2020)
        self.assertEqual(result[1], 2020)

    def test_range_tuples(self):
        # Period: 2010-2020
        result = self.cal._get_ical_period(period=(2010, 2020))
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2010)
        self.assertEqual(result[1], 2020)

        # Bad period: 2020-2010 => Swap terms
        result = self.cal._get_ical_period(period=(2020, 2010))
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2010)
        self.assertEqual(result[1], 2020)

        # Too long of a list => take the extremes
        result = self.cal._get_ical_period(period=(2010, 2015, 2020))
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2010)
        self.assertEqual(result[1], 2020)

        # Assume that the range is only one year
        result = self.cal._get_ical_period(period=(2020, 2020))
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0], 2020)
        self.assertEqual(result[1], 2020)

    def test_bad_ranges(self):
        # Bad period: String
        with self.assertRaises(ICalExportRangeError):
            self.cal._get_ical_period(period="something")

        # Bad period: Dict
        with self.assertRaises(ICalExportRangeError):
            self.cal._get_ical_period(period={"a": 2000, "b": "foobar"})

        # Bad period: List of non-integers
        with self.assertRaises(ICalExportRangeError):
            self.cal._get_ical_period(period=["foo", "bar"])

        # Bad period: Tuple of non-integers
        with self.assertRaises(ICalExportRangeError):
            self.cal._get_ical_period(period=("foo", "bar"))


class ICalExportTargetPath(CoreCalendarTest):
    cal_class = FakeCalendar

    def test_empty_path(self):
        with self.assertRaises(ICalExportTargetPathError):
            self.cal._get_ical_target_path("")

        with self.assertRaises(ICalExportTargetPathError):
            self.cal._get_ical_target_path(None)

    def test_target_is_directory(self):
        temp_dir = Path(tempfile.gettempdir()) / "failed_ical_tests"
        temp_dir.mkdir(parents=True, exist_ok=True)

        with self.assertRaises(ICalExportTargetPathError):
            self.cal._get_ical_target_path(temp_dir)

    def test_no_extension(self):
        # Relative paths
        self.assertEqual(self.cal._get_ical_target_path("a"), Path("a.ics"))
        self.assertEqual(
            self.cal._get_ical_target_path("a/b"),
            Path("a/b.ics")
        )
        # Absolute path
        self.assertEqual(
            self.cal._get_ical_target_path("/path/to/a"),
            Path("/path/to/a.ics")
        )

    def test_known_extensions(self):
        for ext in ('ical', 'ics', 'ifb', 'icalendar'):
            filename = Path(f"a.{ext}")
            self.assertEqual(
                self.cal._get_ical_target_path(filename),
                filename
            )

    def test_added_extensions(self):
        self.assertEqual(self.cal._get_ical_target_path('a.'), Path('a..ics'))
        self.assertEqual(
            self.cal._get_ical_target_path('a.txt'),
            Path('a.txt.ics')
        )
        self.assertEqual(
            self.cal._get_ical_target_path('.test'),
            Path('.test.ics')
        )