Skip to content

Commit

Permalink
update API and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
f-idiris committed Sep 27, 2023
1 parent f691301 commit 1edde5f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 54 deletions.
9 changes: 5 additions & 4 deletions chem_spectra/controller/spectra_layout_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from flask import jsonify, Blueprint
import os
import shutil

spectra_layout_api = Blueprint('spectra_layout_api', __name__)
script_dir = os.path.dirname(__file__)
Expand All @@ -11,10 +12,10 @@ def load_data_types():
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)
except FileNotFoundError:
with open(data_type_json_path, 'w') as new_mapping_file:
initial_data = {"datatypes": {}}
json.dump(initial_data, new_mapping_file, indent=4)
return initial_data
example_json_path = os.path.join(script_dir, '../lib/converter/jcamp/data_type.json.example')
shutil.copy(example_json_path, data_type_json_path)
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)

@spectra_layout_api.route('/spectra_layouts', methods=['GET'])
def get_spectra_layouts():
Expand Down
37 changes: 22 additions & 15 deletions chem_spectra/controller/spectra_type_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
from flask import request, jsonify, Blueprint
import shutil

spectra_type_api = Blueprint('spectra_type_api', __name__)

Expand All @@ -12,35 +13,41 @@ def load_data_types():
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)
except FileNotFoundError:
return {"datatypes": {}}
example_json_path = os.path.join(script_dir, '../lib/converter/jcamp/data_type.json.example')
shutil.copy(example_json_path, data_type_json_path)
with open(data_type_json_path, 'r') as mapping_file:
return json.load(mapping_file)

def save_data_types(data_types):
with open(data_type_json_path, 'w') as mapping_file:
json.dump(data_types, mapping_file, indent=4)

@spectra_type_api.route('/data_type', methods=['POST'])
def create_or_update_data_type():
def create_data_type():
request_data = request.get_json()
new_data_type_mapping = request_data.get("new_data_type")
existing_data_types = load_data_types()

for data_type, layout in new_data_type_mapping.items():
if data_type in existing_data_types["datatypes"]:

for layout, data_type in new_data_type_mapping.items():
if layout in existing_data_types["datatypes"] and data_type not in existing_data_types['datatypes'][layout]:
existing_data_types["datatypes"][layout].append(data_type)
elif layout in existing_data_types["datatypes"] and data_type in existing_data_types['datatypes'][layout]:
return jsonify({"message": f"Data type '{data_type}' already exists"}), 400

existing_data_types["datatypes"].update(new_data_type_mapping)
else:
return jsonify({"message": f"Layout '{layout}' does not exist"}), 400

save_data_types(existing_data_types)

return jsonify({"message": "Data type created/updated successfully"}), 200
return jsonify({"message": "Data type created successfully"}), 200

@spectra_type_api.route('/data_type/<data_type>', methods=['DELETE'])
def delete_data_type(data_type):
existing_data_types = load_data_types()

if data_type in existing_data_types["datatypes"]:
del existing_data_types["datatypes"][data_type]
save_data_types(existing_data_types)
return jsonify({"message": f"Data type '{data_type}' deleted successfully"}), 200
else:
return jsonify({"message": f"Data type '{data_type}' not found"}), 404

for layout, data_types in existing_data_types['datatypes'].items():
if data_type in data_types:
data_types.remove(data_type)
save_data_types(existing_data_types)
return jsonify({"message": f"Data type '{data_type}' deleted successfully"}), 200

return jsonify({"message": f"Data type '{data_type}' not found"}), 404
19 changes: 19 additions & 0 deletions chem_spectra/lib/converter/jcamp/data_type.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"datatypes": {
"NMR": ["NMR SPECTRUM", "NMRSPECTRUM"],
"INFRARED": ["INFRARED SPECTRUM"],
"RAMAN": ["RAMAN SPECTRUM"],
"MS": ["MASS SPECTRUM"],
"HPLC UVVIS": ["HPLC UV/VIS SPECTRUM", "HPLC UV-VIS"],
"UVVIS": ["UV/VIS SPECTRUM", "UV-VIS", "ULTRAVIOLET SPECTRUM"],
"THERMOGRAVIMETRIC ANALYSIS": ["THERMOGRAVIMETRIC ANALYSIS"],
"X-RAY DIFFRACTION": ["X-RAY DIFFRACTION"],
"CYCLIC VOLTAMMETRY": ["CYCLIC VOLTAMMETRY"],
"SIZE EXCLUSION CHROMATOGRAPHY": ["SIZE EXCLUSION CHROMATOGRAPHY"],
"CIRCULAR DICHROISM SPECTROSCOPY": ["CIRCULAR DICHROISM SPECTROSCOPY"],
"SORPTION-DESORPTION MEASUREMENT": ["SORPTION-DESORPTION MEASUREMENT"],
"Emissions": ["Emissions", "EMISSIONS", "FLUORESCENCE SPECTRUM", "FL SPECTRUM"],
"DLS ACF": ["DLS ACF"],
"DLS intensity": ["DLS INTENSITY", "DLS intensity"]
}
}
19 changes: 5 additions & 14 deletions tests/controller/test_spectra_layout_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@ def test_get_spectra_layouts_with_data(client):
response_data = response.json()
assert response.status_code == 200
assert response_data == {
"NMRSPECTRUM": "NMR",
"INFRARED SPECTRUM": "INFRARED"
}

