def test_environments_with_purpose(): """Test the environments_with_purpose function. Only one environment should be returned from the created test data containing two environments. """ env = o.environment(child_elements=[ o.dependency(identifiers=[p.identifier('c', 'd', 'dependency')]) ]) env2 = o.environment( purposes=['a'], child_elements=[ o.dependency(identifiers=[p.identifier('c', 'd', 'dependency')]) ]) obj = o.object(p.identifier('x', 'y', 'object'), child_elements=[env, env2]) xml = ('<premis:environment xmlns:premis="info:lc/xmlns/premis-v2" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' '<premis:environmentPurpose>a</premis:environmentPurpose>' '<premis:dependency>' '<premis:dependencyIdentifier><premis:dependencyIdentifierType>' 'c</premis:dependencyIdentifierType>' '<premis:dependencyIdentifierValue>' 'd</premis:dependencyIdentifierValue></premis:dependencyIdentifier>' '</premis:dependency></premis:environment>') environments = o.iter_environments(obj) penvs = o.environments_with_purpose(environments, purpose='a') assert len(list(penvs)) == 1 for penv in penvs: assert u.compare_trees(penv, ET.fromstring(xml))
def test_object_count(): """Test object_count""" obj1 = o.object(p.identifier('x', 'y1', 'object')) obj2 = o.object(p.identifier('x', 'y2', 'object')) obj3 = o.object(p.identifier('x', 'y3', 'object')) prem = p.premis(child_elements=[obj1, obj2, obj3]) assert o.object_count(prem) == 3
def test_agent_count(): """Test agent_count""" agent1 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi', 'tyyppi1') agent2 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi', 'tyyppi2') agent3 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi', 'tyyppi1') premisroot = p.premis(child_elements=[agent1, agent2, agent3]) assert a.agent_count(premisroot) == 3
def test_events_with_outcome(): """Test events_with_outcome""" outcome1 = e.outcome('success') outcome2 = e.outcome('failure') outcome3 = e.outcome('success') event1 = e.event(p.identifier('local', 'id1', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili1', child_elements=[outcome1]) event2 = e.event(p.identifier('local', 'id2', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili2', child_elements=[outcome2]) event3 = e.event(p.identifier('local', 'id3', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili3', child_elements=[outcome3]) i = 0 for success in e.events_with_outcome([event1, event2, event3], 'success'): assert e.parse_outcome(success) == 'success' i = i + 1 assert i == 2
def event(event_id, event_type, event_date_time, event_detail, child_elements=None, linking_objects=None, linking_agents=None): """Create PREMIS event element. :event_id: PREMIS event identifier :event_type: Type for the event :event_date_time: Event time :event_detail: Event details :child_elements: Any child elements appended to the event (default=None) :linking_objects: Any linking objects appended to the event (default=None) Returns the following ElementTree structure:: <premis:event> <premis:eventType>digital signature validation</premis:eventType> <premis:eventDateTime>2015-02-03T13:04:25</premis:eventDateTime> <premis:eventDetail> Submission information package digital signature validation </premis:eventDetail> {{ child elements }} </premis:event> """ _event = _element('event') _event.append(event_id) _event_type = _subelement(_event, 'eventType') _event_type.text = decode_utf8(event_type) _event_date_time = _subelement(_event, 'eventDateTime') _event_date_time.text = decode_utf8(event_date_time) _event_detail = _subelement(_event, 'eventDetail') _event_detail.text = decode_utf8(event_detail) if child_elements: for elem in child_elements: _event.append(elem) if linking_agents: for _agent in linking_agents: linking_agent = identifier( _agent.findtext('.//' + premis_ns('agentIdentifierType')), _agent.findtext('.//' + premis_ns('agentIdentifierValue')), 'linkingAgent') _event.append(linking_agent) if linking_objects: for _object in linking_objects: linking_object = identifier( _object.findtext('.//' + premis_ns('objectIdentifierType')), _object.findtext('.//' + premis_ns('objectIdentifierValue')), 'linkingObject') _event.append(linking_object) return _event
def test_find_object_by_id(): """Test find_object_by_id""" object1 = o.object(p.identifier('local', 'id1', 'object')) object2 = o.object(p.identifier('local', 'id2', 'object')) object3 = o.object(p.identifier('local', 'id3', 'object')) xml = p.premis(child_elements=[object1, object2, object3]) obj = o.find_object_by_id(xml, 'id2') assert p.parse_identifier_type_value(p.parse_identifier(obj)) == ('local', 'id2')
def test_find_agent_by_id(): """Test find_agent_by_id""" agent1 = a.agent(p.identifier('local', 'id1', 'agent'), 'name', 'type1') agent2 = a.agent(p.identifier('local', 'id2', 'agent'), 'name', 'type2') agent3 = a.agent(p.identifier('local', 'id3', 'agent'), 'name', 'type3') xml = p.premis(child_elements=[agent1, agent2, agent3]) agent = a.find_agent_by_id(xml, 'id2') assert p.parse_identifier_type_value(p.parse_identifier(agent, 'agent'), 'agent') == ('local', 'id2')
def test_contains_object(): """Test contains_object""" obj1 = o.object(p.identifier('x', 'y1', 'object')) obj2 = o.object(p.identifier('x', 'y2', 'object')) obj3 = o.object(p.identifier('x', 'y3', 'object')) prem1 = p.premis(child_elements=[obj1, obj2, obj3]) prem2 = p.premis(child_elements=[obj1, obj2]) assert o.contains_object(obj3, prem1) assert not o.contains_object(obj3, prem2)
def test_event_count(): """Test event_count""" event1 = e.event(p.identifier('local', 'id1', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili1') event2 = e.event(p.identifier('local', 'id2', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili2') event3 = e.event(p.identifier('local', 'id3', 'event'), 'tyyppi2', '2012-12-12T12:12:12', 'detaili2') xml = p.premis(child_elements=[event1, event2, event3]) assert e.event_count(xml) == 3
def test_iter_agents(): """Test iter_agents""" agent1 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi1', 'tyyppi') agent2 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi2', 'tyyppi') agent3 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi3', 'tyyppi') premisroot = p.premis(child_elements=[agent1, agent2, agent3]) i = 0 for _agent in a.iter_agents(premisroot): i = i + 1 assert a.parse_name(_agent) == 'nimi' + str(i) assert i == 3
def test_iter_elements(): """Test iter_elements""" obj1 = o.object(p.identifier('local', 'id01'), original_name='nimi1') obj2 = o.object(p.identifier('local', 'id02'), original_name='nimi2') obj3 = o.object(p.identifier('local', 'id03'), original_name='nimi3') xml = p.premis(child_elements=[obj1, obj2, obj3]) i = 0 for name in p.iter_elements(xml, 'originalName'): i = i + 1 assert name.text == 'nimi' + str(i) assert i == 3
def test_find_event_by_id(): """Test find_event_by_id""" event1 = e.event(p.identifier('local', 'id1', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili1') event2 = e.event(p.identifier('local', 'id2', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili2') event3 = e.event(p.identifier('local', 'id3', 'event'), 'tyyppi2', '2012-12-12T12:12:12', 'detaili2') xml = p.premis(child_elements=[event1, event2, event3]) event = e.find_event_by_id(xml, 'id2') assert p.parse_identifier_type_value(p.parse_identifier(event, 'event'), 'event') == ('local', 'id2')
def test_agents_with_type(): """Test agents_with_type""" agent1 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi1', 'tyyppi1') agent2 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi2', 'tyyppi2') agent3 = a.agent(p.identifier('a', 'b', 'agent'), 'nimi3', 'tyyppi1') agentlist = [] i = 1 for _agent in a.agents_with_type([agent1, agent2, agent3], 'tyyppi1'): assert _agent == ('tyyppi1', 'nimi' + str(i)) agentlist.append(_agent) i = i + 2 assert len(agentlist) == 2
def test_objects_with_type(): """Test objects_with_type""" obj1 = o.object(p.identifier('x1', 'y1', 'object')) obj2 = o.object(p.identifier('x1', 'y2', 'object')) obj3 = o.object(p.identifier('x2', 'y3', 'object')) filtered = o.objects_with_type([obj1, obj2, obj3], 'x1') i = 0 for filt_el in filtered: i = i + 1 (id_type, _) = p.parse_identifier_type_value( p.parse_identifier(filt_el, 'object'), 'object') assert id_type == 'x1' assert i == 2
def test_iter_objects(): """Test iter_objects""" obj1 = o.object(p.identifier('x', 'y1', 'object')) obj2 = o.object(p.identifier('x', 'y2', 'object')) obj3 = o.object(p.identifier('x', 'y3', 'object')) prem = p.premis(child_elements=[obj1, obj2, obj3]) iterator = o.iter_objects(prem) i = 0 for iter_elem in iterator: i = i + 1 (_, id_value) = p.parse_identifier_type_value( p.parse_identifier(iter_elem, 'object'), 'object') assert id_value == 'y' + six.text_type(i) assert i == 3
def test_iter_events(): """Test iter_events""" event1 = e.event(p.identifier('local', 'id1', 'event'), 'tyyppi1', '2012-12-12T12:12:12', 'detaili1') event2 = e.event(p.identifier('local', 'id2', 'event'), 'tyyppi2', '2012-12-12T12:12:12', 'detaili2') event3 = e.event(p.identifier('local', 'id3', 'event'), 'tyyppi3', '2012-12-12T12:12:12', 'detaili2') premisroot = p.premis(child_elements=[event1, event2, event3]) i = 0 for event in e.iter_events(premisroot): i = i + 1 assert e.parse_event_type(event) == 'tyyppi' + six.text_type(i) assert i == 3
def test_filter_objects(): """Test filter_objects""" obj1 = o.object(p.identifier('x', 'y1', 'object')) obj2 = o.object(p.identifier('x', 'y2', 'object')) obj3 = o.object(p.identifier('x', 'y3', 'object')) prem1 = p.premis(child_elements=[obj1, obj2, obj3]) prem2 = p.premis(child_elements=[obj1, obj3]) filtered = o.filter_objects(o.iter_objects(prem1), prem2) i = 0 for filt_el in filtered: i = i + 1 (_, id_value) = p.parse_identifier_type_value( p.parse_identifier(filt_el, 'object'), 'object') assert id_value == 'y2' assert i == 1
def test_event_with_type_and_detail(): """Test event_with_type_and_detail""" event1 = e.event(p.identifier('local', 'id1', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili1') event2 = e.event(p.identifier('local', 'id2', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili2') event3 = e.event(p.identifier('local', 'id3', 'event'), 'tyyppi2', '2012-12-12T12:12:12', 'detaili2') i = 0 for event in e.event_with_type_and_detail([event1, event2, event3], 'tyyppi', 'detaili2'): i = i + 1 assert e.parse_event_type(event) == 'tyyppi' assert e.parse_detail(event) == 'detaili2' assert i == 1
def test_parse_identifier_type_value(): """Test parse_identifier_type_value""" object_identifier = p.identifier('local', 'id01') object_related = p.identifier('local', 'id01', 'relatedObject') event_identifier = p.identifier('local', 'id01', 'event') (idtype, idval) = p.parse_identifier_type_value(object_identifier) assert idtype == 'local' assert idval == 'id01' (idtype, idval) = p.parse_identifier_type_value(object_related, 'relatedObject') assert idtype == 'local' assert idval == 'id01' (idtype, idval) = p.parse_identifier_type_value(event_identifier, 'event') assert idtype == 'local' assert idval == 'id01'
def test_parse_note(): """Test parse_note""" agent = a.agent(p.identifier('a', 'b', 'agent'), 'nimi', 'tyyppi', note='nootti') assert a.parse_note(agent) == 'nootti'
def test_object_order(): """Test that elements in premis:object are ordered as intended.""" fixity = o.fixity('xxx', 'yyy') oc = o.object_characteristics(child_elements=[fixity]) env = o.environment(child_elements=[]) obj1 = o.object(p.identifier('a', 'b'), original_name='c', child_elements=[env, oc]) xml1 = ('<premis:object xmlns:premis="info:lc/xmlns/premis-v2" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' 'xsi:type="premis:file"><premis:objectIdentifier>' '<premis:objectIdentifierType>a</premis:objectIdentifierType>' '<premis:objectIdentifierValue>b</premis:objectIdentifierValue>' '</premis:objectIdentifier><premis:objectCharacteristics ' 'xmlns:premis="info:lc/xmlns/premis-v2"><premis:compositionLevel>' '0</premis:compositionLevel><premis:fixity>' '<premis:messageDigestAlgorithm>yyy' '</premis:messageDigestAlgorithm>' '<premis:messageDigest>xxx</premis:messageDigest>' '</premis:fixity></premis:objectCharacteristics>' '<premis:originalName>c</premis:originalName>' '<premis:environment/></premis:object>') assert u.compare_trees(obj1, ET.fromstring(xml1)) assert obj1[0].tag == '{info:lc/xmlns/premis-v2}objectIdentifier' assert obj1[1].tag == '{info:lc/xmlns/premis-v2}objectCharacteristics' assert obj1[2].tag == '{info:lc/xmlns/premis-v2}originalName' assert obj1[3].tag == '{info:lc/xmlns/premis-v2}environment'
def test_parse_format_noversion(): """Test parse_format""" fd = o.format_designation('xxx') form = o.format(child_elements=[fd]) oc = o.object_characteristics(child_elements=[form]) obj = o.object(p.identifier('x', 'y', 'object'), child_elements=[oc]) assert o.parse_format(obj) == ('xxx', None)
def get_dependency_identifier(object_or_identifier): """Create new dependency indentifier from object containing objectIdentifier or return existing dependencyIdentifier. If both elements exists in given ``object_or_identifier`` return dependencyIdentifier generated from objectIdentifier""" if object_or_identifier.tag == premis_ns("objectIdentifier"): object_identifier = object_or_identifier else: object_identifier = object_or_identifier.find( premis_ns('objectIdentifierType')) if object_identifier is not None: (identifier_type, identifier_value) = parse_identifier_type_value(object_or_identifier) return identifier(identifier_type, identifier_value, 'dependency') if object_or_identifier.tag == premis_ns("dependencyIdentifier"): dependency_identifier = object_or_identifier else: dependency_identifier = object_or_identifier.find( premis_ns('dependencyIdentifier')) if dependency_identifier is not None: return dependency_identifier raise ValueError("Argument object_or_indentifier must contain" "valid objectIdentifier or " "dependencyIdentifier: {}".format(object_or_identifier))
def test_parse_relationship(): """Test parse_relationship""" rel = o.relationship('a', 'b', p.identifier('c', 'd')) obj = o.object(p.identifier('x', 'y', 'object'), child_elements=[rel]) rel2 = o.parse_relationship(obj) xml = ('<premis:relationship xmlns:premis="info:lc/xmlns/premis-v2" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' '<premis:relationshipType>a</premis:relationshipType>' '<premis:relationshipSubType>b</premis:relationshipSubType>' '<premis:relatedObjectIdentification>' '<premis:relatedObjectIdentifierType>' 'c</premis:relatedObjectIdentifierType>' '<premis:relatedObjectIdentifierValue>' 'd</premis:relatedObjectIdentifierValue>' '</premis:relatedObjectIdentification></premis:relationship>') assert u.compare_trees(rel2, ET.fromstring(xml))
def test_parse_format_registry(): """Test parse_format""" fd = o.format_registry('xxx', 'yyy') form = o.format(child_elements=[fd]) oc = o.object_characteristics(child_elements=[form]) obj = o.object(p.identifier('x', 'y', 'object'), child_elements=[oc]) assert o.parse_format_registry(obj) == ('xxx', 'yyy')
def test_identifier(): """Test identifier""" object_identifier = p.identifier('local', 'id01') object_related = p.identifier('local', 'id01', 'relatedObject') event_identifier = p.identifier('local', 'id01', 'event') linking_agent_identifier = p.identifier('local', 'id01', prefix='linkingAgent', role='tester') object_id = ( '<premis:objectIdentifier xmlns:premis="info:lc/xmlns/premis-v2">' '<premis:objectIdentifierType>local</premis:objectIdentifierType>' '<premis:objectIdentifierValue>id01</premis:objectIdentifierValue>' '</premis:objectIdentifier>') xml_related = ('<premis:relatedObjectIdentification' ' xmlns:premis="info:lc/xmlns/premis-v2">' '<premis:relatedObjectIdentifierType>' 'local' '</premis:relatedObjectIdentifierType>' '<premis:relatedObjectIdentifierValue>' 'id01' '</premis:relatedObjectIdentifierValue>' '</premis:relatedObjectIdentification>') xml_event = ( '<premis:eventIdentifier xmlns:premis="info:lc/xmlns/premis-v2">' '<premis:eventIdentifierType>local</premis:eventIdentifierType>' '<premis:eventIdentifierValue>id01</premis:eventIdentifierValue>' '</premis:eventIdentifier>') xml_linking_agent = ( '<premis:linkingAgentIdentifier' ' xmlns:premis="info:lc/xmlns/premis-v2">' '<premis:linkingAgentIdentifierType>' 'local' '</premis:linkingAgentIdentifierType>' '<premis:linkingAgentIdentifierValue>' 'id01' '</premis:linkingAgentIdentifierValue>' '<premis:linkingAgentRole>tester</premis:linkingAgentRole>' '</premis:linkingAgentIdentifier>') assert u.compare_trees(object_identifier, ET.fromstring(object_id)) assert u.compare_trees(object_related, ET.fromstring(xml_related)) assert u.compare_trees(event_identifier, ET.fromstring(xml_event)) assert u.compare_trees(linking_agent_identifier, ET.fromstring(xml_linking_agent))
def test_agent(): """Test agent""" agent_id = p.identifier('a', 'b', 'agent') agent = a.agent(agent_id, 'c', 'd') xml = ('<premis:agent xmlns:premis="info:lc/xmlns/premis-v2">' '<premis:agentIdentifier><premis:agentIdentifierType>' 'a</premis:agentIdentifierType><premis:agentIdentifierValue>' 'b</premis:agentIdentifierValue></premis:agentIdentifier>' '<premis:agentName>c</premis:agentName>' '<premis:agentType>d</premis:agentType></premis:agent>') assert u.compare_trees(agent, ET.fromstring(xml))
def test_object(): """Test premis_premis""" obj1 = o.object(p.identifier('a', 'b'), original_name='c') obj2 = o.object(p.identifier('a', 'b'), representation=True) xml1 = ('<premis:object xmlns:premis="info:lc/xmlns/premis-v2" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' 'xsi:type="premis:file"><premis:objectIdentifier>' '<premis:objectIdentifierType>a</premis:objectIdentifierType>' '<premis:objectIdentifierValue>b</premis:objectIdentifierValue>' '</premis:objectIdentifier><premis:originalName>c' '</premis:originalName></premis:object>') xml2 = ('<premis:object xmlns:premis="info:lc/xmlns/premis-v2" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' 'xsi:type="premis:representation"><premis:objectIdentifier>' '<premis:objectIdentifierType>a</premis:objectIdentifierType>' '<premis:objectIdentifierValue>b</premis:objectIdentifierValue>' '</premis:objectIdentifier></premis:object>') assert u.compare_trees(obj1, ET.fromstring(xml1)) assert u.compare_trees(obj2, ET.fromstring(xml2))
def test_parse_identifier(): """Test parse_identifier""" obj = o.object(p.identifier('local', 'id01')) object_id = ( '<premis:objectIdentifier xmlns:premis="info:lc/xmlns/premis-v2" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' '<premis:objectIdentifierType>local</premis:objectIdentifierType>' '<premis:objectIdentifierValue>id01</premis:objectIdentifierValue>' '</premis:objectIdentifier>') assert u.compare_trees(p.parse_identifier(obj), ET.fromstring(object_id))
def test_parse_outcome_detail_note(): """Test parse_outcome_detail_note""" extensions = [ET.fromstring('<xxx />')] outcome = e.outcome('success', detail_note='xxx', detail_extension=extensions) event = e.event(p.identifier('a', 'b', 'event'), 'tyyppi', '2012-12-12T12:12:12', 'detaili', child_elements=[outcome]) assert e.parse_outcome_detail_note(event) == 'xxx'