示例#1
0
def test_upd_expense():
    """
    Tests for the upd_expense method
    """
    manager = ExpenseManager()
    expense = Expense(1, "Health", 300, "2020-05-12")
    updated_expense = Expense(1, "School", 100.1, "2020-05-11")

    # Checks if method exists
    assert hasattr(manager, "upd_expense")

    # Adds expense
    manager.add_expense(expense)
    assert manager._expenses[1] == expense

    # Checks that update expense works correctly
    manager.upd_expense(1, updated_expense)
    assert manager._expenses[1] == updated_expense
示例#2
0
def test_get_details():
    """
    Tests for the get_details method
    """
    manager = ExpenseManager()
    expense = Expense(1, "School", 200, "2020-02-13")

    # Checks if method exists
    assert hasattr(manager, "get_details")

    # Adds expense
    manager.add_expense(expense)

    # Checks if correct values are returned
    expense_object = manager.get_details(1)

    assert getattr(expense_object, "_Category") == "School"
    assert getattr(expense_object, "_Amount") == "200.00"
示例#3
0
def expense():
    ## Expense ##

    # Get input from html
    try:
        category = request.form['category']
    except KeyError:
        flash("Please select a category")
        return redirect(url_for("index"))

    try:
        amount = float(request.form['amount'])
    except ValueError:
        flash("Please enter a valid number for expense amount")
        return redirect(url_for("index"))

    if category != "":
        # Store as a class Expense object
        EM = ExpenseManager()
        Next_ID = EM.read_largest_id(expense_csv) + 1  # Assign the next ID#
        expense = Expense(Next_ID, category, amount)

        # Add the expense into the Expense Manager
        EM.add_expense(expense)

        # Deduct expense amount from balance
        bal_dict = from_csv(balance_csv)
        bal_dict["balance"] = float(bal_dict["balance"]) - float(
            expense.Amount)
        if bal_dict["balance"] < float(bal_dict["budget"]):
            flash("Your balance has exceeded your budget!")

        # Save expense
        EM.to_csv(expense_csv)

        # Save balance, budget
        add_to_csv(bal_dict["balance"], bal_dict["budget"])
    elif len(category) == 0:
        flash("Category can't be left empty")
        return redirect(url_for("index"))

    flash(f'Expense #{Next_ID} has been added!')
    flash(f'${expense.Amount} deducted from balance')
    return redirect(url_for("index"))
示例#4
0
def test_add_expense():
    """
    Tests for the add_expense method
    """
    manager = ExpenseManager()
    expense1 = Expense(1, "Health", 300, "2020-05-12")
    expense2 = Expense(2, "School", 1000, "2021-04-20")
    expense3 = Expense(3, "Food", 25.6, "2021-4-30")

    # Checks if method exists
    assert hasattr(manager, "add_expense")

    # Tests that method correctly adds expenses to dictionary
    manager.add_expense(expense1)
    manager.add_expense(expense2)
    manager.add_expense(expense3)

    assert manager._expenses[1] == expense1
    assert manager._expenses[2] == expense2
    assert manager._expenses[3] == expense3
示例#5
0
def test_del_expense():
    """
    Tests for the del_expense method
    """
    manager = ExpenseManager()
    expense1 = Expense(1, "Health", 300, "2020-05-12")
    expense2 = Expense(2, "School", 100.1, "2020-05-11")

    # Checks if method exists
    assert hasattr(manager, "del_expense")

    # Adds expenses
    manager.add_expense(expense1)
    manager.add_expense(expense2)
    assert manager._expenses[1] == expense1
    assert manager._expenses[2] == expense2

    # Checks that added expenses are deleted correctly
    manager.del_expense(1)
    manager.del_expense(2)
    with pytest.raises(KeyError):
        assert manager._expenses[1]
        assert manager._expenses[2]
示例#6
0
def test_by_category():
    manager = ExpenseManager()
    Expense1 = Expense(1, "Food", 50, "2020-04-29")
    Expense2 = Expense(2, "School", 20, "2021-04-30")
    Expense3 = Expense(3, "Food", 36.6, "2021-05-01")
    Expense4 = Expense(4, "Family", 100, "2021-05-02")
    Expense5 = Expense(5, "Family", 88.8, "2021-01-02")

    # Add to the manager
    manager.add_expense(Expense1)
    manager.add_expense(Expense2)
    manager.add_expense(Expense3)
    manager.add_expense(Expense4)
    manager.add_expense(Expense5)

    assert manager.by_category()["Food"][0] == 36.6
    assert manager.by_category()["Family"][0] == 188.8
    assert manager.by_category()["Health"][0] == 0
示例#7
0
def test_by_month_expense():
    manager = ExpenseManager()
    Expense1 = Expense(1, "Food", 50, "2020-04-29")
    Expense2 = Expense(2, "School", 20, "2021-04-30")
    Expense3 = Expense(3, "Food", 36.6, "2021-05-01")
    Expense4 = Expense(4, "Family", 100, "2021-05-02")
    Expense5 = Expense(5, "Family", 88.8, "2021-01-02")
    Expense6 = Expense(6, "Health", 62, "2021-01-31")

    # Add to the manager
    manager.add_expense(Expense1)
    manager.add_expense(Expense2)
    manager.add_expense(Expense3)
    manager.add_expense(Expense4)
    manager.add_expense(Expense5)
    manager.add_expense(Expense6)

    assert manager.by_month_expense()["Apr"] == 20
    assert manager.by_month_expense()["May"] == 136.6
    assert manager.by_month_expense()["Jan"] == 150.8