Add more tests for HttpStorage

This commit is contained in:
Markus Unterwaditzer 2014-03-09 02:02:22 +01:00
parent 5e2c66ece1
commit 600cc64e46
2 changed files with 79 additions and 34 deletions

View file

@ -7,7 +7,10 @@
:license: MIT, see LICENSE for more details. :license: MIT, see LICENSE for more details.
''' '''
import mock
import requests
from unittest import TestCase from unittest import TestCase
from . import StorageTests
from .. import assert_item_equals from .. import assert_item_equals
from textwrap import dedent from textwrap import dedent
from vdirsyncer.storage.http import HttpStorage, Item, split_collection from vdirsyncer.storage.http import HttpStorage, Item, split_collection
@ -15,40 +18,80 @@ from vdirsyncer.storage.http import HttpStorage, Item, split_collection
class HttpStorageTests(TestCase): class HttpStorageTests(TestCase):
def _get_storage(self, **kwargs): def test_list(self):
return HttpStorage(**kwargs) collection_url = 'http://127.0.0.1/calendar/collection/'
def test_split_collection(self): with mock.patch('requests.get') as p:
(item,) = list(split_collection( p.return_value = r = mock.Mock()
dedent(u''' r.status_code = 200
BEGIN:VCALENDAR r.text = dedent(u'''
VERSION:2.0 BEGIN:VCALENDAR
PRODID:http://www.example.com/calendarapplication/ VERSION:2.0
METHOD:PUBLISH PRODID:http://www.example.com/calendarapplication/
BEGIN:VEVENT METHOD:PUBLISH
UID:461092315540@example.com BEGIN:VEVENT
ORGANIZER;CN="Alice Balder, Example Inc.":MAILTO:alice@example.com UID:461092315540@example.com
LOCATION:Somewhere ORGANIZER;CN="Alice Balder, Example Inc.":MAILTO:alice@example.com
SUMMARY:Eine Kurzinfo LOCATION:Somewhere
DESCRIPTION:Beschreibung des Termines SUMMARY:Eine Kurzinfo
CLASS:PUBLIC DESCRIPTION:Beschreibung des Termines
DTSTART:20060910T220000Z CLASS:PUBLIC
DTEND:20060919T215900Z DTSTART:20060910T220000Z
DTSTAMP:20060812T125900Z DTEND:20060919T215900Z
END:VEVENT DTSTAMP:20060812T125900Z
END:VCALENDAR END:VEVENT
BEGIN:VEVENT
UID:461092315asdasd540@example.com
LOCATION:Somewhere else
SUMMARY:Eine zweite Kurzinfo
DESCRIPTION:Beschreibung des anderen Termines
BEGIN:VALARM
ACTION:AUDIO
TRIGGER:19980403T120000
ATTACH;FMTTYPE=audio/basic:http://host.com/pub/audio-files/ssbanner.aud
REPEAT:4
DURATION:PT1H
END:VALARM
END:VEVENT
END:VCALENDAR
''') ''')
)) s = HttpStorage(url=collection_url)
assert_item_equals(item, Item(dedent(u'''
BEGIN:VEVENT l = list(s.list())
UID:461092315540@example.com r.side_effect = IOError('Nope.')
ORGANIZER;CN="Alice Balder, Example Inc.":MAILTO:alice@example.com hrefs = set(href for href, etag in l)
LOCATION:Somewhere href1 = u'461092315540@example.com'
SUMMARY:Eine Kurzinfo href2 = u'461092315asdasd540@example.com'
DESCRIPTION:Beschreibung des Termines assert hrefs == set((href1, href2))
CLASS:PUBLIC
DTSTART:20060910T220000Z item, etag = s.get(href1)
DTEND:20060919T215900Z assert_item_equals(item, Item(dedent(u'''
DTSTAMP:20060812T125900Z BEGIN:VEVENT
END:VEVENT UID:461092315540@example.com
ORGANIZER;CN="Alice Balder, Example Inc.":MAILTO:alice@example.com
LOCATION:Somewhere
SUMMARY:Eine Kurzinfo
DESCRIPTION:Beschreibung des Termines
CLASS:PUBLIC
DTSTART:20060910T220000Z
DTEND:20060919T215900Z
DTSTAMP:20060812T125900Z
END:VEVENT
''').strip()))
item, etag = s.get(href2)
assert_item_equals(item, Item(dedent(u'''
BEGIN:VEVENT
UID:461092315asdasd540@example.com
LOCATION:Somewhere else
SUMMARY:Eine zweite Kurzinfo
DESCRIPTION:Beschreibung des anderen Termines
BEGIN:VALARM
ACTION:AUDIO
TRIGGER:19980403T120000
ATTACH;FMTTYPE=audio/basic:http://host.com/pub/audio-files/ssbanner.aud
REPEAT:4
DURATION:PT1H
END:VALARM
END:VEVENT
''').strip())) ''').strip()))

View file

@ -27,6 +27,8 @@ def split_collection(text):
elif item_type is None: elif item_type is None:
item_type = value item_type = value
item.append(line) item.append(line)
else:
item.append(line)
elif key == u'END': elif key == u'END':
if value == collection_type: if value == collection_type:
break break