Skip to content
Snippets Groups Projects
Commit e66ff1f7 authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

add: more unit tests

parent f4662b24
No related branches found
No related tags found
1 merge request!16Clhp map into main
Showing
with 43 additions and 31 deletions
No preview for this file type
......@@ -11,7 +11,7 @@ from server.scheduler import update_scheduler
from server.consts import LAKE_RELATIONS_PATH
from map_handler.get_lake_relation import get_map_data_handler
from map_handler.input_new_data import input_new_Lidar_data
from map_handler.update_measurements import update_measurements, addTestData
from map_handler.update_measurements import update_measurements_handler, addTestData
app = Flask(__name__)
terminate_server = 0
......@@ -82,7 +82,7 @@ class IceHTTP(BaseHTTPRequestHandler):
lake_name = unquote(lake_name_param) # Decode url param
if lake_name_param:
update_measurements(self, lake_name)
update_measurements_handler(self, lake_name)
else:
self.send_response(400)
self.send_header('Content-type', 'application/json')
......
No preview for this file type
No preview for this file type
......@@ -129,10 +129,8 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str):
return 200, feature_collection
except FileNotFoundError as e:
print(f"Failed to find the map file: {e}")
return 404, f"Failed to find the map file: {e}"
except Exception as e:
print(f"Error in adding new map: {e}")
return 500, f"Error in adding new map: {e}"
......
{"test-field1": "test-value1", "test-field2": ["test-value2", "testvalue3"]}
\ No newline at end of file
No preview for this file type
import os
import json
from shapely.geometry import Polygon, LineString
......@@ -37,12 +38,18 @@ def test_write_json_to_file() -> None:
"testvalue3"
],
}
test_path = LAKE_RELATIONS_PATH + '/' + test_lake_name + '_div.json'
# Call the function that is being tested
write_json_to_file(test_lake_name, test_data)
# Try to read the data from the newly created file
with open(LAKE_RELATIONS_PATH + '/' + test_lake_name + '_div.json', 'r') as f:
with open(test_path, 'r') as f:
result = json.load(f)
assert result == test_data
# Remove the test file
os.remove(test_path)
from server.map_handler.update_measurements import update_measurements
def test_update_measurements() -> None:
test_lake_name = "test_lake"
status_code, _ = update_measurements(test_lake_name)
assert status_code == 404
......@@ -7,15 +7,29 @@ from server.consts import LAKE_RELATIONS_PATH
from server.ModelFromNVE.icemodellingscripts.getIceThicknessLakes import get_raw_dates, ice_prognosis_raw_data
def update_measurements(self, lake_name: str):
def update_measurements_handler(self, lake_name: str):
status_code, measurement_data = update_measurements(lake_name)
self.send_response(status_code)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(measurement_data.encode('utf-8'))
def update_measurements(lake_name: str) -> (int, str):
"""
Retrieves LiDar data for a given lake, and adds weather data to each subdivision.
Parameters:
self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler
lake_name (str): The name of the requested lake
"""
try:
# Return immediately if an invalid lake name was provided
if not os.path.exists(LAKE_RELATIONS_PATH + lake_name + "_div.json"):
print("The system lake does not exist")
return 404, f"{lake_name} does not exists in the system"
# Define file path to lidar data file
file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json')
......@@ -115,27 +129,13 @@ def update_measurements(self, lake_name: str):
with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_measurements.json', 'w') as f:
json.dump(measurements, f, indent=4)
if self is not None:
# Convert list of dictionaries to JSON
response_data = json.dumps(measurements, indent=4)
# Set headers
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# Write processed data to response object
self.wfile.write(response_data.encode('utf-8'))
# Convert list of dictionaries to JSON
response_data = json.dumps(measurements, indent=4)
# Set headers
return 200, response_data
except Exception as e:
print(f"Error in updating measurements: {e}")
if self is not None:
# Set headers
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(f"Error in updating measurements: {e}".encode('utf-8'))
return 500, f"Error in updating measurements: {e}".encode('utf-8')
def fill_remaining_subdivisions(lake_name: str, processed_ids: list):
......@@ -171,7 +171,7 @@ def fill_remaining_subdivisions(lake_name: str, processed_ids: list):
if len(ice_stats) > 0 and len(ice_stats[0]) > 0:
total_ice_thickness = ice_stats[0]['Total ice (m)']
accuracy = 1
else: # Initialise empty ice stats
else: # Initialise empty ice stats
ice_stats = {
"Date": "NA",
"Slush ice (m)": 0,
......@@ -252,7 +252,6 @@ def addTestData(self, lake_name: str):
# Create 10 subdivisions for each measurement, with randomized coordinates and thicknesses
for subdiv_id in range(30):
subdivision = {
"SubdivID": sub_div_id,
"MinThickness": round(random.uniform(4, 20), 1),
......
......@@ -2,14 +2,14 @@ import json
import time
import schedule
from map_handler.update_measurements import update_measurements
from map_handler.update_measurements import update_measurements_handler
from server.consts import LAKE_RELATIONS_PATH
def update_all_measurements(lake_names: list):
"""Loops through all lake names and calls get_measurements() on each lake"""
for lake in lake_names:
update_measurements(None, lake)
update_measurements_handler(None, lake)
def update_scheduler():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment