From 2b06e13194eabfd10d0b6da722f3faf819433a47 Mon Sep 17 00:00:00 2001 From: f-idiris Date: Wed, 27 Sep 2023 15:41:03 +0200 Subject: [PATCH] update API and tests --- chem_spectra/controller/spectra_layout_api.py | 9 +++-- chem_spectra/controller/spectra_type_api.py | 37 +++++++++++-------- .../converter/jcamp/data_type.example.json | 19 ++++++++++ tests/controller/test_spectra_layout_api.py | 19 +++------- tests/controller/test_spectra_type_api.py | 36 +++++++++--------- tests/fixtures/test_data_types.json | 8 ++-- 6 files changed, 74 insertions(+), 54 deletions(-) create mode 100644 chem_spectra/lib/converter/jcamp/data_type.example.json diff --git a/chem_spectra/controller/spectra_layout_api.py b/chem_spectra/controller/spectra_layout_api.py index a2c28984..a78202ed 100644 --- a/chem_spectra/controller/spectra_layout_api.py +++ b/chem_spectra/controller/spectra_layout_api.py @@ -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__) @@ -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.example.json') + 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(): diff --git a/chem_spectra/controller/spectra_type_api.py b/chem_spectra/controller/spectra_type_api.py index 0d4aec0f..c55f9fdd 100644 --- a/chem_spectra/controller/spectra_type_api.py +++ b/chem_spectra/controller/spectra_type_api.py @@ -1,6 +1,7 @@ import json import os from flask import request, jsonify, Blueprint +import shutil spectra_type_api = Blueprint('spectra_type_api', __name__) @@ -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.example.json') + 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/', 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 \ No newline at end of file + + 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 diff --git a/chem_spectra/lib/converter/jcamp/data_type.example.json b/chem_spectra/lib/converter/jcamp/data_type.example.json new file mode 100644 index 00000000..5c135ad6 --- /dev/null +++ b/chem_spectra/lib/converter/jcamp/data_type.example.json @@ -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"] + } +} diff --git a/tests/controller/test_spectra_layout_api.py b/tests/controller/test_spectra_layout_api.py index e68f4e7c..dbc490e4 100644 --- a/tests/controller/test_spectra_layout_api.py +++ b/tests/controller/test_spectra_layout_api.py @@ -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) \ No newline at end of file + "INFRARED": ["INFRARED SPECTRUM"], + "MS": ["MASS SPECTRUM"], + "NMR": ["NMR SPECTRUM", "NMRSPECTRUM"], + "RAMAN": ["RAMAN"] + } \ No newline at end of file diff --git a/tests/controller/test_spectra_type_api.py b/tests/controller/test_spectra_type_api.py index 3780a7c0..b14a4701 100644 --- a/tests/controller/test_spectra_type_api.py +++ b/tests/controller/test_spectra_type_api.py @@ -3,10 +3,10 @@ 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" } } @@ -14,14 +14,14 @@ def test_create_or_update_data_type(client): 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" } } @@ -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" diff --git a/tests/fixtures/test_data_types.json b/tests/fixtures/test_data_types.json index dc885e85..1ad3841b 100644 --- a/tests/fixtures/test_data_types.json +++ b/tests/fixtures/test_data_types.json @@ -1,6 +1,8 @@ { - "datatypes": { - "NMRSPECTRUM": "NMR", - "INFRARED SPECTRUM": "INFRARED" + "datatypes": { + "NMR": ["NMR SPECTRUM", "NMRSPECTRUM"], + "INFRARED": ["INFRARED SPECTRUM"], + "RAMAN": ["RAMAN"], + "MS": ["MASS SPECTRUM"] } }