# 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

    # Check individual elements
    tags = ["fieldSeparatingChar", "charset", "recordSeparator", "quotingChar"]
    vals = [";", "UTF-8", "CR+LF", '"']

    for i, tag in enumerate(tags):
        val = addml_etree.find(ADDML_NS + tag).text
        assert val == vals[i]

    # Check that flatFile element is not added
    assert addml_etree.find(ADDML_NS + "flatFile") is None

    # Check that correct amount of headers is created
    assert len(addml_etree.find(ADDML_NS + "fieldDefinitions")) == 3


@pytest.mark.parametrize('is_header', [False, True])
def test_create_addml_with_flatfile(is_header):
    """Tests that ``create_addml`` adds flatFile element if optional
    parameter flatfile_name is provided.
    """

    addml_etree = create_addml.create_addml_metadata(
        csv_file=CSV_FILE, delimiter=DELIMITER, isheader=is_header,
        charset=CHARSET, record_separator=RECORDSEPARATOR,
        quoting_char=QUOTINGCHAR, flatfile_name="path/to/test"
    )

    # Check that URL encoded path is written to flatFile element
    flatfile = addml_etree.find(ADDML_NS + "flatFile")

# 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


    # Check individual elements
    tags = ["fieldSeparatingChar", "charset", "recordSeparator", "quotingChar"]
    vals = [";", "UTF-8", "CR+LF", '"']

    for i, tag in enumerate(tags):
        val = addml_etree.find(ADDML_NS + tag).text
        assert val == vals[i]

    # Check that flatFile element is not added
    assert addml_etree.find(ADDML_NS + "flatFile") is None

    # Check that correct amount of headers is created
    assert len(addml_etree.find(ADDML_NS + "fieldDefinitions")) == 3


@pytest.mark.parametrize('is_header', [False, True])
def test_create_addml_with_flatfile(is_header):
    """Tests that ``create_addml`` adds flatFile element if optional
    parameter flatfile_name is provided.
    """

    addml_etree = create_addml.create_addml_metadata(
        csv_file=CSV_FILE, delimiter=DELIMITER, isheader=is_header,
        charset=CHARSET, record_separator=RECORDSEPARATOR,
        quoting_char=QUOTINGCHAR, flatfile_name="path/to/test"
    )

    # Check that URL encoded path is written to flatFile element
    flatfile = addml_etree.find(ADDML_NS + "flatFile")

    assert decode_path(flatfile.get("name")) == "path/to/test"