def test_get_spectra_layouts_without_data(client):
data_type_json_path = './tests/fixtures/result/spectralayout_test_data.json'

response = client.get('/spectra_layouts')
response_data = response.json()
assert response.status_code == 200
assert response_data == {}

if os.path.exists(data_type_json_path):
os.remove(data_type_json_path)
"INFRARED": ["INFRARED SPECTRUM"],
"MS": ["MASS SPECTRUM"],
"NMR": ["NMR SPECTRUM", "NMRSPECTRUM"],
"RAMAN": ["RAMAN"]
}
36 changes: 18 additions & 18 deletions tests/controller/test_spectra_type_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@

data_type_json_path = './tests/fixtures/test_data_types.json'

def test_create_or_update_data_type(client):
def test_create_data_type(client):
new_data_type = {
"new_data_type": {
"MASS SPECTRUM": "MS"
"MS": "MASS SPEC"
}
}

response = client.post('/data_type', json=new_data_type)
assert response.status_code == 200

response_data = response.json()
assert response_data.get("datatypes") is not None
assert response_data["datatypes"].get("MASS SPECTRUM") == "MS"
assert "message" in response_data
response_data["message"] == "Data type created successfully"

def test_create_or_update_data_type_unchanged(client):
def test_create_data_type_unchanged(client):
# data type already exists in JSON
new_data_type = {
"new_data_type": {
"INFRARED SPECTRUM": "INFRARED"
"INFRARED": "INFRARED SPECTRUM"
}
}

Expand All @@ -33,35 +33,35 @@ def test_create_or_update_data_type_unchanged(client):
assert "message" in response_data
assert response_data["message"] == "Data type 'INFRARED SPECTRUM' already exists"

def test_create_or_update_data_type_file_not_found(client):
data_type_json_path = './tests/fixtures/result/spectratype_test_data.json'

def test_create_data_type_layout_does_not_exist(client):
new_data_type = {
"new_data_type": {
"RAMAN SPECTRUM": "RAMAN"
"UNKNOWN": "NA"
}
}

response = client.post('/data_type', json=new_data_type)
assert response.status_code == 200
response_data = response.json()
assert len(response_data.get("datatypes")) == 1
assert response_data["datatypes"].get("RAMAN SPECTRUM") == "RAMAN"
assert response.status_code == 400

if os.path.exists(data_type_json_path):
os.remove(data_type_json_path)
response_data = response.json()

assert "message" in response_data
assert response_data["message"] == "Layout 'UNKNOWN' does not exist"

def test_delete_data_type(client):
# create a new data type
new_data_type = {
"new_data_type": {
"FLUORESCENCE": "FLUORESCENCE SPECTRUM"
"INFRARED": "IR"
}
}
response = client.post('/data_type', json=new_data_type)
assert response.status_code == 200

# delete the new data type
response = client.delete('/data_type/FLUORESCENCE')
response = client.delete('/data_type/IR')
assert response.status_code == 200
response_data = response.json()

assert "message" in response_data
assert response_data["message"] == "Data type 'IR' deleted successfully"
8 changes: 5 additions & 3 deletions tests/fixtures/test_data_types.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"datatypes": {
"NMRSPECTRUM": "NMR",
"INFRARED SPECTRUM": "INFRARED"
"datatypes": {
"NMR": ["NMR SPECTRUM", "NMRSPECTRUM"],
"INFRARED": ["INFRARED SPECTRUM"],
"RAMAN": ["RAMAN"],
"MS": ["MASS SPECTRUM"]
}
}

0 comments on commit 1edde5f

Please sign in to comment.