1
|
|
|
from yfrake import server, ServerSingleton |
2
|
|
|
import aiohttp |
3
|
|
|
import asyncio |
4
|
|
|
import pytest |
5
|
|
|
import json |
6
|
|
|
import time |
7
|
|
|
import sys |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
if sys.platform == 'win32': |
11
|
|
|
asyncio.set_event_loop_policy( |
12
|
|
|
asyncio.WindowsSelectorEventLoopPolicy() |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
async def test_server(): |
17
|
|
|
server.start() |
18
|
|
|
while not server.is_running(): |
19
|
|
|
time.sleep(0) |
20
|
|
|
assert server.is_running() |
21
|
|
|
|
22
|
|
|
url = 'http://localhost:8888/quote_type' |
23
|
|
|
params = dict(symbol='msft') |
24
|
|
|
|
25
|
|
|
async with aiohttp.ClientSession() as session: |
26
|
|
|
async with session.get(url=url, params=params) as resp: |
27
|
|
|
data = await resp.text() |
28
|
|
|
resp = json.loads(data) |
29
|
|
|
|
30
|
|
|
endpoint = resp.get('endpoint', False) |
31
|
|
|
error = resp.get('error', False) |
32
|
|
|
data = resp.get('data', False) |
33
|
|
|
|
34
|
|
|
assert endpoint is not False |
35
|
|
|
assert error is not False |
36
|
|
|
assert data is not False |
37
|
|
|
|
38
|
|
|
assert isinstance(endpoint, str) |
39
|
|
|
assert isinstance(error, type(None)) |
40
|
|
|
assert isinstance(data, dict) |
41
|
|
|
|
42
|
|
|
assert len(data) > 0 |
43
|
|
|
assert data.get('symbol') == 'MSFT' |
44
|
|
|
|
45
|
|
|
server.stop() |
46
|
|
|
while server.is_running(): |
47
|
|
|
time.sleep(0) |
48
|
|
|
assert not server.is_running() |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def test_exceptions_and_singleton(): |
52
|
|
|
_server = ServerSingleton() |
53
|
|
|
_server.start() |
54
|
|
|
while not _server.is_running(): |
55
|
|
|
time.sleep(0) |
56
|
|
|
with pytest.raises(RuntimeError): |
57
|
|
|
_server.start() |
58
|
|
|
|
59
|
|
|
time.sleep(0) |
60
|
|
|
|
61
|
|
|
_server.stop() |
62
|
|
|
while _server.is_running(): |
63
|
|
|
time.sleep(0) |
64
|
|
|
with pytest.raises(RuntimeError): |
65
|
|
|
_server.stop() |
66
|
|
|
|