-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_async_other_apis.py
118 lines (92 loc) · 4.22 KB
/
test_async_other_apis.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
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
import json
from datetime import datetime, timedelta
import django
import pytest
from asgiref.sync import sync_to_async
from .easy_app.controllers import EasyCrudAPIController
from .easy_app.models import Event, Type
from .easy_app.schema import EventSchema
from .easy_app.services import EventService
dummy_data = dict(
title="AsyncAPIEvent",
start_date=str(datetime.now().date()),
end_date=str((datetime.now() + timedelta(days=5)).date()),
)
@pytest.mark.skipif(django.VERSION < (3, 1), reason="requires django 3.1 or higher")
@pytest.mark.django_db
class TestEasyCrudAPIController:
async def test_crud_apis(self, transactional_db, easy_api_client):
client = easy_api_client(EasyCrudAPIController)
object_data = await EventService.prepare_create_event_data(dummy_data)
response = await client.put(
"/", json=object_data, content_type="application/json"
)
assert response.status_code == 200
assert response.json().get("code") == 201
event_id = response.json().get("data")["id"]
response = await client.get(
f"/{event_id}",
)
assert response.status_code == 200
assert response.json().get("data")["title"] == "AsyncAPIEvent_create"
response = await client.get("/")
assert response.status_code == 200
assert response.json().get("data")[0]["title"] == "AsyncAPIEvent_create"
async def test_base_response(self, transactional_db, easy_api_client):
client = easy_api_client(EasyCrudAPIController)
response = await client.get(
"/base_response/",
)
assert response.status_code == 200
assert response.json().get("data")["data"] == "This is a BaseAPIResponse."
async def test_qs_paginated(self, transactional_db, easy_api_client):
client = easy_api_client(EasyCrudAPIController)
object_data = await EventService.prepare_create_event_data(dummy_data)
response = await client.put(
"/", json=object_data, content_type="application/json"
)
assert response.status_code == 200
assert response.json().get("code") == 201
event_id = response.json().get("data")["id"]
response = await client.get(
"/qs_paginated/",
)
assert response.status_code == 200
assert response.json().get("data")[0]["id"] == event_id
async def test_qs_list(self, transactional_db, easy_api_client):
client = easy_api_client(EasyCrudAPIController)
for i in range(4):
type = await sync_to_async(Type.objects.create)(name=f"Test-Type-{i}")
object_data = await EventService.prepare_create_event_data(dummy_data)
object_data.update(
title=f"{object_data['title']}_qs_list_{i}", type=type.id
)
await client.put("/", json=object_data)
type = await sync_to_async(Type.objects.create)(name="Test-Type-88")
object_data = await EventService.prepare_create_event_data(dummy_data)
object_data.update(title=f"{object_data['title']}_qs_list_88", type=type)
event = await sync_to_async(Event.objects.create)(**object_data)
response = await client.get(
"/qs_list/",
)
assert response.status_code == 200
data = response.json().get("data")
assert data[0]["title"] == "AsyncAPIEvent_create_qs_list_0"
event_schema = json.loads(EventSchema.from_orm(event).json())
assert (
event_schema["title"]
== data[4]["title"]
== "AsyncAPIEvent_create_qs_list_88"
)
async def test_qs_(self, transactional_db, easy_api_client):
client = easy_api_client(EasyCrudAPIController)
for i in range(4):
type = await sync_to_async(Type.objects.create)(name=f"Test-Type-{i}")
object_data = await EventService.prepare_create_event_data(dummy_data)
object_data.update(title=f"{object_data['title']}_qs_{i}", type=type.id)
await client.put("/", json=object_data)
response = await client.get(
"/qs/",
)
assert response.status_code == 200
assert response.json().get("data")[0]["title"] == "AsyncAPIEvent_create_qs_0"