def test_sections_count():
    """Test sections_count by asserting that the number of sections
    counted matches the testdata input.
    """
    file1 = f.definition_elems('flatFile', 'file1', reference='def1')
    file2 = f.definition_elems('flatFile', 'file2', reference='def2')
    file3 = f.definition_elems('flatFile', 'file3', reference='def3')
    xml = a.addml(child_elements=[file1, file2, file3])
    assert a.sections_count(xml, 'flatFile') == 3
예제 #2
0
def test_parse_charset():
    """Test the parse_charset function by asserting the output value
    from addml data.
    """
    charset = f.addml_basic_elem('charset', 'UTF-8')
    fftype = f.wrapper_elems('flatFileTypes', child_elements=[charset])
    xml = a.addml(child_elements=[fftype])

    assert f.parse_charset(xml) == 'UTF-8'
def test_addml():
    """Test ADDML root generation"""
    tree = ET.tostring(a.addml())
    xml = """<addml:addml
        xsi:schemaLocation="http://www.arkivverket.no/standarder/addml http://schema.arkivverket.no/ADDML/latest/addml.xsd"
        xmlns:addml = "http://www.arkivverket.no/standarder/addml"
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
        ><addml:dataset/></addml:addml>"""
    tree_xml = ET.tostring(ET.fromstring(xml))
    assert tree == tree_xml
def test_find_section_by_name_fail():
    """Test that the find_section_by_name function returns None
    if no section matches the name.
    """
    file1 = f.definition_elems('flatFile', 'file1', reference='def1')
    file2 = f.definition_elems('flatFile', 'file2', reference='def2')
    file3 = f.definition_elems('flatFile', 'file3', reference='def3')
    xml = a.addml(child_elements=[file1, file2, file3])

    ffile = a.find_section_by_name(xml, 'flatFile', 'filer4')
    assert ffile is None
예제 #5
0
def test_flatfiledefinition_count():
    """Test flatfiledefinition_count by asserting that the number of
    counted sections matches the testdata.
    """
    file1 = f.definition_elems('flatFile', 'file1', reference='def1')
    file2 = f.definition_elems('flatFile', 'file2', reference='def2')
    file3 = f.definition_elems('flatFile', 'file3', reference='def3')
    def1 = f.definition_elems('flatFileDefinition', 'def1', reference='type1')
    def2 = f.definition_elems('flatFileDefinition', 'def2', reference='type2')
    xml = a.addml(child_elements=[file1, file2, file3, def1, def2])
    assert f.flatfiledefinition_count(xml) == 2
def test_find_section_by_name():
    """Test find_section_by_name by asserting that the correct
    section and correct attribute value is returned from the testdata.
    """
    file1 = f.definition_elems('flatFile', 'file1', reference='def1')
    file2 = f.definition_elems('flatFile', 'file2', reference='def2')
    file3 = f.definition_elems('flatFile', 'file3', reference='def3')
    xml = a.addml(child_elements=[file1, file2, file3])

    ffile = a.find_section_by_name(xml, 'flatFile', 'file2')
    assert ffile.tag == '{http://www.arkivverket.no/standarder/addml}flatFile'
    assert ffile.get('definitionReference') == 'def2'
def test_iter_sections():
    """Test iter_sections by asserting that all relevant sections
    are iterated through.
    """
    file1 = f.definition_elems('flatFile', 'file1', reference='def1')
    file2 = f.definition_elems('flatFile', 'file2', reference='def2')
    file3 = f.definition_elems('flatFile', 'file3', reference='def3')
    xml = a.addml(child_elements=[file1, file2, file3])
    i = 0
    for iter_elem in a.iter_sections(xml, 'flatFile'):
        i = i + 1
        assert iter_elem.get('name') == 'file' + str(i)
    assert i == 3
예제 #8
0
def test_iter_flatfiledefinitions():
    """Test iter_flatfiledefinitions by asserting that only the
    relevant sections are iterated through from the testdata.
    """
    file1 = f.definition_elems('flatFile', 'file1', reference='def1')
    file2 = f.definition_elems('flatFile', 'file2', reference='def2')
    file3 = f.definition_elems('flatFile', 'file3', reference='def3')
    def1 = f.definition_elems('flatFileDefinition', 'def1', reference='type1')
    def2 = f.definition_elems('flatFileDefinition', 'def2', reference='type2')
    xml = a.addml(child_elements=[file1, file2, file3, def1, def2])
    i = 0
    for iter_elem in f.iter_flatfiledefinitions(xml):
        i = i + 1
        assert iter_elem.get('name') == 'def' + six.text_type(i)
    assert i == 2
def create_new_addml(root, flatfiledefinition):
    """Creates new addml metadata for each flatFileDefinition in the
    original addml metadata. Only the relevant sections from flatFiles,
    flatFileTypes and recordTypes are included in the new addml metadata
    as well as the fieldTypes section. The sections relevance is derived
    from reading the corresponding @typeReference and @name attributes
    from each section starting from the <flatFileDefinition> element.
    """
    flatfiles_list = []

    namereference = parse_name(flatfiledefinition)
    for flatfile in iter_flatfiles(root):
        if parse_reference(flatfile) == namereference:
            flatfiles_list.append(copy.deepcopy(flatfile))

    typereference = parse_reference(flatfiledefinition)
    flatfiledefinitions = wrapper_elems(
        'flatFileDefinitions',
        child_elements=[copy.deepcopy(flatfiledefinition)])
    flatfiles_list.append(flatfiledefinitions)

    structuretypes_list = []

    flatfiletype = find_section_by_name(root, 'flatFileType', typereference)
    flatfiletypes = wrapper_elems('flatFileTypes',
                                  child_elements=[copy.deepcopy(flatfiletype)])
    structuretypes_list.append(flatfiletypes)

    for recorddefinition in iter_sections(flatfiledefinitions,
                                          'recordDefinition'):
        if parse_reference(recorddefinition):
            recordtype = find_section_by_name(
                root, 'recordType', parse_reference(recorddefinition))
            recordtypes = wrapper_elems(
                'recordTypes', child_elements=[copy.deepcopy(recordtype)])
            structuretypes_list.append(recordtypes)

    for fieldtypes in iter_sections(root, 'fieldTypes'):
        structuretypes_list.append(copy.deepcopy(fieldtypes))

    structuretypes = wrapper_elems('structureTypes',
                                   child_elements=structuretypes_list)
    flatfiles_list.append(structuretypes)
    flatfiles = wrapper_elems('flatFiles', child_elements=flatfiles_list)

    addmldata = addml(child_elements=[flatfiles])

    return addmldata