-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_api.py
70 lines (52 loc) · 2.48 KB
/
test_api.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
import unittest
from app import db, app
import json
import time
class endpointsCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.sqlite3'
self.app = app.test_client()
self.app.testing = True
db.create_all()
def tearDown(self):
db.drop_all()
def test_empty_query(self):
self.assertEqual(self.app.get("/news").status_code, 404)
self.assertEqual(self.app.get("/stock-graph").status_code, 404)
self.assertEqual(self.app.get("/get-summary").status_code, 404)
self.assertEqual(self.app.get("/stock-graph?query=tata").status_code,
404)
def test_endpoints_status_code(self):
self.assertEqual(self.app.get("/news?query=tesla").status_code, 200)
self.assertEqual(self.app.get("/stock-graph?query=tesla").status_code, 200)
self.assertEqual(self.app.get("/get-summary?query=tesla").status_code, 200)
self.assertEqual(self.app.get("/news?query=tesla").status_code, 200)
def test_endpoints_fake_ticker(self):
self.assertEqual(self.app.get("/news?query=desdaey';';'aefaeuf").status_code, 404)
self.assertEqual(self.app.get("/stock-graph?query=desdaey';';'aefaeuf").status_code, 404)
self.assertEqual(self.app.get("/get-summary?query=desdaey';';'aefaeuf").status_code, 404)
@staticmethod
def checkResponse(resp):
assert len(resp["predict"]) == 4
assert resp["predict"]["1"] is not None
assert resp["predict"]["7"] is not None
assert resp["predict"]["15"] is not None
assert resp["predict"]["30"] is not None
def test_response_object(self):
try:
self.checkResponse(json.loads(self.app.get("/news?query=Tesla").data))
except Exception as e: # pragma: no cover
self.fail(msg="Response format is invalid: " + str(e)) # pragma: no cover
def test_invalid_ticker(self):
self.assertEqual(self.app.get("/news?query=narendramodi").status_code, 200)
self.assertEqual(self.app.get("/stock-graph?query=narendramodi").status_code, 404)
def test_timeout_query(self):
self.assertEqual(self.app.get("/news?query=tesla").status_code, 200)
time.sleep(6*60)
self.assertEqual(self.app.get("/news?query=tesla").status_code, 200)