Skip to content

Commit ea864ad

Browse files
committed
Add tests for AsyncClient
1 parent c1d6515 commit ea864ad

1 file changed

Lines changed: 45 additions & 13 deletions

File tree

tests/test_webservice.py

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import os
2-
1+
import asyncio
32
import json
3+
import os
44
from io import open
5+
from typing import Type, Union
56

67
# httpretty currently doesn't work, but mocket with the compat interface
78
# does.
@@ -22,6 +23,8 @@
2223

2324

2425
class BaseTest(unittest.TestCase):
26+
client_class: Union[Type[AsyncClient], Type[Client]] = Client
27+
2528
def setUp(self):
2629
self.client = self.client_class(42, "abcdef123456")
2730

@@ -132,7 +135,7 @@ def create_error(self, status_code=400, text="", content_type=None):
132135
body=text,
133136
content_type=content_type,
134137
)
135-
return getattr(self.client, self.type)(self.full_request)
138+
return self.run_client(getattr(self.client, self.type)(self.full_request))
136139

137140
def create_success(self, text=None, client=None, request=None):
138141
uri = "/".join(
@@ -154,26 +157,29 @@ def create_success(self, text=None, client=None, request=None):
154157
if request is None:
155158
request = self.full_request
156159
print(client)
157-
return getattr(client, self.type)(request)
160+
return self.run_client(getattr(client, self.type)(request))
161+
162+
def run_client(self, v):
163+
return v
158164

159165
@httprettified
160166
def test_named_constructor_args(self):
161167
id = "47"
162168
key = "1234567890ab"
163169
for client in (
164-
Client(account_id=id, license_key=key),
165-
Client(account_id=id, license_key=key),
170+
self.client_class(account_id=id, license_key=key),
171+
self.client_class(account_id=id, license_key=key),
166172
):
167173
self.assertEqual(client._account_id, id)
168174
self.assertEqual(client._license_key, key)
169175

170176
@httprettified
171177
def test_missing_constructor_args(self):
172178
with self.assertRaises(TypeError):
173-
Client(license_key="1234567890ab")
179+
self.client_class(license_key="1234567890ab")
174180

175181
with self.assertRaises(TypeError):
176-
Client("47")
182+
self.client_class("47")
177183

178184

179185
class BaseTransactionTest(BaseTest):
@@ -205,7 +211,7 @@ def test_200_on_request_with_nones(self):
205211
@httprettified
206212
def test_200_with_locales(self):
207213
locales = ("fr",)
208-
client = Client(42, "abcdef123456", locales=locales)
214+
client = self.client_class(42, "abcdef123456", locales=locales)
209215
model = self.create_success(client=client)
210216
response = json.loads(self.response)
211217
if self.has_ip_location():
@@ -245,29 +251,25 @@ def test_insufficient_funds(self):
245251
class TestFactors(BaseTransactionTest):
246252
type = "factors"
247253
cls = Factors
248-
client_class = Client
249254
request_file = "full-transaction-request.json"
250255
response_file = "factors-response.json"
251256

252257

253258
class TestInsights(BaseTransactionTest):
254259
type = "insights"
255260
cls = Insights
256-
client_class = Client
257261
request_file = "full-transaction-request.json"
258262
response_file = "insights-response.json"
259263

260264

261265
class TestScore(BaseTransactionTest):
262266
type = "score"
263267
cls = Score
264-
client_class = Client
265268
request_file = "full-transaction-request.json"
266269
response_file = "score-response.json"
267270

268271

269272
class TestReportTransaction(BaseTest):
270-
client_class = Client
271273
type = "report"
272274
request_file = "full-report-request.json"
273275
response_file = "report-response.json"
@@ -290,4 +292,34 @@ def test_204_on_request_with_nones(self):
290292
)
291293

292294

295+
class AsyncBase:
296+
def setUp(self):
297+
self._loop = asyncio.new_event_loop()
298+
super().setUp()
299+
300+
def tearDown(self):
301+
self._loop.run_until_complete(self.client.close())
302+
self._loop.close()
303+
super().tearDown()
304+
305+
def run_client(self, v):
306+
return self._loop.run_until_complete(v)
307+
308+
309+
class TestAsyncFactors(AsyncBase, TestFactors):
310+
client_class = AsyncClient
311+
312+
313+
class TestAsyncInsights(AsyncBase, TestInsights):
314+
client_class = AsyncClient
315+
316+
317+
class TestAsyncScore(AsyncBase, TestScore):
318+
client_class = AsyncClient
319+
320+
321+
class TestAsyncReportTransaction(AsyncBase, TestReportTransaction):
322+
client_class = AsyncClient
323+
324+
293325
del BaseTest, BaseTransactionTest

0 commit comments

Comments
 (0)