Skip to content

Commit 8b7fd04

Browse files
Merge pull request #87 from killianrochet/eLabFTW/testServer
Modification of download tests
2 parents aa6b3a5 + 55c4a4b commit 8b7fd04

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

elab_bridge/server_interface.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def upload_experiment(experiment_file, server_config_json, experiment_title):
7878
Returns
7979
-------
8080
(dict) Content of the experiment as registered on the server
81+
(int) ID of the experiment
8182
"""
8283

8384
try:
@@ -114,7 +115,29 @@ def upload_experiment(experiment_file, server_config_json, experiment_title):
114115

115116
metadata = json.loads(experiment_obj.metadata)
116117

117-
return metadata
118+
return metadata, item_id
119+
120+
121+
def delete_experiment(experiment_id, server_config_json):
122+
"""
123+
Delete an existing experiment.
124+
125+
Parameters
126+
----------
127+
experiment_id: int
128+
ID of the experiment you want to delete
129+
server_config_json: str
130+
Path to the json file containing the api_url and the api_token
131+
"""
132+
api_client = get_elab_config(server_config_json)
133+
experiment_api = elabapi_python.ExperimentsApi(api_client)
134+
135+
res = experiment_api.delete_experiment_with_http_info(id=experiment_id)
136+
status_delete = res[1]
137+
138+
if status_delete != 204:
139+
raise ValueError('Deletion of an experiment on server failed.'
140+
' Check your internet connection and permissions.')
118141

119142

120143
def upload_template(template_file, server_config_json, template_title):
@@ -134,6 +157,7 @@ def upload_template(template_file, server_config_json, template_title):
134157
Returns
135158
-------
136159
(dict) Content of the template as registered on the server
160+
(int) ID of the template
137161
"""
138162

139163
try:
@@ -173,7 +197,29 @@ def upload_template(template_file, server_config_json, template_title):
173197

174198
metadata = json.loads(template_obj.metadata)
175199

176-
return metadata
200+
return metadata, template_id
201+
202+
203+
def delete_template(template_id, server_config_json):
204+
"""
205+
Delete an existing template.
206+
207+
Parameters
208+
----------
209+
template_id: int
210+
ID of the experiment you want to delete
211+
server_config_json: str
212+
Path to the json file containing the api_url and the api_token
213+
"""
214+
api_client = get_elab_config(server_config_json)
215+
template_api = elabapi_python.ExperimentsTemplatesApi(api_client)
216+
217+
res = template_api.delete_experiment_template_with_http_info(id=template_id)
218+
status_delete = res[1]
219+
220+
if status_delete != 204:
221+
raise ValueError('Deletion of an template on server failed.'
222+
' Check your internet connection and permissions.')
177223

178224

179225
def get_elab_config(server_config_json):
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
from diglab_utils.test_utils import (test_directory, initialize_test_dir)
2-
from elab_bridge.server_interface import (download_experiment, upload_template, upload_experiment)
2+
from elab_bridge.server_interface import (download_experiment, upload_template, upload_experiment,
3+
delete_template, delete_experiment)
34

45
SERVER_CONFIG_YAML = (test_directory / 'testfiles_elab' / 'TestProject' / 'project.json').resolve()
56

67

78
def test_upload_template(initialize_test_dir):
89
template_file = test_directory / 'testfiles_elab' / 'template.json'
9-
template = upload_template(server_config_json=SERVER_CONFIG_YAML,
10-
template_file=template_file,
11-
template_title='Testproject')
10+
template, template_id = upload_template(server_config_json=SERVER_CONFIG_YAML,
11+
template_file=template_file,
12+
template_title='Testproject')
1213

1314
assert 'elabftw' in template
1415
assert 'extra_fields' in template
1516

17+
# cleanup
18+
delete_template(server_config_json=SERVER_CONFIG_YAML, template_id=template_id)
19+
1620

1721
def test_upload_experiment(initialize_test_dir):
1822
template_file = test_directory / 'testfiles_elab' / 'experiment.json'
1923

20-
experiment = upload_experiment(server_config_json=SERVER_CONFIG_YAML,
21-
experiment_file=template_file,
22-
experiment_title='TestExperiment')
24+
experiment, experiment_id = upload_experiment(server_config_json=SERVER_CONFIG_YAML,
25+
experiment_file=template_file,
26+
experiment_title='TestExperiment')
2327

2428
assert 'extra_fields' in experiment
2529

30+
# cleanup
31+
delete_experiment(server_config_json=SERVER_CONFIG_YAML, experiment_id=experiment_id)
32+
2633

2734
def test_download_experiment(initialize_test_dir):
2835
json_file = test_directory / 'testfiles_elab' / 'downloaded_experiment.json'
36+
upload_experiment_file = test_directory / 'testfiles_elab' / 'experiment.json'
37+
upload, experiment_id = upload_experiment(server_config_json=SERVER_CONFIG_YAML,
38+
experiment_file=upload_experiment_file,
39+
experiment_title='UploadExperiment')
40+
2941
experiment = download_experiment(save_to=json_file,
3042
server_config_json=SERVER_CONFIG_YAML,
31-
experiment_id=232,
43+
experiment_id=experiment_id,
3244
format='json')
3345

46+
assert len(upload) == len(experiment)
47+
assert upload == experiment
48+
3449
assert json_file.exists()
3550
assert 'extra_fields' in experiment
3651

3752
# cleanup
53+
delete_experiment(server_config_json=SERVER_CONFIG_YAML, experiment_id=experiment_id)
3854
json_file.unlink()

0 commit comments

Comments
 (0)