0

I'm encountering the following bug with one of check50s test scripts:

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

I manually tested it and it definitely values definitely show up (even formatted) on the views. Here is my check() function in application.py

@app.route("/buy", methods=["GET", "POST"])

@login_required def buy(): """Buy shares of stock""" # Only implement the actuall post request to buy a share """Get stock quote.""" if request.method == "GET": return render_template("buy.html")

if request.method == "POST":
    # Pass data to lookup method
    # Check if symbol was inputted
    if not request.form.get("symbol"):
        return apology("Please input a valid symbol")

    # Check if no. of shares was inputted
    if not request.form.get("shares"):
        return apology("Please input the no of shares")

    try:
        int(request.form.get("shares"))
    except:
        return apology("Please input a real integer")

    # Check if postive int was inputted
    if int(request.form.get("shares")) <= 0:
        return apology("No. of shares must be positive")

    # Check if stock exists
    if not lookup(request.form.get("symbol")):
        return apology("Stock doesn't exist")

    # Get price data
    stock = lookup(request.form.get("symbol"))

    # Get how many shares to purchase data
    shares = request.form.get("shares")


    # Get user id
    user_id = session["user_id"]

    # Get balance of user
    balance = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = user_id)

    # Check if users balance is sufficient ####
    if float(stock['price']) * float(shares) < float(balance[0]['cash']):
        # Complete purchase
        # Deduct the purchase amount from the users balance
        newbalance = (float(balance[0]['cash'] - ( float(stock['price']) * float(shares) ) ) )
        db.execute("UPDATE users SET cash = :newbalance WHERE id = :id", newbalance = newbalance, id = user_id)
        # Test if it worked
        tbalance = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = user_id)
        # Get current datetime
        now = datetime.datetime.now()
        current_datetime = now.strftime('%Y-%m-%d %H:%M:%S')
        # Create a transaction in the new transaction table with user id, no of shares, price and datetime
        db.execute("INSERT INTO transactions ('user_id', 'price', 'quantity', 'date', 'symbol', 'type') VALUES (:user_id, :price, :quantity, :datetime, :symbol, 'buy')", user_id=user_id, price=float(stock['price']), quantity=float(shares), datetime=current_datetime, symbol=stock['symbol'] )
        details = ["{:.2f}".format(int(shares)), usd(stock['price'])]
        return render_template("success2.html", type="Purchase", details=details)
    else:
        # Show error with insufficient balance
        return apology("Insufficient balance")

And here is my success2.html file that renders those values:

    {% extends "layout.html" %}
{% block title %}
    {{type}} Success!
{% endblock %}

{% block main %}
    <h3>{{ type }} of {{ details[0] }} shares for {{ details[1] }} each successfully executed</h3>
    <img alt="{{ top }}" class="border" src="https://media.giphy.com/media/l3q2Z6S6n38zjPswo/giphy.gif" title="{{ top }}">
    <br>

    <h4>Click <a href="{{ url_for('index') }}">here</a> to check your new portfolio</h4>
{% endblock %}

Did somebody manage to overcome the test in this years pset without encountering the bug or do I have a mistake somewhere else? THX guys!

1 Answer 1

0

Solved it by simply using

return redirect("/")

instead of

return render_template("success2.html", type="Purchase", details=details)

still kinda sucks that this test fails...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .