Skip to content

Commit

Permalink
Fix broken tests, refactor code to use best practices (#52)
Browse files Browse the repository at this point in the history
* Fix tests and refactor to use best practices

* Sort imports and refactor slightly
  • Loading branch information
IsaacCheng9 authored Dec 25, 2023
1 parent dab4e20 commit ebdb012
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/trading_portfolio_tracker/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import duckdb
import sys
import os
import sys

import duckdb
from PySide6 import QtWidgets

from src.trading_portfolio_tracker.portfolio import MainWindow

DB_PATH = "resources/portfolio.db"
Expand Down
13 changes: 7 additions & 6 deletions src/trading_portfolio_tracker/finance.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""
Module containing functions relating to retrieving financial data from yahoo finance
using yfinance and performing financial calculations
Contains functions related to retrieval of financial data from Yahoo Finance
and performing financial calculations.
"""

from datetime import date, datetime
from decimal import Decimal

import duckdb
import pandas as pd
import requests
import yfinance as yf
import pandas as pd
from decimal import Decimal
from requests import exceptions
from datetime import datetime, date

DB_PATH = "resources/portfolio.db"

Expand Down Expand Up @@ -240,7 +241,7 @@ def get_total_paid_into_portfolio(database_path: str = DB_PATH) -> Decimal:
result = conn.execute(
"SELECT SUM(CAST(paid_gbp AS DECIMAL)) FROM portfolio"
).fetchone()
return Decimal(result[0]) if result[0] != None else Decimal(0)
return Decimal(result[0]) if result[0] is not None else Decimal(0)


def get_exchange_rate(
Expand Down
2 changes: 1 addition & 1 deletion src/trading_portfolio_tracker/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from PySide6.QtWidgets import QDialog, QMainWindow

from src.trading_portfolio_tracker.finance import (
get_rate_of_return,
get_exchange_rate,
get_info,
get_name_from_symbol,
get_rate_of_return,
get_total_paid_into_portfolio,
upsert_transaction_into_portfolio,
)
Expand Down
40 changes: 14 additions & 26 deletions tests/test_finance.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import duckdb
import pytest
import os
from decimal import Decimal

import duckdb
import pandas as pd
import pytest
import yfinance as yf

from requests import exceptions
from src.trading_portfolio_tracker import finance, app

from src.trading_portfolio_tracker import app, finance

DB_PATH = "resources/test.db"

Expand All @@ -23,10 +25,6 @@ def setup_and_teardown_database():
[
("Apple", "AAPL"),
("FTSE 100", "^FTSE"),
(
"T. Rowe Price Funds OEIC Asian Opportunities Equity Fund Class C GBP",
"0P0001BVXP.L",
),
],
)
def test_get_symbol_valid(name: str, expected_result: str) -> None:
Expand All @@ -39,7 +37,7 @@ def test_get_symbol_valid(name: str, expected_result: str) -> None:
expected_result: Expected symbol to be returned (AAPL...).
"""
symbol = finance.get_symbol(name)
assert type(symbol) is str
assert isinstance(symbol, str)
assert symbol == expected_result


Expand All @@ -60,10 +58,6 @@ def test_get_symbol_invalid() -> None:
[
("TSLA", "Tesla, Inc."),
("^FTSE", "FTSE 100"),
(
"0P0001BVXP.L",
"T. Rowe Price Funds OEIC Asian Opportunities Equity Fund Class C GBP",
),
],
)
def test_get_name_from_symbol_valid(symbol: str, expected_result: str) -> None:
Expand All @@ -76,7 +70,7 @@ def test_get_name_from_symbol_valid(symbol: str, expected_result: str) -> None:
expected_result: Expected name to be returned.
"""
name = finance.get_name_from_symbol(symbol)
assert type(name) is str
assert isinstance(name, str)
assert name == expected_result


Expand All @@ -86,7 +80,7 @@ def test_get_name_from_symbol_invalid() -> None:
blank name is returned.
"""
name = finance.get_name_from_symbol("INVALID")
assert type(name) == str
assert isinstance(name, str)
assert name == ""


Expand All @@ -95,7 +89,6 @@ def test_get_name_from_symbol_invalid() -> None:
[
("Tesla"),
("FTSE 100"),
("T. Rowe Price Funds OEIC Asian Opportunities Equity Fund Class C GBP"),
],
)
def test_get_history_valid(name: str) -> None:
Expand Down Expand Up @@ -146,7 +139,7 @@ def test_get_info_valid(
info = finance.get_info(symbol)
assert info["currency"] == expected_currency
assert info["type"] == expected_type
assert info["current_value"] >= 0
assert float(info["current_value"]) >= 0.0


def test_get_info_lse() -> None:
Expand Down Expand Up @@ -193,7 +186,7 @@ def test_get_rate_of_return_valid(
purchase: Purchase price of the asset.
expected_result: Expected absolute rate of return
"""
calculated_ror = finance.get_rate_of_return(current, purchase)
calculated_ror = finance.get_rate_of_return(Decimal(current), Decimal(purchase))
assert calculated_ror == expected_result


Expand All @@ -202,7 +195,7 @@ def test_get_rate_of_return_no_purchase_price() -> None:
Tests the get_rate_of_return method whilst passing no purchase
price into the method.
"""
calculated_ror = finance.get_rate_of_return(100, None)
calculated_ror = finance.get_rate_of_return(Decimal(100), Decimal(0))
assert calculated_ror == 0


Expand All @@ -211,7 +204,7 @@ def test_get_rate_of_return_invalid() -> None:
Tests the get_rate_of_return method whilst passing no purchase
and current price into the method
"""
calculated_ror = finance.get_rate_of_return(None, None)
calculated_ror = finance.get_rate_of_return(Decimal(0), Decimal(0))
assert calculated_ror == 0


Expand All @@ -220,7 +213,7 @@ def test_upsert_transaction_into_portfolio_valid_buy(
) -> None:
""" """
finance.upsert_transaction_into_portfolio(
"buy", "BTC-USD", "USD", 1000, 100, 100, DB_PATH
"buy", "BTC-USD", "USD", Decimal(1000), Decimal(100), Decimal(100), DB_PATH
)

with duckdb.connect(DB_PATH) as conn:
Expand Down Expand Up @@ -258,11 +251,6 @@ def test_remove_security_from_portfolio_valid(setup_and_teardown_database) -> No
pass


def test_remove_security_from_portfolio_valid(setup_and_teardown_database) -> None:
""" """
pass


def test_remove_security_from_portfolio_invalid(setup_and_teardown_database) -> None:
""" """
pass
Expand Down

0 comments on commit ebdb012

Please sign in to comment.