test_parsing.py 9.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import os
16
17
18
from io import StringIO
import json
import pytest
19
20

from nomadcore.local_meta_info import loadJsonFile
21
import nomad_meta_info
22

23
from nomad import utils
24
from nomad.parsing import JSONStreamWriter, parser_dict, match_parser
25
from nomad.parsing import LocalBackend, BadContextURI
26

27
parser_examples = [
28
    ('parsers/random', 'test/data/parsers/random_0'),
29
    ('parsers/template', 'tests/data/parsers/template.json'),
30
31
    ('parsers/exciting', 'tests/data/parsers/exciting/Ag/INFO.OUT'),
    ('parsers/exciting', 'tests/data/parsers/exciting/GW/INFO.OUT'),
32
    ('parsers/vasp', 'tests/data/parsers/vasp/vasp.xml'),
33
    ('parsers/vaspoutcar', 'tests/data/parsers/vasp_outcar/OUTCAR'),
speckhard's avatar
speckhard committed
34
    ('parsers/fhi-aims', 'tests/data/parsers/fhi-aims/aims.out'),
35
    ('parsers/cp2k', 'tests/data/parsers/cp2k/si_bulk8.out'),
36
37
    ('parsers/crystal', 'tests/data/parsers/crystal/si.out'),
    ('parsers/cpmd', 'tests/data/parsers/cpmd/geo_output.out'),
speckhard's avatar
speckhard committed
38
    ('parsers/nwchem', 'tests/data/parsers/nwchem/single_point/output.out'),
39
    ('parsers/bigdft', 'tests/data/parsers/bigdft/n2_output.out'),
40
    ('parsers/wien2k', 'tests/data/parsers/wien2k/AlN/AlN_ZB.scf'),
41
42
]

43
44
45
46
faulty_unknown_one_d_matid_example = [
    ('parsers/template', 'tests/data/normalizers/no_sim_cell_boolean_positions.json')
]

47
correct_num_output_files = 15
48

49
50
51
52
53

class TestLocalBackend(object):

    @pytest.fixture(scope='session')
    def meta_info(self):
54
55
        file_dir = os.path.dirname(os.path.abspath(nomad_meta_info.__file__))
        path = os.path.join(file_dir, 'all.nomadmetainfo.json')
56
57
58
59
60
61
62
        meta_info, _ = loadJsonFile(path)
        return meta_info

    @pytest.fixture(scope='function')
    def backend(self, meta_info):
        return LocalBackend(meta_info, debug=True)

63
    def test_meta_info(self, meta_info, no_warn):
64
65
        assert 'section_topology' in meta_info

66
67
68
69
70
71
72
73
74
75
    def test_metadata(self, backend, no_warn):
        g_index = backend.openSection('section_calculation_info')
        assert g_index == 0
        backend.addValue('calc_id', 't0')
        backend.closeSection('section_calculation_info', 0)
        g_index = backend.openSection('section_repository_info')
        backend.addValue('repository_calc_id', 1)
        backend.closeSection('section_repository_info', 0)
        assert json.dumps(backend.metadata()) is not None

76
    def test_section(self, backend, no_warn):
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        g_index = backend.openSection('section_run')
        assert g_index == 0
        backend.addValue('program_name', 't0')
        backend.closeSection('section_run', 0)

        g_index = backend.openSection('section_run')
        assert g_index == 1

        g_index = backend.openSection('section_run')
        assert g_index == 2

        backend.addValue('program_name', 't1', 1)
        backend.addValue('program_name', 't2', 2)

        backend.closeSection('section_run', 1)
        backend.closeSection('section_run', 2)

        assert backend.get_sections('section_run') == [0, 1, 2]
        for i in range(0, 3):
            assert backend.get_value('program_name', i) == 't%d' % i

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    def test_two_sections(self, backend, no_warn):
        g_index = backend.openSection('section_run')
        assert g_index == 0
        backend.addValue('program_name', 't0')
        backend.closeSection('section_run', 0)

        g_index = backend.openSection('section_calculation_info')
        assert g_index == 0
        backend.addValue('parser_name', 'p0')
        backend.closeSection('section_calculation_info', 0)

        assert backend.get_sections('section_run') == [0]
        assert backend.get_sections('section_calculation_info') == [0]

        output = StringIO()
        backend.write_json(output)
        archive = json.loads(output.getvalue())
        assert 'section_run' in archive
        assert 'section_calculation_info' in archive

118
    def test_subsection(self, backend: LocalBackend, no_warn):
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
        backend.openSection('section_run')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)

        backend.openSection('section_method')
        backend.closeSection('section_method', -1)

        backend.openSection('section_run')
        backend.closeSection('section_run', 0)
        backend.closeSection('section_run', 1)

        backend.openSection('section_method')
        backend.closeSection('section_method', -1)

        runs = backend.data['section_run']
        assert len(runs) == 2
        assert len(runs[0]['section_method']) == 2
        assert len(runs[1]['section_method']) == 1

