-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_basic.py
85 lines (70 loc) · 3.38 KB
/
test_basic.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
import os
import unittest
import base64
import json
from app import app
class BasicTests(unittest.TestCase):
############################
#### setup and teardown ####
############################
# executed prior to each test
def setUp(self):
app.config['TESTING'] = True
app.config['DEBUG'] = False
self.app = app.test_client()
self.assertEqual(app.debug, False)
###############
#### tests ####
###############
def test_main_page(self):
response = self.app.get('/', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_addition(self):
equation = '2 + 2'
base64EncodedBytes = base64.b64encode(equation.encode('UTF-8'))
base64EncodedBytesAsString = str(base64EncodedBytes, 'UTF-8')
response = self.app.get('/calculus?query='+base64EncodedBytesAsString, follow_redirects=True)
responseJson = json.loads(response.data)
self.assertEqual(responseJson['message'], 4)
def test_subtraction(self):
equation = '2 - 2'
base64EncodedBytes = base64.b64encode(equation.encode('UTF-8'))
base64EncodedBytesAsString = str(base64EncodedBytes, 'UTF-8')
response = self.app.get('/calculus?query='+base64EncodedBytesAsString, follow_redirects=True)
responseJson = json.loads(response.data)
self.assertEqual(responseJson['message'], 0)
def test_multiplication(self):
equation = '2 * 2'
base64EncodedBytes = base64.b64encode(equation.encode('UTF-8'))
base64EncodedBytesAsString = str(base64EncodedBytes, 'UTF-8')
response = self.app.get('/calculus?query='+base64EncodedBytesAsString, follow_redirects=True)
responseJson = json.loads(response.data)
self.assertEqual(responseJson['message'], 4)
def test_division(self):
equation = '2 / 2'
base64EncodedBytes = base64.b64encode(equation.encode('UTF-8'))
base64EncodedBytesAsString = str(base64EncodedBytes, 'UTF-8')
response = self.app.get('/calculus?query='+base64EncodedBytesAsString, follow_redirects=True)
responseJson = json.loads(response.data)
self.assertEqual(responseJson['message'], 1.0)
def test_brackets(self):
equation = '2 + (2/2) - 2'
base64EncodedBytes = base64.b64encode(equation.encode('UTF-8'))
base64EncodedBytesAsString = str(base64EncodedBytes, 'UTF-8')
response = self.app.get('/calculus?query='+base64EncodedBytesAsString, follow_redirects=True)
responseJson = json.loads(response.data)
self.assertEqual(responseJson['message'], 1.0)
def test_invalid_expressions_with_syntax_error(self):
equation = '2 + )(2/2) - 2'
base64EncodedBytes = base64.b64encode(equation.encode('UTF-8'))
base64EncodedBytesAsString = str(base64EncodedBytes, 'UTF-8')
response = self.app.get('/calculus?query='+base64EncodedBytesAsString, follow_redirects=True)
responseJson = json.loads(response.data)
self.assertEqual(responseJson['error'], True)
def test_invalid_base64_String(self):
response = self.app.get('/calculus?query=MiAqICgyMy8oMyozKSktIDIzICogKDIqMyk', follow_redirects=True)
responseJson = json.loads(response.data)
print(responseJson['message'])
self.assertEqual(responseJson['error'], True)
if __name__ == "__main__":
unittest.main()