File size: 4,453 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from core.config import settings
from api.endpoints.course import create_course
from tests.api.api_v1 import test_department
from fastapi.encoders import jsonable_encoder
import models

from . import test_department


headers = {
    "accept": "application/json",
    "Content-Type": "application/json",
}


def test_post_course(super_user_client, db):
    test_department.test_post_department(super_user_client, db)
    department_id = (
        db.query(models.Department)
        .filter(models.Department.name == "Test Department")
        .first()
        .id
    )

    data = {
        "course_code": "TEST101",
        "course_name": "Test Course",
        "course_credit": 3,
        "department_id": department_id,
    }

    post_req = super_user_client.post(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/",
        json=data,
    )
    assert post_req.status_code == 200, "Course post request failed"


def test_get_course(super_user_client):
    get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/",
    )
    assert get_req.status_code == 200

    courses = get_req.json()
    print(courses)
    created_course = [
        course for course in courses if (course.get("course_name") == "Test Course")
    ]
    assert len(created_course) == 1


def test_get_specific_course(super_user_client):
    get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/",
    )
    assert get_req.status_code == 200

    courses = get_req.json()
    created_course = [
        course for course in courses if (course.get("course_name") == "Test Course")
    ]

    specific_get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/{created_course[0]['id']}/",
    )

    assert specific_get_req.status_code == 200

    created_course_specific = specific_get_req.json()
    assert created_course_specific
    assert created_course_specific.get("course_name") == "Test Course"


def test_put_specific_course(super_user_client):
    get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/",
    )
    assert get_req.status_code == 200

    courses = get_req.json()
    created_course = [
        course for course in courses if (course.get("course_name") == "Test Course")
    ]

    data = {
        "course_name": "Updated Course",
        "course_code": "TEST102",
        "course_credit": 2,
    }

    put_req = super_user_client.put(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/{created_course[0]['id']}/",
        json=data,
    )

    assert put_req.status_code == 200

    specific_get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/{created_course[0]['id']}/",
    )

    assert specific_get_req.status_code == 200

    created_course_specific = specific_get_req.json()
    assert created_course_specific
    assert created_course_specific.get("course_name") == "Updated Course"
    assert created_course_specific.get("course_code") == "TEST102"
    assert created_course_specific.get("course_credit") == 2


def test_delete_course(super_user_client, course_id=None):
    get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/",
    )
    assert get_req.status_code == 200

    courses = get_req.json()
    if not course_id:
        created_course = [
            course
            for course in courses
            if (course.get("course_name") == "Updated Course")
        ]
    else:
        created_course = [
            course for course in courses if (course.get("id") == course_id)
        ]

    delete_req = super_user_client.delete(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/{created_course[0]['id']}/",
    )

    assert delete_req.status_code == 200

    get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/course/",
    )
    assert get_req.status_code == 200

    courses = get_req.json()
    filtered_courses = [
        course
        for course in courses
        if (
            course.get("name") == "Updated Course"
            or course.get("course_name") == "Test Course"
        )
    ]
    assert not filtered_courses
    test_department.test_delete_department(
        super_user_client, department_id=created_course[0]["department_id"]
    )