|
4 | 4 |
|
5 | 5 | from flask import json |
6 | 6 | from flask.testing import FlaskClient |
| 7 | +import pytest |
7 | 8 |
|
8 | 9 |
|
9 | | -def test_courses_route_with_input_name(test_client: FlaskClient) -> None: |
10 | | - """ |
11 | | - GIVEN a Flask application configured for testing |
12 | | - WHEN the '/courses' page is requested (GET) |
13 | | - THEN check that the response is valid |
14 | | - """ |
15 | | - response = test_client.get("/courses", json={"input": "data"}) |
16 | | - |
17 | | - assert response.status_code == 200 |
18 | | - |
19 | | - json_data = json.loads(response.data) |
20 | | - |
21 | | - assert json_data[0]["code"] == "CSCI4390" |
22 | | - assert json_data[0]["name"] == "Data Mining" |
23 | | - |
24 | | - |
25 | | -def test_courses_route_with_input_code(test_client: FlaskClient) -> None: |
26 | | - """ |
27 | | - GIVEN a Flask application configured for testing |
28 | | - WHEN the '/courses' page is requested (GET) |
29 | | - THEN check that the response is valid |
30 | | - """ |
31 | | - response = test_client.get("/courses", json={"input": "cs"}) |
32 | | - |
33 | | - assert response.status_code == 200 |
34 | | - |
35 | | - json_data = json.loads(response.data) |
36 | | - |
37 | | - course_data = ( |
38 | | - ("CSCI2300", "CSCI2961", "CSCI4390", "CSCI4430"), |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "request_json, expected_status, expected_response", |
| 12 | + [ |
39 | 13 | ( |
40 | | - "Introduction to Algorithms", |
41 | | - "Rensselaer Center for Open Source", |
42 | | - "Data Mining", |
43 | | - "Programming Languages", |
| 14 | + {"input": "data"}, |
| 15 | + 200, |
| 16 | + [{"code": "CSCI4390", "name": "Data Mining"}], |
44 | 17 | ), |
45 | | - ) |
46 | | - |
47 | | - for i, major in enumerate(json_data): |
48 | | - assert major["code"] == course_data[0][i] |
49 | | - assert major["name"] == course_data[1][i] |
50 | | - |
51 | | - |
52 | | -def test_courses_route_no_json(test_client: FlaskClient) -> None: |
| 18 | + ( |
| 19 | + {"input": "cs"}, |
| 20 | + 200, |
| 21 | + [ |
| 22 | + {"code": "CSCI2300", "name": "Introduction to Algorithms"}, |
| 23 | + {"code": "CSCI2961", "name": "Rensselaer Center for Open Source"}, |
| 24 | + {"code": "CSCI4390", "name": "Data Mining"}, |
| 25 | + {"code": "CSCI4430", "name": "Programming Languages"}, |
| 26 | + ], |
| 27 | + ), |
| 28 | + (None, 400, None), |
| 29 | + ({"wrong": "wrong"}, 400, None), |
| 30 | + ({"input": "not found"}, 404, None), |
| 31 | + ], |
| 32 | +) |
| 33 | +def test_courses_route(test_client: FlaskClient, request_json, expected_status, expected_response) -> None: |
53 | 34 | """ |
54 | 35 | GIVEN a Flask application configured for testing |
55 | | - WHEN the '/courses' page is requested (GET) |
56 | | - THEN check that the response is valid |
| 36 | + WHEN the '/courses' page is requested (GET) with various inputs |
| 37 | + THEN check that the response status and data are as expected |
57 | 38 | """ |
58 | | - response = test_client.get("/courses") |
| 39 | + response = test_client.get("/courses", json=request_json) if request_json else test_client.get("/courses") |
| 40 | + |
| 41 | + assert response.status_code == expected_status |
59 | 42 |
|
60 | | - assert response.status_code == 400 |
| 43 | + if expected_response is not None: |
| 44 | + json_data = json.loads(response.data) |
| 45 | + if expected_status == 200: |
| 46 | + assert json_data == expected_response |
| 47 | + else: |
| 48 | + assert json_data is not None |
61 | 49 |
|
62 | 50 |
|
63 | | -def test_courses_route_incorrect_json(test_client: FlaskClient) -> None: |
| 51 | +@pytest.mark.parametrize( |
| 52 | + "input_name, course_data", |
| 53 | + [ |
| 54 | + ( |
| 55 | + "cs", |
| 56 | + ( |
| 57 | + ("CSCI2300", "CSCI2961", "CSCI4390", "CSCI4430"), |
| 58 | + ( |
| 59 | + "Introduction to Algorithms", |
| 60 | + "Rensselaer Center for Open Source", |
| 61 | + "Data Mining", |
| 62 | + "Programming Languages", |
| 63 | + ), |
| 64 | + ), |
| 65 | + ) |
| 66 | + ], |
| 67 | +) |
| 68 | +def test_courses_route_with_specific_input(test_client: FlaskClient, input_name, course_data) -> None: |
64 | 69 | """ |
65 | 70 | GIVEN a Flask application configured for testing |
66 | | - WHEN the '/courses' page is requested (GET) |
67 | | - THEN check that the response is valid |
| 71 | + WHEN the '/courses' page is requested (GET) with specific course input names |
| 72 | + THEN check that the response data matches the expected courses |
68 | 73 | """ |
69 | | - response = test_client.get("/courses", json={"wrong": "wrong"}) |
70 | | - |
71 | | - assert response.status_code == 400 |
| 74 | + response = test_client.get("/courses", json={"input": input_name}) |
72 | 75 |
|
| 76 | + assert response.status_code == 200 |
73 | 77 |
|
74 | | -def test_courses_not_found(test_client: FlaskClient) -> None: |
75 | | - """ |
76 | | - GIVEN a Flask application configured for testing |
77 | | - WHEN the '/courses' page is requested (GET) |
78 | | - THEN check that the response is valid |
79 | | - """ |
80 | | - response = test_client.get("/courses", json={"input": "not found"}) |
| 78 | + json_data = json.loads(response.data) |
81 | 79 |
|
82 | | - assert response.status_code == 404 |
| 80 | + for i, course in enumerate(json_data): |
| 81 | + assert course["code"] == course_data[0][i] |
| 82 | + assert course["name"] == course_data[1][i] |
0 commit comments