test_api.py 5.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
import pytest
from threading import Thread
import subprocess
import shlex
import time
import json
from mongoengine import connect
from mongoengine.connection import disconnect

10
from nomad import config, api, files, processing
11

12
from tests.test_processing import example_files
13
14
from tests.test_files import assert_exists
# import fixtures
15
from tests.test_files import clear_files, archive_id  # pylint: disable=unused-import
16
17
18
from tests.test_normalizing import normalized_vasp_example  # pylint: disable=unused-import
from tests.test_parsing import parsed_vasp_example  # pylint: disable=unused-import
from tests.test_search import example_entry  # pylint: disable=unused-import
19
from tests.test_processing import celery_config, celery_includes  # pylint: disable=unused-import
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

@pytest.fixture
def client():
    disconnect()
    connect('users_test', host=config.mongo.host, is_mock=True)

    api.app.config['TESTING'] = True
    client = api.app.test_client()

    yield client


def assert_uploads(upload_json_str, count=0, **kwargs):
    data = json.loads(upload_json_str)
    assert isinstance(data, list)
    assert len(data) == count

    if count > 0:
        assert_upload(json.dumps(data[0]), **kwargs)


def assert_upload(upload_json_str, id=None):
    data = json.loads(upload_json_str)
Markus Scheidgen's avatar
Markus Scheidgen committed
44
    assert 'upload_id' in data
45
    if id is not None:
Markus Scheidgen's avatar
Markus Scheidgen committed
46
        assert id == data['upload_id']
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
    assert 'create_time' in data
    assert 'presigned_url' in data

    return data


def test_no_uploads(client):
    rv = client.get('/uploads')

    assert rv.status_code == 200
    assert_uploads(rv.data, count=0)


def test_bad_upload_id(client):
    rv = client.get('/uploads/bad_id')
    assert rv.status_code == 400


def test_not_existing_upload(client):
    rv = client.get('/uploads/123456789012123456789012')
    assert rv.status_code == 404


def test_create_upload(client):
    rv = client.post('/uploads')

    assert rv.status_code == 200
Markus Scheidgen's avatar
Markus Scheidgen committed
74
    upload_id = assert_upload(rv.data)['upload_id']
75
76
77
78
79
80
81
82
83
84

    rv = client.get('/uploads/%s' % upload_id)
    assert rv.status_code == 200
    assert_upload(rv.data, id=upload_id)

    rv = client.get('/uploads')
    assert rv.status_code == 200
    assert_uploads(rv.data, count=1, id=upload_id)


85
86
87
88
89
90
91
92
def test_create_upload_with_name(client):
    rv = client.post(
        '/uploads', data=json.dumps(dict(name='test_name')), content_type='application/json')
    assert rv.status_code == 200
    upload = assert_upload(rv.data)
    assert upload['name'] == 'test_name'


93
@pytest.mark.parametrize("file", example_files)
Markus Scheidgen's avatar
Markus Scheidgen committed
94
@pytest.mark.timeout(30)
95
def test_upload_to_upload(client, file):
96
97
98
99
100
101
    rv = client.post('/uploads')
    assert rv.status_code == 200
    upload = assert_upload(rv.data)

    @files.upload_put_handler
    def handle_upload_put(received_upload_id: str):
Markus Scheidgen's avatar
Markus Scheidgen committed
102
        assert upload['upload_id'] == received_upload_id
103
104
105
106
107
108
109
110
111
112
        raise StopIteration

    def handle_uploads():
        handle_upload_put(received_upload_id='provided by decorator')

    handle_uploads_thread = Thread(target=handle_uploads)
    handle_uploads_thread.start()

    time.sleep(1)
    upload_url = upload['presigned_url']
113
    cmd = files.create_curl_upload_cmd(upload_url).replace('<ZIPFILE>', file)
114
115
116
117
    subprocess.call(shlex.split(cmd))

    handle_uploads_thread.join()

Markus Scheidgen's avatar
Markus Scheidgen committed
118
    assert_exists(config.files.uploads_bucket, upload['upload_id'])
119
120
121


@pytest.mark.parametrize("file", example_files)
Markus Scheidgen's avatar
Markus Scheidgen committed
122
@pytest.mark.timeout(30)
123
def test_processing(client, file, celery_session_worker):
Markus Scheidgen's avatar
Markus Scheidgen committed
124
    handle_uploads_thread = processing.handle_uploads_thread(quit=True)
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    rv = client.post('/uploads')
    assert rv.status_code == 200
    upload = assert_upload(rv.data)

    time.sleep(1)
    upload_url = upload['presigned_url']
    cmd = files.create_curl_upload_cmd(upload_url).replace('<ZIPFILE>', file)
    subprocess.call(shlex.split(cmd))

    handle_uploads_thread.join()

    while True:
        time.sleep(1)

Markus Scheidgen's avatar
Markus Scheidgen committed
140
        rv = client.get('/uploads/%s' % upload['upload_id'])
141
142
143
        assert rv.status_code == 200
        upload = assert_upload(rv.data)
        assert 'upload_time' in upload
Markus Scheidgen's avatar
Markus Scheidgen committed
144
145
        if upload['proc']['status'] in ['SUCCESS', 'FAILURE']:
            break
146

Markus Scheidgen's avatar
Markus Scheidgen committed
147
    proc = upload['proc']
148
    assert proc['status'] == 'SUCCESS'
Markus Scheidgen's avatar
Markus Scheidgen committed
149
150
151
    assert 'calc_procs' in proc
    assert proc['calc_procs'] is not None
    assert proc['current_task_name'] == 'cleanup'
Markus Scheidgen's avatar
Markus Scheidgen committed
152
    assert len(proc['task_names']) == 4
Markus Scheidgen's avatar
Markus Scheidgen committed
153
    assert_exists(config.files.uploads_bucket, upload['upload_id'])
154
155


156
def test_get_repo_calc(client, example_entry):
157
158
159
160
    rv = client.get('/repo/%s/%s' % (example_entry.upload_hash, example_entry.calc_hash))
    assert rv.status_code == 200


161
def test_non_existing_repo_cals(client):
162
163
164
165
    rv = client.get('/repo/doesnt/exist')
    assert rv.status_code == 404


166
167
168
169
170
171
172
173
174
175
def test_get_repo_calcs(client, example_entry):
    rv = client.get('/repo')
    assert rv.status_code == 200
    data = json.loads(rv.data)
    results = data.get('results', None)
    assert results is not None
    assert isinstance(results, list)
    assert len(results) >= 1


176
177
def test_get_archive(client, archive_id):
    rv = client.get('/archive/%s' % archive_id)
178
    assert rv.status_code == 302
179
180
181
182
183


def test_get_non_existing_archive(client):
    rv = client.get('/archive/%s' % 'doesnt/exist')
    assert rv.status_code == 404