-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
316 lines (240 loc) · 11.5 KB
/
tests.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
"""This module contains all tests."""
import http
import unittest
import json
from unittest.mock import patch
import xml.etree.ElementTree as ET
import xmltodict
import requests
import models
import api
from app import app
def get_privat_response(*args, **kwds):
"""Mock function to omit real calls to external PrivatBank's API
Returns:
Response: An instance of a mock class that pretends to be an instance of real
Response class and contains some pre-defined values
"""
print("get_privat_response")
class Response:
def __init__(self, response):
self.text = json.dumps(response)
def json(self):
return json.loads(self.text)
return Response([{"ccy": "USD", "base_ccy": "UAH", "sale": "30.0"}])
class Test(unittest.TestCase):
# def setUp(self):
# models.init_db()
@unittest.skip("skip")
def test_privat_usd(self):
""" Procedure:
1. Get current USD-UAH rate from the DB
2. Update USD-UAH rate using PrivatBank API
3. Compare previous and updated values (predict that the new one is greater)
4. Get corresponding (the latest) entry from external API calls log
---------
Verification:
5. Updated rate greater than 25
6. Updated rate greater than previous value (TODO: upd this part)
7. API log contains something
8. API log contains corresponding request URL
9. API log the response contains some text
10. Response text contains expected JSON data pattern (UAH and USD strings)
"""
xrate = models.XRate.get(from_currency=840, to_currency=980)
updated_before = xrate.updated
api.update_rate(840, 980)
xrate = models.XRate.get(from_currency=840, to_currency=980)
updated_after = xrate.updated
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertGreater(xrate.rate, 25)
self.assertGreater(updated_after, updated_before)
self.assertIsNotNone(api_log)
self.assertEqual(api_log.request_url, "https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11")
self.assertIsNotNone(api_log.response_text)
self.assertIn('{"ccy":"USD","base_ccy":"UAH",', api_log.response_text)
@unittest.skip("skip")
def test_privat_btc(self):
xrate = models.XRate.get(from_currency=1000, to_currency=840)
updated_before = xrate.updated
api.update_rate(1000, 840)
xrate = models.XRate.get(from_currency=1000, to_currency=840)
updated_after = xrate.updated
self.assertGreater(xrate.rate, 4000)
self.assertGreater(updated_after, updated_before)
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertIsNotNone(api_log)
self.assertEqual(api_log.request_url, "https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11")
@unittest.skip("skip")
def test_cbr(self):
xrate = models.XRate.get(from_currency=840, to_currency=643)
updated_before = xrate.updated
api.update_rate(840, 643)
xrate = models.XRate.get(from_currency=840, to_currency=643)
updated_after = xrate.updated
self.assertGreater(xrate.rate, 60)
self.assertGreater(updated_after, updated_before)
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertIsNotNone(api_log)
self.assertEqual(api_log.request_url, "http://www.cbr.ru/scripts/XML_daily.asp")
self.assertIsNotNone(api_log.response_text)
self.assertIn("<NumCode>840</NumCode>", api_log.response_text)
@unittest.skip("skip")
@patch('api._Api._send', new=get_privat_response)
def test_privat_mock(self):
xrate = models.XRate.get(id=1)
updated_before = xrate.updated
api.update_rate(840, 980)
xrate = models.XRate.get(id=1)
updated_after = xrate.updated
self.assertEqual(xrate.rate, 30)
self.assertGreater(updated_after, updated_before)
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertIsNotNone(api_log)
self.assertEqual(api_log.request_url, "https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11")
self.assertIsNotNone(api_log.response_text)
self.assertEqual('[{"ccy": "USD", "base_ccy": "UAH", "sale": "30.0"}]', api_log.response_text)
@unittest.skip("skip")
def test_api_error(self):
api.HTTP_TIMEOUT = 0.001
xrate = models.XRate.get(id=1)
updated_before = xrate.updated
self.assertRaises(requests.exceptions.RequestException, api.update_rate, 840, 980)
xrate = models.XRate.get(id=1)
updated_after = xrate.updated
self.assertEqual(xrate.rate, 1.0)
self.assertEqual(updated_after, updated_before)
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertIsNotNone(api_log)
self.assertEqual(api_log.request_url, "https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11")
self.assertIsNone(api_log.response_text)
self.assertIsNotNone(api_log.error)
error_log = models.ErrorLog.select().order_by(models.ErrorLog.created.desc()).first()
self.assertIsNotNone(error_log)
self.assertEqual(error_log.request_url, "https://api.privatbank.ua/p24api/pubinfo?exchange&json&coursid=11")
self.assertIsNotNone(error_log.traceback)
self.assertEqual(api_log.error, error_log.error)
self.assertIn("Connection to api.privatbank.ua timed out", error_log.error)
api.HTTP_TIMEOUT = 15
@unittest.skip("skip")
def test_cryptonator_uah(self):
from_currency = 1000
to_currency = 980
xrate = models.XRate.get(from_currency=from_currency, to_currency=to_currency)
updated_before = xrate.updated
self.assertEqual(xrate.rate, 1.0)
api.update_rate(from_currency, to_currency)
xrate = models.XRate.get(from_currency=from_currency, to_currency=to_currency)
updated_after = xrate.updated
self.assertGreater(xrate.rate, 100000)
self.assertGreater(updated_after, updated_before)
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertIsNotNone(api_log)
self.assertEqual(api_log.request_url, "https://api.cryptonator.com/api/ticker/btc-uah")
self.assertIsNotNone(api_log.response_text)
self.assertIn('{"base":"BTC","target":"UAH","price":', api_log.response_text)
@unittest.skip("skip")
def test_blockchaininfo_rub(self):
from_currency = 1000
to_currency = 643
xrate = models.XRate.get(from_currency=from_currency, to_currency=to_currency)
updated_before = xrate.updated
self.assertEqual(xrate.rate, 1.0)
api.update_rate(from_currency, to_currency)
xrate = models.XRate.get(from_currency=from_currency, to_currency=to_currency)
updated_after = xrate.updated
self.assertGreater(xrate.rate, 100000)
self.assertGreater(updated_after, updated_before)
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertIsNotNone(api_log)
self.assertEqual(api_log.request_url, "https://blockchain.info/ticker")
self.assertIsNotNone(api_log.response_text)
self.assertIn('"RUB": {', api_log.response_text)
@unittest.skip("skip")
def test_coinmarketcap_uah(self):
from_currency = 1000
to_currency = 980
xrate = models.XRate.get(from_currency=from_currency, to_currency=to_currency)
updated_before = xrate.updated
self.assertEqual(xrate.rate, 1.0)
api.update_rate(from_currency, to_currency)
xrate = models.XRate.get(from_currency=from_currency, to_currency=to_currency)
updated_after = xrate.updated
self.assertGreater(xrate.rate, 100000)
self.assertGreater(updated_after, updated_before)
api_log = models.ApiLog.select().order_by(models.ApiLog.created.desc()).first()
self.assertIsNotNone(api_log)
self.assertIn('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=',
api_log.request_url)
self.assertIsNotNone(api_log.response_text)
self.assertIn('"quote":{"UAH":{"price":', api_log.response_text)
def test_xml_api(self):
client = app.test_client()
r = client.get("http://localhost:5000/api/xrates/xml")
self.assertIn("<xrates>", r.text)
xml_rates = xmltodict.parse(r.text)
self.assertIn("xrates", xml_rates)
self.assertIsInstance(xml_rates["xrates"]["xrate"], list)
self.assertEqual(len(xml_rates["xrates"]["xrate"]), 5)
def test_json_api(self):
client = app.test_client()
r = client.get("http://localhost:5000/api/xrates/json")
json_rates = r.json
self.assertIsInstance(json_rates, list)
self.assertEqual(len(json_rates), 5)
for rate in json_rates:
self.assertIn("from", rate)
self.assertIn("to", rate)
self.assertIn("rate", rate)
def test_json_api_uah(self):
client = app.test_client()
r = client.get("http://localhost:5000/api/xrates/json?to_currency=980")
json_rates = r.json
self.assertIsInstance(json_rates, list)
self.assertEqual(len(json_rates), 2)
@unittest.skip("skip")
def test_html_xrates(self):
client = app.test_client()
r = client.get("http://localhost:5000/xrates")
self.assertEqual(r.status_code, http.HTTPStatus.OK)
self.assertIn('<table border="1">', r.text)
root = ET.fromstring(r.text)
body = root.find("body")
self.assertIsNotNone(body)
table = body.find("table")
self.assertIsNotNone(table)
rows = table.findall("tr")
self.assertEqual(len(rows), 5)
@unittest.skip("skip")
def test_html_logs(self):
client = app.test_client()
r = client.get("http://localhost:5000/logs/api/html")
self.assertEqual(r.status_code, http.HTTPStatus.OK)
root = ET.fromstring(r.text)
body = root.find("body")
self.assertIsNotNone(body)
ul = body.find("tr")
self.assertIsNotNone(ul)
lis = ul.findall("td")
self.assertGreaterEqual(len(lis), 0)
def test_json_errors(self):
client = app.test_client()
r = client.get("http://localhost:5000/logs/errors/json")
self.assertEqual(r.status_code, http.HTTPStatus.OK)
r = r.json
self.assertEqual(type(r), list)
def test_xrate_edit(self):
client = app.test_client()
from_currency, to_currency = 840, 980
xrate = models.XRate.get(from_currency=from_currency, to_currency=to_currency)
old_rate = xrate.rate
data = {'new_rate': 999.0}
r = client.post(f'http://127.0.0.1:5000/edit/{from_currency}/{to_currency}', data=data)
self.assertEqual(r.status_code, http.HTTPStatus.FOUND)
# print(models.XRate.get(from_currency=from_currency, to_currency=to_currency).rate)
self.assertEqual(models.XRate.get(from_currency=from_currency, to_currency=to_currency).rate, 999.0)
xrate.rate = old_rate
xrate.save()
self.assertEqual(models.XRate.get(from_currency=from_currency, to_currency=to_currency).rate, 1.0)
if __name__ == '__main__':
unittest.main()