From 8634fc697859da675e25c28edacf0c87a4009e01 Mon Sep 17 00:00:00 2001 From: Fathia Idiris Date: Fri, 22 Sep 2023 16:16:50 +0200 Subject: [PATCH] Add tests: spectra type API --- tests/controller/test_spectra_type_api.py | 52 +++++++++++++++++++++++ tests/fixtures/test_data_types.json | 6 +++ 2 files changed, 58 insertions(+) create mode 100644 tests/controller/test_spectra_type_api.py create mode 100644 tests/fixtures/test_data_types.json diff --git a/tests/controller/test_spectra_type_api.py b/tests/controller/test_spectra_type_api.py new file mode 100644 index 00000000..1f4a3c31 --- /dev/null +++ b/tests/controller/test_spectra_type_api.py @@ -0,0 +1,52 @@ +import os +import json + +test_json = './tests/fixtures/test_data_types.json' + +def test_create_or_update_data_type(client): + new_data_type = { + "new_data_type": { + "MASS SPECTRUM": "MS" + } + } + + response = client.post('/spectra_type_api/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" + +def test_create_or_update_data_type_unchanged(client): + # data type already exists in JSON + new_data_type = { + "new_data_type": { + "INFRARED SPECTRUM": "INFRARED" + } + } + + response = client.post('/spectra_type_api/data_type', json=new_data_type) + assert response.status_code == 400 + + response_data = response.json() + + 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): + original_test_json = test_json + test_json = './tests/fixtures/non_existent_test_data.json' + + new_data_type = { + "new_data_type": { + "RAMAN SPECTRUM": "RAMAN" + } + } + + response = client.post('/spectra_type_api/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 diff --git a/tests/fixtures/test_data_types.json b/tests/fixtures/test_data_types.json new file mode 100644 index 00000000..dc885e85 --- /dev/null +++ b/tests/fixtures/test_data_types.json @@ -0,0 +1,6 @@ +{ + "datatypes": { + "NMRSPECTRUM": "NMR", + "INFRARED SPECTRUM": "INFRARED" + } +}