diff --git a/chem_spectra/controller/spectra_layout_api.py b/chem_spectra/controller/spectra_layout_api.py index 75c9f64e..a2c28984 100644 --- a/chem_spectra/controller/spectra_layout_api.py +++ b/chem_spectra/controller/spectra_layout_api.py @@ -11,10 +11,12 @@ def load_data_types(): with open(data_type_json_path, 'r') as mapping_file: return json.load(mapping_file) except FileNotFoundError: - return {"datatypes": {}} + 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 @spectra_layout_api.route('/spectra_layouts', methods=['GET']) def get_spectra_layouts(): existing_data_types = load_data_types() - spectra_layouts = list(set(existing_data_types["datatypes"].values())) - return jsonify(spectra_layouts), 200 \ No newline at end of file + return jsonify(existing_data_types["datatypes"]), 200 \ No newline at end of file diff --git a/chem_spectra/controller/spectra_type_api.py b/chem_spectra/controller/spectra_type_api.py index 93c8449c..0d4aec0f 100644 --- a/chem_spectra/controller/spectra_type_api.py +++ b/chem_spectra/controller/spectra_type_api.py @@ -32,4 +32,15 @@ def create_or_update_data_type(): save_data_types(existing_data_types) - return jsonify({"message": "Data type created/updated successfully"}), 200 \ No newline at end of file + return jsonify({"message": "Data type created/updated 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 diff --git a/tests/controller/test_spectra_layout_api.py b/tests/controller/test_spectra_layout_api.py new file mode 100644 index 00000000..e68f4e7c --- /dev/null +++ b/tests/controller/test_spectra_layout_api.py @@ -0,0 +1,23 @@ +import os +import json + +def test_get_spectra_layouts_with_data(client): + data_type_json_path = './tests/fixtures/test_data_types.json' + response = client.get('/spectra_layouts') + 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 diff --git a/tests/controller/test_spectra_type_api.py b/tests/controller/test_spectra_type_api.py index 1f4a3c31..3780a7c0 100644 --- a/tests/controller/test_spectra_type_api.py +++ b/tests/controller/test_spectra_type_api.py @@ -1,7 +1,7 @@ import os import json -test_json = './tests/fixtures/test_data_types.json' +data_type_json_path = './tests/fixtures/test_data_types.json' def test_create_or_update_data_type(client): new_data_type = { @@ -10,7 +10,7 @@ def test_create_or_update_data_type(client): } } - response = client.post('/spectra_type_api/data_type', json=new_data_type) + response = client.post('/data_type', json=new_data_type) assert response.status_code == 200 response_data = response.json() @@ -25,7 +25,7 @@ def test_create_or_update_data_type_unchanged(client): } } - response = client.post('/spectra_type_api/data_type', json=new_data_type) + response = client.post('/data_type', json=new_data_type) assert response.status_code == 400 response_data = response.json() @@ -34,8 +34,7 @@ def test_create_or_update_data_type_unchanged(client): assert response_data["message"] == "Data type 'INFRARED SPECTRUM' already exists" def test_create_or_update_data_type_file_not_found(client): - original_test_json = test_json - test_json = './tests/fixtures/non_existent_test_data.json' + data_type_json_path = './tests/fixtures/result/spectratype_test_data.json' new_data_type = { "new_data_type": { @@ -43,10 +42,26 @@ def test_create_or_update_data_type_file_not_found(client): } } - response = client.post('/spectra_type_api/data_type', json=new_data_type) + 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" - test_json = original_test_json + if os.path.exists(data_type_json_path): + os.remove(data_type_json_path) + +def test_delete_data_type(client): + # create a new data type + new_data_type = { + "new_data_type": { + "FLUORESCENCE": "FLUORESCENCE SPECTRUM" + } + } + 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') + assert response.status_code == 200 +