File: test_utc_offset.py

package info (click to toggle)
python-ical 12.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,776 kB
  • sloc: python: 15,157; sh: 9; makefile: 5
file content (61 lines) | stat: -rw-r--r-- 1,948 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
"""Tests for UTC-OFFSET data types."""

import datetime

import pytest
from ical.exceptions import CalendarParseError

from ical.component import ComponentModel
from ical.parsing.property import ParsedProperty
from ical.types import UtcOffset


class FakeModel(ComponentModel):
    """Model under test."""

    example: UtcOffset


def test_utc_offset() -> None:
    """Test for UTC offset fields."""

    model = FakeModel.model_validate(
        {"example": [ParsedProperty(name="example", value="-0400")]}
    )
    assert model.example.offset == datetime.timedelta(hours=-4)

    model = FakeModel.model_validate(
        {"example": [ParsedProperty(name="example", value="0500")]}
    )
    assert model.example.offset == datetime.timedelta(hours=5)

    model = FakeModel(example=UtcOffset(offset=datetime.timedelta(hours=5)))
    assert model.example.offset == datetime.timedelta(hours=5)

    with pytest.raises(CalendarParseError, match=r".*match UTC-OFFSET pattern.*"):
        FakeModel.model_validate(
            {"example": [ParsedProperty(name="example", value="abcdef")]},
        )


def test_optional_seconds() -> None:
    """Test for UTC offset fields with optional seconds."""
    model = FakeModel.model_validate(
        {"example": [ParsedProperty(name="example", value="+0019")]}
    )
    assert model.example.offset == datetime.timedelta(minutes=19)

    model = FakeModel.model_validate(
        {"example": [ParsedProperty(name="example", value="+001932")]}
    )
    assert model.example.offset == datetime.timedelta(minutes=19, seconds=32)

    model = FakeModel.model_validate(
        {"example": [ParsedProperty(name="example", value="-0019")]}
    )
    assert model.example.offset == datetime.timedelta(minutes=-19)

    model = FakeModel.model_validate(
        {"example": [ParsedProperty(name="example", value="-001932")]}
    )
    assert model.example.offset == datetime.timedelta(minutes=-19, seconds=-32)