From 0eae8ceb046756eafcfdac08e7b6049b17dc7059 Mon Sep 17 00:00:00 2001 From: Nicco Kunzmann Date: Wed, 1 Nov 2023 01:20:52 +0000 Subject: [PATCH] Add test file for equality tests --- src/icalendar/cal.py | 5 ++++ src/icalendar/tests/test_equality.py | 43 ++++++++++++++++++++++++++++ src/icalendar/tests/test_examples.py | 16 +---------- 3 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 src/icalendar/tests/test_equality.py diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index 0fd8ec2..ad3b87b 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -465,6 +465,11 @@ class Component(CaselessDict): return True + def copy(self): + """Create a copy of the component.""" + copy = super().copy() + copy.subcomponents = self.subcomponents[:] + return copy ####################################### # components defined in RFC 5545 diff --git a/src/icalendar/tests/test_equality.py b/src/icalendar/tests/test_equality.py new file mode 100644 index 0000000..e3fb03f --- /dev/null +++ b/src/icalendar/tests/test_equality.py @@ -0,0 +1,43 @@ +"""Test the equality and inequality of components.""" +import copy +import pytz + + +def test_parsed_calendars_are_equal(ics_file): + """Ensure that a calendar equals the same calendar.""" + copy_of_calendar = ics_file.__class__.from_ical(ics_file.to_ical()) + assert copy_of_calendar == ics_file + assert not copy_of_calendar != ics_file + + +def test_copies_are_equal(ics_file): + """Ensure that copies are equal.""" + assert ics_file.copy() == ics_file.copy() + assert ics_file.copy() == ics_file + assert not ics_file.copy() != ics_file.copy() + assert not ics_file.copy() != ics_file + +def test_deep_copies_are_equal(ics_file): + """Ensure that deep copies are equal.""" + try: + assert copy.deepcopy(ics_file) == copy.deepcopy(ics_file) + assert copy.deepcopy(ics_file) == ics_file + assert not copy.deepcopy(ics_file) != copy.deepcopy(ics_file) + assert not copy.deepcopy(ics_file) != ics_file + except pytz.UnknownTimeZoneError: + # Ignore errors when a custom time zone is used. + # This is still covered by the parsing test. + pass + + +def test_a_components_copy_also_copies_subcomponents(calendars): + """A calendar's copy does not have the identical subcompoenets! + + We expect to be able to modify a copy but not its values. + """ + cal = calendars.timezoned + copy = cal.copy() + assert copy is not cal + assert copy.subcomponents + assert copy.subcomponents is not cal.subcomponents + assert copy.subcomponents == cal.subcomponents diff --git a/src/icalendar/tests/test_examples.py b/src/icalendar/tests/test_examples.py index 492d439..0c1c132 100644 --- a/src/icalendar/tests/test_examples.py +++ b/src/icalendar/tests/test_examples.py @@ -3,7 +3,7 @@ import datetime from icalendar import Calendar, Event import pytest -import copy + def test_creating_calendar_with_unicode_fields(calendars, utc): ''' create a calendar with events that contain unicode characters in their fields ''' @@ -37,17 +37,3 @@ def test_creating_calendar_with_unicode_fields(calendars, utc): cal.add_component(event2) assert cal.to_ical() == calendars.created_calendar_with_unicode_fields.raw_ics - - -def test_parsed_calendars_are_equal(ics_file): - """Ensure that a calendar equals the same calendar.""" - copy_of_calendar = ics_file.__class__.from_ical(ics_file.to_ical()) - assert copy_of_calendar == ics_file - -def test_copies_are_equal(ics_file): - """Ensure that copies are equal.""" - assert ics_file.copy() == ics_file.copy() == ics_file - -def test_deep_copies_are_equal(ics_file): - """Ensure that deep copies are equal.""" - assert copy.deepcopy(ics_file) == copy.deepcopy(ics_file) == ics_file