def make_colorsample(colorspace, sample, addsample, extrasample):
    """ Modifies MIX metadata with given colorspace and sample info.

    :colorspace: Colorspace to be tested
    :sample: Number of samples required by the colorspace
    :addsample: Offset for the required sample
    :extraSample: Boolean, true if extra sample is defined.
    :returns: Modified MIX XML tree
    """
    # Prepare the MIX data for the colorspace and sample test
    (mix, root) = parse_xml_file('mix_valid_minimal.xml')
    make_colorprofile(root)
    elem_handler = find_element(root, 'ImageColorEncoding', 'mix')
    xml = '''<mix:Colormap xmlns:mix="%(mix)s">
             <mix:colormapReference/></mix:Colormap>''' % NAMESPACES
    elem_handler.append(ET.XML(xml))
    elem_handler = find_element(root, 'colorSpace', 'mix')
    elem_handler.text = colorspace
    elem_handler = find_element(root, 'samplesPerPixel', 'mix')
    elem_handler.text = str(sample + addsample)
    elem_handler = find_element(root, 'ImageColorEncoding', 'mix')
    elem_extra = find_element(elem_handler, 'extraSamples', 'mix')
    if elem_extra is not None:
        elem_handler.remove(elem_extra)
    if extrasample:
        extra = '{%(mix)s}extraSamples' % NAMESPACES
        elem_handler.insert(2, ET.Element(extra))

    return mix
def test_recordstatus(schematron_fx):
    """Test that RECORDSTATUS can always be 'submission',
    and also 'update' and 'dissemination' for CSC.
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    hdr = find_element(root, 'metsHdr', 'mets')

    set_attribute(hdr, 'RECORDSTATUS', 'mets', 'update')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    set_attribute(hdr, 'RECORDSTATUS', 'mets', 'dissemination')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    agent = find_element(hdr, 'name', 'mets')
    agent.text = 'CSC - IT Center for Science Ltd.'

    set_attribute(hdr, 'RECORDSTATUS', 'mets', 'submission')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    set_attribute(hdr, 'RECORDSTATUS', 'mets', 'update')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    set_attribute(hdr, 'RECORDSTATUS', 'mets', 'dissemination')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0
def test_palette_profile(schematron_fx):
    """Palette color profile test. Palettte requires Colormap.

    :schematron_fx: Schematron compile fixture
    """
    (mix, root) = parse_xml_file('mix_valid_minimal.xml')
    elem_handler = find_element(root, 'colorSpace', 'mix')
    elem_handler.text = 'PaletteColor'
    elem_handler = find_element(root, 'samplesPerPixel', 'mix')
    elem_handler.text = '1'

    # Colormap missing
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mix)
    assert svrl.count(SVRL_FAILED) == 1

    # Colormap exists, but it requires a reference
    elem_handler = find_element(root, 'ImageColorEncoding', 'mix')
    elem_handler = set_element(elem_handler, 'Colormap', 'mix')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mix)
    assert svrl.count(SVRL_FAILED) == 1

    # Reference added
    set_element(elem_handler, 'colormapReference', 'mix')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mix)
    assert svrl.count(SVRL_FAILED) == 0
def test_mdtype_items_mdwrap(schematron_fx, context, mdtype, othermdtype,
                             mdtypeversion):
    """Test that all valid metadata types and their versions work properly.

    :schematron_fx: Schematron compile fixture
    :context: METS section to be tested
    :mdtype: MDTYPE attribute value
    :othermdtype: OTHERMDTYPE attribute valur
    :mdtypeversion: MDTYPEVERSION attribute value
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    fix_version_17(root)
    elem_handler = find_element(root, context, 'mets')
    elem_handler = find_element(elem_handler, 'mdWrap', 'mets')
    set_attribute(elem_handler, 'MDTYPE', 'mets', mdtype)
    if othermdtype is not None:
        set_attribute(elem_handler, 'OTHERMDTYPE', 'mets', othermdtype)

    # Test that unknown OTHERMDTYPE gives error, if MDTYPE is not 'OTHER'
    set_attribute(elem_handler, 'MDTYPEVERSION', 'mets', mdtypeversion[0])
    set_attribute(elem_handler, 'OTHERMDTYPE', 'mets', 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    if mdtype == 'OTHER':
        assert svrl.count(SVRL_FAILED) == 0
    else:
        assert svrl.count(SVRL_FAILED) == 1
def test_mix_optional_element_rules(schematron_fx, elementkey, subelements,
                                    condition, newfired):
    """Test for checks to elements that become mandatory when optional element
    has been used and optionally some condition in true.

    :schematron_fx: Schematron compile fixture
    :elementkey: Optional element
    :subelements: Subelement
    :condition: Element and value that is needed to make subelements mandatory
                If None, the subelement becomes mandatory with elementkey
    :newfired: Such errors that are expected to be generated, which are
               irrelevant in this test
    """
    (mix, root) = parse_xml_file('mix_valid_minimal.xml')
    found = root
    for iter_elem in elementkey:
        elem_handler = find_element(root, iter_elem, 'mix')
        if elem_handler is None:
            found = set_element(found, iter_elem, 'mix')
        else:
            found = elem_handler
    elem_handler = found
    if condition is not None:
        elem_condition = set_element(elem_handler, condition[0], 'mix')
        if condition[1] is not None:
            elem_condition.text = condition[1]
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mix)
    assert svrl.count(SVRL_FAILED) == len(subelements) + newfired[0]

    for subelement in subelements:
        set_element(elem_handler, subelement, 'mix')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mix)
    assert svrl.count(SVRL_FAILED) == newfired[1]
