I am getting this message in the following checks
- :| sell page has all required elements/
- :| sell handles invalid number of shares
- :| sell handles valid sale
I guess the problem is in by sell or buy functions or history functions since most of the previous checks went well.
Thanks in advanced!
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
buy_db = db.execute("SELECT DISTINCT(Symbol) FROM buy WHERE user_id = :user_id",
user_id=session["user_id"])
"""Sell shares of stock"""
if request.method == "POST":
if not request.form.get("symbol"):
return apology("must enter a symbol", 400)
elif not bool(lookup(request.form.get("symbol"))):
return apology("symbol does not exists", 400)
elif not request.form.get("shares"):
return apology("must enter a quantity of shares", 400)
elif not request.form.get("shares").isnumeric():
return apology("mus enter a integer number", 400)
elif int(request.form.get("shares")) < 0:
return apology("must enter a positive number", 400)
else:
buy_db = db.execute("SELECT SUM(Shares) as Shares, Symbol FROM buy WHERE user_id = :user_id AND Symbol = :symbol GROUP BY user_id",
user_id=session["user_id"], symbol = request.form.get("symbol"))
if len(buy_db) == 0:
return apology("you do not own any share of this company", 400)
elif buy_db[0]["Shares"] < int(request.form.get("shares")):
return apology("you do not own enough shares of this company", 400)
else:
owned_shares = buy_db[0]["Shares"]
to_be_sold = int(request.form.get("shares"))
price = lookup(request.form.get("symbol"))['price']
db.execute("INSERT INTO buy (user_id, Symbol, Shares, Price) VALUES (?, ?, ?, ?)",
session["user_id"], request.form.get("symbol"), to_be_sold * -1, price)
#return render_template("test.html", buy_db = buy_db)
return redirect("/")
else:
return render_template("sell.html", buy_db = buy_db)
--
def buy():
"""Buy shares of stock"""
if request.method == "POST":
if not request.form.get("symbol"):
return apology("must enter a symbol", 400)
elif not bool(lookup(request.form.get("symbol"))):
return apology("symbol does not exists", 400)
elif not request.form.get("shares"):
return apology("must enter a quantity of shares", 400)
elif not request.form.get("shares").isnumeric():
return apology("mus enter a integer number", 400)
elif int(request.form.get("shares")) < 0:
return apology("must enter a positive number", 400)
# symbol = request.form.get("symbol")
# shares = request.form.get("shares")
# price = lookup(request.form.get("symbol"))['price']
else:
price = lookup(request.form.get("symbol"))['price']
cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])[0]['cash']
shares = int(request.form.get("shares"))
user_id = session["user_id"]
if shares*price > cash:
return apology("there is no enough cash", 403)
else:
price = usd(price)
db.execute("INSERT INTO buy (user_id, Symbol, Shares, Price) VALUES (?, ?, ?, ?)",
session["user_id"], request.form.get("symbol"), shares, price)
return redirect("/")
# return render_template("test.html", price = type(price), cash = type(cash), shares = type(shares),user_id = user_id)
else:
return render_template("buy.html")
-- @app.route("/history") @login_required def history(): """Show history of transactions""" buy_db = db.execute("SELECT * FROM buy WHERE user_id = :user_id", user_id=session["user_id"])
return render_template("history.html", buy_db = buy_db)
Here is the complete response I am getting
:) application.py exists
Log
checking that application.py exists...
--
:) application starts up
Log
sending GET request to /
checking that status code 200 is returned...
--
:) register page has all required elements
Log
sending GET request to /register
found required "username" field
found required "password" field
found required "confirmation" field
--
:) registering user succeeds
Log
sending POST request to /register
checking that status code 200 is returned...
--
:) registration with an empty field fails
Log
sending POST request to /register
checking that status code 400 is returned...
sending POST request to /register
checking that status code 400 is returned...
sending POST request to /register
checking that status code 400 is returned...
--
:) registration with password mismatch fails
Log
sending POST request to /register
checking that status code 400 is returned...
--
:) registration rejects duplicate username
Log
sending POST request to /register
checking that status code 200 is returned...
sending POST request to /register
checking that status code 400 is returned...
--
:) login page has all required elements
Log
sending GET request to /login
found required "username" field
found required "password" field
--
:) logging in as registered user succceeds
Log
sending POST request to /login
checking that status code 200 is returned...
sending GET request to /
checking that status code 200 is returned...
--
:) quote page has all required elements
Log
sending POST request to /login
sending GET request to /quote
found required "symbol" field
--
:) quote handles invalid ticker symbol
Log
sending POST request to /login
sending POST request to /quote
checking that status code 400 is returned...
--
:) quote handles blank ticker symbol
Log
sending POST request to /login
sending POST request to /quote
checking that status code 400 is returned...
--
:) quote handles valid ticker symbol
Log
sending POST request to /login
sending POST request to /quote
checking that status code 200 is returned...
checking that "28.00" is in page
--
:) buy page has all required elements
Log
sending POST request to /login
sending GET request to /buy
found required "symbol" field
found required "shares" field
--
:) buy handles invalid ticker symbol
Log
sending POST request to /login
sending POST request to /buy
checking that status code 400 is returned...
--
:) buy handles fractional, negative, and non-numeric shares
Log
sending POST request to /login
sending POST request to /buy
checking that status code 400 is returned...
sending POST request to /buy
checking that status code 400 is returned...
sending POST request to /buy
checking that status code 400 is returned...
--
:( buy handles valid purchase
Cause
expected to find "112.00" in page, but it wasn't found
Log
sending POST request to /login
sending POST request to /buy
checking that "112.00" is in page
--
:| sell page has all required elements
Cause
can't check until a frown turns upside down
:| sell handles invalid number of shares
Cause
can't check until a frown turns upside down
:| sell handles valid sale
Cause
can't check until a frown turns upside down
My sell.html id down here:
{% extends "layout.html" %}
{% block title %}
Sell
{% endblock %}
{% block main %}
<form action="/sell" method="post">
<div class="form-group">
<select name = "symbol">
<option disabled selected value = "Symbol" value ="">Symbol</option>
{%for row in buy_db %}
<option value = {{row.Symbol}}>{{row.Symbol}}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<input autocomplete = "off" class="form-control" name="shares" placeholder="Shares" type="number" min = 0 >
</div>
<button class="btn btn-primary" type="submit">Sell</button>
</form>
{% endblock %}