-
Notifications
You must be signed in to change notification settings - Fork 13
/
TestFixtures.py
376 lines (278 loc) · 12.2 KB
/
TestFixtures.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from fastapi_wrapper import cli
import json
import unittest # https://docs.python.org/2/library/unittest.html
class FastAPIWrapperTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testFastAPIWrapper(self):
'''
### Test FastAPI Wrapper
'''
print('### FastAPI Wrapper Test')
# -- Default routes config --
# cli.main(init_routes_with_config_db=True, start_server=False, host='localhost', port=8000)
# -- Named routes config --
# cli.main(config_db='routes_config.db', init_routes_with_config_db=True, start_server=False, host='localhost', port=8000)
cli.main(config_db='apiness_routes_config_TEST.db', init_routes_with_config_db=True, start_server=False, host='localhost', port=8000)
# -- In-memory db + server start --
# cli.main(database=':memory:', data_path='./data/test.csv', data_format='CSV', start_server=True, if_exists='replace', host='localhost', port=8000)
# -- Database creation --
# cli.main(database='test', data_path='./data/test.csv', data_format='CSV', start_server=False, if_exists='replace', host='localhost', port=8000)
# cli.main(database='gcfs', data_path='./data/GCFS Countries.xlsx', data_format='XLSX', host='localhost', port=8001)
class FastAPIRouterTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testFastAPIRouter(self):
'''
### Test FastAPI Router
'''
print('### FastAPI Router Test')
# /routers/my_router.py
from fastapi import APIRouter
router = APIRouter()
@router.get("/some")
@router.get("/someone")
@router.get("/<path:path>")
async def some_path(**kwargs):
pass
@router.get("/path")
async def some_other_path(**kwargs):
pass
@router.post("/some_post_path")
async def some_post_path(**kwargs):
pass
# /main.py
# from routers import my_router
from fastapi import FastAPI
import uvicorn
app = FastAPI()
app.include_router(
# my_router.router,
router,
prefix="/custom_path",
tags=["Endpoints in my router!"],
)
uvicorn.run(app, host='localhost', port=9090)
class FastAPIMultiprocessTests(unittest.TestCase):
def setUp(self):
from fastapi import FastAPI
class FastAPI_MiniWrapper(FastAPI):
def __init__(self) -> None:
"""
Initializes a FastAPI instance that serves data from a CSV file.
"""
print('Initializing FastAPI_Wrapper...')
super().__init__()
from typing import Optional
@self.get("/shutdown")
async def shutdown():
# Add shutdown event (would only be of any use in a multi-process, not multi-thread situation)
import os
import time
import psutil
import threading
def suicide():
time.sleep(1)
# parent = psutil.Process(psutil.Process(os.getpid()).ppid())
# parent.kill()
myself = psutil.Process(os.getpid())
myself.kill()
threading.Thread(target=suicide, daemon=True).start()
print(f'>>> Successfully killed API <<<')
return {"success": True}
self.on_event('shutdown')(shutdown)
@self.get("/")
def read_root():
return {"Hello": "World"}
@self.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}
# Pickle/Dill hook
def __getstate__(self):
# this method is called when you are
# going to pickle the class, to know what to pickle
state = self.__dict__.copy()
# don't pickle the parameter openapi. otherwise will raise
# AttributeError: Can't pickle local object 'FastAPI.setup.<locals>.openapi'
# E.g.
# del state['openapi']
#
# See here for further inspiration: https://gist.github.com/aryzhov/067ecc12d39715217d787896a34915c6
def do_something(val):
return val
for k, v in state.items():
if type(v) not in {int, float, str, tuple, None}:
state[k] = do_something(v)
return state
# Pickle/Dill hook
def __setstate__(self, state):
self.__dict__.update(state)
# Pickle/Dill hook
def __setstate__(self, state):
def do_something(state):
return state
ustate = do_something(state)
self.__dict__.update(ustate)
self.app = FastAPI_MiniWrapper()
def tearDown(self):
pass
# NOTE! Can't get this forked process to work since the FastAPI_Wrapper has nested
# classes and methods which can't be serialized / pickled, so must use multi-threading :-(
# (See:https://medium.com/@jwnx/multiprocessing-serialization-in-python-with-pickle-9844f6fa1812)
def HIDDEN_testFastAPIProcessFork(self):
'''
### Test FastAPI Process Fork
'''
print('### FastAPI Process Fork Test')
import os
from multiprocessing import Process
import uvicorn
import time
import dill as pickle
# A little check here to try to fix the pickle issues
# Pickling will invoke the __getstate__, __setstate__ hooks
# where the pickle state can be modified
filepath = os.path.join('./pickle', f'fastapi-wrapper.pkl')
try:
with open(filepath, 'wb') as f:
pickle.dump(self.app, f)
with open(filepath, 'rb') as f:
self.app = pickle.load(f)
except Exception as e:
print(f'Error - got into a pickle! {e}')
proc = Process( target=uvicorn.run,
args=(self.app,),
kwargs={
'host': '127.0.0.1',
'port': 6000,
'log_level': 'info'
},
daemon=True )
proc.start()
time.sleep(1.0) # time for the server to start
# NOTE! This blocks so shouldn't really use in test suite
def HIDDEN_testFastAPIThread(self):
'''
### Test FastAPI Thread
'''
print('### FastAPI Thread Test')
import threading
import uvicorn
# BLOCKING
def thread_runner(app, host, port):
uvicorn.run(app, host=host, port=port)
thread_name = 'testFastAPIThread'
print(f'>>> Starting {thread_name} <<<')
thread = threading.Thread(name=thread_name, target=thread_runner, args=(self.app, '127.0.0.1', 7000))
thread.start()
def HIDDEN_testFastAPIStoppableThread(self):
'''
### Test FastAPI StoppableThread
'''
print('### FastAPI StoppableThread Test')
from utils.UvicornServer import Server
server = Server(app=self.app, host='127.0.0.1', port=8000, log_level='info')
# BLOCKING + AUTO-TERMINATING
with server.run_in_thread():
print('Server is started.')
input('\n>>> Press Enter to stop...\n\n')
# ...
# Server will be stopped once code put here is completed
# ...
print('Server is stopped.')
# WARNING: Run this test in the debugger
# Terminate the job by calling /shutdown api
def testFastAPIJobBootstrapper(self):
'''
### Test FastAPI Job Bootstrapper
'''
print('### FastAPI Job Bootstraapper Test')
import os
import subprocess
import threading
import requests
import time
def run(job):
print (f"\nRunning job: {job}\n")
proc = subprocess.Popen(job)
proc.wait()
return proc
def start_shutdown_thread(delay):
def shutdown(delay):
print (f"Sleeping for {delay} secs before shutdown...")
time.sleep(delay)
requests.get('http://127.0.0.1:8000/shutdown')
thread = threading.Thread(target=shutdown, args=(delay,), daemon=True)
thread.start()
job = ['python', os.path.join('./', 'bootstrapper.py'), 'routes_config.db', '127.0.0.1', '8000']
### BLOCKING SERVER ###
# start shutdown thread
start_shutdown_thread(10)
# THE MAIN THREAD WILL BLOCK (must be killed via /shutdown API call)
run(job)
### NON-BLOCKING SERVER ###
# start shutdown thread
start_shutdown_thread(10)
# NON-BLOCKING - THE MAIN THREAD WILL NOT BLOCK (the process will end when the test ends)
thread = threading.Thread(name='FastAPI-Bootstrapper', target=run, args=(job,), daemon=True)
thread.start()
# we can wait for server to start, and then...
time.sleep(5)
# ...make a web request
response = requests.get('http://127.0.0.1:8000/test/test?cmd=LIMIT%203')
print(response.text)
# this waits for the thread to end (i.e. when it's killed by the shutdown thread!)
thread.join()
class MiscTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def HIDE_testRandom(self):
'''
### Test Misc
'''
print('### Misc Test')
from flatten_dict import flatten, unflatten
from flatten_dict.reducer import make_reducer
from flatten_dict.splitter import make_splitter
from pprint import pprint
state = {'a': {'b': {'c': 1, 'd': {'e': 2}, 'f': [3, 4, 5, {'g': {'h': 6}}]}}}
pprint(state)
fstate = flatten(state, reducer=make_reducer(delimiter='.'))
pprint(fstate)
state = unflatten(fstate, splitter=make_splitter(delimiter='.'))
pprint(state)
def testRoutesConfigDb(self):
'''
### Test Routes Config
'''
print('### Misc Routes Config')
from fastapi_wrapper.fastapi_wrapper import query_database
routes = query_database('routes_config.db', 'select route_path from routes_config')
print(routes)
for route in routes:
route_path = route['route_path']
print(route_path)
route_name = query_database('routes_config.db', f'select route_name from routes_config where route_path="{route_path}"')
print(route_name[0]['route_name'])
route_tags_json = query_database('routes_config.db', f'select route_tags from routes_config where route_path="{route_path}"')
route_tags = json.loads(route_tags_json[0]['route_tags'])
print(route_tags)
query_params_json = query_database('routes_config.db', f'select query_params from routes_config where route_path="{route_path}"')
query_params = json.loads(query_params_json[0]['query_params'])
for query_param in query_params:
qp = query_param[1]
type_ = str if query_param[2] == 'str' else (float if query_param[2] == 'float' else int)
print(f'{qp} / {type_}')
if __name__ == '__main__':
# suite = unittest.TestLoader().loadTestsFromTestCase(MiscTests)
# suite = unittest.TestLoader().loadTestsFromTestCase(FastAPIMultiprocessTests)
suite = unittest.TestLoader().loadTestsFromTestCase(FastAPIWrapperTests)
# suite = unittest.TestLoader().loadTestsFromTestCase(FastAPIRouterTests)
unittest.TextTestRunner(verbosity=2).run(suite)
#unittest.main()