Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 60 additions & 60 deletions tests/test_courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
223 changes: 95 additions & 128 deletions tests/test_departments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Loading