def teardown(self):
        # Restore the backed-up file
        p = self.dpath / "693_UNCI.dcm"
        shutil.copy(self.dpath / "PYTEST_BACKUP", p)
        os.remove(self.dpath / "PYTEST_BACKUP")

        if 'mylib' in external_data_sources():
            del external_data_sources()['mylib']
    def test_get_testdata_file_external_ignore_hash(self):
        """Test that non-pydicom-data external source ignores hash check."""
        external_data_sources()['mylib'] = external_data_sources(
        )['pydicom-data']
        p = self.dpath / "693_UNCI.dcm"
        with open(p, 'wb') as f:
            f.write(b"\x00\x01")

        ext_hash = calculate_file_hash(p)
        ref_hash = get_cached_filehash(p.name)
        assert ext_hash != ref_hash
        fpath = self.as_posix(get_testdata_file(p.name))
        assert "data_store/data" in fpath
    def test_get_dataset(self):
        """Test the different functions to get lists of data files."""
        # The cached files downloaded from the pydicom-data repo
        cached_data_test_files = str(get_data_dir())

        # If pydicom-data is available locally
        ext_path = None
        if 'pydicom-data' in external_data_sources():
            ext_path = os.fspath(
                external_data_sources()['pydicom-data'].data_path)

        # Test base locations
        charbase = os.path.join(DATA_ROOT, 'charset_files')
        assert os.path.exists(charbase)

        testbase = os.path.join(DATA_ROOT, 'test_files')
        assert os.path.exists(testbase)

        # Test file get
        chardata = get_charset_files()
        assert 15 < len(chardata)

        # Test that top level file is included
        bases = [basename(x) for x in chardata]

        # Test that subdirectory files included
        testdata = get_testdata_files()
        bases = [basename(x) for x in testdata]
        assert '2693' in bases
        assert 70 < len(testdata)

        # The files should be from their respective bases
        for x in testdata:
            # Don't check files from external sources other than pydicom-data
            if (testbase not in x and cached_data_test_files not in x
                    and (ext_path not in x if ext_path else True)):
                continue

            assert (testbase in x or cached_data_test_files in x
                    or (ext_path in x if ext_path else False))

        for x in chardata:
            assert charbase in x
    def setup(self):
        self.dpath = external_data_sources()["pydicom-data"].data_path

        # Backup the 693_UNCI.dcm file
        p = self.dpath / "693_UNCI.dcm"
        shutil.copy(p, self.dpath / "PYTEST_BACKUP")
from os.path import basename
from pathlib import Path
import shutil

import pytest

from pydicom.data import (get_charset_files, get_testdata_files,
                          get_palette_files, fetch_data_files)
from pydicom.data.data_manager import (DATA_ROOT, get_testdata_file,
                                       external_data_sources)
from pydicom.data import download
from pydicom.data.download import (get_data_dir, calculate_file_hash,
                                   get_cached_filehash)

EXT_PYDICOM = False
if 'pydicom-data' in external_data_sources():
    EXT_PYDICOM = True


@pytest.fixture
def download_failure():
    """Simulate a download failure."""
    download._SIMULATE_NETWORK_OUTAGE = True
    yield
    download._SIMULATE_NETWORK_OUTAGE = False


class TestGetData:
    def test_get_dataset(self):
        """Test the different functions to get lists of data files."""
        # The cached files downloaded from the pydicom-data repo