File: test_cifblock.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (68 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download | duplicates (2)
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
import pytest
from ase.io.cif import CIFBlock, parse_loop, CIFLoop


def test_parse_cifloop_simple():
    dct = parse_loop(['_apples',
                      '_verysmallrocks',
                      '2 200',
                      '3 300',
                      '4 400'][::-1])
    assert dct['_apples'] == [2, 3, 4]
    assert dct['_verysmallrocks'] == [200, 300, 400]


def test_parse_cifloop_warn_duplicate_header():
    with pytest.warns(UserWarning):
        parse_loop(['_hello', '_hello'])


def test_parse_cifloop_incomplete():
    with pytest.raises(RuntimeError):
        parse_loop(['_spam', '_eggs', '1 2', '1'][::-1])


def test_cifloop_roundtrip():
    loop = CIFLoop()
    loop.add('_potatoes', [2.5, 3.0, -1.0], '{:8.5f}')
    loop.add('_eggs', [1, 2, 3], '{:2d}')
    string = loop.tostring() + '\n'
    print('hmm', string)
    lines = string.splitlines()[::-1]
    assert lines.pop() == 'loop_'

    for line in lines:
        print(repr(line))
    dct = parse_loop(lines)
    assert dct['_potatoes'] == pytest.approx([2.5, 3.0, -1.0])
    assert dct['_eggs'] == [1, 2, 3]


@pytest.fixture
def cifblock():
    return CIFBlock('hello', {'_cifkey': 42})


def test_repr(cifblock):
    text = repr(cifblock)
    assert 'hello' in text
    assert '_cifkey' in text


def test_mapping(cifblock):
    assert len(cifblock) == 1
    assert len(list(cifblock)) == 1


def test_various(cifblock):
    assert cifblock.get_cellpar() is None
    assert cifblock.get_cell().rank == 0


def test_deuterium():
    # Verify that the symbol 'D' becomes hydrogen ('H') with mass 2(-ish).
    symbols = ['H', 'D', 'D', 'He']
    block = CIFBlock('deuterium', dict(_atom_site_type_symbol=symbols))
    assert block.get_symbols() == ['H', 'H', 'H', 'He']
    masses = block._get_masses()
    assert all(masses.round().astype(int) == [1, 2, 2, 4])