def test_partial_sync_revert(): a = MemoryStorage(instance_name='a') b = MemoryStorage(instance_name='b') status = {} a.upload(Item('UID:1')) b.upload(Item('UID:2')) b.read_only = True sync(a, b, status, partial_sync='revert') assert len(status) == 2 assert items(a) == {'UID:1', 'UID:2'} assert items(b) == {'UID:2'} sync(a, b, status, partial_sync='revert') assert len(status) == 1 assert items(a) == {'UID:2'} assert items(b) == {'UID:2'} # Check that updates get reverted a.items[next(iter(a.items))] = ('foo', Item('UID:2\nupdated')) assert items(a) == {'UID:2\nupdated'} sync(a, b, status, partial_sync='revert') assert len(status) == 1 assert items(a) == {'UID:2\nupdated'} sync(a, b, status, partial_sync='revert') assert items(a) == {'UID:2'} # Check that deletions get reverted a.items.clear() sync(a, b, status, partial_sync='revert', force_delete=True) sync(a, b, status, partial_sync='revert', force_delete=True) assert items(a) == {'UID:2'}
def test_partial_sync_revert(): a = MemoryStorage(instance_name="a") b = MemoryStorage(instance_name="b") status = {} a.upload(Item("UID:1")) b.upload(Item("UID:2")) b.read_only = True sync(a, b, status, partial_sync="revert") assert len(status) == 2 assert items(a) == {"UID:1", "UID:2"} assert items(b) == {"UID:2"} sync(a, b, status, partial_sync="revert") assert len(status) == 1 assert items(a) == {"UID:2"} assert items(b) == {"UID:2"} # Check that updates get reverted a.items[next(iter(a.items))] = ("foo", Item("UID:2\nupdated")) assert items(a) == {"UID:2\nupdated"} sync(a, b, status, partial_sync="revert") assert len(status) == 1 assert items(a) == {"UID:2\nupdated"} sync(a, b, status, partial_sync="revert") assert items(a) == {"UID:2"} # Check that deletions get reverted a.items.clear() sync(a, b, status, partial_sync="revert", force_delete=True) sync(a, b, status, partial_sync="revert", force_delete=True) assert items(a) == {"UID:2"}
def test_rollback(error_callback): a = MemoryStorage() b = MemoryStorage() status = {} a.items['0'] = ('', Item('UID:0')) b.items['1'] = ('', Item('UID:1')) b.upload = b.update = b.delete = action_failure if error_callback: errors = [] sync(a, b, status=status, conflict_resolution='a wins', error_callback=errors.append) assert len(errors) == 1 assert isinstance(errors[0], ActionIntentionallyFailed) assert len(status) == 1 assert status['1'] else: with pytest.raises(ActionIntentionallyFailed): sync(a, b, status=status, conflict_resolution='a wins')
def test_no_uids(): a = MemoryStorage() b = MemoryStorage() a.upload(Item("ASDF")) b.upload(Item("FOOBAR")) status = {} sync(a, b, status) assert items(a) == items(b) == {"ASDF", "FOOBAR"}
def test_no_uids(): a = MemoryStorage() b = MemoryStorage() a.upload(Item('ASDF')) b.upload(Item('FOOBAR')) status = {} sync(a, b, status) assert items(a) == items(b) == {'ASDF', 'FOOBAR'}
def test_conflict_resolution_invalid_mode(): a = MemoryStorage() b = MemoryStorage() item_a = Item('UID:1\nitem a') item_b = Item('UID:1\nitem b') a.upload(item_a) b.upload(item_b) with pytest.raises(ValueError): sync(a, b, {}, conflict_resolution='yolo')
def test_changed_uids(): a = MemoryStorage() b = MemoryStorage() href_a, etag_a = a.upload(Item('UID:A-ONE')) href_b, etag_b = b.upload(Item('UID:B-ONE')) status = {} sync(a, b, status) a.update(href_a, Item('UID:A-TWO'), etag_a) sync(a, b, status)
def test_empty_storage_dataloss(): a = MemoryStorage() b = MemoryStorage() a.upload(Item('UID:1')) a.upload(Item('UID:2')) status = {} sync(a, b, status) with pytest.raises(StorageEmpty): sync(MemoryStorage(), b, status) with pytest.raises(StorageEmpty): sync(a, MemoryStorage(), status)
def test_ident_conflict(sync_inbetween): a = MemoryStorage() b = MemoryStorage() status = {} href_a, etag_a = a.upload(Item('UID:aaa')) href_b, etag_b = a.upload(Item('UID:bbb')) if sync_inbetween: sync(a, b, status) a.update(href_a, Item('UID:xxx'), etag_a) a.update(href_b, Item('UID:xxx'), etag_b) with pytest.raises(IdentConflict): sync(a, b, status)
def test_repair_uids(uid): s = MemoryStorage() s.items = { 'one': ('asdf', Item(f'BEGIN:VCARD\nFN:Hans\nUID:{uid}\nEND:VCARD')), 'two': ('asdf', Item(f'BEGIN:VCARD\nFN:Peppi\nUID:{uid}\nEND:VCARD')) } uid1, uid2 = [s.get(href)[0].uid for href, etag in s.list()] assert uid1 == uid2 repair_storage(s, repair_unsafe_uid=False) uid1, uid2 = [s.get(href)[0].uid for href, etag in s.list()] assert uid1 != uid2
def test_missing_status_and_different_items(): a = MemoryStorage() b = MemoryStorage() status = {} item1 = Item('UID:1\nhaha') item2 = Item('UID:1\nhoho') a.upload(item1) b.upload(item2) with pytest.raises(SyncConflict): sync(a, b, status) assert not status sync(a, b, status, conflict_resolution='a wins') assert items(a) == items(b) == {item1.raw}
def test_updated_and_deleted(): a = MemoryStorage() b = MemoryStorage() href_a, etag_a = a.upload(Item('UID:1')) status = {} sync(a, b, status, force_delete=True) (href_b, etag_b), = b.list() b.delete(href_b, etag_b) updated = Item('UID:1\nupdated') a.update(href_a, updated, etag_a) sync(a, b, status, force_delete=True) assert items(a) == items(b) == {updated.raw}
def test_repair_uids(uid): s = MemoryStorage() s.items = { "one": ("asdf", Item(f"BEGIN:VCARD\nFN:Hans\nUID:{uid}\nEND:VCARD")), "two": ("asdf", Item(f"BEGIN:VCARD\nFN:Peppi\nUID:{uid}\nEND:VCARD")), } uid1, uid2 = [s.get(href)[0].uid for href, etag in s.list()] assert uid1 == uid2 repair_storage(s, repair_unsafe_uid=False) uid1, uid2 = [s.get(href)[0].uid for href, etag in s.list()] assert uid1 != uid2
def test_insert_hash(): a = MemoryStorage() b = MemoryStorage() status = {} item = Item('UID:1') href, etag = a.upload(item) sync(a, b, status) for d in status['1']: del d['hash'] a.update(href, Item('UID:1\nHAHA:YES'), etag) sync(a, b, status) assert 'hash' in status['1'][0] and 'hash' in status['1'][1]
def test_read_only_and_prefetch(): a = MemoryStorage() b = MemoryStorage() b.read_only = True status = {} item1 = Item('UID:1\nhaha') item2 = Item('UID:2\nhoho') a.upload(item1) a.upload(item2) sync(a, b, status, force_delete=True) sync(a, b, status, force_delete=True) assert not items(a) and not items(b)
def test_insert_hash(): a = MemoryStorage() b = MemoryStorage() status = {} item = Item("UID:1") href, etag = a.upload(item) sync(a, b, status) for d in status["1"]: del d["hash"] a.update(href, Item("UID:1\nHAHA:YES"), etag) sync(a, b, status) assert "hash" in status["1"][0] and "hash" in status["1"][1]
def test_uses_get_multi(monkeypatch): def breakdown(*a, **kw): raise AssertionError('Expected use of get_multi') get_multi_calls = [] old_get = MemoryStorage.get def get_multi(self, hrefs): hrefs = list(hrefs) get_multi_calls.append(hrefs) for href in hrefs: item, etag = old_get(self, href) yield href, item, etag monkeypatch.setattr(MemoryStorage, 'get', breakdown) monkeypatch.setattr(MemoryStorage, 'get_multi', get_multi) a = MemoryStorage() b = MemoryStorage() item = Item('UID:1') expected_href, etag = a.upload(item) sync(a, b, {}) assert get_multi_calls == [[expected_href]]
def sync_local_changes(self, davStorageUrl): davStorage = self.davStorages[davStorageUrl] collectionId = self.get_collection_id(davStorageUrl) for deletedItem in self.db((self.db.colitems.local_status == 2) & (self.db.colitems.collection == collectionId)).select(): print("Deleting locally removed item with etag={} from server".format(deletedItem.etag)) davStorage.delete(deletedItem.href, deletedItem.etag) deletedItem.delete_record() for modifiedItem in self.db((self.db.colitems.local_status == 1) & (self.db.colitems.collection == collectionId)).select(): print("Updating locally modified item with etag={} at server".format(modifiedItem.etag)) newEtag = davStorage.update(modifiedItem.href, Item(modifiedItem.content), modifiedItem.etag) modifiedItem.update_record(etag=newEtag, local_status=0) for newItem in self.db((self.db.colitems.local_status == 3) & (self.db.colitems.collection == collectionId)).select(): print("Adding a new ical to the server") href, etag = davStorage.upload(Item(newItem.content)) newItem.update_record(etag=etag, href=href, local_status=0) self.db.commit()
def test_already_synced(): a = MemoryStorage(fileext=".a") b = MemoryStorage(fileext=".b") item = Item("UID:1") a.upload(item) b.upload(item) status = { "1": ( { "href": "1.a", "hash": item.hash, "etag": a.get("1.a")[1] }, { "href": "1.b", "hash": item.hash, "etag": b.get("1.b")[1] }, ) } old_status = deepcopy(status) a.update = b.update = a.upload = b.upload = lambda *a, **kw: pytest.fail( "Method shouldn't have been called.") for _ in (1, 2): sync(a, b, status) assert status == old_status assert items(a) == items(b) == {item.raw}
def test_dav_broken_item(self, s): item = Item(u'HAHA:YES') try: s.upload(item) except (exceptions.Error, requests.exceptions.HTTPError): pass assert not list(s.list())
def test_already_synced(): a = MemoryStorage(fileext='.a') b = MemoryStorage(fileext='.b') item = Item('UID:1') a.upload(item) b.upload(item) status = { '1': ({ 'href': '1.a', 'hash': item.hash, 'etag': a.get('1.a')[1] }, { 'href': '1.b', 'hash': item.hash, 'etag': b.get('1.b')[1] }) } old_status = deepcopy(status) a.update = b.update = a.upload = b.upload = \ lambda *a, **kw: pytest.fail('Method shouldn\'t have been called.') for _ in (1, 2): sync(a, b, status) assert status == old_status assert items(a) == items(b) == {item.raw}
def test_moved_href(): ''' Concrete application: ppl_ stores contact aliases in filenames, which means item's hrefs get changed. Vdirsyncer doesn't synchronize this data, but also shouldn't do things like deleting and re-uploading to the server. .. _ppl: http://ppladdressbook.org/ ''' a = MemoryStorage() b = MemoryStorage() status = {} href, etag = a.upload(Item('UID:haha')) sync(a, b, status) b.items['lol'] = b.items.pop('haha') # The sync algorithm should prefetch `lol`, see that it's the same ident # and not do anything else. a.get_multi = blow_up # Absolutely no prefetch on A # No actual sync actions a.delete = a.update = a.upload = b.delete = b.update = b.upload = blow_up sync(a, b, status) assert len(status) == 1 assert items(a) == items(b) == {'UID:haha'} assert status['haha'][1]['href'] == 'lol' old_status = deepcopy(status) # Further sync should be a noop. Not even prefetching should occur. b.get_multi = blow_up sync(a, b, status) assert old_status == status assert items(a) == items(b) == {'UID:haha'}
def test_post_hook_inactive(self, tmpdir, monkeypatch): def check_call_mock(*args, **kwargs): raise AssertionError() monkeypatch.setattr(subprocess, 'call', check_call_mock) s = self.storage_class(str(tmpdir), '.txt', post_hook=None) s.upload(Item('UID:a/b/c'))
def test_ignore_tmp_files(self, tmpdir): """Test that files with .tmp suffix beside .ics files are ignored.""" s = self.storage_class(str(tmpdir), '.ics') s.upload(Item('UID:xyzxyz')) item_file, = tmpdir.listdir() item_file.copy(item_file.new(ext='tmp')) assert len(tmpdir.listdir()) == 2 assert len(list(s.list())) == 1
def test_conflict_resolution_both_etags_new(winning_storage): a = MemoryStorage() b = MemoryStorage() item = Item("UID:1") href_a, etag_a = a.upload(item) href_b, etag_b = b.upload(item) status = {} sync(a, b, status) assert status item_a = Item("UID:1\nitem a") item_b = Item("UID:1\nitem b") a.update(href_a, item_a, etag_a) b.update(href_b, item_b, etag_b) with pytest.raises(SyncConflict): sync(a, b, status) sync(a, b, status, conflict_resolution=f"{winning_storage} wins") assert (items(a) == items(b) == {item_a.raw if winning_storage == "a" else item_b.raw})
def test_ignore_tmp_files_empty_fileext(self, tmpdir): """Test that files with .tmp suffix are ignored with empty fileext.""" s = self.storage_class(str(tmpdir), '') s.upload(Item('UID:xyzxyz')) item_file, = tmpdir.listdir() item_file.copy(item_file.new(ext='tmp')) assert len(tmpdir.listdir()) == 2 # assert False, tmpdir.listdir() # enable to see the created filename assert len(list(s.list())) == 1
def test_conflict_resolution_command(): def check_call(command): command, a_tmp, b_tmp = command assert command == os.path.expanduser('~/command') with open(a_tmp) as f: assert f.read() == a.raw with open(b_tmp) as f: assert f.read() == b.raw with open(b_tmp, 'w') as f: f.write(a.raw) a = Item('UID:AAAAAAA') b = Item('UID:BBBBBBB') assert _resolve_conflict_via_command( a, b, ['~/command'], 'a', 'b', _check_call=check_call ).raw == a.raw
def test_list(monkeypatch): collection_url = 'http://127.0.0.1/calendar/collection.ics' items = [(u'BEGIN:VEVENT\n' u'SUMMARY:Eine Kurzinfo\n' u'DESCRIPTION:Beschreibung des Termines\n' u'END:VEVENT'), (u'BEGIN:VEVENT\n' u'SUMMARY:Eine zweite Küèrzinfo\n' u'DESCRIPTION:Beschreibung des anderen Termines\n' u'BEGIN:VALARM\n' u'ACTION:AUDIO\n' u'TRIGGER:19980403T120000\n' u'ATTACH;FMTTYPE=audio/basic:http://host.com/pub/ssbanner.aud\n' u'REPEAT:4\n' u'DURATION:PT1H\n' u'END:VALARM\n' u'END:VEVENT')] responses = [ u'\n'.join([u'BEGIN:VCALENDAR'] + items + [u'END:VCALENDAR']) ] * 2 def get(self, method, url, *a, **kw): assert method == 'GET' assert url == collection_url r = Response() r.status_code = 200 assert responses r._content = responses.pop().encode('utf-8') r.headers['Content-Type'] = 'text/calendar' r.encoding = 'ISO-8859-1' return r monkeypatch.setattr('requests.sessions.Session.request', get) s = HttpStorage(url=collection_url) found_items = {} for href, etag in s.list(): item, etag2 = s.get(href) assert item.uid is not None assert etag2 == etag found_items[item.hash] = href expected = set( Item(u'BEGIN:VCALENDAR\n' + x + '\nEND:VCALENDAR').hash for x in items) assert set(found_items) == expected for href, etag in s.list(): item, etag2 = s.get(href) assert item.uid is not None assert etag2 == etag assert found_items[item.hash] == href
def test_partial_sync_error(): a = MemoryStorage() b = MemoryStorage() status = {} a.upload(Item('UID:0')) b.read_only = True with pytest.raises(PartialSync): sync(a, b, status, partial_sync='error')
def test_duplicate_hrefs(): a = MemoryStorage() b = MemoryStorage() a.list = lambda: [('a', 'a')] * 3 a.items['a'] = ('a', Item('UID:a')) status = {} sync(a, b, status) with pytest.raises(AssertionError): sync(a, b, status)