test_parsing.py 1.33 KB
Newer Older
1
from nomad.parsing import JSONStreamWriter, parser_dict
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
from io import StringIO
import json
import pytest


def create_reference(data, pretty):
    if (pretty):
        return json.dumps(data, indent=2)
    else:
        return json.dumps(data, separators=(',', ':'))


@pytest.mark.parametrize("pretty", [False, True])
def test_stream_generator(pretty):
    example_data = [
        {
            'key1': 'value',
            'key2': 1
        },
        {
            'key': {
                'key': 'value'
            }
        }
    ]

    out = StringIO()
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    writer = JSONStreamWriter(out, pretty=pretty)
    writer.open_array()
    writer.open_object()
    writer.key('key1')
    writer.value('value')
    writer.key('key2')
    writer.value(1)
    writer.close_object()
    writer.open_object()
    writer.key('key')
    writer.open_object()
    writer.key('key')
    writer.value('value')
    writer.close_object()
    writer.close_object()
    writer.close_array()
    writer.close()
46
47
48

    assert create_reference(example_data, pretty) == out.getvalue()

49
50
51

def test_vasp_parser():
    vasp_parser = parser_dict['parsers/vasp']
52
53
54
55
56
    example_mainfile = '.dependencies/parsers/vasp/test/examples/xml/perovskite.xml'
    status, errors = vasp_parser.run(example_mainfile)

    assert status == 'ParseSuccess'
    assert errors is None or len(errors) == 0