File: test_vobject.py

package info (click to toggle)
vdirsyncer 0.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 944 kB
  • sloc: python: 7,380; makefile: 205; sh: 66
file content (385 lines) | stat: -rw-r--r-- 10,559 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from __future__ import annotations

from textwrap import dedent

import hypothesis.strategies as st
import pytest
from hypothesis import assume
from hypothesis import given
from hypothesis.stateful import Bundle
from hypothesis.stateful import RuleBasedStateMachine
from hypothesis.stateful import rule

import vdirsyncer.vobject as vobject
from tests import BARE_EVENT_TEMPLATE
from tests import EVENT_TEMPLATE
from tests import EVENT_WITH_TIMEZONE_TEMPLATE
from tests import VCARD_TEMPLATE
from tests import normalize_item
from tests import uid_strategy

_simple_split = [
    VCARD_TEMPLATE.format(r=123, uid=123),
    VCARD_TEMPLATE.format(r=345, uid=345),
    VCARD_TEMPLATE.format(r=678, uid=678),
]

_simple_joined = "\r\n".join(
    ["BEGIN:VADDRESSBOOK"] + _simple_split + ["END:VADDRESSBOOK\r\n"]
)


def test_split_collection_simple(benchmark):
    given = benchmark(lambda: list(vobject.split_collection(_simple_joined)))

    assert [normalize_item(item) for item in given] == [
        normalize_item(item) for item in _simple_split
    ]

    assert [x.splitlines() for x in given] == [x.splitlines() for x in _simple_split]


def test_split_collection_multiple_wrappers(benchmark):
    joined = "\r\n".join(
        "BEGIN:VADDRESSBOOK\r\n" + x + "\r\nEND:VADDRESSBOOK\r\n" for x in _simple_split
    )
    given = benchmark(lambda: list(vobject.split_collection(joined)))

    assert [normalize_item(item) for item in given] == [
        normalize_item(item) for item in _simple_split
    ]

    assert [x.splitlines() for x in given] == [x.splitlines() for x in _simple_split]


def test_join_collection_simple(benchmark):
    given = benchmark(lambda: vobject.join_collection(_simple_split))
    assert normalize_item(given) == normalize_item(_simple_joined)
    assert given.splitlines() == _simple_joined.splitlines()


def test_join_collection_vevents(benchmark):
    actual = benchmark(
        lambda: vobject.join_collection(
            [
                dedent(
                    """
            BEGIN:VCALENDAR
            VERSION:2.0
            PRODID:HUEHUE
            BEGIN:VTIMEZONE
            VALUE:The Timezone
            END:VTIMEZONE
            BEGIN:VEVENT
            VALUE:Event {}
            END:VEVENT
            END:VCALENDAR
         """
                ).format(i)
                for i in range(3)
            ]
        )
    )

    expected = dedent(
        """
        BEGIN:VCALENDAR
        VERSION:2.0
        PRODID:HUEHUE
        BEGIN:VTIMEZONE
        VALUE:The Timezone
        END:VTIMEZONE
        BEGIN:VEVENT
        VALUE:Event 0
        END:VEVENT
        BEGIN:VEVENT
        VALUE:Event 1
        END:VEVENT
        BEGIN:VEVENT
        VALUE:Event 2
        END:VEVENT
        END:VCALENDAR
    """
    ).lstrip()

    assert actual.splitlines() == expected.splitlines()


def test_split_collection_timezones():
    items = [
        BARE_EVENT_TEMPLATE.format(r=123, uid=123),
        BARE_EVENT_TEMPLATE.format(r=345, uid=345),
    ]

    timezone = (
        "BEGIN:VTIMEZONE\r\n"
        "TZID:/mozilla.org/20070129_1/Asia/Tokyo\r\n"
        "X-LIC-LOCATION:Asia/Tokyo\r\n"
        "BEGIN:STANDARD\r\n"
        "TZOFFSETFROM:+0900\r\n"
        "TZOFFSETTO:+0900\r\n"
        "TZNAME:JST\r\n"
        "DTSTART:19700101T000000\r\n"
        "END:STANDARD\r\n"
        "END:VTIMEZONE"
    )

    full = "\r\n".join(["BEGIN:VCALENDAR"] + items + [timezone, "END:VCALENDAR"])

    given = {normalize_item(item) for item in vobject.split_collection(full)}
    expected = {
        normalize_item(
            "\r\n".join(("BEGIN:VCALENDAR", item, timezone, "END:VCALENDAR"))
        )
        for item in items
    }

    assert given == expected


