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
|
"""Tests for RELATED-TO data types."""
from pydantic import field_serializer
import pytest
from ical.exceptions import CalendarParseError
from ical.component import ComponentModel
from ical.parsing.component import ParsedComponent
from ical.parsing.property import ParsedProperty, ParsedPropertyParameter
from ical.types import RelatedTo, RelationshipType
from ical.types.data_types import serialize_field
class FakeModel(ComponentModel):
"""Model under test."""
example: RelatedTo
serialize_fields = field_serializer("*")(serialize_field) # type: ignore[pydantic-field]
def test_default_reltype() -> None:
"""Test for no explicit reltype specified."""
model = FakeModel.model_validate(
{
"example": [
ParsedProperty(
name="example",
value="example-uid@example.com",
params=[ParsedPropertyParameter(name="RELTYPE", values=["PARENT"])],
)
]
},
)
assert model.example
assert model.example.uid == "example-uid@example.com"
assert model.example.reltype == "PARENT"
@pytest.mark.parametrize(
"reltype",
[
("PARENT"),
("CHILD"),
("SIBBLING"),
],
)
def test_reltype(reltype: str) -> None:
"""Test for no explicit reltype specified."""
model = FakeModel.model_validate(
{
"example": [
ParsedProperty(
name="example",
value="example-uid@example.com",
params=[ParsedPropertyParameter(name="reltype", values=[reltype])],
)
]
},
)
assert model.example
assert model.example.uid == "example-uid@example.com"
assert model.example.reltype == reltype
def test_invalid_reltype() -> None:
with pytest.raises(CalendarParseError):
model = FakeModel.model_validate(
{
"example": [
ParsedProperty(
name="example",
value="example-uid@example.com",
params=[
ParsedPropertyParameter(
name="reltype", values=["invalid-reltype"]
)
],
)
]
},
)
def test_too_many_reltype_values() -> None:
with pytest.raises(CalendarParseError):
FakeModel.model_validate(
{
"example": [
ParsedProperty(
name="example",
value="example-uid@example.com",
params=[
ParsedPropertyParameter(
name="reltype", values=["PARENT", "SIBBLING"]
)
],
)
]
},
)
def test_encode_default_reltype() -> None:
"""Test encoded period."""
model = FakeModel(example=RelatedTo(uid="example-uid@example.com"))
assert model.__encode_component_root__() == ParsedComponent(
name="FakeModel",
properties=[
ParsedProperty(
name="example",
value="example-uid@example.com",
params=[ParsedPropertyParameter(name="RELTYPE", values=["PARENT"])],
),
],
)
def test_encode_reltype() -> None:
"""Test encoded period."""
model = FakeModel(
example=RelatedTo(uid="example-uid@example.com", reltype=RelationshipType.CHILD)
)
assert model.__encode_component_root__() == ParsedComponent(
name="FakeModel",
properties=[
ParsedProperty(
name="example",
value="example-uid@example.com",
params=[ParsedPropertyParameter(name="RELTYPE", values=["CHILD"])],
),
],
)
|