def test_dependent_attributes_structmap(schematron_fx, nspace):
    """Test attribute dependencies with another attribute. Some attributes
    become mandatory or disallowed, if another attribute is used.

    :schematron_fx: Schematron compile fixture
    :nspace: Namespace key of the attributes
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, 'structMap', 'mets')
    if nspace == 'fi':
        fix_version_17(root)

    # Both attributes
    set_attribute(elem_handler, 'PID', nspace, 'xxx')
    set_attribute(elem_handler, 'PIDTYPE', nspace, 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # Just the second attribute
    del_attribute(elem_handler, 'PID', nspace)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    # No attributes
    del_attribute(elem_handler, 'PIDTYPE', nspace)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # Just the first attribute
    set_attribute(elem_handler, 'PID', nspace, 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_fileid(schematron_fx):
    """Test that FILEID is allowed in fptr or area, but disallowed,
    if missing.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # FILEID in fptr and area
    elem_handler = find_element(root, 'fptr', 'mets')
    elem_handler_area = set_element(elem_handler, 'area', 'mets')
    set_attribute(elem_handler_area, 'FILEID', 'mets',
                  get_attribute(elem_handler, 'FILEID', 'mets'))
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # FILEID only in area
    del_attribute(elem_handler, 'FILEID', 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # Also area missing
    del_element(elem_handler, 'area', 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_mdtype_items_digiprovmd(schematron_fx, mdtype, othermdtype,
                                 mdtypeversion):
    """Test that all valid metadata types and their versions work properly.

    :schematron_fx: Schematron compile fixture
    :mdtype: MDTYPE attribute value
    :othermdtype: OTHERMDTYPE attribute valur
    :mdtypeversion: MDTYPEVERSION attribute value
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, 'digiprovMD', 'mets')
    elem_handler = find_element(elem_handler, 'mdWrap', 'mets')
    set_attribute(elem_handler, 'MDTYPE', 'mets', mdtype)
    if othermdtype is not None:
        set_attribute(elem_handler, 'OTHERMDTYPE', 'mets', othermdtype)

    # Test that all MDTYPEVERSIONs work with all specifications
    for specversion in [
            '1.5.0', '1.6.0', '1.7.0', '1.7.1', '1.7.2', '1.7.3', '1.7.4'
    ]:
        if specversion in ['1.7.0', '1.7.1', '1.7.2', '1.7.3', '1.7.4']:
            fix_version_17(root)
        else:
            set_attribute(root, 'CATALOG', 'fikdk', specversion)
            set_attribute(root, 'SPECIFICATION', 'fikdk', specversion)
        for version in mdtypeversion:
            set_attribute(elem_handler, 'MDTYPEVERSION', 'mets', version)
            svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
            assert svrl.count(SVRL_FAILED) == 0

    # Test unknown version
    fix_version_17(root)
    set_attribute(elem_handler, 'MDTYPEVERSION', 'mets', 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_objid_unique(schematron_fx):
    """Check that error is given, if OBJID is not unique with METS IDs,
    except for CONTENTID that can be identical to OBJID.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    objid = get_attribute(root, 'OBJID', 'mets')
    contentid = get_attribute(root, 'CONTENTID', 'fikdk')
    set_attribute(root, 'CONTENTID', 'fikdk', objid)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    set_attribute(root, 'CONTENTID', 'fikdk', contentid)
    elem_handler = find_element(root, 'dmdSec', 'mets')
    section_id = get_attribute(elem_handler, 'ID', 'mets')
    set_attribute(root, 'OBJID', 'mets', section_id)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    set_attribute(root, 'OBJID', 'mets', objid)
    fix_version_17(root)
    set_attribute(root, 'CONTRACTID', 'fi', objid)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_value_items_root(schematron_fx, attribute, nspace, fixed):
    """Test that a value is required in a certain attributes.

    :schematron_fx: Schematron compile fixture
    :attribute: Attribute, where the value is required
    :nspace: Namespace key of the attribute
    :fixed: Boolean, if the required value is fixed
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    if nspace == 'fi':
        fix_version_17(root)

    # Use arbitrary value
    elem_handler = find_element(root, 'mets', 'mets')
    set_attribute(elem_handler, attribute, nspace, 'aaa')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    if fixed:
        assert svrl.count(SVRL_FAILED) == 1
    else:
        assert svrl.count(SVRL_FAILED) == 0

    # Use empty value
    set_attribute(elem_handler, attribute, nspace, '')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
예제 #11
0
def test_mandatory_elements(
        schematron_fx, testset, namespace, filename, schfile):
    """Test mandatory element checks for metadata. Here we practically give a
    minimal XML, which is valid in a certain namespace. Then we start to remove
    elements from it one by one (until nothing remains). With every loop the
    number of fired schematron rules and failures are different. Since the
    next loop is dependent on the previous loop, the whole looping is done
    inside one test run.

    :schematron_fx: Schematron compile fixture
    :testset: A list of tuples containing the element to be removed, number of
              expected schematron checks, and number of expected failures.
    :namespace: Namespace key, where the test is done.
    :filename: File name containing the valid minimal METS
    :schfile: Schematron file to use in the test
    """
    (mets, root) = parse_xml_file(filename)
    for testcase in testset:
        (leafelement, fired, failed) = testcase
        if leafelement is not None:
            elem_handler = find_element(root, leafelement, namespace)
            elem_handler.clear()
        svrl = schematron_fx(schematronfile=schfile, xmltree=mets,
                             params=False)
        assert svrl.count(SVRL_FIRED) == fired
        assert svrl.count(SVRL_FAILED) == failed

        svrl = schematron_fx(schematronfile=schfile, xmltree=mets)
        assert svrl.count(SVRL_FAILED) == failed
예제 #12
0
def test_mdtype_items_rightsmd(schematron_fx):
    """Test that all valid metadata types and their versions work properly.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, 'rightsMD', 'mets')
    elem_handler = find_element(elem_handler, 'mdWrap', 'mets')
    set_attribute(elem_handler, 'MDTYPE', 'mets', 'PREMIS:RIGHTS')

    # Test that all MDTYPEVERSIONs work with all specifications
    for specversion in [
            '1.5.0', '1.6.0', '1.7.0', '1.7.1', '1.7.2', '1.7.3', '1.7.4'
    ]:
        if specversion in ['1.7.0', '1.7.1', '1.7.3', '1.7.4']:
            fix_version_17(root)
        else:
            set_attribute(root, 'CATALOG', 'fikdk', specversion)
            set_attribute(root, 'SPECIFICATION', 'fikdk', specversion)
        for version in ['2.3', '2.2']:
            set_attribute(elem_handler, 'MDTYPEVERSION', 'mets', version)
            svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
            assert svrl.count(SVRL_FAILED) == 0

    # Test unknown version
    fix_version_17(root)
    set_attribute(elem_handler, 'MDTYPEVERSION', 'mets', 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_dependent_attributes_mdwrap(schematron_fx):
    """Test attribute dependencies with another attribute. Some attributes
    become mandatory or disallowed, if another attribute is used.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, 'mdWrap', 'mets')

    # Both attributes
    set_attribute(elem_handler, 'CHECKSUM', 'mets', 'xxx')
    set_attribute(elem_handler, 'CHECKSUMTYPE', 'mets', 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # Just the second attribute
    del_attribute(elem_handler, 'CHECKSUM', 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    # No attributes
    del_attribute(elem_handler, 'CHECKSUMTYPE', 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # Just the first attribute
    set_attribute(elem_handler, 'CHECKSUM', 'mets', 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_othermdtype(schematron_fx, context):
    """Test that OTHERMDTYPE and the version are mandatory, but can be anything
    in all METS sections, if MDTYPE is OTHER.

    :schematron_fx: Schematron compile fixture
    :context: METS section to be tested
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # OTHERMDTYPE and version can be anything
    elem_handler = find_element(root, context, 'mets')
    elem_handler = find_element(elem_handler, 'mdWrap', 'mets')
    set_attribute(elem_handler, 'MDTYPE', 'mets', 'OTHER')
    set_attribute(elem_handler, 'OTHERMDTYPE', 'mets', 'xxx')
    set_attribute(elem_handler, 'MDTYPEVERSION', 'mets', 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # Version is mandatory
    del_attribute(elem_handler, 'MDTYPEVERSION', 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    # OTHERMDTYPE is missing
    del_attribute(elem_handler, 'OTHERMDTYPE', 'mets')
    set_attribute(elem_handler, 'MDTYPEVERSION', 'mets', 'xxx')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 2
예제 #15
0
def test_missing_links_dmdsec(schematron_fx):
    """Test the case where linking missing from ADMID, DMDID and FILEID to
    corresponding METS sections.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, 'div', 'mets')
    refs = get_attribute(elem_handler, 'DMDID', 'mets').split()
    set_attribute(elem_handler, 'DMDID', 'mets', '')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == len(refs)
def test_disallowed_items_sourcemd(schematron_fx):
    """Test if use of disallowed atrtibute or element causes error.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # Set disallowed attribute/element
    elem_handler = find_element(root, 'sourceMD', 'mets')
    set_element(elem_handler, 'mdRef', 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_disallowed_items_root(schematron_fx, disallowed):
    """Test if use of disallowed atrtibute or element causes error.

    :schematron_fx: Schematron compile fixture
    :disallowed: Disallowed element or attribute, use '@' for attributes.
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # Set disallowed attribute/element
    elem_handler = find_element(root, 'mets', 'mets')
    set_element(elem_handler, disallowed, 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_missing_ids_structmap(schematron_fx, context):
    """Test the case where sections are missing for ADMID, DMDID and FILEID
    links.

    :schematron_fx: Schematron compile fixture
    :context: Section to be removed.
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, context, 'mets')
    # We actually just remove the id
    set_attribute(elem_handler, 'ID', 'mets', '')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_mandatory_items_mdref(schematron_fx, mandatory, nspace):
    """Test mandatory attributes of mdRef

    :schematron_fx: Schematron compile fixture
    :mandatory: Mandatory attribute
    :nspace: Namespace key of the mandatory attribute
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, 'mdRef', 'mets')

    # Remove mandatory attribute
    del_attribute(elem_handler, mandatory, nspace)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_valid_complete_sourcemd(schematron_fx):
    """Test valid METS, where all mandatory and optional METS elements and
    attributes have been used at least once.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0

    # Use new specification
    fix_version_17(root)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0
def test_arbitrary_attributes_structmap(schematron_fx, context):
    """Test that arbitrary attributes are forbidden in METS anyAttribute
       sections.
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, context, 'mets')
    for spec in [None, '1.7.0']:
        if spec == '1.7.0':
            fix_version_17(root)
        for ns in ['fi', 'fikdk', 'dc']:
            set_attribute(elem_handler, 'xxx', ns, 'xxx')
            svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
            assert svrl.count(SVRL_FAILED) == 1
            del_attribute(elem_handler, 'xxx', ns)
def test_mandatory_agents(schematron_fx):
    """Test that mandatory agents work.
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, 'agent', 'mets')

    set_attribute(elem_handler, 'ROLE', 'mets', 'CREATOR')
    set_attribute(elem_handler, 'TYPE', 'mets', 'OTHER')
    set_attribute(elem_handler, 'OTHERTYPE', 'mets', 'SOFTWARE')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    fix_version_17(root)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0
def test_mandatory_items_structmap(schematron_fx, mandatory, nspace, context):
    """Test mandatory attributes

    :schematron_fx: Schematron compile fixture
    :mandatory: Mandatory attribute
    :nspace: Namespace key of the mandatory attribute
    :context: Element, where the attribute exists
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    elem_handler = find_element(root, context, 'mets')

    # Remove mandatory attribute
    del_attribute(elem_handler, mandatory, nspace)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_native_stream(schematron_fx):
    """
    Test a case where a video container listed in specifications contains
    an unsupported stream, and fi-dpres-no-file-format-validation is
    marked. In this case, streams are not required in METS.
    """
    (mets, root) = parse_xml_file("mets_video_container.xml")
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    elem_handler = find_element(root, "file", "mets")
    set_attribute(elem_handler, "USE", "mets",
                  "fi-dpres-no-file-format-validation")
    del_element(elem_handler, "stream", "mets")
    del_element(elem_handler, "stream", "mets")
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0
def test_value_items_metshdr(schematron_fx):
    """Test that a value is required in a certain attributes.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # Use arbitrary value
    elem_handler = find_element(root, 'metsHdr', 'mets')
    set_attribute(elem_handler, 'RECORDSTATUS', 'mets', 'aaa')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    # Use empty value
    set_attribute(elem_handler, 'RECORDSTATUS', 'mets', '')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_disallowed_items_structmap(schematron_fx, disallowed, context):
    """Test if use of disallowed atrtibute or element causes error.

    :schematron_fx: Schematron compile fixture
    :disallowed: Disallowed element or attribute, use '@' for attributes.
    :context: Context element, where the attribute or element exists
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # Set disallowed attribute/element
    elem_handler = find_element(root, context, 'mets')
    if disallowed[0] == '@':
        set_attribute(elem_handler, disallowed[1:], 'mets', 'default')
    else:
        set_element(elem_handler, disallowed, 'mets')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_icc_profile(schematron_fx):
    """ICC profile test. Color profile is required with ICC.

    :schematron_fx: Schematron compile fixture
    """
    (mix, root) = parse_xml_file('mix_valid_minimal.xml')
    elem_handler = find_element(root, 'colorSpace', 'mix')

    # ColorProfile missing
    for color in ['ICCLab', 'ICCBased']:
        elem_handler.text = color
        svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mix)
        assert svrl.count(SVRL_FAILED) == 1

    # ColorProfile added
    make_colorprofile(root)
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mix)
    assert svrl.count(SVRL_FAILED) == 0
def test_value_items_structmap(schematron_fx, attribute, nspace):
    """Test that a value is required in a certain attributes.

    :schematron_fx: Schematron compile fixture
    :attribute: Attribute, where the value is required
    :nspace: Namespace key of the attribute
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # Use arbitrary value
    elem_handler = find_element(root, 'mptr', 'mets')
    set_attribute(elem_handler, attribute, nspace, 'aaa')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1

    # Use empty value
    set_attribute(elem_handler, attribute, nspace, '')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 1
def test_new_mets_attributes_root(schematron_fx, specification, failed):
    """Test that CONTENTID, and CONTRACTID are
    disallowed in old catalog versions.

    :schematron_fx: Schematron compile fixture
    :specification: Specification to test
    :failed: Number of failures
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')
    if specification == '1.7.4':
        fix_version_17(root)
        svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
        assert svrl.count(SVRL_FAILED) == failed
    else:
        set_attribute(root, 'CATALOG', 'fikdk', specification)
        set_attribute(root, 'SPECIFICATION', 'fikdk', specification)
        set_attribute(root, 'CONTRACTID', 'fikdk', specification)
        svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
        assert svrl.count(SVRL_FAILED) == failed
def test_catalogs(schematron_fx):
    """Test the Schema catalog version numbering in METS.

    :schematron_fx: Schematron compile fixture
    """
    (mets, root) = parse_xml_file('mets_valid_complete.xml')

    # Conflict between fi:CATALOG and fi:SPECIFICATION
    set_attribute(root, 'CATALOG', 'fikdk', '1.5.0')
    del_attribute(root, 'CONTENTID', 'fikdk')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)

    assert svrl.count(SVRL_FAILED) == 1
    assert svrl.count(SVRL_REPORT) == 1

    # Deprecated specification
    set_attribute(root, 'SPECIFICATION', 'fikdk', '1.5.0')
    svrl = schematron_fx(schematronfile=SCHFILE, xmltree=mets)
    assert svrl.count(SVRL_FAILED) == 0
    assert svrl.count(SVRL_REPORT) == 1