diff --git a/.coverage b/.coverage index 947808bfdf95dfe02c7c8f3a2eb95b8642601207..33e83fadb35bea307b53549f033bd58858f40a41 100644 Binary files a/.coverage and b/.coverage differ diff --git a/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc b/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc index 0fd58c6d138d4c59f692b857fd1258738781ea3b..f00723a019e00b75f19812ef8cd73a543fdbf61b 100644 Binary files a/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc and b/server/map_handler/__pycache__/get_lake_relation.cpython-311.pyc differ diff --git a/server/map_handler/get_lake_relation.py b/server/map_handler/get_lake_relation.py index 2b820eb07048155c7bd393c6016298eeeaf71362..220ebf52b804c072d792a4a86763461f88c3378b 100644 --- a/server/map_handler/get_lake_relation.py +++ b/server/map_handler/get_lake_relation.py @@ -1,13 +1,23 @@ from server.consts import LAKE_RELATIONS_PATH -# Writes contents of a lake json file to the response def get_map_data(self, file_name: str, measurement: bool): + status_code, map_data = fetch_data(file_name, measurement) + + # Set HTTP headers + self.send_response(status_code) + self.send_header("Content-type", "application/json") + self.end_headers() + + # Write contents of the JSON file to response + self.wfile.write(map_data.encode('utf-8')) + + +def fetch_data(file_name: str, measurement: bool) -> (int, str): """ - Reads a map file and writes its contents to the response object. + Returns the contents of the requested map file. Parameters: - self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler file_name (str): The name of the requested file/lake measurement (bool): Whether the file is of type _measurements.json or _div.json """ @@ -21,19 +31,10 @@ def get_map_data(self, file_name: str, measurement: bool): with open(LAKE_RELATIONS_PATH + file_name + file_type, "r") as file: data = file.read() - # Set HTTP headers - self.send_response(200) - self.send_header("Content-type", "application/json") - self.end_headers() + return 200, data - # Write contents of the JSON file to response - self.wfile.write(data.encode('utf-8')) except FileNotFoundError: - self.send_response(404) - self.send_header("Content-type", "application/json") - self.end_headers() + return 404, [] except Exception: - self.send_response(500) - self.send_header("Content-type", "application/json") - self.end_headers() + return 500, [] diff --git a/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-311-pytest-8.2.0.pyc b/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-311-pytest-8.2.0.pyc new file mode 100644 index 0000000000000000000000000000000000000000..498461db33caa3aaa66d942e959682911a937c5d Binary files /dev/null and b/server/map_handler/unit_tests/__pycache__/test_get_lake_relation.cpython-311-pytest-8.2.0.pyc differ diff --git a/server/map_handler/unit_tests/test_get_lake_relation.py b/server/map_handler/unit_tests/test_get_lake_relation.py new file mode 100644 index 0000000000000000000000000000000000000000..26ba06b5db5730415a5d7ac220c1af35ec066362 --- /dev/null +++ b/server/map_handler/unit_tests/test_get_lake_relation.py @@ -0,0 +1,25 @@ +from server.map_handler.get_lake_relation import fetch_data + + +def test_fetch_data_true() -> None: + test_lake_name = "Mjøsa" + + status_code, _ = fetch_data(test_lake_name, True) + + assert status_code == 200 + + +def test_fetch_data_false() -> None: + test_lake_name = "Mjøsa" + + status_code, _ = fetch_data(test_lake_name, False) + + assert status_code == 200 + + +def test_fetch_data_no_lake() -> None: + test_lake_name = "non-existent-lake" + + status_code, _ = fetch_data(test_lake_name, False) + + assert status_code == 404