# Path: siptools/utils.py
# 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 io
import os.path
import json
import shutil
import sys
import lxml.etree as ET
import pytest
import siptools.scripts.create_audiomd as create_audiomd
from siptools.utils import read_md_references

    filepath = os.path.join(
        testpath, 'eae4d239422e21f3a8cfa57bb2afcb9e-AudioMD-amd.xml'
        # testpath, '704fbd57169eac3af9388e03c89dd919-AudioMD-amd.xml'
        # testpath, '4dab7d9d5bab960188ea0f25e478cb17-AudioMD-amd.xml'
    )

    assert os.path.isfile(filepath)


def test_main_utf8_files(testpath, run_cli):
    """Test for ``main`` function with filenames that contain non-ascii
    characters.
    """
    # Create sample data directory with file that has non-ascii characters in
    # filename
    os.makedirs(os.path.join(testpath, 'data'))
    relative_path = os.path.join('data', 'äöå.wav')
    full_path = os.path.join(testpath, relative_path)
    shutil.copy('tests/data/audio/valid__wav.wav', full_path)

    # Call main function with encoded filename as parameter
    run_cli(
        create_audiomd.main, [
            '--workspace', testpath, '--base_path', testpath,
            relative_path.encode(sys.getfilesystemencoding())
        ]
    )

    # Check that filename is found in amd-reference file.

# Path: siptools/utils.py
# 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_audiomd.py
import io
import os.path
import json
import shutil
import sys
import lxml.etree as ET
import pytest
import siptools.scripts.create_audiomd as create_audiomd
from siptools.utils import read_md_references


    filepath = os.path.join(
        testpath, 'eae4d239422e21f3a8cfa57bb2afcb9e-AudioMD-amd.xml'
        # testpath, '704fbd57169eac3af9388e03c89dd919-AudioMD-amd.xml'
        # testpath, '4dab7d9d5bab960188ea0f25e478cb17-AudioMD-amd.xml'
    )

    assert os.path.isfile(filepath)


def test_main_utf8_files(testpath, run_cli):
    """Test for ``main`` function with filenames that contain non-ascii
    characters.
    """
    # Create sample data directory with file that has non-ascii characters in
    # filename
    os.makedirs(os.path.join(testpath, 'data'))
    relative_path = os.path.join('data', 'äöå.wav')
    full_path = os.path.join(testpath, relative_path)
    shutil.copy('tests/data/audio/valid__wav.wav', full_path)

    # Call main function with encoded filename as parameter
    run_cli(
        create_audiomd.main, [
            '--workspace', testpath, '--base_path', testpath,
            relative_path.encode(sys.getfilesystemencoding())
        ]
    )

    # Check that filename is found in amd-reference file.

    refs = read_md_references(testpath, 'create-audiomd-md-references.jsonl')