From d485cbdd374d49cea54cad295243c4169d23de89 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:46:34 -0400 Subject: [PATCH 01/14] updated test_user.py test parameterization --- tests/test_user.py | 269 ++++++++++++++++++++------------------------- 1 file changed, 120 insertions(+), 149 deletions(-) diff --git a/tests/test_user.py b/tests/test_user.py index ab075777..dd254891 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -4,197 +4,168 @@ from flask import json from flask.testing import FlaskClient +import pytest -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["last_name"] == "Cenzano" - assert json_data["preferred_name"] == "Raf" - assert json_data["email"] == "cenzar@rpi.edu" - - 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( + "user_id, expected_status_code, expected_data", + [ ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - "Spring", - 2024, - True, + 1, + 200, + { + "id": 1, + "first_name": "Rafael", + "last_name": "Cenzano", + "preferred_name": "Raf", + "email": "cenzar@rpi.edu", + "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, + 2, + 200, + { + "id": 2, + "first_name": "RCOS", + "last_name": "RCOS", + "preferred_name": None, + "email": "test@rpi.edu", + "departments": [ + {"department_id": "Computer Science", "user_id": 2}, + ], + "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_with_input(test_client: FlaskClient, user_id, expected_status_code, expected_data) -> None: """ GIVEN a Flask application configured for testing WHEN the '/user' page is requested (GET) - THEN check that the response is valid + THEN check that the response and data are valid """ - response = test_client.get("/user", json={"id": 2}) - - assert response.status_code == 200 + response = test_client.get("/user", json={"id": str(user_id)}) + assert response.status_code == expected_status_code json_data = json.loads(response.data) + assert json_data == expected_data - 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" - - 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: +@pytest.mark.parametrize( + "user_id, expected_status_code, expected_opportunities", + [ + ( + 1, + 200, + [ + ( + "Automated Cooling System", + "Energy efficient AC system", + "Thermodynamics", + 15.0, + "Spring", + 2024, + True, + ), + ( + "Iphone 15 durability test", + "Scratching the Iphone, drop testing etc.", + "Experienced in getting angry and throwing temper tantrum", + None, + "Spring", + 2024, + True, + ), + ], + ), + ( + 2, + 200, + [ + ( + "Checking out cubes", + "Material Sciences", + "Experienced in materials.", + None, + "Fall", + 2024, + True, + ), + ( + "Test the water", + "Testing the quality of water in Troy pipes", + "Understanding of lead poisoning", + None, + "Summer", + 2024, + True, + ), + ], + ), + ], +) +def test_user_opportunity_cards(test_client: FlaskClient, user_id, expected_status_code, expected_opportunities) -> None: """ GIVEN a Flask application configured for testing WHEN the '/user' page is requested (GET) - THEN check that the response is valid + THEN check that the opportunity cards are valid """ - response = test_client.get("/user", json={"id": 2}) - - assert response.status_code == 200 + response = test_client.get("/user", json={"id": user_id}) + assert response.status_code == expected_status_code json_data = json.loads(response.data) - - lab_manager_opportunities_data = ( - ( - "Checking out cubes", - "Material Sciences", - "Experienced in materials.", - None, - "Fall", - 2024, - True, - ), - ( - "Test the water", - "Testing the quality of water in Troy pipes", - "Understanding of lead poisioning", - None, - "Summer", - 2024, - 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] + assert item["name"] == expected_opportunities[i][0] + assert item["description"] == expected_opportunities[i][1] + assert item["recommended_experience"] == expected_opportunities[i][2] + assert item["pay"] == expected_opportunities[i][3] + assert item["semester"] == expected_opportunities[i][4] + assert item["year"] == expected_opportunities[i][5] + assert item["active"] == expected_opportunities[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 + WHEN the '/user' page is requested (GET) with no JSON payload + THEN check that the response is a 400 error """ response = test_client.get("/user") - assert response.status_code == 400 def test_user_route_incorrect_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 + WHEN the '/user' page is requested (GET) with incorrect JSON payload + THEN check that the response is a 400 error """ response = test_client.get("/user", json={"wrong": "wrong"}) - assert response.status_code == 400 def test_user_not_found(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 + WHEN the '/user' page is requested (GET) with a non-existent user ID + THEN check that the response is a 404 error """ response = test_client.get("/user", json={"id": "not found"}) - - print(json.loads(response.data)) - assert response.status_code == 404 From 7dc9d961383e806b0b8945d79c06bbda5a19341d Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:51:27 -0400 Subject: [PATCH 02/14] updated test_opportunity.py test parameterization --- tests/test_opportunity.py | 301 +++++++++++++++----------------------- 1 file changed, 120 insertions(+), 181 deletions(-) diff --git a/tests/test_opportunity.py b/tests/test_opportunity.py index 55f7c24b..33364bf3 100644 --- a/tests/test_opportunity.py +++ b/tests/test_opportunity.py @@ -1,136 +1,88 @@ -""" -Test opportunity routes -""" - +import pytest import json -from flask import json 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: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity' page is requested (GET) - THEN check that the response is valid - """ - response1 = test_client.get("/opportunity", json={"id": 1}) - response2 = test_client.get("/opportunity", json={"id": 2}) +from labconnect.models import Opportunities - 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 = ( +@pytest.mark.parametrize( + "opportunity_id, expected_data", + [ ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - False, - False, - False, - True, - "Spring", - 2024, - True, + 1, + ( + "Automated Cooling System", + "Energy efficient AC system", + "Thermodynamics", + 15.0, + False, + False, + False, + True, + "Spring", + 2024, + 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, + ( + "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, + ), ), - ) + ], +) +def test_get_opportunity(test_client: FlaskClient, opportunity_id, expected_data) -> None: + response = test_client.get("/opportunity", json={"id": opportunity_id}) - 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] - - print(json_data2) + assert response.status_code == 200 + + json_data = json.loads(response.data) + + assert json_data["name"] == expected_data[0] + assert json_data["description"] == expected_data[1] + assert json_data["recommended_experience"] == expected_data[2] + assert json_data["pay"] == expected_data[3] + assert json_data["one_credit"] == expected_data[4] + assert json_data["two_credits"] == expected_data[5] + assert json_data["three_credits"] == expected_data[6] + assert json_data["four_credits"] == expected_data[7] + assert json_data["semester"] == expected_data[8] + assert json_data["year"] == expected_data[9] + assert json_data["active"] == expected_data[10] def test_get_opportunity_no_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity' page is requested (GET) - THEN check that the response is valid - """ response = test_client.get("/opportunity") - assert response.status_code == 400 -def test_opportunity_incorrect_json(test_client: FlaskClient) -> None: - """ - GIVEN a Flask application configured for testing - WHEN the '/opportunity' page is requested (GET) - THEN check that the response is valid - """ - response = test_client.get("/opportunity", json={"wrong": "wrong"}) - +@pytest.mark.parametrize( + "wrong_data", [{"wrong": "wrong"}, {"invalid": "data"}] +) +def test_opportunity_incorrect_json(test_client: FlaskClient, wrong_data) -> None: + response = test_client.get("/opportunity", json=wrong_data) assert response.status_code == 400 -def test_get_opportunity_meta(test_client: FlaskClient) -> None: - """ - 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 - """ - - response = test_client.get("/getOpportunityMeta/1", content_type="application/json") - - # assert response.status_code == 200 - - data = json.loads(response.data) - data = data["data"] +@pytest.mark.parametrize( + "opportunity_meta_id", + [1, 2], +) +def test_get_opportunity_meta(test_client: FlaskClient, opportunity_meta_id) -> None: + response = test_client.get(f"/getOpportunityMeta/{opportunity_meta_id}", content_type="application/json") + data = json.loads(response.data)["data"] - # Assertions on the expected data assert "name" in data assert "description" in data assert "recommended_experience" in data @@ -143,19 +95,19 @@ def test_get_opportunity_meta(test_client: FlaskClient) -> None: 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") +@pytest.mark.parametrize( + "opportunity_id", + [1, 2], +) +def test_get_opportunity_by_id(test_client: FlaskClient, opportunity_id) -> None: + response = test_client.get(f"/getOpportunity/{opportunity_id}") assert response.status_code == 200 - # Load the response data as JSON - data = json.loads(response.data.decode("utf-8")) - data = data["data"] + data = json.loads(response.data)["data"] - # Test that the "name" key exists assert "id" in data assert "name" in data assert "description" in data @@ -169,40 +121,35 @@ def test_get_opportunity(test_client: FlaskClient) -> None: assert "description" in eachSection -def test_get_opportunity_professor(test_client: FlaskClient) -> None: - response = test_client.get("/getOpportunityByProfessor/led") - +@pytest.mark.parametrize( + "professor_name", ["led", "drsmith"] +) +def test_get_opportunity_professor(test_client: FlaskClient, professor_name) -> None: + response = test_client.get(f"/getOpportunityByProfessor/{professor_name}") assert response.status_code == 200 - # Load the response data as JSON - data = json.loads(response.data.decode("utf-8")) - data = data["data"] + data = json.loads(response.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" - ) +@pytest.mark.parametrize( + "professor_name", ["led", "drsmith"] +) +def test_get_professor_opportunity_cards(test_client: FlaskClient, professor_name) -> None: + response = test_client.get(f"/getProfessorOpportunityCards/{professor_name}", content_type="application/json") assert response.status_code == 200 - data = json.loads(response.data.decode("utf-8")) - data = data["data"] + data = json.loads(response.data)["data"] for eachCard in data: assert "title" in eachCard @@ -211,15 +158,14 @@ def test_get_professor_opportunity_cards(test_client: FlaskClient) -> None: assert "id" in eachCard -def test_profile_opportunities(test_client: FlaskClient) -> None: - response = test_client.get( - "/getProfileOpportunities/led", content_type="application/json" - ) - +@pytest.mark.parametrize( + "professor_name", ["led", "drsmith"] +) +def test_profile_opportunities(test_client: FlaskClient, professor_name) -> None: + response = test_client.get(f"/getProfileOpportunities/{professor_name}", content_type="application/json") assert response.status_code == 200 - data = json.loads(response.data.decode("utf-8")) - data = data["data"] + data = json.loads(response.data)["data"] for eachCard in data: assert "id" in eachCard @@ -229,51 +175,45 @@ def test_profile_opportunities(test_client: FlaskClient) -> None: assert "activeStatus" in eachCard -def test_create_opportunity(test_client: FlaskClient) -> None: - """ - 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 - """ - - 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", - } - +@pytest.mark.parametrize( + "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], + "location": "TBD", + } + ], +) +def test_create_opportunity(test_client: FlaskClient, test_data) -> None: response = test_client.post( "/createOpportunity", data=json.dumps(test_data), 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", + Opportunities.name == test_data["name"], + Opportunities.description == test_data["description"], + Opportunities.recommended_experience == test_data["recommended_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}), @@ -282,20 +222,19 @@ def test_create_opportunity(test_client: FlaskClient) -> None: 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") +@pytest.mark.parametrize( + "professor_name", ["led", "drsmith"] +) +def test_professor_opportunity_cards(test_client: FlaskClient, professor_name) -> None: + response = test_client.get(f"/getProfessorOpportunityCards/{professor_name}") assert response.status_code == 200 - # Load the response data as JSON - data = json.loads(response.data.decode("utf-8")) + data = json.loads(response.data)["data"] - # Test that the "name" key exists assert len(data.keys()) > 0 for eachCard in data["data"]: assert "title" in eachCard From 94faf17bee1dd5f517fe4615b3affba7d45de0fc Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:53:00 -0400 Subject: [PATCH 03/14] updated test_majors.py test parameterization --- tests/test_majors.py | 97 ++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/tests/test_majors.py b/tests/test_majors.py index d7594743..b4c89d8b 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,71 @@ 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] From 17cd39d83f19d8a5c6be2a55a046697de8bf7931 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:54:15 -0400 Subject: [PATCH 04/14] update test_departments.py test parameterization --- tests/test_departments.py | 127 ++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 54 deletions(-) diff --git a/tests/test_departments.py b/tests/test_departments.py index 445cd3c1..9aab8915 100644 --- a/tests/test_departments.py +++ b/tests/test_departments.py @@ -4,9 +4,35 @@ from flask import json from flask.testing import FlaskClient +import pytest -def test_departments_route(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "expected_departments", + [ + ( + ( + "Computer Science", + "Biology", + "Materials Engineering", + "Math", + "Environmental Engineering", + "Aerospace Engineering", + "Aeronautical Engineering", + ), + ( + "DS", + "life", + "also pretty cool", + "quick maths", + "water", + "space, the final frontier", + "flying, need for speed", + ), + ) + ], +) +def test_departments_route(test_client: FlaskClient, expected_departments) -> None: """ GIVEN a Flask application configured for testing WHEN the '/departments' page is requested (GET) @@ -18,80 +44,73 @@ def test_departments_route(test_client: FlaskClient) -> None: 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", - ), - ) - for department in json_data: - assert department["name"] in rpi_departments_data[0] - assert department["description"] in rpi_departments_data[1] + assert department["name"] in expected_departments[0] + assert department["description"] in expected_departments[1] -def test_department_route(test_client: FlaskClient) -> None: +@pytest.mark.parametrize( + "input_data, expected_data", + [ + ( + {"department": "Computer Science"}, + { + "name": "Computer Science", + "description": "DS", + "school_id": "School of Science", + "professors": [ + {"name": "Duy Le", "rcs_id": "led"}, + {"name": "Rafael", "rcs_id": "cenzar"}, + {"name": "Turner", "rcs_id": "turner"}, + {"name": "Kuzmin", "rcs_id": "kuzmin"}, + {"name": "Goldschmidt", "rcs_id": "goldd"}, + ], + "opportunities": [ + {"id": 1, "name": "Automated Cooling System"}, + {"id": 2, "name": "Iphone 15 durability test"}, + ], + }, + ) + ], +) +def test_department_route(test_client: FlaskClient, input_data, expected_data) -> 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={"department": "Computer Science"}) + response = test_client.get("/department", json=input_data) 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" - - prof_names = ["Duy Le", "Rafael", "Turner", "Kuzmin", "Goldschmidt"] - prof_rcs_ids = ["led", "cenzar", "turner", "kuzmin", "goldd"] + assert json_data["name"] == expected_data["name"] + assert json_data["description"] == expected_data["description"] + assert json_data["school_id"] == expected_data["school_id"] 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"] + assert prof["name"] in [p["name"] for p in expected_data["professors"]] + assert prof["rcs_id"] in [p["rcs_id"] for p in expected_data["professors"]] 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: + assert opportunity["id"] in [o["id"] for o in expected_data["opportunities"]] + assert opportunity["name"] in [o["name"] for o in expected_data["opportunities"]] + + +@pytest.mark.parametrize( + "input_data", + [ + (None,), # Testing with no JSON + ({"wrong": "wrong"},) # Testing with incorrect JSON + ], +) +def test_department_route_invalid_json(test_client: FlaskClient, input_data) -> 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"}) + response = test_client.get("/department", json=input_data) assert response.status_code == 400 From 7e24cda373dec75bc8e08d0dc889e474cba7ae05 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:59:09 -0400 Subject: [PATCH 05/14] update test_general.py test parameterization --- tests/test_general.py | 61 +++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/tests/test_general.py b/tests/test_general.py index e472206a..e69a2224 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,48 @@ 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 +91,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 +105,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" From 8e58ff1bce2d18c7960d67beb6dbc5a30749f86b Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:02:51 -0400 Subject: [PATCH 06/14] update test_opportunities_filtering.py test parameterization --- tests/test_opportunities_filtering.py | 333 ++++++-------------------- 1 file changed, 67 insertions(+), 266 deletions(-) diff --git a/tests/test_opportunities_filtering.py b/tests/test_opportunities_filtering.py index 9297cf75..6610b25e 100644 --- a/tests/test_opportunities_filtering.py +++ b/tests/test_opportunities_filtering.py @@ -2,284 +2,85 @@ 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}}]} + json_data = {"filters": filters} 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"]}, - ] - } - 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 From bfdd042b240aa7fda183d745eeb69183f7cc23e8 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:03:50 -0400 Subject: [PATCH 07/14] update test_lab_manager.py test parameterization --- tests/test_lab_manager.py | 167 ++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 95 deletions(-) diff --git a/tests/test_lab_manager.py b/tests/test_lab_manager.py index 0cea0a03..d0a27c20 100644 --- a/tests/test_lab_manager.py +++ b/tests/test_lab_manager.py @@ -4,114 +4,91 @@ from flask import json from flask.testing import FlaskClient +import pytest -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, - } - - 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: +@pytest.mark.parametrize( + "json_data, expected_status, expected_response", + [ + ( + {"rcs_id": "cenzar"}, + 200, + { + "website": None, + "rcs_id": "cenzar", + "name": "Rafael", + "alt_email": None, + "phone_number": None, + "email": None, + }, + ), + (None, 400, None), + ({"wrong": "wrong"}, 400, None), + ], +) +def test_lab_manager_route(test_client: FlaskClient, json_data, expected_status, expected_response) -> None: """ GIVEN a Flask application configured for testing WHEN the '/lab_manager' page is requested (GET) - THEN check that the response is valid + THEN check that the response status and data are as expected """ - response = test_client.get("/lab_manager", json={"wrong": "wrong"}) + response = test_client.get("/lab_manager", json=json_data) - assert response.status_code == 400 + assert response.status_code == expected_status + if expected_response is not None: + json_response = json.loads(response.data) + assert json_response == expected_response -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 = ( - ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - "Spring", - 2024, - True, - ), +@pytest.mark.parametrize( + "json_data, expected_status, expected_opportunities", + [ ( - "Iphone 15 durability test", - "Scratching the Iphone, drop testing etc.", - "Experienced in getting angry and throwing temper tantrum", - None, - "Spring", - 2024, - True, + {"rcs_id": "cenzar"}, + 200, + [ + ( + "Automated Cooling System", + "Energy efficient AC system", + "Thermodynamics", + 15.0, + "Spring", + 2024, + True, + ), + ( + "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), + ({"wrong": "wrong"}, 400, None), + ], +) +def test_lab_manager_opportunity_cards(test_client: FlaskClient, json_data, expected_status, expected_opportunities) -> 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") - - assert response.status_code == 400 - - -def test_lab_manager_opportunity_cards_incorrect_json(test_client: FlaskClient) -> None: + THEN check that the response status and data are as expected """ - 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={"wrong": "wrong"}) - - assert response.status_code == 400 + response = test_client.get("/lab_manager/opportunities", json=json_data) + + assert response.status_code == expected_status + + if expected_opportunities is not None: + json_response = json.loads(response.data) + for i, item in enumerate(json_response["cenzar"]): + assert item["name"] == expected_opportunities[i][0] + assert item["description"] == expected_opportunities[i][1] + assert item["recommended_experience"] == expected_opportunities[i][2] + assert item["pay"] == expected_opportunities[i][3] + assert item["semester"] == expected_opportunities[i][4] + assert item["year"] == expected_opportunities[i][5] + assert item["active"] == expected_opportunities[i][6] From c8a5aa473bc5853ab1349855dae143235d889d08 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:06:03 -0400 Subject: [PATCH 08/14] update test_errors.py test parameterization --- tests/test_errors.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/tests/test_errors.py b/tests/test_errors.py index 388aa8b9..d44acaa3 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -4,27 +4,28 @@ 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 From 0127a6872702abcc40fc0af706b71ad0d15bc980 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:09:18 -0400 Subject: [PATCH 09/14] update test_courses.py test parameterization --- tests/test_courses.py | 120 +++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/tests/test_courses.py b/tests/test_courses.py index 9a29803e..27d8eec3 100644 --- a/tests/test_courses.py +++ b/tests/test_courses.py @@ -4,79 +4,79 @@ from flask import json from flask.testing import FlaskClient +import pytest -def test_courses_route_with_input_name(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": "data"}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - assert json_data[0]["code"] == "CSCI4390" - assert json_data[0]["name"] == "Data Mining" - - -def test_courses_route_with_input_code(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": "cs"}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - course_data = ( - ("CSCI2300", "CSCI2961", "CSCI4390", "CSCI4430"), +@pytest.mark.parametrize( + "request_json, expected_status, expected_response", + [ ( - "Introduction to Algorithms", - "Rensselaer Center for Open Source", - "Data Mining", - "Programming Languages", + {"input": "data"}, + 200, + [{"code": "CSCI4390", "name": "Data Mining"}], ), - ) - - 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: + ( + {"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") + response = test_client.get("/courses", json=request_json) if request_json else test_client.get("/courses") + + assert response.status_code == expected_status - assert response.status_code == 400 + 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_incorrect_json(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={"wrong": "wrong"}) - - assert response.status_code == 400 + response = test_client.get("/courses", json={"input": input_name}) + assert response.status_code == 200 -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"}) + json_data = json.loads(response.data) - 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] From d03552338706bb066e72cda623f9bad6d0710af9 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Sat, 9 Nov 2024 14:40:28 -0500 Subject: [PATCH 10/14] updated test departments again with the new updated tests --- tests/test_departments.py | 223 ++++++++++++++++---------------------- 1 file changed, 95 insertions(+), 128 deletions(-) diff --git a/tests/test_departments.py b/tests/test_departments.py index 4d3dd497..866f55cc 100644 --- a/tests/test_departments.py +++ b/tests/test_departments.py @@ -4,139 +4,106 @@ 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 = ( +@pytest.mark.parametrize( + "endpoint, request_json, expected_status, expected_response_checks", + [ ( - "Computer Science", - "Biology", - "Materials Engineering", - "Math", - "Environmental Engineering", - "Aerospace Engineering", - "Areonautical 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 + } + ] ), ( - "DS", - "life", - "also pretty cool", - "quick maths", - "water", - "space, the final frontier", - "flying, need for speed", + "/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"]} + ] + } + ] ), - ( - "School of science", - "School of science", - "School of engineering", - "School of science", - "School of engineering", - "School of engineering", - "School of engineering", - ), - ( - "CSCI", - "BIOL", - "MTLE" - "MATH", - "ENVI", - "MANE", - "MANE", - ), - ( - "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" - 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 + response = test_client.get(endpoint, json=request_json) if request_json else test_client.get(endpoint) + 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"] From d9801be846e321162fcd281785d18da82ca8af4f Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Sat, 9 Nov 2024 14:41:52 -0500 Subject: [PATCH 11/14] updated test lab manager again --- tests/test_lab_manager.py | 179 ++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 105 deletions(-) diff --git a/tests/test_lab_manager.py b/tests/test_lab_manager.py index 7e288834..562f6b2c 100644 --- a/tests/test_lab_manager.py +++ b/tests/test_lab_manager.py @@ -1,118 +1,87 @@ """ -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: +@pytest.mark.parametrize( + "input_json, expected_status, expected_response", + [ + ({"rcs_id": "cenzar"}, 200, { + "website": None, + "rcs_id": "cenzar", + "name": "Rafael", + "alt_email": None, + "phone_number": None, + "email": None, + "description": 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' 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", 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: + 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={"rcs_id": "cenzar"}) - - assert response.status_code == 200 - - json_data = json.loads(response.data) - - lab_manager_opportunities_data = ( - ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - "Spring", - 2024, - True, - ), - ( - "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: - """ - 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") - - assert response.status_code == 400 - - -def test_lab_manager_opportunity_cards_incorrect_json(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={"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"] From ffcaa42907aedafbb7ec2befb22c6cd055a42df9 Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Sat, 9 Nov 2024 14:45:59 -0500 Subject: [PATCH 12/14] update test user again --- tests/test_user.py | 318 ++++++++++++++++++--------------------------- 1 file changed, 126 insertions(+), 192 deletions(-) diff --git a/tests/test_user.py b/tests/test_user.py index d1e7d40a..0e9ee867 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -2,213 +2,147 @@ 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 = ( - ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - "Spring", - 2024, - True, - ), - ( - "Iphone 15 durability test", - "Scratching the Iphone, drop testing etc.", - "Experienced in getting angry and throwing temper tantrum", - None, - "Spring", - 2024, - True, - ), +@pytest.mark.parametrize("input_data, expected_status, expected_output", [ + ( + {"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"} + ] + } + ), + ( + {"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["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: + assert json_data == expected_output + +@pytest.mark.parametrize("input_data, expected_opportunities", [ + ( + {"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 + } + ] + ), + ( + {"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 + } + ] + ) +]) +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={"id": 2}) - + response = test_client.get("/user", json=input_data) assert response.status_code == 200 - json_data = json.loads(response.data) - lab_manager_opportunities_data = ( - ( - "Checking out cubes", - "Material Sciences", - "Experienced in materials.", - None, - "Fall", - 2024, - True, - ), - ( - "Test the water", - "Testing the quality of water in Troy pipes", - "Understanding of lead poisioning", - None, - "Summer", - 2024, - 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: + assert item == expected_opportunities[i] + +@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") - - assert response.status_code == 400 - - -def test_user_route_incorrect_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", json={"wrong": "wrong"}) - - assert response.status_code == 400 - - -def test_user_not_found(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": "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 From 9a7063a15905430e17662ef78aa98a99487fed8b Mon Sep 17 00:00:00 2001 From: Mrunal Athaley <112527910+athalm@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:30:07 -0500 Subject: [PATCH 13/14] updated test_opportunity test parameterization --- tests/test_opportunity.py | 384 +++++++++++--------------------------- 1 file changed, 109 insertions(+), 275 deletions(-) diff --git a/tests/test_opportunity.py b/tests/test_opportunity.py index e37f9545..b6ade780 100644 --- a/tests/test_opportunity.py +++ b/tests/test_opportunity.py @@ -1,305 +1,139 @@ -""" -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 = ( - ( - "Automated Cooling System", - "Energy efficient AC system", - "Thermodynamics", - 15.0, - False, - False, - False, - True, - "Spring", - 2024, - 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, - ), - ) - - 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] - - print(json_data2) - - -def test_get_opportunity_no_json(test_client: FlaskClient) -> None: + test_cases = [ + (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, + }), + (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, + }), + ] + + for opportunity_id, expected_data in test_cases: + response = test_client.get("/opportunity", json={"id": opportunity_id}) + assert response.status_code == 200 + + 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): """ 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") - - 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") - + 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 - 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 From b160331587e6d1407db9f3c36815d72669773687 Mon Sep 17 00:00:00 2001 From: Rafael Cenzano <32753063+RafaelCenzano@users.noreply.github.com> Date: Sat, 23 Nov 2024 09:57:04 -0500 Subject: [PATCH 14/14] run black formatter --- tests/test_courses.py | 16 +- tests/test_departments.py | 106 ++++++++---- tests/test_errors.py | 4 +- tests/test_general.py | 15 +- tests/test_lab_manager.py | 47 ++++-- tests/test_majors.py | 8 +- tests/test_opportunities_filtering.py | 65 +++++--- tests/test_opportunity.py | 120 +++++++------ tests/test_user.py | 232 ++++++++++++++------------ 9 files changed, 366 insertions(+), 247 deletions(-) diff --git a/tests/test_courses.py b/tests/test_courses.py index 27d8eec3..cb2fd309 100644 --- a/tests/test_courses.py +++ b/tests/test_courses.py @@ -30,14 +30,20 @@ ({"input": "not found"}, 404, None), ], ) -def test_courses_route(test_client: FlaskClient, request_json, expected_status, expected_response) -> 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) with various inputs THEN check that the response status and data are as expected """ - response = test_client.get("/courses", json=request_json) if request_json else test_client.get("/courses") - + response = ( + test_client.get("/courses", json=request_json) + if request_json + else test_client.get("/courses") + ) + assert response.status_code == expected_status if expected_response is not None: @@ -65,7 +71,9 @@ def test_courses_route(test_client: FlaskClient, request_json, expected_status, ) ], ) -def test_courses_route_with_specific_input(test_client: FlaskClient, input_name, course_data) -> None: +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) with specific course input names diff --git a/tests/test_departments.py b/tests/test_departments.py index 866f55cc..fbeb88a8 100644 --- a/tests/test_departments.py +++ b/tests/test_departments.py @@ -6,6 +6,7 @@ from flask.testing import FlaskClient import pytest + @pytest.mark.parametrize( "endpoint, request_json, expected_status, expected_response_checks", [ @@ -17,41 +18,52 @@ { "field": "name", "values": [ - "Computer Science", "Biology", "Materials Engineering", - "Math", "Environmental Engineering", "Aerospace Engineering", - "Areonautical Engineering" - ] + "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" - ] + "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" - ] + "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"] + "values": ["CSCI", "BIOL", "MTLE", "MATH", "ENVI", "MANE", "MANE"], }, { "field": "image", "values": [ "https://cdn-icons-png.flaticon.com/512/5310/5310672.png" - ] * 7 + ] + * 7, }, - { - "field": "webcite", - "values": ["https://www.rpi.edu"] * 7 - } - ] + {"field": "webcite", "values": ["https://www.rpi.edu"] * 7}, + ], ), ( "/department", @@ -64,36 +76,66 @@ {"field": "id", "values": ["CSCI"]}, { "field": "image", - "values": ["https://cdn-icons-png.flaticon.com/512/5310/5310672.png"] + "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"]} - ] + { + "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"]} - ] - } - ] + { + "subfield": "name", + "values": [ + "Automated Cooling System", + "Iphone 15 durability test", + ], + }, + ], + }, + ], ), ("/department", None, 400, None), - ("/department", {"wrong": "wrong"}, 400, None) - ] + ("/department", {"wrong": "wrong"}, 400, None), + ], ) -def test_department_routes(test_client: FlaskClient, endpoint, request_json, expected_status, expected_response_checks) -> 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 various '/departments' or '/department' routes are requested (GET) THEN check that the response status and data are as expected """ - response = test_client.get(endpoint, json=request_json) if request_json else test_client.get(endpoint) + response = ( + test_client.get(endpoint, json=request_json) + if request_json + else test_client.get(endpoint) + ) assert response.status_code == expected_status if expected_response_checks: @@ -106,4 +148,6 @@ def test_department_routes(test_client: FlaskClient, endpoint, request_json, exp else: for item in json_data.get(check["field"], []): for subfield_check in check["subfields"]: - assert item[subfield_check["subfield"]] in subfield_check["values"] + assert ( + item[subfield_check["subfield"]] in subfield_check["values"] + ) diff --git a/tests/test_errors.py b/tests/test_errors.py index d44acaa3..d09edc6e 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -20,7 +20,9 @@ ), ], ) -def test_error_pages(test_client: FlaskClient, route, expected_status, expected_response) -> None: +def test_error_pages( + test_client: FlaskClient, route, expected_status, expected_response +) -> None: """ GIVEN a Flask application configured for testing WHEN the specified error route is requested (GET) diff --git a/tests/test_general.py b/tests/test_general.py index e69a2224..71d19ee0 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -41,11 +41,14 @@ def test_discover_route(test_client: FlaskClient) -> None: @pytest.mark.parametrize( "input_id, expected_profile", [ - (1, { - "id": "cenzar", - "first_name": "Rafael", - "opportunities": [ ... ] # Replace with expected opportunities data - }) + ( + 1, + { + "id": "cenzar", + "first_name": "Rafael", + "opportunities": [...], # Replace with expected opportunities data + }, + ) ], ) def test_profile_page(test_client: FlaskClient, input_id, expected_profile) -> None: @@ -115,7 +118,7 @@ def test_professor_profile(test_client: FlaskClient) -> None: THEN check that the response is valid """ response = test_client.get("/getProfessorProfile/1") - + assert response.status_code == 200 # Load the response data as JSON diff --git a/tests/test_lab_manager.py b/tests/test_lab_manager.py index 562f6b2c..5d54c2cd 100644 --- a/tests/test_lab_manager.py +++ b/tests/test_lab_manager.py @@ -10,20 +10,26 @@ @pytest.mark.parametrize( "input_json, expected_status, expected_response", [ - ({"rcs_id": "cenzar"}, 200, { - "website": None, - "rcs_id": "cenzar", - "name": "Rafael", - "alt_email": None, - "phone_number": None, - "email": None, - "description": None, - }), + ( + {"rcs_id": "cenzar"}, + 200, + { + "website": None, + "rcs_id": "cenzar", + "name": "Rafael", + "alt_email": None, + "phone_number": None, + "email": None, + "description": None, + }, + ), (None, 400, None), # No input JSON case - ({"wrong": "wrong"}, 400, None) # Incorrect JSON structure case - ] + ({"wrong": "wrong"}, 400, None), # Incorrect JSON structure case + ], ) -def test_lab_manager_route(test_client: FlaskClient, input_json, expected_status, expected_response) -> None: +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' page is requested (GET) with different JSON inputs @@ -42,10 +48,12 @@ def test_lab_manager_route(test_client: FlaskClient, input_json, expected_status [ ({"rcs_id": "cenzar"}, 200), (None, 400), # No input JSON case - ({"wrong": "wrong"}, 400) # Incorrect JSON structure case - ] + ({"wrong": "wrong"}, 400), # Incorrect JSON structure case + ], ) -def test_lab_manager_opportunity_cards(test_client: FlaskClient, input_json, expected_status) -> None: +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) with different JSON inputs @@ -79,8 +87,13 @@ def test_lab_manager_opportunity_cards(test_client: FlaskClient, input_json, exp 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["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"] diff --git a/tests/test_majors.py b/tests/test_majors.py index b4c89d8b..d7d6df87 100644 --- a/tests/test_majors.py +++ b/tests/test_majors.py @@ -54,7 +54,9 @@ def test_majors_route(test_client: FlaskClient, expected_majors) -> None: ), ], ) -def test_majors_route_with_input_name(test_client: FlaskClient, input_data, expected_majors) -> None: +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) @@ -87,7 +89,9 @@ def test_majors_route_with_input_name(test_client: FlaskClient, input_data, expe ), ], ) -def test_majors_route_with_input_code(test_client: FlaskClient, input_data, expected_majors) -> None: +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) diff --git a/tests/test_opportunities_filtering.py b/tests/test_opportunities_filtering.py index 6610b25e..1c067014 100644 --- a/tests/test_opportunities_filtering.py +++ b/tests/test_opportunities_filtering.py @@ -6,70 +6,89 @@ from flask import json from flask.testing import FlaskClient + @pytest.mark.parametrize( "filters, expected_opportunities", [ ( [{"field": "pay", "value": {"min": 14.9, "max": 21}}], - ["Automated Cooling System"] + ["Automated Cooling System"], ), ( [{"field": "departments", "value": ["Material Science"]}], - ["Checking out cubes"] + ["Checking out cubes"], ), ( - [{"field": "departments", "value": ["Computer Science", "Material Science"]}], - ["Iphone 15 durability test", "Checking out cubes", "Automated Cooling System"] + [ + { + "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"] + [ + "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"] + [ + "Iphone 15 durability test", + "Checking out cubes", + "Automated Cooling System", + ], ), ( [{"field": "credits", "value": [1]}], - ["Iphone 15 durability test", "Checking out cubes"] + ["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"] + [ + "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"] + ["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"] + ["Iphone 15 durability test", "Checking out cubes", "Test the water"], ), ( [ {"field": "location", "value": "In-Person"}, - {"field": "departments", "value": ["Computer Science"]} + {"field": "departments", "value": ["Computer Science"]}, ], - ["Iphone 15 durability test"] + ["Iphone 15 durability test"], ), ( [ {"field": "credits", "value": [2, 4]}, - {"field": "departments", "value": ["Computer Science"]} + {"field": "departments", "value": ["Computer Science"]}, ], - ["Iphone 15 durability test", "Automated Cooling System"] + ["Iphone 15 durability test", "Automated Cooling System"], ), ], ) -def test_opportunity_filter(test_client: FlaskClient, filters, expected_opportunities) -> None: +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) diff --git a/tests/test_opportunity.py b/tests/test_opportunity.py index b6ade780..bd0e9da4 100644 --- a/tests/test_opportunity.py +++ b/tests/test_opportunity.py @@ -2,6 +2,7 @@ import pytest from flask.testing import FlaskClient + def test_get_opportunity_parametrized(test_client: FlaskClient): """ GIVEN a Flask application configured for testing @@ -9,32 +10,38 @@ def test_get_opportunity_parametrized(test_client: FlaskClient): THEN check that the responses are valid """ test_cases = [ - (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, - }), - (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, - }), + ( + 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, + }, + ), + ( + 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, + }, + ), ] for opportunity_id, expected_data in test_cases: @@ -69,32 +76,40 @@ def test_opportunity_incorrect_json(test_client: FlaskClient): @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", - ]), + ( + "/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): +def test_opportunity_meta_parametrized( + test_client: FlaskClient, endpoint, expected_keys +): """ GIVEN a Flask application configured for testing WHEN specific opportunity endpoints are requested @@ -116,11 +131,12 @@ def test_opportunity_meta_parametrized(test_client: FlaskClient, endpoint, expec @pytest.mark.parametrize( - "endpoint", [ + "endpoint", + [ "/getOpportunityByProfessor/led", "/getProfessorOpportunityCards/led", "/getProfileOpportunities/led", - ] + ], ) def test_professor_related_opportunities(test_client: FlaskClient, endpoint): """ diff --git a/tests/test_user.py b/tests/test_user.py index 0e9ee867..2b6744e1 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -6,62 +6,64 @@ from flask import json from flask.testing import FlaskClient -@pytest.mark.parametrize("input_data, expected_status, expected_output", [ - ( - {"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"} - ] - } - ), - ( - {"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"} - ] - } - ) -]) -def test_user_route(test_client: FlaskClient, input_data, expected_status, expected_output) -> None: + +@pytest.mark.parametrize( + "input_data, expected_status, expected_output", + [ + ( + {"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"}, + ], + }, + ), + ( + {"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"} + ], + }, + ), + ], +) +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) with input data @@ -72,55 +74,61 @@ def test_user_route(test_client: FlaskClient, input_data, expected_status, expec json_data = json.loads(response.data) assert json_data == expected_output -@pytest.mark.parametrize("input_data, expected_opportunities", [ - ( - {"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 - } - ] - ), - ( - {"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 - } - ] - ) -]) -def test_user_opportunity_cards(test_client: FlaskClient, input_data, expected_opportunities) -> None: + +@pytest.mark.parametrize( + "input_data, expected_opportunities", + [ + ( + {"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, + }, + ], + ), + ( + {"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, + }, + ], + ), + ], +) +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) with input data @@ -133,12 +141,14 @@ def test_user_opportunity_cards(test_client: FlaskClient, input_data, expected_o for i, item in enumerate(json_data["opportunities"]): assert item == expected_opportunities[i] -@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: + +@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) with various edge case inputs