diff --git a/tests/test_courses.py b/tests/test_courses.py index 9a29803e..cb2fd309 100644 --- a/tests/test_courses.py +++ b/tests/test_courses.py @@ -4,79 +4,87 @@ from flask import json from flask.testing import FlaskClient +import pytest -def test_courses_route_with_input_name(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "request_json, expected_status, expected_response", + [ + ( + {"input": "data"}, + 200, + [{"code": "CSCI4390", "name": "Data Mining"}], + ), + ( + {"input": "cs"}, + 200, + [ + {"code": "CSCI2300", "name": "Introduction to Algorithms"}, + {"code": "CSCI2961", "name": "Rensselaer Center for Open Source"}, + {"code": "CSCI4390", "name": "Data Mining"}, + {"code": "CSCI4430", "name": "Programming Languages"}, + ], + ), + (None, 400, None), + ({"wrong": "wrong"}, 400, None), + ({"input": "not found"}, 404, None), + ], +) +def test_courses_route( + test_client: FlaskClient, request_json, expected_status, expected_response +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/courses' page is requested (GET) - THEN check that the response is valid + WHEN the '/courses' page is requested (GET) with various inputs + THEN check that the response status and data are as expected """ - response = test_client.get("/courses", json={"input": "data"}) - - assert response.status_code == 200 + response = ( + test_client.get("/courses", json=request_json) + if request_json + else test_client.get("/courses") + ) - json_data = json.loads(response.data) + assert response.status_code == expected_status - assert json_data[0]["code"] == "CSCI4390" - assert json_data[0]["name"] == "Data Mining" + if expected_response is not None: + json_data = json.loads(response.data) + if expected_status == 200: + assert json_data == expected_response + else: + assert json_data is not None -def test_courses_route_with_input_code(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "input_name, course_data", + [ + ( + "cs", + ( + ("CSCI2300", "CSCI2961", "CSCI4390", "CSCI4430"), + ( + "Introduction to Algorithms", + "Rensselaer Center for Open Source", + "Data Mining", + "Programming Languages", + ), + ), + ) + ], +) +def test_courses_route_with_specific_input( + test_client: FlaskClient, input_name, course_data +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/courses' page is requested (GET) - THEN check that the response is valid + WHEN the '/courses' page is requested (GET) with specific course input names + THEN check that the response data matches the expected courses """ - response = test_client.get("/courses", json={"input": "cs"}) + response = test_client.get("/courses", json={"input": input_name}) assert response.status_code == 200 json_data = json.loads(response.data) - course_data = ( - ("CSCI2300", "CSCI2961", "CSCI4390", "CSCI4430"), - ( - "Introduction to Algorithms", - "Rensselaer Center for Open Source", - "Data Mining", - "Programming Languages", - ), - ) - - for i, major in enumerate(json_data): - assert major["code"] == course_data[0][i] - assert major["name"] == course_data[1][i] - - -def test_courses_route_no_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/courses' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/courses") - - assert response.status_code == 400 - - -def test_courses_route_incorrect_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/courses' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/courses", json={"wrong": "wrong"}) - - assert response.status_code == 400 - - -def test_courses_not_found(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/courses' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/courses", json={"input": "not found"}) - - assert response.status_code == 404 + for i, course in enumerate(json_data): + assert course["code"] == course_data[0][i] + assert course["name"] == course_data[1][i] diff --git a/tests/test_departments.py b/tests/test_departments.py index 7f64ac18..fbeb88a8 100644 --- a/tests/test_departments.py +++ b/tests/test_departments.py @@ -4,140 +4,150 @@ from flask import json from flask.testing import FlaskClient +import pytest -def test_departments_route(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/departments' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/departments") - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - rpi_departments_data = ( - ( - "Computer Science", - "Biology", - "Materials Engineering", - "Math", - "Environmental Engineering", - "Aerospace Engineering", - "Areonautical Engineering", - ), - ( - "DS", - "life", - "also pretty cool", - "quick maths", - "water", - "space, the final frontier", - "flying, need for speed", - ), +@pytest.mark.parametrize( + "endpoint, request_json, expected_status, expected_response_checks", + [ ( - "School of science", - "School of science", - "School of engineering", - "School of science", - "School of engineering", - "School of engineering", - "School of engineering", + "/departments", + None, + 200, + [ + { + "field": "name", + "values": [ + "Computer Science", + "Biology", + "Materials Engineering", + "Math", + "Environmental Engineering", + "Aerospace Engineering", + "Areonautical Engineering", + ], + }, + { + "field": "description", + "values": [ + "DS", + "life", + "also pretty cool", + "quick maths", + "water", + "space, the final frontier", + "flying, need for speed", + ], + }, + { + "field": "school_id", + "values": [ + "School of science", + "School of science", + "School of engineering", + "School of science", + "School of engineering", + "School of engineering", + "School of engineering", + ], + }, + { + "field": "id", + "values": ["CSCI", "BIOL", "MTLE", "MATH", "ENVI", "MANE", "MANE"], + }, + { + "field": "image", + "values": [ + "https://cdn-icons-png.flaticon.com/512/5310/5310672.png" + ] + * 7, + }, + {"field": "webcite", "values": ["https://www.rpi.edu"] * 7}, + ], ), ( - "CSCI", - "BIOL", - "MTLE" "MATH", - "ENVI", - "MANE", - "MANE", + "/department", + {"department": "Computer Science"}, + 200, + [ + {"field": "name", "values": ["Computer Science"]}, + {"field": "description", "values": ["DS"]}, + {"field": "school_id", "values": ["School of Science"]}, + {"field": "id", "values": ["CSCI"]}, + { + "field": "image", + "values": [ + "https://cdn-icons-png.flaticon.com/512/5310/5310672.png" + ], + }, + {"field": "webcite", "values": ["https://www.rpi.edu"]}, + { + "field": "professors", + "subfields": [ + { + "subfield": "name", + "values": [ + "Duy Le", + "Rafael", + "Turner", + "Kuzmin", + "Goldschmidt", + ], + }, + { + "subfield": "rcs_id", + "values": ["led", "cenzar", "turner", "kuzmin", "goldd"], + }, + ], + }, + { + "field": "opportunities", + "subfields": [ + {"subfield": "id", "values": [1, 2]}, + { + "subfield": "name", + "values": [ + "Automated Cooling System", + "Iphone 15 durability test", + ], + }, + ], + }, + ], ), - ( - "https://cdn-icons-png.flaticon.com/512/5310/5310672.png", - "https://cdn-icons-png.flaticon.com/512/5310/5310672.png", - "https://cdn-icons-png.flaticon.com/512/5310/5310672.png" - "https://cdn-icons-png.flaticon.com/512/5310/5310672.png", - "https://cdn-icons-png.flaticon.com/512/5310/5310672.png", - "https://cdn-icons-png.flaticon.com/512/5310/5310672.png", - "https://cdn-icons-png.flaticon.com/512/5310/5310672.png", - ), - ( - "https://www.rpi.edu", - "https://www.rpi.edu", - "https://www.rpi.edu", - "https://www.rpi.edu", - "https://www.rpi.edu", - "https://www.rpi.edu", - "https://www.rpi.edu", - ), - ) - - for department in json_data: - assert department["name"] in rpi_departments_data[0] - assert department["description"] in rpi_departments_data[1] - # Added - assert department["school_id"] in rpi_departments_data[2] - assert department["id"] in rpi_departments_data[3] - assert department["image"] in rpi_departments_data[4] - assert department["webcite"] in rpi_departments_data[5] - - -def test_department_route(test_client: FlaskClient) -> None: + ("/department", None, 400, None), + ("/department", {"wrong": "wrong"}, 400, None), + ], +) +def test_department_routes( + test_client: FlaskClient, + endpoint, + request_json, + expected_status, + expected_response_checks, +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/department' page is requested (GET) - THEN check that the response is valid + WHEN various '/departments' or '/department' routes are requested (GET) + THEN check that the response status and data are as expected """ - response = test_client.get("/department", json={"department": "Computer Science"}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - assert json_data["name"] == "Computer Science" - assert json_data["description"] == "DS" - assert json_data["school_id"] == "School of Science" - # Added - assert json_data["id"] == "CSCI" - assert ( - json_data["image"] == "https://cdn-icons-png.flaticon.com/512/5310/5310672.png" + response = ( + test_client.get(endpoint, json=request_json) + if request_json + else test_client.get(endpoint) ) - assert json_data["webcite"] == "https://www.rpi.edu" - - prof_names = ["Duy Le", "Rafael", "Turner", "Kuzmin", "Goldschmidt"] - prof_rcs_ids = ["led", "cenzar", "turner", "kuzmin", "goldd"] - - for prof in json_data["professors"]: - assert prof["name"] in prof_names - assert prof["rcs_id"] in prof_rcs_ids - - opportunity_ids = [1, 2] - opportunity_names = ["Automated Cooling System", "Iphone 15 durability test"] - - for opportunity in json_data["opportunities"]: - assert opportunity["id"] in opportunity_ids - assert opportunity["name"] in opportunity_names - - -def test_department_route_no_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/department' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/department") - - assert response.status_code == 400 - - -def test_department_route_incorrect_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/department' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/department", json={"wrong": "wrong"}) - - assert response.status_code == 400 + assert response.status_code == expected_status + + if expected_response_checks: + json_data = json.loads(response.data) + + for check in expected_response_checks: + if "subfields" not in check: + for item in json_data: + assert item[check["field"]] in check["values"] + else: + for item in json_data.get(check["field"], []): + for subfield_check in check["subfields"]: + assert ( + item[subfield_check["subfield"]] in subfield_check["values"] + ) diff --git a/tests/test_errors.py b/tests/test_errors.py index 388aa8b9..d09edc6e 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -4,27 +4,30 @@ from flask import json from flask.testing import FlaskClient +import pytest -def test_404_page(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "route, expected_status, expected_response", + [ + ("/abcsd", 404, {"error": "404 not found"}), + ( + "/500", + 500, + { + "error": "500 server error. You can report issues here: https://github.com/RafaelCenzano/LabConnect/issues" + }, + ), + ], +) +def test_error_pages( + test_client: FlaskClient, route, expected_status, expected_response +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/abcsd' page is requested (GET) - THEN check that the response is the 404 page + WHEN the specified error route is requested (GET) + THEN check that the response status and data are as expected """ - response = test_client.get("/abcsd") - assert response.status_code == 404 - assert {"error": "404 not found"} == json.loads(response.data) - - -def test_500_page(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/professor/' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/500") - assert response.status_code == 500 - assert { - "error": "500 server error. You can report issues here: https://github.com/RafaelCenzano/LabConnect/issues" - } == json.loads(response.data) + response = test_client.get(route) + assert response.status_code == expected_status + assert json.loads(response.data) == expected_response diff --git a/tests/test_general.py b/tests/test_general.py index e472206a..71d19ee0 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -4,6 +4,7 @@ from flask import json from flask.testing import FlaskClient +import pytest def test_home_page(test_client: FlaskClient) -> None: @@ -15,7 +16,6 @@ def test_home_page(test_client: FlaskClient) -> None: response = test_client.get("/") assert response.status_code == 200 - assert {"Hello": "There"} == json.loads(response.data) @@ -26,9 +26,10 @@ def test_discover_route(test_client: FlaskClient) -> None: THEN check that the response is valid """ response = test_client.get("/discover") - # data = json.loads(response.data.decode("utf-8")) + assert response.status_code == 200 - # print(data) + # Uncomment and modify the following line with expected response data + # data = json.loads(response.data.decode("utf-8")) # assert data["data"][0] == { # "title": "Nelson", # "major": "CS", @@ -37,23 +38,51 @@ def test_discover_route(test_client: FlaskClient) -> None: # } -def test_profile_page(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "input_id, expected_profile", + [ + ( + 1, + { + "id": "cenzar", + "first_name": "Rafael", + "opportunities": [...], # Replace with expected opportunities data + }, + ) + ], +) +def test_profile_page(test_client: FlaskClient, input_id, expected_profile) -> None: """ GIVEN a Flask application configured for testing WHEN the '/profile/' page is requested (GET) THEN check that the response is valid """ - response = test_client.get("/profile", json={"id": 1}) + response = test_client.get("/profile", json={"id": input_id}) assert response.status_code == 200 json_data = json.loads(response.data) - assert json_data["id"] == "cenzar" - assert json_data["first_name"] == "Rafael" + assert json_data["id"] == expected_profile["id"] + assert json_data["first_name"] == expected_profile["first_name"] assert json_data["opportunities"] != [] -def test_schools_route(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "expected_schools", + [ + ( + ( + "School of Science", + "School of Engineering", + ), + ( + "the coolest of them all", + "also pretty cool", + ), + ) + ], +) +def test_schools_route(test_client: FlaskClient, expected_schools) -> None: """ GIVEN a Flask application configured for testing WHEN the '/schools' page is requested (GET) @@ -65,14 +94,9 @@ def test_schools_route(test_client: FlaskClient) -> None: json_data = json.loads(response.data) - rpi_schools_data = ( - ("School of Science", "School of Engineering"), - ("the coolest of them all", "also pretty cool"), - ) - for school in json_data: - assert school["name"] in rpi_schools_data[0] - assert school["description"] in rpi_schools_data[1] + assert school["name"] in expected_schools[0] + assert school["description"] in expected_schools[1] def test_years_route(test_client: FlaskClient) -> None: @@ -84,20 +108,22 @@ def test_years_route(test_client: FlaskClient) -> None: response = test_client.get("/years") assert response.status_code == 200 - assert [2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031] == json.loads(response.data) def test_professor_profile(test_client: FlaskClient) -> None: - + """ + GIVEN a Flask application configured for testing + WHEN the '/getProfessorProfile/' page is requested (GET) + THEN check that the response is valid + """ response = test_client.get("/getProfessorProfile/1") + assert response.status_code == 200 # Load the response data as JSON data = json.loads(response.data) - print(data) - # Test that the "name" key exists assert data["first_name"] == "Rafael" assert data["last_name"] == "Cenzano" assert data["preferred_name"] == "Raf" diff --git a/tests/test_lab_manager.py b/tests/test_lab_manager.py index 7e288834..5d54c2cd 100644 --- a/tests/test_lab_manager.py +++ b/tests/test_lab_manager.py @@ -1,118 +1,100 @@ """ -Test lab manager routes +Test lab manager routes with parameterization """ +import pytest from flask import json from flask.testing import FlaskClient -def test_lab_manager_route_with_input_id(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/lab_manager' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/lab_manager", json={"rcs_id": "cenzar"}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - cenzar_data = { - "website": None, - "rcs_id": "cenzar", - "name": "Rafael", - "alt_email": None, - "phone_number": None, - "email": None, - "description": None, - } - - assert json_data == cenzar_data - - -def test_lab_manager_route_no_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/lab_manager' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/lab_manager") - - assert response.status_code == 400 - - -def test_lab_manager_route_incorrect_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/lab_manager' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/lab_manager", json={"wrong": "wrong"}) - - assert response.status_code == 400 - - -def test_lab_manager_opportunity_cards(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/lab_manager/opportunities' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/lab_manager/opportunities", json={"rcs_id": "cenzar"}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - lab_manager_opportunities_data = ( +@pytest.mark.parametrize( + "input_json, expected_status, expected_response", + [ ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - "Spring", - 2024, - True, + {"rcs_id": "cenzar"}, + 200, + { + "website": None, + "rcs_id": "cenzar", + "name": "Rafael", + "alt_email": None, + "phone_number": None, + "email": None, + "description": None, + }, ), - ( - "Iphone 15 durability test", - "Scratching the Iphone, drop testing etc.", - "Experienced in getting angry and throwing temper tantrum", - None, - "Spring", - 2024, - True, - ), - ) - - for i, item in enumerate(json_data["cenzar"]): - assert item["name"] == lab_manager_opportunities_data[i][0] - assert item["description"] == lab_manager_opportunities_data[i][1] - assert item["recommended_experience"] == lab_manager_opportunities_data[i][2] - assert item["pay"] == lab_manager_opportunities_data[i][3] - assert item["semester"] == lab_manager_opportunities_data[i][4] - assert item["year"] == lab_manager_opportunities_data[i][5] - assert item["active"] == lab_manager_opportunities_data[i][6] - - -def test_lab_manager_opportunity_cards_no_json(test_client: FlaskClient) -> None: + (None, 400, None), # No input JSON case + ({"wrong": "wrong"}, 400, None), # Incorrect JSON structure case + ], +) +def test_lab_manager_route( + test_client: FlaskClient, input_json, expected_status, expected_response +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/lab_manager/opportunities' page is requested (GET) - THEN check that the response is valid + WHEN the '/lab_manager' page is requested (GET) with different JSON inputs + THEN check that the response matches the expected outcome """ - response = test_client.get("/lab_manager/opportunities") - - assert response.status_code == 400 - - -def test_lab_manager_opportunity_cards_incorrect_json(test_client: FlaskClient) -> None: + response = test_client.get("/lab_manager", json=input_json) + assert response.status_code == expected_status + + if expected_response: + json_data = json.loads(response.data) + assert json_data == expected_response + + +@pytest.mark.parametrize( + "input_json, expected_status", + [ + ({"rcs_id": "cenzar"}, 200), + (None, 400), # No input JSON case + ({"wrong": "wrong"}, 400), # Incorrect JSON structure case + ], +) +def test_lab_manager_opportunity_cards( + test_client: FlaskClient, input_json, expected_status +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/lab_manager/opportunities' page is requested (GET) - THEN check that the response is valid + WHEN the '/lab_manager/opportunities' page is requested (GET) with different JSON inputs + THEN check that the response matches the expected status code """ - response = test_client.get("/lab_manager/opportunities", json={"wrong": "wrong"}) - - assert response.status_code == 400 + response = test_client.get("/lab_manager/opportunities", json=input_json) + assert response.status_code == expected_status + + if input_json == {"rcs_id": "cenzar"} and expected_status == 200: + json_data = json.loads(response.data) + lab_manager_opportunities_data = [ + { + "name": "Automated Cooling System", + "description": "Energy efficient AC system", + "recommended_experience": "Thermodynamics", + "pay": 15.0, + "semester": "Spring", + "year": 2024, + "active": True, + }, + { + "name": "Iphone 15 durability test", + "description": "Scratching the Iphone, drop testing etc.", + "recommended_experience": "Experienced in getting angry and throwing temper tantrum", + "pay": None, + "semester": "Spring", + "year": 2024, + "active": True, + }, + ] + + for i, item in enumerate(json_data["cenzar"]): + assert item["name"] == lab_manager_opportunities_data[i]["name"] + assert ( + item["description"] == lab_manager_opportunities_data[i]["description"] + ) + assert ( + item["recommended_experience"] + == lab_manager_opportunities_data[i]["recommended_experience"] + ) + assert item["pay"] == lab_manager_opportunities_data[i]["pay"] + assert item["semester"] == lab_manager_opportunities_data[i]["semester"] + assert item["year"] == lab_manager_opportunities_data[i]["year"] + assert item["active"] == lab_manager_opportunities_data[i]["active"] diff --git a/tests/test_majors.py b/tests/test_majors.py index d7594743..d7d6df87 100644 --- a/tests/test_majors.py +++ b/tests/test_majors.py @@ -4,9 +4,25 @@ from flask import json from flask.testing import FlaskClient +import pytest -def test_majors_route(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "expected_majors", + [ + ( + ("CSCI", "ECSE", "BIOL", "MATH", "COGS"), + ( + "Computer Science", + "Electrical, Computer, and Systems Engineering", + "Biological Science", + "Mathematics", + "Cognitive Science", + ), + ) + ], +) +def test_majors_route(test_client: FlaskClient, expected_majors) -> None: """ GIVEN a Flask application configured for testing WHEN the '/majors' page is requested (GET) @@ -18,68 +34,75 @@ def test_majors_route(test_client: FlaskClient) -> None: json_data = json.loads(response.data) - majors_data = ( - ("CSCI", "ECSE", "BIOL", "MATH", "COGS"), - ( - "Computer Science", - "Electrical, Computer, and Systems Engineering", - "Biological Science", - "Mathematics", - "Cognitive Science", - ), - ) - for major in json_data: - assert major["code"] in majors_data[0] - assert major["name"] in majors_data[1] + assert major["code"] in expected_majors[0] + assert major["name"] in expected_majors[1] -def test_majors_route_with_input_name(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "input_data, expected_majors", + [ + ( + {"input": "computer"}, + ( + ("CSCI", "ECSE"), + ( + "Computer Science", + "Electrical, Computer, and Systems Engineering", + ), + ), + ), + ], +) +def test_majors_route_with_input_name( + test_client: FlaskClient, input_data, expected_majors +) -> None: """ GIVEN a Flask application configured for testing WHEN the '/majors' page is requested (GET) THEN check that the response is valid """ - response = test_client.get("/majors", json={"input": "computer"}) + response = test_client.get("/majors", json=input_data) assert response.status_code == 200 json_data = json.loads(response.data) - majors_data = ( - ("CSCI", "ECSE"), - ( - "Computer Science", - "Electrical, Computer, and Systems Engineering", - ), - ) - for i, major in enumerate(json_data): - assert major["code"] == majors_data[0][i] - assert major["name"] == majors_data[1][i] + assert major["code"] == expected_majors[0][i] + assert major["name"] == expected_majors[1][i] -def test_majors_route_with_input_code(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "input_data, expected_majors", + [ + ( + {"input": "cs"}, + ( + ("CSCI", "ECSE", "MATH"), + ( + "Computer Science", + "Electrical, Computer, and Systems Engineering", + "Mathematics", + ), + ), + ), + ], +) +def test_majors_route_with_input_code( + test_client: FlaskClient, input_data, expected_majors +) -> None: """ GIVEN a Flask application configured for testing WHEN the '/majors' page is requested (GET) THEN check that the response is valid """ - response = test_client.get("/majors", json={"input": "cs"}) + response = test_client.get("/majors", json=input_data) assert response.status_code == 200 json_data = json.loads(response.data) - majors_data = ( - ("CSCI", "ECSE", "MATH"), - ( - "Computer Science", - "Electrical, Computer, and Systems Engineering", - "Mathematics", - ), - ) - for i, major in enumerate(json_data): - assert major["code"] == majors_data[0][i] - assert major["name"] == majors_data[1][i] + assert major["code"] == expected_majors[0][i] + assert major["name"] == expected_majors[1][i] diff --git a/tests/test_opportunities_filtering.py b/tests/test_opportunities_filtering.py index 9297cf75..1c067014 100644 --- a/tests/test_opportunities_filtering.py +++ b/tests/test_opportunities_filtering.py @@ -2,284 +2,104 @@ Test opportunity filtering routes """ +import pytest from flask import json from flask.testing import FlaskClient -def test_opportunity_filter_pay(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "filters, expected_opportunities", + [ + ( + [{"field": "pay", "value": {"min": 14.9, "max": 21}}], + ["Automated Cooling System"], + ), + ( + [{"field": "departments", "value": ["Material Science"]}], + ["Checking out cubes"], + ), + ( + [ + { + "field": "departments", + "value": ["Computer Science", "Material Science"], + } + ], + [ + "Iphone 15 durability test", + "Checking out cubes", + "Automated Cooling System", + ], + ), + ( + [{"field": "majors", "value": ["BIOL"]}], + [ + "Iphone 15 durability test", + "Checking out cubes", + "Automated Cooling System", + ], + ), + ( + [{"field": "majors", "value": ["CSCI", "BIOL"]}], + [ + "Iphone 15 durability test", + "Checking out cubes", + "Automated Cooling System", + ], + ), + ( + [{"field": "credits", "value": [1]}], + ["Iphone 15 durability test", "Checking out cubes"], + ), + ( + [{"field": "credits", "value": [2, 4]}], + [ + "Iphone 15 durability test", + "Checking out cubes", + "Automated Cooling System", + "Test the water", + ], + ), + ([{"field": "class_year", "value": [2025]}], ["Iphone 15 durability test"]), + ( + [{"field": "class_year", "value": [2025, 2027]}], + ["Iphone 15 durability test", "Automated Cooling System"], + ), + ([{"field": "location", "value": "Remote"}], ["Automated Cooling System"]), + ( + [{"field": "location", "value": "In-Person"}], + ["Iphone 15 durability test", "Checking out cubes", "Test the water"], + ), + ( + [ + {"field": "location", "value": "In-Person"}, + {"field": "departments", "value": ["Computer Science"]}, + ], + ["Iphone 15 durability test"], + ), + ( + [ + {"field": "credits", "value": [2, 4]}, + {"field": "departments", "value": ["Computer Science"]}, + ], + ["Iphone 15 durability test", "Automated Cooling System"], + ), + ], +) +def test_opportunity_filter( + test_client: FlaskClient, filters, expected_opportunities +) -> None: """ GIVEN a Flask application configured for testing WHEN the '/opportunity/filter' page is requested (GET) THEN check that the response is valid """ - - json_data = {"filters": [{"field": "pay", "value": {"min": 14.9, "max": 21}}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - assert "Automated Cooling System" == json_data[0]["name"] - - -def test_opportunity_filter_department(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = {"filters": [{"field": "departments", "value": ["Material Science"]}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - assert "Checking out cubes" == json_data[0]["name"] - - -def test_opportunity_filter_departments(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = { - "filters": [ - {"field": "departments", "value": ["Computer Science", "Material Science"]} - ] - } - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ( - "Iphone 15 durability test", - "Checking out cubes", - "Automated Cooling System", - ) - - for data in json_data: - assert data["name"] in opportunities - - -def test_opportunity_filter_major(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = {"filters": [{"field": "majors", "value": ["BIOL"]}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ( - "Iphone 15 durability test", - "Checking out cubes", - "Automated Cooling System", - ) - - for data in json_data: - assert data["name"] in opportunities - - -def test_opportunity_filter_majors(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = {"filters": [{"field": "majors", "value": ["CSCI", "BIOL"]}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ( - "Iphone 15 durability test", - "Checking out cubes", - "Automated Cooling System", - ) - - for data in json_data: - assert data["name"] in opportunities - - -def test_opportunity_filter_credits(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = {"filters": [{"field": "credits", "value": [1]}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ( - "Iphone 15 durability test", - "Checking out cubes", - ) - - for data in json_data: - assert data["name"] in opportunities - - json_data = {"filters": [{"field": "credits", "value": [2, 4]}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ( - "Iphone 15 durability test", - "Checking out cubes", - "Automated Cooling System", - "Test the water", - ) - - for data in json_data: - assert data["name"] in opportunities - - -def test_opportunity_filter_class_years(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = {"filters": [{"field": "class_year", "value": [2025]}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ("Iphone 15 durability test",) - - for data in json_data: - assert data["name"] in opportunities - - json_data = {"filters": [{"field": "class_year", "value": [2025, 2027]}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ( - "Iphone 15 durability test", - "Automated Cooling System", - ) - - for data in json_data: - assert data["name"] in opportunities - - -def test_opportunity_filter_location_remote(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = {"filters": [{"field": "location", "value": "Remote"}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - assert "Automated Cooling System" == json_data[0]["name"] - - -def test_opportunity_filter_location_in_person(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = {"filters": [{"field": "location", "value": "In-Person"}]} - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ( - "Iphone 15 durability test", - "Checking out cubes", - "Test the water", - ) - - for data in json_data: - assert data["name"] in opportunities - - -def test_opportunity_filter_location_departments(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = { - "filters": [ - {"field": "location", "value": "In-Person"}, - {"field": "departments", "value": ["Computer Science"]}, - ] - } - response = test_client.get("/opportunity/filter", json=json_data) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - opportunities = ("Iphone 15 durability test",) - - for data in json_data: - assert data["name"] in opportunities - - -def test_opportunity_filter_credits_departments(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity/filter' page is requested (GET) - THEN check that the response is valid - """ - - json_data = { - "filters": [ - {"field": "credits", "value": [2, 4]}, - {"field": "departments", "value": ["Computer Science"]}, - ] - } + json_data = {"filters": filters} response = test_client.get("/opportunity/filter", json=json_data) assert response.status_code == 200 json_data = json.loads(response.data) - opportunities = ("Iphone 15 durability test", "Automated Cooling System") - for data in json_data: - assert data["name"] in opportunities - - -# TODO: Add test for no fields + assert data["name"] in expected_opportunities diff --git a/tests/test_opportunity.py b/tests/test_opportunity.py index e37f9545..bd0e9da4 100644 --- a/tests/test_opportunity.py +++ b/tests/test_opportunity.py @@ -1,305 +1,155 @@ -""" -Test opportunity routes -""" - import json - -from flask import json +import pytest from flask.testing import FlaskClient -from labconnect import db -from labconnect.helpers import OrJSONProvider, SemesterEnum -from labconnect.models import ( - ClassYears, - Courses, - LabManager, - Leads, - Majors, - Opportunities, - RecommendsClassYears, - RecommendsCourses, - RecommendsMajors, - RPIDepartments, - RPISchools, -) - -def test_get_opportunity(test_client: FlaskClient) -> None: +def test_get_opportunity_parametrized(test_client: FlaskClient): """ GIVEN a Flask application configured for testing - WHEN the '/opportunity' page is requested (GET) - THEN check that the response is valid + WHEN the '/opportunity' page is requested (GET) with different IDs + THEN check that the responses are valid """ - response1 = test_client.get("/opportunity", json={"id": 1}) - response2 = test_client.get("/opportunity", json={"id": 2}) - - assert response1.status_code == 200 - assert response2.status_code == 200 - - json_data1 = json.loads(response1.data) - json_data2 = json.loads(response2.data) - - lab_manager_opportunities_data = ( + test_cases = [ ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - False, - False, - False, - True, - "Spring", - 2024, - True, + 1, + { + "name": "Automated Cooling System", + "description": "Energy efficient AC system", + "recommended_experience": "Thermodynamics", + "pay": 15.0, + "one_credit": False, + "two_credits": False, + "three_credits": False, + "four_credits": True, + "semester": "Spring", + "year": 2024, + "active": True, + }, ), ( - "Iphone 15 durability test", - "Scratching the Iphone, drop testing etc.", - "Experienced in getting angry and throwing temper tantrum", - None, - True, - True, - True, - True, - "Spring", - 2024, - True, + 2, + { + "name": "Iphone 15 durability test", + "description": "Scratching the Iphone, drop testing etc.", + "recommended_experience": "Experienced in getting angry and throwing temper tantrum", + "pay": None, + "one_credit": True, + "two_credits": True, + "three_credits": True, + "four_credits": True, + "semester": "Spring", + "year": 2024, + "active": True, + }, ), - ) - - assert json_data1["name"] == lab_manager_opportunities_data[0][0] - assert json_data1["description"] == lab_manager_opportunities_data[0][1] - assert json_data1["recommended_experience"] == lab_manager_opportunities_data[0][2] - assert json_data1["pay"] == lab_manager_opportunities_data[0][3] - assert json_data1["one_credit"] == lab_manager_opportunities_data[0][4] - assert json_data1["two_credits"] == lab_manager_opportunities_data[0][5] - assert json_data1["three_credits"] == lab_manager_opportunities_data[0][6] - assert json_data1["four_credits"] == lab_manager_opportunities_data[0][7] - assert json_data1["semester"] == lab_manager_opportunities_data[0][8] - assert json_data1["year"] == lab_manager_opportunities_data[0][9] - assert json_data1["active"] == lab_manager_opportunities_data[0][10] + ] - assert json_data2["name"] == lab_manager_opportunities_data[1][0] - assert json_data2["description"] == lab_manager_opportunities_data[1][1] - assert json_data2["recommended_experience"] == lab_manager_opportunities_data[1][2] - assert json_data2["pay"] == lab_manager_opportunities_data[1][3] - assert json_data2["one_credit"] == lab_manager_opportunities_data[1][4] - assert json_data2["two_credits"] == lab_manager_opportunities_data[1][5] - assert json_data2["three_credits"] == lab_manager_opportunities_data[1][6] - assert json_data2["four_credits"] == lab_manager_opportunities_data[1][7] - assert json_data2["semester"] == lab_manager_opportunities_data[1][8] - assert json_data2["year"] == lab_manager_opportunities_data[1][9] - assert json_data2["active"] == lab_manager_opportunities_data[1][10] + for opportunity_id, expected_data in test_cases: + response = test_client.get("/opportunity", json={"id": opportunity_id}) + assert response.status_code == 200 - print(json_data2) + json_data = json.loads(response.data) + for key, value in expected_data.items(): + assert json_data[key] == value -def test_get_opportunity_no_json(test_client: FlaskClient) -> None: +def test_get_opportunity_no_json(test_client: FlaskClient): """ GIVEN a Flask application configured for testing - WHEN the '/opportunity' page is requested (GET) - THEN check that the response is valid + WHEN the '/opportunity' page is requested (GET) without JSON payload + THEN check that the response is 400 """ response = test_client.get("/opportunity") - assert response.status_code == 400 -def test_opportunity_incorrect_json(test_client: FlaskClient) -> None: +def test_opportunity_incorrect_json(test_client: FlaskClient): """ GIVEN a Flask application configured for testing - WHEN the '/opportunity' page is requested (GET) - THEN check that the response is valid + WHEN the '/opportunity' page is requested (GET) with incorrect JSON + THEN check that the response is 400 """ response = test_client.get("/opportunity", json={"wrong": "wrong"}) - assert response.status_code == 400 -def test_get_opportunity_meta(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "endpoint, expected_keys", + [ + ( + "/getOpportunityMeta/1", + [ + "name", + "description", + "recommended_experience", + "pay", + "credits", + "semester", + "year", + "application_due", + "active", + "courses", + "majors", + "years", + ], + ), + ( + "/getOpportunity/2", + [ + "id", + "name", + "description", + "recommended_experience", + "author", + "department", + "aboutSection", + ], + ), + ], +) +def test_opportunity_meta_parametrized( + test_client: FlaskClient, endpoint, expected_keys +): """ GIVEN a Flask application configured for testing - WHEN the '/getOpportunityMeta' endpoint is requested (GET) with valid data - THEN check that the response is valid and contains expected opportunity data + WHEN specific opportunity endpoints are requested + THEN check that the response contains the expected keys """ - - response = test_client.get("/getOpportunityMeta/1", content_type="application/json") - - # assert response.status_code == 200 - - data = json.loads(response.data) - data = data["data"] - - # Assertions on the expected data - assert "name" in data - assert "description" in data - assert "recommended_experience" in data - assert "pay" in data - assert "credits" in data - assert "semester" in data - assert "year" in data - assert "application_due" in data - assert "active" in data - assert "courses" in data - assert "majors" in data - assert "years" in data - assert "active" in data - - -def test_get_opportunity(test_client: FlaskClient) -> None: - response = test_client.get("/getOpportunity/2") - + response = test_client.get(endpoint, content_type="application/json") assert response.status_code == 200 - # Load the response data as JSON - data = json.loads(response.data.decode("utf-8")) - data = data["data"] - - # Test that the "name" key exists - assert "id" in data - assert "name" in data - assert "description" in data - assert "recommended_experience" in data - assert "author" in data - assert "department" in data - assert "aboutSection" in data - - for eachSection in data["aboutSection"]: - assert "title" in eachSection - assert "description" in eachSection - - -def test_get_opportunity_professor(test_client: FlaskClient) -> None: - response = test_client.get("/getOpportunityByProfessor/led") - - assert response.status_code == 200 - - # Load the response data as JSON - data = json.loads(response.data.decode("utf-8")) - data = data["data"] - - # Test that the "name" key exists - for opportunity in data: - assert "id" in opportunity - assert "name" in opportunity - assert "description" in opportunity - assert "recommended_experience" in opportunity - assert "pay" in opportunity - # assert "credits" in opportunity - assert "semester" in opportunity - assert "year" in opportunity - assert "application_due" in opportunity - assert "active" in opportunity - # assert "professor" in opportunity - # assert "department" in opportunity - - -def test_get_professor_opportunity_cards(test_client: FlaskClient) -> None: - response = test_client.get( - "/getProfessorOpportunityCards/led", content_type="application/json" - ) - - assert response.status_code == 200 - - data = json.loads(response.data.decode("utf-8")) - data = data["data"] - - for eachCard in data: - assert "title" in eachCard - assert "body" in eachCard - assert "attributes" in eachCard - assert "id" in eachCard - - -def test_profile_opportunities(test_client: FlaskClient) -> None: - response = test_client.get( - "/getProfileOpportunities/led", content_type="application/json" - ) - - assert response.status_code == 200 - - data = json.loads(response.data.decode("utf-8")) - data = data["data"] - - for eachCard in data: - assert "id" in eachCard - assert "title" in eachCard - assert "body" in eachCard - assert "attributes" in eachCard - assert "activeStatus" in eachCard - - -def test_create_opportunity(test_client: FlaskClient) -> None: + data = json.loads(response.data) + if "data" in data: + data = data["data"] + + for key in expected_keys: + if isinstance(data, list): + for item in data: + assert key in item + else: + assert key in data + + +@pytest.mark.parametrize( + "endpoint", + [ + "/getOpportunityByProfessor/led", + "/getProfessorOpportunityCards/led", + "/getProfileOpportunities/led", + ], +) +def test_professor_related_opportunities(test_client: FlaskClient, endpoint): """ GIVEN a Flask application configured for testing - WHEN the '/createOpportunity' endpoint is requested (POST) with valid data - THEN check that the response is valid and contains expected data + WHEN professor-related endpoints are requested + THEN check that the response contains expected keys in each card """ - - test_data = { - "authorID": "led", - "name": "Some test opportunity", - "description": "Some test description", - "recommended_experience": "Some test experience", - "pay": 25.0, - "credits": ["1", "2", "3", "4"], - "semester": "FALL", - "year": 2024, - "application_due": "2024-03-30", - "active": True, - "courses": ["CSCI4430"], - "majors": ["BIOL"], - "years": [2023, 2024], - "active": True, - "location": "TBD", - } - - response = test_client.post( - "/createOpportunity", - data=json.dumps(test_data), - content_type="application/json", - ) - + response = test_client.get(endpoint, content_type="application/json") assert response.status_code == 200 - # query database to check for new opportunity with the same name - query = db.session.query(Opportunities).filter( - Opportunities.name == "Some test opportunity", - Opportunities.description == "Some test description", - Opportunities.recommended_experience == "Some test experience", - ) - - data = query.first() - assert data is not None - id = data.id - - # delete the opportunity by sending request to deleteOpportunity - response = test_client.post( - "/deleteOpportunity", - data=json.dumps({"id": id}), - content_type="application/json", - ) - - assert response.status_code == 200 - - # check that the opportunity was deleted - query = db.session.query(Opportunities).filter(Opportunities.id == id) - assert query.first() is None - - -def test_professor_opportunity_cards(test_client: FlaskClient) -> None: - - response = test_client.get("/getProfessorOpportunityCards/led") - assert response.status_code == 200 - - # Load the response data as JSON - data = json.loads(response.data.decode("utf-8")) - - # Test that the "name" key exists - assert len(data.keys()) > 0 - for eachCard in data["data"]: - assert "title" in eachCard - assert "body" in eachCard - assert "attributes" in eachCard - assert "id" in eachCard + data = json.loads(response.data)["data"] + for each_card in data: + assert "id" in each_card + assert "title" in each_card or "name" in each_card + assert "body" in each_card or "description" in each_card + assert "attributes" in each_card or "recommended_experience" in each_card diff --git a/tests/test_user.py b/tests/test_user.py index 122a7c63..2b6744e1 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -2,219 +2,157 @@ Test user routes """ +import pytest from flask import json from flask.testing import FlaskClient -def test_user_route_with_input_id_1(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/user' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/user", json={"id": "1"}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - assert json_data["id"] == 1 - assert json_data["first_name"] == "Rafael" - assert json_data["preferred_name"] == "Raf" - assert json_data["last_name"] == "Cenzano" - assert json_data["email"] == "cenzar@rpi.edu" - # Added - assert json_data["description"] == "labconnect is the best RCOS project" - assert ( - json_data["profile_picture"] - == "https://rafael.sirv.com/Images/rafael.jpeg?thumbnail=350&format=webp&q=90" - ) - assert json_data["website"] == "https://rafaelcenzano.com" - # class year - assert json_data["class_year"] == "2025" - # lab manager id - assert json_data["lab_manager_id"] == 1 - - departments_data = [ - {"user_id": 1, "department_id": "Computer Science"}, - {"user_id": 1, "department_id": "Math"}, - ] - - major_data = [ - {"user_id": 1, "major_code": "CSCI"}, - {"user_id": 1, "major_code": "MATH"}, - ] - - course_data = [ - {"in_progress": False, "user_id": 1, "course_code": "CSCI2300"}, - {"in_progress": True, "user_id": 1, "course_code": "CSCI4430"}, - ] - - assert json_data["departments"] == departments_data - assert json_data["majors"] == major_data - assert json_data["courses"] == course_data - - -def test_user_1_opportunity_cards(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/user' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/user", json={"id": 1}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - lab_manager_opportunities_data = ( +@pytest.mark.parametrize( + "input_data, expected_status, expected_output", + [ ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - "Spring", - 2024, - True, + {"id": "1"}, + 200, + { + "id": 1, + "first_name": "Rafael", + "preferred_name": "Raf", + "last_name": "Cenzano", + "email": "cenzar@rpi.edu", + "description": "labconnect is the best RCOS project", + "profile_picture": "https://rafael.sirv.com/Images/rafael.jpeg?thumbnail=350&format=webp&q=90", + "website": "https://rafaelcenzano.com", + "class_year": "2025", + "lab_manager_id": 1, + "departments": [ + {"user_id": 1, "department_id": "Computer Science"}, + {"user_id": 1, "department_id": "Math"}, + ], + "majors": [ + {"user_id": 1, "major_code": "CSCI"}, + {"user_id": 1, "major_code": "MATH"}, + ], + "courses": [ + {"in_progress": False, "user_id": 1, "course_code": "CSCI2300"}, + {"in_progress": True, "user_id": 1, "course_code": "CSCI4430"}, + ], + }, ), ( - "Iphone 15 durability test", - "Scratching the Iphone, drop testing etc.", - "Experienced in getting angry and throwing temper tantrum", - None, - "Spring", - 2024, - True, + {"id": "2"}, + 200, + { + "id": 2, + "first_name": "RCOS", + "preferred_name": None, + "last_name": "RCOS", + "email": "test@rpi.edu", + "description": None, + "profile_picture": "https://www.svgrepo.com/show/206842/professor.svg", + "website": None, + "class_year": None, + "lab_manager_id": None, + "departments": [{"user_id": 2, "department_id": "Computer Science"}], + "majors": [{"user_id": 2, "major_code": "CSCI"}], + "courses": [ + {"in_progress": False, "user_id": 2, "course_code": "CSCI2300"} + ], + }, ), - ) - - for i, item in enumerate(json_data["opportunities"]): - assert item["name"] == lab_manager_opportunities_data[i][0] - assert item["description"] == lab_manager_opportunities_data[i][1] - assert item["recommended_experience"] == lab_manager_opportunities_data[i][2] - assert item["pay"] == lab_manager_opportunities_data[i][3] - assert item["semester"] == lab_manager_opportunities_data[i][4] - assert item["year"] == lab_manager_opportunities_data[i][5] - assert item["active"] == lab_manager_opportunities_data[i][6] - - -def test_user_route_with_input_id_2(test_client: FlaskClient) -> None: + ], +) +def test_user_route( + test_client: FlaskClient, input_data, expected_status, expected_output +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/user' page is requested (GET) - THEN check that the response is valid + WHEN the '/user' page is requested (GET) with input data + THEN check that the response is valid and matches expected output """ - response = test_client.get("/user", json={"id": 2}) - - assert response.status_code == 200 - + response = test_client.get("/user", json=input_data) + assert response.status_code == expected_status json_data = json.loads(response.data) + assert json_data == expected_output - assert json_data["id"] == 2 - assert json_data["first_name"] == "RCOS" - assert json_data["last_name"] == "RCOS" - assert json_data["preferred_name"] is None - assert json_data["email"] == "test@rpi.edu" - # Added - assert json_data["description"] is None - assert ( - json_data["profile_picture"] - == "https://www.svgrepo.com/show/206842/professor.svg" - ) # Adjust based on your test data - assert json_data["website"] is None - assert json_data["class_year"] is None - assert json_data["lab_manager_id"] is None - departments_data = [ - {"department_id": "Computer Science", "user_id": 2}, - ] - - major_data = [ - {"user_id": 2, "major_code": "CSCI"}, - ] - - course_data = [ - {"in_progress": False, "user_id": 2, "course_code": "CSCI2300"}, - ] - - assert json_data["departments"] == departments_data - assert json_data["majors"] == major_data - assert json_data["courses"] == course_data - - -def test_user_2_opportunity_cards(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/user' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/user", json={"id": 2}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - lab_manager_opportunities_data = ( +@pytest.mark.parametrize( + "input_data, expected_opportunities", + [ ( - "Checking out cubes", - "Material Sciences", - "Experienced in materials.", - None, - "Fall", - 2024, - True, + {"id": 1}, + [ + { + "name": "Automated Cooling System", + "description": "Energy efficient AC system", + "recommended_experience": "Thermodynamics", + "pay": 15.0, + "semester": "Spring", + "year": 2024, + "active": True, + }, + { + "name": "Iphone 15 durability test", + "description": "Scratching the Iphone, drop testing etc.", + "recommended_experience": "Experienced in getting angry and throwing temper tantrum", + "pay": None, + "semester": "Spring", + "year": 2024, + "active": True, + }, + ], ), ( - "Test the water", - "Testing the quality of water in Troy pipes", - "Understanding of lead poisioning", - None, - "Summer", - 2024, - True, + {"id": 2}, + [ + { + "name": "Checking out cubes", + "description": "Material Sciences", + "recommended_experience": "Experienced in materials.", + "pay": None, + "semester": "Fall", + "year": 2024, + "active": True, + }, + { + "name": "Test the water", + "description": "Testing the quality of water in Troy pipes", + "recommended_experience": "Understanding of lead poisioning", + "pay": None, + "semester": "Summer", + "year": 2024, + "active": True, + }, + ], ), - ) - - for i, item in enumerate(json_data["opportunities"]): - assert item["name"] == lab_manager_opportunities_data[i][0] - assert item["description"] == lab_manager_opportunities_data[i][1] - assert item["recommended_experience"] == lab_manager_opportunities_data[i][2] - assert item["pay"] == lab_manager_opportunities_data[i][3] - assert item["semester"] == lab_manager_opportunities_data[i][4] - assert item["year"] == lab_manager_opportunities_data[i][5] - assert item["active"] == lab_manager_opportunities_data[i][6] - - -def test_user_route_no_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/user' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/user") - - assert response.status_code == 400 - - -def test_user_route_incorrect_json(test_client: FlaskClient) -> None: + ], +) +def test_user_opportunity_cards( + test_client: FlaskClient, input_data, expected_opportunities +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/user' page is requested (GET) - THEN check that the response is valid + WHEN the '/user' page is requested (GET) with input data + THEN check that the opportunity cards in the response are valid """ - response = test_client.get("/user", json={"wrong": "wrong"}) + response = test_client.get("/user", json=input_data) + assert response.status_code == 200 + json_data = json.loads(response.data) - assert response.status_code == 400 + for i, item in enumerate(json_data["opportunities"]): + assert item == expected_opportunities[i] -def test_user_not_found(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "input_data, expected_status", + [(None, 400), ({"wrong": "wrong"}, 400), ({"id": "not found"}, 404)], +) +def test_user_route_edge_cases( + test_client: FlaskClient, input_data, expected_status +) -> None: """ GIVEN a Flask application configured for testing - WHEN the '/user' page is requested (GET) - THEN check that the response is valid + WHEN the '/user' page is requested (GET) with various edge case inputs + THEN check that the response status code is as expected """ - response = test_client.get("/user", json={"id": "not found"}) - - print(json.loads(response.data)) - - assert response.status_code == 404 + response = test_client.get("/user", json=input_data) + assert response.status_code == expected_status