def test_split_contacts():
    bare = "\r\n".join([VCARD_TEMPLATE.format(r=x, uid=x) for x in range(4)])
    with_wrapper = "BEGIN:VADDRESSBOOK\r\n" + bare + "\nEND:VADDRESSBOOK\r\n"

    for _ in (bare, with_wrapper):
        split = list(vobject.split_collection(bare))
        assert len(split) == 4
        assert vobject.join_collection(split).splitlines() == with_wrapper.splitlines()


def test_hash_item():
    a = EVENT_TEMPLATE.format(r=1, uid=1)
    b = "\n".join(line for line in a.splitlines() if "PRODID" not in line)
    assert vobject.hash_item(a) == vobject.hash_item(b)


def test_multiline_uid(benchmark):
    a = "BEGIN:FOO\r\nUID:123456789abcd\r\n efgh\r\nEND:FOO\r\n"
    assert benchmark(lambda: vobject.Item(a).uid) == "123456789abcdefgh"


complex_uid_item = dedent(
    """
    BEGIN:VCALENDAR
    BEGIN:VTIMEZONE
    TZID:Europe/Rome
    X-LIC-LOCATION:Europe/Rome
    BEGIN:DAYLIGHT
    TZOFFSETFROM:+0100
    TZOFFSETTO:+0200
    TZNAME:CEST
    DTSTART:19700329T020000
    RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
    END:DAYLIGHT
    BEGIN:STANDARD
    TZOFFSETFROM:+0200
    TZOFFSETTO:+0100
    TZNAME:CET
    DTSTART:19701025T030000
    RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
    END:STANDARD
    END:VTIMEZONE
    BEGIN:VEVENT
    DTSTART:20140124T133000Z
    DTEND:20140124T143000Z
    DTSTAMP:20140612T090652Z
    UID:040000008200E00074C5B7101A82E0080000000050AAABEEF50DCF
     001000000062548482FA830A46B9EA62114AC9F0EF
    CREATED:20140110T102231Z
    DESCRIPTION:Test.
    LAST-MODIFIED:20140123T095221Z
    LOCATION:25.12.01.51
    SEQUENCE:0
    STATUS:CONFIRMED
    SUMMARY:Präsentation
    TRANSP:OPAQUE
    END:VEVENT
    END:VCALENDAR
    """
).strip()


def test_multiline_uid_complex(benchmark):
    assert benchmark(lambda: vobject.Item(complex_uid_item).uid) == (
        "040000008200E00074C5B7101A82E008000000005"
        "0AAABEEF50DCF001000000062548482FA830A46B9"
        "EA62114AC9F0EF"
    )


def test_replace_multiline_uid(benchmark):
    def inner():
        return vobject.Item(complex_uid_item).with_uid("a").uid

    assert benchmark(inner) == "a"


@pytest.mark.parametrize(
    "template", [EVENT_TEMPLATE, EVENT_WITH_TIMEZONE_TEMPLATE, VCARD_TEMPLATE]
)
@given(uid=st.one_of(st.none(), uid_strategy))
def test_replace_uid(template, uid):
    item = vobject.Item(template.format(r=123, uid=123)).with_uid(uid)
    assert item.uid == uid
    if uid:
        assert item.raw.count(f"\nUID:{uid}") == 1
    else:
        assert "\nUID:" not in item.raw


def test_broken_item():
    with pytest.raises(ValueError) as excinfo:
        vobject._Component.parse("END:FOO")

    assert "Parsing error at line 1" in str(excinfo.value)

    item = vobject.Item("END:FOO")
    assert item.parsed is None


def test_mismatched_end():
    with pytest.raises(ValueError) as excinfo:
        vobject._Component.parse(
            [
                "BEGIN:FOO",
                "END:BAR",
            ]
        )

    assert "Got END:BAR, expected END:FOO at line 2" in str(excinfo.value)


