forked from sopherapps/pydantic-redis
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_ext_fastapi_crudrouter.py
220 lines (163 loc) · 7.28 KB
/
test_ext_fastapi_crudrouter.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from typing import List
import pytest
import pytest_asyncio
from fastapi import FastAPI
from httpx import AsyncClient
from pydantic_aioredis import Model as PAModel
from pydantic_aioredis.config import RedisConfig
from pydantic_aioredis.ext.FastAPI import PydanticAioredisCRUDRouter
from pydantic_aioredis.store import Store
class Model(PAModel):
_primary_key_field = "name"
name: str
value: int
class ModelNoSync(PAModel):
_primary_key_field = "name"
name: str
value: int
_auto_sync = False
@pytest_asyncio.fixture()
async def test_app(redis_server):
store = Store(
name="sample",
redis_config=RedisConfig(port=redis_server, db=1), # nosec
life_span_in_seconds=3600,
)
store.register_model(Model)
app = FastAPI()
router = PydanticAioredisCRUDRouter(schema=Model, store=store)
app.include_router(router)
yield store, app, Model
@pytest_asyncio.fixture()
def test_models():
return [Model(name=f"test{i}", value=i) for i in range(1, 10)]
@pytest.mark.asyncio
async def test_crudrouter_get_many_200_empty(test_app):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.get("/model")
assert response.status_code == 200
assert response.json() == []
@pytest.mark.asyncio
async def test_crudrouter_get_one_404(test_app):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.get("/model/test")
assert response.status_code == 404
@pytest.mark.asyncio
async def test_crudrouter_get_many_200(test_app, test_models):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
await test_app[2].insert(test_models)
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.get("/model")
assert response.status_code == 200
result = response.json()
assert len(result) == len(test_models)
@pytest.mark.asyncio
async def test_crudrouter_get_many_200_pagination(test_app, test_models):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
await test_app[2].insert(test_models)
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.get("/model", params={"skip": 2, "limit": 5})
assert response.status_code == 200
result = response.json()
assert len(result) == 5
@pytest.mark.asyncio
async def test_crudrouter_get_many_200(test_app, test_models):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
await test_app[2].insert(test_models)
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.get("/model")
assert response.status_code == 200
result = response.json()
assert len(result) == len(test_models)
@pytest.mark.asyncio
async def test_crudrouter_get_one_200(test_app, test_models):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
await test_app[2].insert(test_models)
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.get(f"/model/{test_models[0].name}")
assert response.status_code == 200
result = response.json()
assert result["name"] == test_models[0].name
@pytest.mark.asyncio
async def test_crudrouter_post_200(test_app, test_models):
"""Tests that crudrouter will post properly"""
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.post(f"/model", json=test_models[0].dict())
assert response.status_code == 200
result = response.json()
assert result["name"] == test_models[0].name
@pytest.mark.asyncio
async def test_crudrouter_post_422(test_app, test_models):
"""Tests that crudrouter post will 422 with invalid data"""
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.post(f"/model", json={"invalid": "stuff"})
assert response.status_code == 422
@pytest.mark.asyncio
async def test_crudrouter_put_404(test_app, test_models):
"""Tests that crudrouter put will 404 when no instance exists"""
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.put(f"/model/test", json=test_models[0].dict())
assert response.status_code == 404
@pytest.mark.asyncio
async def test_crudrouter_put_200(test_app, test_models):
"""Tests that crudrouter put will 200 on a successful update"""
await test_app[2].insert(test_models)
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.put(
f"/model/{test_models[0].name}",
json={"name": test_models[0].name, "value": 100},
)
assert response.status_code == 200
result = response.json()
assert result["name"] == test_models[0].name
assert result["value"] == 100
@pytest.mark.asyncio
async def test_crudrouter_put_200_no_autosync(redis_server):
"""Tests that crudrouter put will 404 when no instance exists"""
store = Store(
name="sample",
redis_config=RedisConfig(port=redis_server, db=1), # nosec
life_span_in_seconds=3600,
)
store.register_model(ModelNoSync)
app = FastAPI()
router = PydanticAioredisCRUDRouter(schema=ModelNoSync, store=store)
app.include_router(router)
await ModelNoSync.insert(ModelNoSync(name="test", value=20))
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.put(
f"/modelnosync/test",
json={"name": "test", "value": 100},
)
assert response.status_code == 200
result = response.json()
assert result["name"] == "test"
assert result["value"] == 100
@pytest.mark.asyncio
async def test_crudrouter_put_404(test_app, test_models):
"""Tests that crudrouter put will 404 when no instance exists"""
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.put(
f"/model/{test_models[0].name}", json=test_models[0].dict()
)
assert response.status_code == 404
@pytest.mark.asyncio
async def test_crudrouter_delete_200(test_app, test_models):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
await test_app[2].insert(test_models)
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.delete(f"/model/{test_models[0].name}")
assert response.status_code == 200
result = response.json()
assert result["name"] == test_models[0].name
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.delete(f"/model")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_crudrouter_delete_404(test_app, test_models):
"""Tests that select_or_404 will raise a 404 error on an empty return"""
async with AsyncClient(app=test_app[1], base_url="http://test") as client:
response = await client.delete(f"/model/{test_models[0].name}")
assert response.status_code == 404