1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
# fmt: off
"""
Tests of the plugin functionality for defining IO formats
outside of the ase package
"""
import copy
import io
from importlib.metadata import EntryPoint
import pytest
from ase.build import bulk
from ase.io import formats, read, write
from ase.io.formats import define_external_io_format
from ase.utils.plugins import ExternalIOFormat
@pytest.fixture(autouse=True)
def reset_ioformats_after_test():
ioformats_before = copy.deepcopy(formats.ioformats)
try:
yield
finally:
formats.ioformats = ioformats_before
formats.all_formats = ioformats_before
VALID_IO_FORMAT = ExternalIOFormat(
desc='Test IO format',
code='1F',
module='ase.test.fio.test_external_io_formats'
)
# These are dummy functions for reading and writing the dummy io format
def read_dummy(file):
return "Atoms dummy"
def write_dummy(file, atoms):
file.write("dummy output")
def test_external_ioformat_valid(tmp_path):
"""
Test of the external io format utility correctly
registering a valid external entry format
"""
test_entry_point = EntryPoint(
name='dummy',
value='ase.test.fio.test_external_io_formats:VALID_IO_FORMAT',
group='ase.ioformats')
define_external_io_format(test_entry_point)
assert 'dummy' in formats.ioformats
TEST_FILE = """
THIS IS A DUMMY
"""
assert read(io.StringIO(TEST_FILE), format='dummy') == 'Atoms dummy'
atom = bulk('Ti')
write(tmp_path / 'dummy_output', atom, format='dummy')
with open(tmp_path / 'dummy_output') as file:
assert file.read() == 'dummy output'
def test_external_ioformat_already_existing():
"""
Test of the external io format utility correctly
refusing to register an IOformat that is already present
"""
test_entry_point = EntryPoint(
name='xyz',
value='ase.test.fio.test_external_io_formats:VALID_IO_FORMAT',
group='ase.ioformats')
with pytest.raises(ValueError, match='Format xyz already defined'):
define_external_io_format(test_entry_point)
assert 'xyz' in formats.ioformats
assert formats.ioformats['xyz'].description != 'Test IO format'
# Io format not specified with the required namedtuple
INVALID_IO_FORMAT = {
'desc': 'Test IO format',
'code': '1F',
'module': 'ase.test.fio.test_external_io_formats'
}
def test_external_ioformat_wrong_type():
"""
Test of the external io format utility correctly
refusing to register an IOformat that is not specified using the
namedtuple
"""
test_entry_point = EntryPoint(
name='dummy',
value='ase.test.fio.test_external_io_formats:INVALID_IO_FORMAT',
group='ase.ioformats')
with pytest.raises(TypeError,
match='Wrong type for registering external IO formats'):
define_external_io_format(test_entry_point)
assert 'dummy' not in formats.ioformats
def test_external_ioformat_import_error():
"""
Test of the external io format utility correctly
refusing to register an IOformat that is already present
"""
test_entry_point = EntryPoint(
name='dummy',
value='ase.test.fio.test_external_io_formats:NOT_EXISTING',
group='ase.ioformats')
with pytest.raises(AttributeError):
define_external_io_format(test_entry_point)
assert 'dummy' not in formats.ioformats
|