-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_15_delete_data.py
84 lines (77 loc) · 2.85 KB
/
test_15_delete_data.py
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
import sys
import pytest
from fastapi import FastAPI
from httpx import AsyncClient
from tests.test_01_auth import Acc
from sqlalchemy.orm import Session
from db.crud_answer import get_answer_by_question
pytestmark = pytest.mark.asyncio
sys.path.append("..")
account = Acc(True)
class TestFileRoutes():
@pytest.mark.asyncio
async def test_delete_data(self, app: FastAPI, session: Session,
client: AsyncClient) -> None:
data = await client.get(
app.url_path_for("data:get_by_id", id=1),
headers={"Authorization": f"Bearer {account.token}"})
data.status_code == 200
data = data.json()
assert data["answer"] == [
{
"question": 1,
"value": "Option 2"
},
{
"question": 2,
"value": 10
},
{
"question": 3,
"value": "-7.836114|110.331143"
},
{
"question": 4,
"value": "Bandung"
},
]
res = await client.delete(
app.url_path_for("data:delete", id=data["id"]),
headers={"Authorization": f"Bearer {account.token}"})
assert res.status_code == 204
res = await client.get(
app.url_path_for("data:get", form_id=1),
headers={"Authorization": f"Bearer {account.token}"})
assert res.status_code == 200
res = res.json()
assert res["total"] == 3
deleted_data = await client.get(
app.url_path_for("data:get_by_id", id=data["id"]),
headers={"Authorization": f"Bearer {account.token}"})
deleted_data.status_code == 401
for q in data["answer"]:
answer = get_answer_by_question(session=session,
question=q["question"])
for a in answer:
assert a.data != 1
@pytest.mark.asyncio
async def test_bulk_delete_data(self, app: FastAPI,
client: AsyncClient) -> None:
data = await client.get(
app.url_path_for("data:get", form_id=1),
headers={"Authorization": f"Bearer {account.token}"})
data = data.json()
ids = [d["id"] for d in data["data"]]
assert data["total"] == 3
assert ids == [4, 3, 2]
res = await client.delete(
app.url_path_for("data:bulk-delete"),
params="id=2&id=3&id=4",
headers={"Authorization": f"Bearer {account.token}"})
assert res.status_code == 204
data = await client.get(
app.url_path_for("data:get", form_id=1),
headers={"Authorization": f"Bearer {account.token}"})
assert data.status_code == 404
data = data.json()
assert data == {"detail": "Not found"}