Skip to content

Commit ac4407b

Browse files
authored
Merge pull request #60 from cs130-w21/bug-fix
Bug Fixes pull
2 parents 5dc546a + 4278e5a commit ac4407b

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

flaskr/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def create_app(test_config=None):
5858
"""
5959

6060
app = Flask(__name__, instance_relative_config=True)
61-
finnhub_client = make_client(api_key="sandbox_c0bfrg748v6to0roveg0")
61+
finnhub_client = make_client(api_key="c0bfrg748v6to0rovefg")
6262

6363
# Load config (if it exists) or take a test config
6464
if test_config is None:
@@ -113,13 +113,18 @@ def index():
113113
stock_symbol = request.form['stock'].upper()
114114
volume = request.form['volume']
115115
symbol_quote = finnhub_client.quote(stock_symbol)
116+
etf_country = finnhub_client.etfs_country_exp(stock_symbol)
116117
error = None
117118
if not volume.isdigit():
118119
error = "Number of Shares must be a positive integer"
119120
elif int(volume) < 1:
120121
error = "Number of Shares must be a positive integer"
122+
elif int(volume) > 1e20:
123+
error = "Number of Shares too large"
121124
elif symbol_quote['c'] == 0:
122125
error = "Invalid stock symbol: {}".format(stock_symbol)
126+
elif etf_country['symbol'] != '':
127+
error = "Cannot process ETF: {}".format(stock_symbol)
123128

124129
# TODO: Send form results to DB, fetch all added stocks and display them
125130
# Code below uses Flask session to store data which can be moved to

tests/test_index.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
TEST_SHARES = '1'
55
TEST_STOCK2 = 'AAPL'
66
TEST_SHARES2 = '3'
7+
TEST_ETF = 'VOO'
78
ENCODING = 'utf-8'
89

910
# run with `pytest`
@@ -36,6 +37,18 @@ def test_index(client):
3637
assert b"Number of Shares must be a positive integer" in post_response.data
3738
assert bytes(TEST_STOCK2, encoding=ENCODING) not in post_response.data
3839

40+
# Add humongous volume
41+
post_response = client.post('/', data=dict(
42+
stock=TEST_STOCK2,
43+
volume='29999313486231599000000000000000000000000000000000000'+
44+
'0000000000000000000000000000000000000000000000000000000000000'+
45+
'00000000000000000000000000000000000000000000000000000100000000'+
46+
'000000000000000000000000000000000000000000000000000000000000000'+
47+
'0000000000000000000000000000000000000000000000000000000000000000000010'
48+
), follow_redirects=True)
49+
assert b"Number of Shares too large" in post_response.data
50+
assert bytes(TEST_STOCK2, encoding=ENCODING) not in post_response.data
51+
3952
# Add non-positive volume
4053
post_response = client.post('/', data=dict(
4154
stock=TEST_STOCK2,
@@ -44,6 +57,14 @@ def test_index(client):
4457
assert b"Number of Shares must be a positive integer" in post_response.data
4558
assert bytes(TEST_STOCK2, encoding=ENCODING) not in post_response.data
4659

60+
# Add ETF stock symbol
61+
post_response = client.post('/', data=dict(
62+
stock=TEST_ETF,
63+
volume='3'
64+
), follow_redirects=True)
65+
assert bytes("Cannot process ETF: {}".format(TEST_ETF),
66+
encoding=ENCODING) in post_response.data
67+
4768
# Add TEST_STOCK2
4869
post_response = client.post('/', data=dict(
4970
stock=TEST_STOCK2,

tests/test_results.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def fake_financials(_self, symbol, typ):
109109
def test_sectors(client, monkeypatch):
110110
def fake_profile2(_self, **params):
111111
return dict(finnhubIndustry='gamer')
112+
# set one hobby with fake function
112113
monkeypatch.setattr('finnhub.Client.company_profile2', fake_profile2)
113114

114115
client.post('/', data=dict(
@@ -127,7 +128,7 @@ def fake_profile2(_self, **params):
127128
def test_generate_uid(client, app, monkeypatch):
128129
def fake_secrets_choice(sequence):
129130
return 'A'
130-
131+
# Replace choice function with fake
131132
monkeypatch.setattr('secrets.choice', fake_secrets_choice)
132133

133134
client.post('/', data=dict(

0 commit comments

Comments
 (0)