File size: 4,298 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
from fastapi.encoders import jsonable_encoder
import models

from core.config import settings
from . import test_school


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


def test_post_department(super_user_client, db):
    test_school.test_post_school(super_user_client)
    school_id = (
        db.query(models.School).filter(models.School.name == "Test School").first().id
    )

    data = {"name": "Test Department", "school_id": school_id}
    post_req = super_user_client.post(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/department/",
        json=data,
    )
    assert post_req.status_code == 200, "Department post request failed"


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

    departments = get_req.json()
    created_department = [
        department
        for department in departments
        if (department.get("name") == "Test Department")
    ]
    assert len(created_department) == 1


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

    departments = get_req.json()
    created_department = [
        department
        for department in departments
        if (department.get("name") == "Test Department")
    ]

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

    assert specific_get_req.status_code == 200

    created_department_specific = specific_get_req.json()
    assert created_department_specific
    assert created_department_specific.get("name") == "Test Department"


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

    departments = get_req.json()
    created_department = [
        department
        for department in departments
        if (department.get("name") == "Test Department")
    ]

    data = {
        "name": "Updated Department",
    }

    put_req = super_user_client.put(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/department/{created_department[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}/department/{created_department[0]['id']}/",
    )

    assert specific_get_req.status_code == 200

    created_department_specific = specific_get_req.json()
    assert created_department_specific
    assert created_department_specific.get("name") == "Updated Department"


def test_delete_department(super_user_client, department_id=None):
    get_req = super_user_client.get(
        f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/department/",
    )
    assert get_req.status_code == 200

    departments = get_req.json()
    if not department_id:
        created_department = [
            department
            for department in departments
            if (department.get("name") == "Updated Department")
        ]
    else:
        created_department = [
            department
            for department in departments
            if (department.get("id") == department_id)
        ]

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

    assert delete_req.status_code == 200

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

    departments = get_req.json()
    filtered_department = [
        department
        for department in departments
        if (
            department.get("name") == "Updated School"
            or department.get("name") == "Test Department"
        )
    ]
    assert not filtered_department
    test_school.test_delete_school(
        super_user_client, school_id=created_department[0]["school_id"]
    )