forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
136 lines (100 loc) · 3.36 KB
/
conftest.py
File metadata and controls
136 lines (100 loc) · 3.36 KB
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
"""
tests.conftest.py
Global conftest file for shared pytest fixtures
"""
import datetime
import os
import pytest
from async_asgi_testclient import TestClient as AsyncTestClient
from fastapi.testclient import TestClient
from app.main import APP
from app.utils import httputils
try:
from unittest.mock import AsyncMock
except ImportError:
# Python 3.7 backwards compat
from asyncmock import AsyncMock
try:
from contextlib import asynccontextmanager
except ImportError:
# Python 3.6 backwards compat
from async_generator import asynccontextmanager
@pytest.fixture
def api_client():
"""
Returns a fastapi.testclient.TestClient.
The test client uses the requests library for making http requests.
"""
return TestClient(APP)
@pytest.fixture
async def async_api_client():
"""
Returns an async_asgi_testclient.TestClient.
"""
return AsyncTestClient(APP)
class DateTimeStrpTime:
"""Returns instance of `DateTimeStrpTime`
when calling `app.services.location.jhu.datetime.trptime(date, '%m/%d/%y').isoformat()`.
"""
def __init__(self, date, strformat):
self.date = date
self.strformat = strformat
def isoformat(self):
return datetime.datetime.strptime(self.date, self.strformat).isoformat()
class FakeRequestsGetResponse:
"""Fake instance of a response from `aiohttp.ClientSession.get`.
"""
def __init__(self, url, filename, state):
self.url = url
self.filename = filename
self.state = state
async def text(self):
return self.read_file(self.state)
def read_file(self, state):
"""
Mock HTTP GET-method and return text from file
"""
state = state.lower()
# Determine filepath.
filepath = os.path.join(os.path.dirname(__file__), "example_data/{}.csv".format(state))
# Return fake response.
print("Try to read {}".format(filepath))
with open(filepath, "r") as file:
return file.read()
@pytest.fixture(scope="class")
def mock_client_session_class(request):
"""Class fixture to expose an AsyncMock to unittest.TestCase subclasses.
See: https://docs.pytest.org/en/5.4.1/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks
"""
httputils.CLIENT_SESSION = request.cls.mock_client_session = AsyncMock()
httputils.CLIENT_SESSION.get = mocked_session_get
try:
yield
finally:
del httputils.CLIENT_SESSION
@pytest.fixture
async def mock_client_session():
"""Context manager fixture that replaces the global client_session with an AsyncMock
instance.
"""
httputils.CLIENT_SESSION = AsyncMock()
httputils.CLIENT_SESSION.get = mocked_session_get
try:
yield httputils.CLIENT_SESSION
finally:
del httputils.CLIENT_SESSION
@asynccontextmanager
async def mocked_session_get(*args, **kwargs):
"""Mock response from client_session.get.
"""
url = args[0]
filename = url.split("/")[-1]
# clean up for id token (e.g. Deaths)
state = filename.split("-")[-1].replace(".csv", "").lower().capitalize()
yield FakeRequestsGetResponse(url, filename, state)
def mocked_strptime_isoformat(*args, **kwargs):
"""Mock return value from datetime.strptime().isoformat().
"""
date = args[0]
strformat = args[1]
return DateTimeStrpTime(date, strformat)