138
    def test_context(self, backend: LocalBackend, no_warn):
139
140
141
142
143
        backend.openSection('section_run')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)
        backend.closeSection('section_run', -1)

144
145
        backend.openSection('section_run')
        backend.closeSection('section_run', -1)
146

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        backend.openContext('/section_run/0')
        backend.addValue('program_name', 't1')
        backend.closeContext('/section_run/0')

        backend.openContext('/section_run/1')
        backend.addValue('program_name', 't2')
        backend.closeContext('/section_run/1')

        backend.openContext('/section_run/0/section_method/0')
        backend.closeContext('/section_run/0/section_method/0')

        runs = backend.data['section_run']
        assert runs[0]['program_name'] == 't1'
        assert runs[1]['program_name'] == 't2'

162
    def test_multi_context(self, backend: LocalBackend, no_warn):
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        backend.openSection('section_run')
        backend.closeSection('section_run', -1)

        backend.openContext('/section_run/0')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)
        backend.closeContext('/section_run/0')

        backend.openContext('/section_run/0')
        backend.openSection('section_method')
        backend.closeSection('section_method', -1)
        backend.closeContext('/section_run/0')

        assert len(backend.data['section_method']) == 1

178
    def test_bad_context(self, backend: LocalBackend, no_warn):
179
180
181
182
183
184
185
186
187
188
189
190
        try:
            backend.openContext('section_run/0')
            assert False
        except BadContextURI:
            pass

        try:
            backend.openContext('dsfds')
            assert False
        except BadContextURI:
            pass

191
192
193
194
195
196
197
198
199

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])
200
def test_stream_generator(pretty, no_warn):
201
202
203
204
205
206
207
208
209
210
211
212
213
    example_data = [
        {
            'key1': 'value',
            'key2': 1
        },
        {
            'key': {
                'key': 'value'
            }
        }
    ]

    out = StringIO()
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    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()
231
232
233

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

234

235
236
def assert_parser_result(backend):
    status, errors = backend.status
237
238
    assert status == 'ParseSuccess'
    assert errors is None or len(errors) == 0
239
240


241
242
def run_parser(parser_name, mainfile):
    parser = parser_dict[parser_name]
243
244
    result = parser.run(mainfile, logger=utils.get_logger(__name__))
    return add_calculation_info(result)
Markus Scheidgen's avatar
Markus Scheidgen committed
245
246


247
248
249
@pytest.fixture
def parsed_vasp_example() -> LocalBackend:
    return run_parser(
250
        'parsers/vasp', 'dependencies/parsers/vasp/test/examples/xml/perovskite.xml')
Markus Scheidgen's avatar
Markus Scheidgen committed
251
252


253
254
255
256
257
258
@pytest.fixture
def parsed_template_example() -> LocalBackend:
    return run_parser(
        'parsers/template', 'tests/data/parsers/template.json')


259
260
261
262
263
264
265
@pytest.fixture(
    params=faulty_unknown_one_d_matid_example, ids=lambda spec: '%s-%s' % spec)
def parsed_faulty_unknown_matid_example(caplog, request) -> LocalBackend:
    parser_name, mainfile = request.param
    return run_parser(parser_name, mainfile)


266
267
268
@pytest.fixture(params=parser_examples, ids=lambda spec: '%s-%s' % spec)
def parsed_example(request) -> LocalBackend:
    parser_name, mainfile = request.param
Markus Scheidgen's avatar
Markus Scheidgen committed
269
270
    result = run_parser(parser_name, mainfile)
    return result
271
272


273
274
275
def add_calculation_info(backend: LocalBackend) -> LocalBackend:
    backend.openNonOverlappingSection('section_calculation_info')
    backend.addValue('upload_id', 'test_upload_id')
276
277
    backend.addValue('calc_id', 'test_calc_id')
    backend.addValue('calc_hash', 'test_calc_hash')
278
279
280
281
    backend.addValue('main_file', 'test/mainfile.txt')
    backend.addValue('parser_name', 'testParser')
    backend.closeNonOverlappingSection('section_calculation_info')
    return backend
282
283


284
@pytest.mark.parametrize('parser_name, mainfile', parser_examples)
285
def test_parser(parser_name, mainfile):
286
    parsed_example = run_parser(parser_name, mainfile)
287
    assert_parser_result(parsed_example)
Markus Scheidgen's avatar
Markus Scheidgen committed
288
289


290
def test_match(no_warn):
291
    directory = 'tests/data/parsers'
292
293
294
295
    count = 0
    for dirpath, _, filenames in os.walk(directory):
        for filename in filenames:
            fullname = os.path.join(dirpath, filename)
296
297
298
299
300
            parser = match_parser(fullname, lambda: open(fullname, 'rb'))
            if parser is not None:
                count += 1
            else:
                print(fullname)
301

302
    assert count == correct_num_output_files