def add_object(self, ref, deleted=False): if self.closed: raise RuntimeError('Cannot add object to closed xml generator') if deleted: xg = XMLGen(init=False) xg.openTag(b'record') xg.openTag(b'datafield', [[b'tag', b'970'], [b'ind1', b' '], [b'ind2', b' ']]) xg.writeTag(b'subfield', b'INDICO.{}'.format(make_compound_id(ref)), [[b'code', b'a']]) xg.closeTag(b'datafield') xg.openTag(b'datafield', [[b'tag', b'980'], [b'ind1', b' '], [b'ind2', b' ']]) xg.writeTag(b'subfield', b'DELETED', [[b'code', b'c']]) xg.closeTag(b'datafield') xg.closeTag(b'record') self.xml_generator.xml += xg.xml elif ref['type'] in {EntryType.event, EntryType.contribution, EntryType.subcontribution}: obj = obj_deref(ref) if obj is None: raise ValueError('Cannot add deleted object') elif isinstance(obj, Category) and not obj.getOwner(): raise ValueError('Cannot add object without owner: {}'.format(obj)) if obj.is_deleted or obj.event_new.is_deleted: pass elif ref['type'] == EntryType.event: self.xml_generator.xml += self._event_to_marcxml(obj) elif ref['type'] == EntryType.contribution: self.xml_generator.xml += self._contrib_to_marcxml(obj) elif ref['type'] == EntryType.subcontribution: self.xml_generator.xml += self._subcontrib_to_marcxml(obj) elif ref['type'] == EntryType.category: pass # we don't send category updates else: raise ValueError('unknown object ref: {}'.format(ref['type'])) return self.xml_generator.getXml()
def test_make_compound_id_errors(ref_type): with pytest.raises(ValueError): make_compound_id({'type': ref_type})
def test_make_compound_id(create_event, ref, expected): evt = create_event(123) evt.contributions = [Contribution(id=456, title='test', duration=timedelta(hours=1))] evt.contributions[0].subcontributions = [SubContribution(id=789, title='foo', duration=timedelta(minutes=10))] assert make_compound_id(ref) == expected