test_dcat.py 2.68 KB
Newer Older
1
2
3
4
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
5
6
7
8
9
#
# 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
#
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
12
#
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
15
16
# 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.
17
#
18
19
20
21

import pytest
from datetime import datetime

22
from nomad import infrastructure, config
23
from nomad.datamodel import EntryMetadata
Markus Scheidgen's avatar
Markus Scheidgen committed
24
from nomad.app_fastapi.flask.dcat.mapping import Mapping
25
26

from tests.conftest import clear_elastic
Markus Scheidgen's avatar
Markus Scheidgen committed
27
from tests.app_fastapi.flask.test_app import BlueprintClient
28

Maja-Olivia Lenz's avatar
Maja-Olivia Lenz committed
29

30
31
32
33
34
35
@pytest.fixture(scope='session')
def api(session_client):
    return BlueprintClient(session_client, '/dcat')


@pytest.fixture(scope='module')
Maja-Olivia Lenz's avatar
Maja-Olivia Lenz committed
36
def example_entry(test_user, other_test_user):
37
38
39
40
41
42
43
44
45
46
47
48

    entry = EntryMetadata(
        calc_id='test-id',
        upload_id='upload-id',
        upload_time=datetime.now(),
        last_processing=datetime.now(),
        uploader=test_user,
        coauthors=[other_test_user],
        comment='this is a calculation comment',
        formula='H20',
        published=True)

Markus Scheidgen's avatar
Markus Scheidgen committed
49
    yield entry
50
51


Markus Scheidgen's avatar
Markus Scheidgen committed
52
53
54
55
def test_mapping(example_entry):
    mapping = Mapping()
    mapping.map_entry(example_entry)
    assert mapping.g is not None
Maja-Olivia Lenz's avatar
Maja-Olivia Lenz committed
56
    # print(mapping.g.serialize(format='ttl').decode('utf-8'))
Markus Scheidgen's avatar
Markus Scheidgen committed
57
58


Maja-Olivia Lenz's avatar
Maja-Olivia Lenz committed
59
60
61
def test_get_dataset(elastic_infra, api, example_entry):
    clear_elastic(elastic_infra)
    example_entry.a_elastic.index()
62
63
64
    calc_id = 'test-id'
    rv = api.get('/datasets/%s' % calc_id)
    assert rv.status_code == 200
Maja-Olivia Lenz's avatar
Maja-Olivia Lenz committed
65
66

    clear_elastic(elastic_infra)
67
68


69
70
71
72
73
@pytest.mark.parametrize('after,modified_since', [
    (None, None),
    (None, '2020-01-07'),
    ('test-id-3', '2020-01-07')])
def test_get_catalog(elastic_infra, api, example_entry, after, modified_since):
74
75
76
77
78
79
80
81
82
83
    clear_elastic(elastic_infra)

    for i in range(1, 11):
        example_entry.calc_id = 'test-id-%d' % i
        example_entry.upload_time = datetime(2000, 1, 1)
        example_entry.last_processing = datetime(2020, 1, i)
        example_entry.a_elastic.index()

    infrastructure.elastic_client.indices.refresh(index=config.elastic.index_name)

84
85
86
87
88
89
    url = '/catalog/?format=turtle'
    if after:
        url += '&after=' + after
    if modified_since:
        url += '&modified_since=' + modified_since
    rv = api.get(url)
90
91
92
    assert rv.status_code == 200

    clear_elastic(elastic_infra)