0

I`am bug fixing my application and can't find a solution for the ":( buy handles valid purchase" test. Here is Log:

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

Like suggested here CS50 seems to want to have a page where the price of the bought shares is displayed. So I already added a new HTML-template "bought.html" with these information, but the test still fails.

Here is my code:

Python:

@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""

 # User reached quote via POST (as by submitting a form via POST)
if request.method == "POST":

    # Get user input: stock and share
    # Parse data of stock
    stock = request.form.get("symbol")
    quote = lookup(stock)

    # Check if user entered a valid symbol
    if not stock:
        return apology("symbol")

    if quote == None:
        return apology("symbol")

    # Check if user entered a valid value for share

    try:
        share = int(request.form.get("shares"))
    except ValueError:
        return apology("shares must be a posative integer", 400)

    share = int(request.form.get("shares"))

    if not share or share < 0:
        return apology("share", 400)

    price = quote["price"]

    # Get money of user
    cash_before = db.execute("SELECT cash FROM users WHERE id = :user", user = session["user_id"])

    # Check if money is sufficient for buying number of shares of given stock
    if not cash_before or float(cash_before[0]["cash"]) < quote["price"] * share:
        return apology("Not enough money")

    check = db.execute("SELECT * FROM buy WHERE user_id = :user_id", user_id = session["user_id"])

    if not check or check[0]["stock"] != stock:
        # Create entry in buy table
        db.execute("INSERT INTO buy ('user_id','stock','price','share') VALUES (:user_id,:stock,:price,:share)", user_id = session["user_id"], stock = quote["symbol"], price = quote["price"], share = request.form.get("share"))

        # Create entry in transaction table
        db.execute("INSERT INTO 'transaction' ('user_id','share','price','symbol','date','type') VALUES (:user_id,:share,:price,:symbol,:date,:type)", user_id = session["user_id"], share = request.form.get("share"), price = quote["price"], symbol = request.form.get("symbol"), date = datetime.datetime.now(), type = "buy")

        # Update user table
        db.execute("UPDATE users SET cash = cash - :value WHERE id = :user_id", value = quote["price"] * share, user_id = session["user_id"])

    else:
        # Update buy table
        db.execute("UPDATE buy SET share = share + :share WHERE user_id = :user_id AND stock = :stock", share = request.form.get("share"), user_id = session["user_id"], stock = request.form.get("symbol"))
        db.execute("UPDATE buy SET price = :price WHERE user_id = :user_id AND stock = :stock", price = price, user_id = session["user_id"], stock = request.form.get("symbol"))

        # Create entry in transaction table
        db.execute("INSERT INTO 'transaction' ('user_id','share','price','symbol','date','type') VALUES (:user_id,:share,:price,:symbol,:date,:type)", user_id = session["user_id"], share = request.form.get("share"), price = quote["price"], symbol = request.form.get("symbol"), date = datetime.datetime.now(), type = "buy")

        # Update user table
        db.execute("UPDATE users SET cash = cash - :value WHERE id = :user_id", value = quote["price"] * share, user_id = session["user_id"])

    new_cash = db.execute("SELECT cash from users WHERE id = :user_id", user_id = session["user_id"])

    return render_template("bought.html", stock = stock, price = usd(price), share = share, new_cash = new_cash[0]["cash"])

else:
     return render_template("buy.html")

HTML-template for Index

  {% extends "layout.html" %}
   {% block title %}
    Log In
   {% endblock %}

  {% block main %}

    <table class="table">
  <thead>
    <tr>
      <th>Symbol</th>
      <th>Shares</th>
      <th>Price</th>
      <th>TOTAL</th>
    </tr>
  </thead>
  <tbody>

    {% for investment in portfolio %}
    <tr>
      <td>{{ investment.stock }}</td>
      <td>{{ investment.total_shares }}</td>
      <td>{{ investment.price | usd }}</td>
      <td>{{ (investment.total_shares * investment.price) | usd }}</td>
    </tr>
    {% endfor %}
    <tr>
      <td></td>
      <td></td>
      <td>Remaining Balance</td>
      <td>{{ available_cash | usd }}</td>
    </tr>
  </tbody>
</table>
  {% endblock %}

HTML-Template for bought

  {% extends "layout.html" %}
  {% block title %}
   Log In
  {% endblock %}

  {% block main %}
  <p>You bought {{ share }} shares of {{ stock }} at {{ price }}.</p>
  <p>Now you have {{ new_cash }} to spend.</p>
  {% endblock %}

An other Thread suggest that the usd() helper-function could solve the problem. I although think it's a format issue, but couldn't figure how to implement the usd() function correctly.

Looking forward to your thoughts, thank you very much!

Cheers, Matthias

1

You already use usd in both possible ways, using like usd(price) in Python (bought.html), or {{ price | usd }} in the template (index.html).

I think the issue is that the value it searches for is the total price of the transaction. Most people would redirect to index/portfolio, maybe using a flash message to inform the user of the successful operation.

Oh, and something's funny about the buy table. For example, what if I buy two symbols, and then again the second symbol?

| improve this answer | |

You must log in to answer this question.

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