Skip to content

Commit

Permalink
Merge pull request #600 from niccokunzmann/refactor-test-4
Browse files Browse the repository at this point in the history
Refactor property parameter tests
  • Loading branch information
jacadzaca committed Mar 26, 2024
2 parents 68893d7 + df92604 commit af34e22
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 65 deletions.
21 changes: 21 additions & 0 deletions src/icalendar/tests/calendars/property_params.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID://RESEARCH IN MOTION//BIS 3.0
METHOD:REQUEST
BEGIN:VEVENT
SEQUENCE:2
X-RIM-REVISION:0
SUMMARY:Test meeting from BB
X-MICROSOFT-CDO-ALLDAYEVENT:TRUE
CLASS:PUBLIC
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="RembrandXS":MAILTO:rembrand@xs4all.nl
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="RembrandDX":MAILTO:rembrand@daxlab.com
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="RembrandSB":MAILTO:rembspam@xs4all.nl
UID:XRIMCAL-628059586-522954492-9750559
DTSTART;VALUE=DATE:20120814
DTEND;VALUE=DATE:20120815
DESCRIPTION:Test meeting from BB
DTSTAMP:20120813T151458Z
ORGANIZER:mailto:rembrand@daxlab.com
END:VEVENT
END:VCALENDAR
107 changes: 42 additions & 65 deletions src/icalendar/tests/test_property_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ def test_parameter_to_ical_is_inverse_of_from_ical(parameter, expected):
assert parameter.to_ical() == expected
assert Parameters.from_ical(expected.decode('utf-8')) == parameter


def test_parse_parameter_string_without_quotes():
assert Parameters.from_ical('PARAM1=Value 1;PARA2=Value 2') == Parameters({'PARAM1': 'Value 1', 'PARA2': 'Value 2'})


def test_parametr_is_case_insensitive():
parameter = Parameters(parameter1='Value1')
assert parameter['parameter1'] == parameter['PARAMETER1'] == parameter['PaRaMeTer1']


def test_parameter_keys_are_uppercase():
parameter = Parameters(parameter1='Value1')
assert list(parameter.keys()) == ['PARAMETER1']


@pytest.mark.parametrize('cn_param, cn_quoted', [
# not double-quoted
('Aramis', 'Aramis'),
Expand All @@ -67,68 +71,41 @@ def test_quoting(cn_param, cn_quoted):
event.add('ATTENDEE', attendee)
assert f'ATTENDEE;CN={cn_quoted}:test@example.com' in event.to_ical().decode('utf-8')

class TestPropertyParams(unittest.TestCase):

def test_property_params(self):
# Property parameters with values containing a COLON character, a
# SEMICOLON character or a COMMA character MUST be placed in quoted
# text.
cal_address = vCalAddress('mailto:john.doe@example.org')
cal_address.params["CN"] = "Doe, John"
ical = Calendar()
ical.add('organizer', cal_address)

ical_str = Calendar.to_ical(ical)
exp_str = b"""BEGIN:VCALENDAR\r\nORGANIZER;CN="Doe, John":"""\
b"""mailto:john.doe@example.org\r\nEND:VCALENDAR\r\n"""

self.assertEqual(ical_str, exp_str)

# other way around: ensure the property parameters can be restored from
# an icalendar string.
ical2 = Calendar.from_ical(ical_str)
self.assertEqual(ical2.get('ORGANIZER').params.get('CN'), 'Doe, John')

def test_parse_and_access_property_params(self):
"""Parse an ics string and access some property parameters then.
This is a follow-up of a question received per email.
"""
ics = """BEGIN:VCALENDAR
VERSION:2.0
PRODID://RESEARCH IN MOTION//BIS 3.0
METHOD:REQUEST
BEGIN:VEVENT
SEQUENCE:2
X-RIM-REVISION:0
SUMMARY:Test meeting from BB
X-MICROSOFT-CDO-ALLDAYEVENT:TRUE
CLASS:PUBLIC
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="RembrandXS":MAILTO:rembrand@xs4all.nl
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="RembrandDX":MAILTO:rembrand@daxlab.com
ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="RembrandSB":MAILTO:rembspam@xs4all.nl
UID:XRIMCAL-628059586-522954492-9750559
DTSTART;VALUE=DATE:20120814
DTEND;VALUE=DATE:20120815
DESCRIPTION:Test meeting from BB
DTSTAMP:20120813T151458Z
ORGANIZER:mailto:rembrand@daxlab.com
END:VEVENT
END:VCALENDAR"""

cal = icalendar.Calendar.from_ical(ics)
event = cal.walk("VEVENT")[0]
event['attendee'][0]
self.assertEqual(event['attendee'][0].to_ical(),
b'MAILTO:rembrand@xs4all.nl')
self.assertEqual(event['attendee'][0].params.to_ical(),
b'CN=RembrandXS;PARTSTAT=NEEDS-ACTION;RSVP=TRUE')
self.assertEqual(event['attendee'][0].params['cn'], 'RembrandXS')

def test_repr(self):
"""Test correct class representation.
"""
it = Parameters(parameter1='Value1')
self.assertTrue(
re.match(r"Parameters\({u?'PARAMETER1': u?'Value1'}\)", str(it))
)

def test_property_params():
"""Property parameters with values containing a COLON character, a
SEMICOLON character or a COMMA character MUST be placed in quoted
text."""
cal_address = vCalAddress('mailto:john.doe@example.org')
cal_address.params["CN"] = "Doe, John"
ical = Calendar()
ical.add('organizer', cal_address)

ical_str = Calendar.to_ical(ical)
exp_str = b"""BEGIN:VCALENDAR\r\nORGANIZER;CN="Doe, John":"""\
b"""mailto:john.doe@example.org\r\nEND:VCALENDAR\r\n"""

assert ical_str == exp_str

# other way around: ensure the property parameters can be restored from
# an icalendar string.
ical2 = Calendar.from_ical(ical_str)
assert ical2.get('ORGANIZER').params.get('CN') == 'Doe, John'


def test_parse_and_access_property_params(calendars):
"""Parse an ics string and access some property parameters then.
This is a follow-up of a question received per email.
"""
event = calendars.property_params.walk("VEVENT")[0]
attendee = event['attendee'][0]
assert attendee.to_ical() == b'MAILTO:rembrand@xs4all.nl'
assert attendee.params.to_ical() == b'CN=RembrandXS;PARTSTAT=NEEDS-ACTION;RSVP=TRUE'
assert attendee.params['cn'] == 'RembrandXS'

def test_repr():
"""Test correct class representation.
"""
it = Parameters(parameter1='Value1')
assert re.match(r"Parameters\({u?'PARAMETER1': u?'Value1'}\)", str(it))

0 comments on commit af34e22

Please sign in to comment.