Skip to content

Commit

Permalink
Add tests and add delete data type endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
f-idiris committed Sep 25, 2023
1 parent 8634fc6 commit cbe0786
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
8 changes: 5 additions & 3 deletions chem_spectra/controller/spectra_layout_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return jsonify(existing_data_types["datatypes"]), 200
13 changes: 12 additions & 1 deletion chem_spectra/controller/spectra_type_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return jsonify({"message": "Data type created/updated 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
23 changes: 23 additions & 0 deletions tests/controller/test_spectra_layout_api.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 22 additions & 7 deletions tests/controller/test_spectra_type_api.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -34,19 +34,34 @@ 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": {
"RAMAN SPECTRUM": "RAMAN"
}
}

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

0 comments on commit cbe0786

Please sign in to comment.