-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
184 lines (148 loc) · 10.3 KB
/
test.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
from forex_python.converter import CurrencyRates, CurrencyCodes
from unittest import TestCase
from app import app
from flask import session
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
currency = CurrencyRates()
code = CurrencyCodes()
class ViewTesting(TestCase):
def test_home_view(self):
"""basic test logic for main home view."""
with app.test_client() as client:
res = client.get('/')
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn('<button class="btn btn-primary">Convert</button>', html)
def test_conversion_view(self):
"""basic test for conversion route (route that will be rendered once the form is submitted)."""
with app.test_client() as client:
res = client.get('/conversion')
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn('<button class="btn btn-primary">Home</button>', html)
def test_calculate_form(self):
"""basic test for calculate route once form is submitted.
NOTE: The forex API for Python uses live information based on CURRENT currency exchance rates, your result amount will need to be updated based on the calculation that is received whenever you ping the API in your terminal/app.
"""
with app.test_client() as client:
form_data = {'current-currency':'usd', 'new-currency':'jpy', 'amount':100}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("<h1>The result is: ¥ 13817.04</h1>", html)
def test_calculate_form_usd_to_eur(self):
"""second basic form data test (from usd to eur)
NOTE: The forex API for Python uses live information based on CURRENT currency exchance rates, your result amount will need to be updated based on the calculation that is received whenever you ping the API in your terminal/app.
"""
with app.test_client() as client:
form_data = {'current-currency':'usd', 'new-currency':'eur', 'amount':100}
res = client.post('/calculate', data= form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("<h1>The result is: € 98.05</h1>", html)
def test_blank_form_redirect_response(self):
"""function is testing for redirect status code and location."""
with app.test_client() as client:
form_data = {'current-currency':'', 'new-currency':'', 'amount': ''}
res = client.post('/calculate', data=form_data)
html = res.get_data(as_text=True)
# NOTE: code below is testing to see if the blank form gets the redirect code and if the location is the same as the main route per the logic in app.py file.
self.assertEqual(res.status_code, 302)
self.assertEqual(res.location, 'http://localhost/')
def test_blank_form_redirect_view(self):
"""function is testing redirect view whenever a blank form is submitted."""
# NOTE: Code below is testing to see if the actual view of the redirect (html code) is being seen and if the correct message is being displayed on the alert at the top of the page after a user is redirected.
with app.test_client() as client:
form_data = {'current-currency':'', 'new-currency':'', 'amount': ''}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
ALL FIELDS MUST BE FILLED IN BEFORE SUBMITTING!
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_blank_from_curr_input(self):
"""function is testing the first input of the form in the main view route and verifying that the correct message is being seen if nothing is entered in but the rest of the fields are."""
with app.test_client() as client:
form_data = {'current-currency':'', 'new-currency':'jpy', 'amount': 1000}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
FIRST FIELD CANNOT BE BLANK!
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_blank_to_curr_input(self):
"""function is testing the second input of the form in the main view route and verifying that the correct message is being seen if nothing is entered under the second input area."""
with app.test_client() as client:
form_data = {'current-currency':'eur', 'new-currency':'', 'amount': 1000}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
SECOND FIELD CANNOT BE BLANK!
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_blank_amount_input(self):
"""function is testing the third input of the form in the main view route and verifying that the correct message is being seen if nothing is entered under the third input area."""
with app.test_client() as client:
form_data = {'current-currency':'eur', 'new-currency':'usd', 'amount':''}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
AMOUNT CANNOT BE BLANK!
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_invalid_amount(self):
"""function is testing value amount to verify that if a user enters a string instead of a number, they will be routed to the main page again and the main page will show the correct error message"""
with app.test_client() as client:
form_data = {'current-currency':'eur', 'new-currency':'usd', 'amount':'asdfasdf'}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
AMOUNT MUST BE A NUMBER!
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_invalid_from_curr(self):
"""function is testing invalid first currecy code input and making sure that the correct message is being seen after the user is routed back to the main page."""
with app.test_client() as client:
form_data = {'current-currency':'aaa', 'new-currency':'usd', 'amount': 100}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
INVALID CURRENCY CODE: AAA
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_invalid_to_curr(self):
"""function is testing invalid second currecy code input and making sure that the correct message is being seen after the user is routed back to the main page."""
with app.test_client() as client:
form_data = {'current-currency':'usd', 'new-currency':'zzz', 'amount': 100}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
INVALID CURRENCY CODE: ZZZ
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_invalid_currencies(self):
"""function is testing if both currecy code inputs are invalid and making sure that the correct message is being seen after the user is routed back to the main page."""
with app.test_client() as client:
form_data = {'current-currency':'aaa', 'new-currency':'zzz', 'amount': 100}
res = client.post('/calculate', data=form_data, follow_redirects=True)
html = res.get_data(as_text=True)
self.assertEqual(res.status_code, 200)
self.assertIn("""<div class="alert alert-danger alert-dismissible fade show" role="alert">
BOTH CURRENCY CODES INVALID: AAA, ZZZ
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>""", html)
def test_session_info(self):
"""function is testing to verify that all requried information is stored in the session object based on the form data that is passed through."""
with app.test_client() as client:
form_data = {'current-currency':'usd', 'new-currency':'jpy', 'amount':100}
res = client.post('/calculate', data= form_data, follow_redirects=True)
self.assertEqual(res.status_code, 200)
self.assertEqual(session['result'], 13817.04)
self.assertEqual(session['currency_sign'], '¥')