def test_missing_end():
    with pytest.raises(ValueError) as excinfo:
        vobject._Component.parse(
            [
                "BEGIN:FOO",
                "BEGIN:BAR",
                "END:BAR",
            ]
        )

    assert "Missing END for component(s): FOO" in str(excinfo.value)


def test_multiple_items():
    with pytest.raises(ValueError) as excinfo:
        vobject._Component.parse(
            [
                "BEGIN:FOO",
                "END:FOO",
                "BEGIN:FOO",
                "END:FOO",
            ]
        )

    assert "Found 2 components, expected one" in str(excinfo.value)

    c1, c2 = vobject._Component.parse(
        [
            "BEGIN:FOO",
            "END:FOO",
            "BEGIN:FOO",
            "END:FOO",
        ],
        multiple=True,
    )
    assert c1.name == c2.name == "FOO"


def test_input_types():
    lines = ["BEGIN:FOO", "FOO:BAR", "END:FOO"]

    for x in (lines, "\r\n".join(lines), "\r\n".join(lines).encode("ascii")):
        c = vobject._Component.parse(x)
        assert c.name == "FOO"
        assert c.props == ["FOO:BAR"]
        assert not c.subcomponents


value_strategy = st.text(
    st.characters(
        exclude_categories=("Zs", "Zl", "Zp", "Cc", "Cs"), exclude_characters=":="
    ),
    min_size=1,
).filter(lambda x: x.strip() == x)


class VobjectMachine(RuleBasedStateMachine):
    Unparsed = Bundle("unparsed")
    Parsed = Bundle("parsed")

    @rule(target=Unparsed, joined=st.booleans(), encoded=st.booleans())
    def get_unparsed_lines(self, joined, encoded):
        rv = ["BEGIN:FOO", "FOO:YES", "END:FOO"]
        if joined:
            rv = "\r\n".join(rv)
            if encoded:
                rv = rv.encode("utf-8")
        elif encoded:
            assume(False)
        return rv

    @rule(unparsed=Unparsed, target=Parsed)
    def parse(self, unparsed):
        return vobject._Component.parse(unparsed)

    @rule(parsed=Parsed, target=Unparsed)
    def serialize(self, parsed):
        return list(parsed.dump_lines())

    @rule(c=Parsed, key=uid_strategy, value=uid_strategy)
    def add_prop(self, c, key, value):
        c[key] = value
        assert c[key] == value
        assert key in c
        assert c.get(key) == value
        dump = "\r\n".join(c.dump_lines())
        assert key in dump and value in dump

    @rule(
        c=Parsed,
        key=uid_strategy,
        value=uid_strategy,
        params=st.lists(st.tuples(value_strategy, value_strategy)),
    )
    def add_prop_raw(self, c, key, value, params):
        params_str = ",".join(k + "=" + v for k, v in params)
        c.props.insert(0, f"{key};{params_str}:{value}")
        assert c[key] == value
        assert key in c
        assert c.get(key) == value

    @rule(c=Parsed, sub_c=Parsed)
    def add_component(self, c, sub_c):
        assume(sub_c is not c and sub_c not in c)
        c.subcomponents.append(sub_c)
        assert "\r\n".join(sub_c.dump_lines()) in "\r\n".join(c.dump_lines())

    @rule(c=Parsed)
    def sanity_check(self, c):
        c1 = vobject._Component.parse(c.dump_lines())
        assert c1 == c


TestVobjectMachine = VobjectMachine.TestCase


def test_dupe_consecutive_keys():
    state = VobjectMachine()
    unparsed_0 = state.get_unparsed_lines(encoded=False, joined=False)
    parsed_0 = state.parse(unparsed=unparsed_0)
    state.add_prop_raw(c=parsed_0, key="0", params=[], value="0")
    state.add_prop_raw(c=parsed_0, key="0", params=[], value="0")
    state.add_prop(c=parsed_0, key="0", value="1")
    state.teardown()


def test_component_contains():
    item = vobject._Component.parse(["BEGIN:FOO", "FOO:YES", "END:FOO"])

    assert "FOO" in item
    assert "BAZ" not in item

    with pytest.raises(ValueError):
        42 in item  # noqa: B015