diff --git a/CHANGES.rst b/CHANGES.rst index af520da2..1c96ea69 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,7 @@ Changelog Minor changes: - add ``__all__`` variable to each modules in ``icalendar`` package +- Improve test coverage. - Adapt ``test_with_doctest.py`` to correctly run on Windows. Breaking changes: @@ -21,6 +22,7 @@ Bug fixes: - Fix link to stable release of tox in documentation. - Fix a bad bytes replace in unescape_char. +- Handle ``ValueError`` in ``vBinary.from_ical``. 6.0.0a0 (2024-07-03) -------------------- diff --git a/src/icalendar/prop.py b/src/icalendar/prop.py index 509179ec..e7f58aba 100644 --- a/src/icalendar/prop.py +++ b/src/icalendar/prop.py @@ -88,7 +88,7 @@ def __init__(self, obj): self.params = Parameters(encoding='BASE64', value="BINARY") def __repr__(self): - return f"vBinary('{self.to_ical()}')" + return f"vBinary({self.to_ical()})" def to_ical(self): return binascii.b2a_base64(self.obj.encode('utf-8'))[:-1] @@ -97,7 +97,7 @@ def to_ical(self): def from_ical(ical): try: return base64.b64decode(ical) - except UnicodeError: + except (ValueError, UnicodeError): raise ValueError('Not valid base 64 encoding.') def __eq__(self, other): @@ -160,7 +160,7 @@ def __new__(cls, value, encoding=DEFAULT_ENCODING): return self def __repr__(self): - return f"vCalAddress('{self.to_ical()}')" + return f"vCalAddress('{self}')" def to_ical(self): return self.encode(DEFAULT_ENCODING) diff --git a/src/icalendar/tests/prop/test_unit.py b/src/icalendar/tests/prop/test_unit.py index 57a6ed62..9240036e 100644 --- a/src/icalendar/tests/prop/test_unit.py +++ b/src/icalendar/tests/prop/test_unit.py @@ -44,6 +44,9 @@ def test_prop_vDDDLists(self): dt_list = vDDDLists([datetime(2000, 1, 1), datetime(2000, 11, 11)]) self.assertEqual(dt_list.to_ical(), b'20000101T000000,20001111T000000') + + instance = vDDDLists([]) + self.assertFalse(instance == "value") def test_prop_vDate(self): from icalendar.prop import vDate @@ -110,6 +113,7 @@ def test_prop_vFrequency(self): self.assertRaises(ValueError, vFrequency, 'bad test') self.assertEqual(vFrequency('daily').to_ical(), b'DAILY') self.assertEqual(vFrequency('daily').from_ical('MONTHLY'), 'MONTHLY') + self.assertRaises(ValueError, vFrequency.from_ical, 234) def test_prop_vRecur(self): from icalendar.prop import vRecur diff --git a/src/icalendar/tests/prop/test_vBinary.py b/src/icalendar/tests/prop/test_vBinary.py index 7fde7579..d3a44893 100644 --- a/src/icalendar/tests/prop/test_vBinary.py +++ b/src/icalendar/tests/prop/test_vBinary.py @@ -1,4 +1,6 @@ """Test vBinary""" +import pytest + from icalendar import vBinary from icalendar.parser import Parameters @@ -27,3 +29,13 @@ def test_long_data(): txt_ical = b'YWFh' * 33 assert (vBinary(txt).to_ical() == txt_ical) assert (vBinary.from_ical(txt_ical) == txt) + +def test_repr(): + instance = vBinary("value") + assert repr(instance) == "vBinary(b'dmFsdWU=')" + +def test_from_ical(): + with pytest.raises(ValueError, match='Not valid base 64 encoding.'): + vBinary.from_ical("value") + with pytest.raises(ValueError, match='Not valid base 64 encoding.'): + vBinary.from_ical("áèਮ") \ No newline at end of file diff --git a/src/icalendar/tests/prop/test_vCalAddress.py b/src/icalendar/tests/prop/test_vCalAddress.py index 6a8ff803..b8e1f789 100644 --- a/src/icalendar/tests/prop/test_vCalAddress.py +++ b/src/icalendar/tests/prop/test_vCalAddress.py @@ -17,3 +17,8 @@ def test_params(): def test_from_ical(): assert vCalAddress.from_ical(txt) == 'MAILTO:maxm@mxm.dk' + + +def test_repr(): + instance = vCalAddress("value") + assert repr(instance) == "vCalAddress('value')" \ No newline at end of file