# Path: siptools/utils.py
# def decode_path(path, suffix=''):
#     """
#     Decode given path from URL encoding and remove given suffix.
# 
#     :path: Path to decode
#     :suffix: Suffix to remove
#     :returns: Decoded string without given suffix
#     """
#     if six.PY2:
#         path = unquote_plus(path.encode("utf-8")).decode("utf-8")
#     else:
#         path = unquote_plus(path)
# 
#     if path.endswith(suffix):
#         path = path.replace(suffix, '', 1)
#     return path
# 
# def read_md_references(workspace, ref_file):
#     """Read all the MD IDs as a dictionary.
# 
#     :workspace: path to workspace directory
#     :ref_file: Metadata reference file
#     :returns: A dict of references or None if reference file doesn't
#               exist
#     """
#     reference_file = os.path.join(workspace, ref_file)
# 
#     if os.path.isfile(reference_file):
#         references = {}
#         with open(reference_file) as in_file:
#             for line in in_file:
#                 references.update(json.loads(line))
#         return references
#     return None


import os
import pytest
import lxml.etree as ET
import siptools.scripts.create_addml as create_addml
from siptools.utils import decode_path, read_md_references
    Test that ``create_addml`` creates addml files and
    md-reference file without unnecessary duplication.
    """
    # Common expectations
    exp_csvs = [
        ['tests/data/simple_csv.csv', 'tests/data/simple_csv_2.csv'],
        ['tests/data/csvfile.csv']
    ]
    exp_flatfiles_child_count = [4, 3]

    _create_addml(testpath, isheader)

    # Check that md-reference and the ADDML-amd files with correct content
    # are created
    assert os.path.isfile(
        os.path.join(testpath, 'create-addml-md-references.jsonl')
    )

    for amd_file_index, exp_amd_file in enumerate(exp_amd_files):
        amd_file = os.path.join(testpath, exp_amd_file)
        assert os.path.isfile(amd_file)

        root = ET.parse(amd_file)
        flat_files = root.find(ADDML_NS + "flatFiles")

        # Verify the number of child elements in flatFiles
        assert len(flat_files) == exp_flatfiles_child_count[amd_file_index]

        # Verify that right CSV files are in flatFiles
        for index, exp_csv in enumerate(exp_csvs[amd_file_index]):

# Path: siptools/utils.py
# def decode_path(path, suffix=''):
#     """
#     Decode given path from URL encoding and remove given suffix.
# 
#     :path: Path to decode
#     :suffix: Suffix to remove
#     :returns: Decoded string without given suffix
#     """
#     if six.PY2:
#         path = unquote_plus(path.encode("utf-8")).decode("utf-8")
#     else:
#         path = unquote_plus(path)
# 
#     if path.endswith(suffix):
#         path = path.replace(suffix, '', 1)
#     return path
# 
# def read_md_references(workspace, ref_file):
#     """Read all the MD IDs as a dictionary.
# 
#     :workspace: path to workspace directory
#     :ref_file: Metadata reference file
#     :returns: A dict of references or None if reference file doesn't
#               exist
#     """
#     reference_file = os.path.join(workspace, ref_file)
# 
#     if os.path.isfile(reference_file):
#         references = {}
#         with open(reference_file) as in_file:
#             for line in in_file:
#                 references.update(json.loads(line))
#         return references
#     return None


# Path: tests/scripts/test_create_addml.py
import os
import pytest
import lxml.etree as ET
import siptools.scripts.create_addml as create_addml
from siptools.utils import decode_path, read_md_references

    Test that ``create_addml`` creates addml files and
    md-reference file without unnecessary duplication.
    """
    # Common expectations
    exp_csvs = [
        ['tests/data/simple_csv.csv', 'tests/data/simple_csv_2.csv'],
        ['tests/data/csvfile.csv']
    ]
    exp_flatfiles_child_count = [4, 3]

    _create_addml(testpath, isheader)

    # Check that md-reference and the ADDML-amd files with correct content
    # are created
    assert os.path.isfile(
        os.path.join(testpath, 'create-addml-md-references.jsonl')
    )

    for amd_file_index, exp_amd_file in enumerate(exp_amd_files):
        amd_file = os.path.join(testpath, exp_amd_file)
        assert os.path.isfile(amd_file)

        root = ET.parse(amd_file)
        flat_files = root.find(ADDML_NS + "flatFiles")

        # Verify the number of child elements in flatFiles
        assert len(flat_files) == exp_flatfiles_child_count[amd_file_index]

        # Verify that right CSV files are in flatFiles
        for index, exp_csv in enumerate(exp_csvs[amd_file_index]):

            assert decode_path(flat_files[index].get('name')) == exp_csv