Skip to content

Commit

Permalink
Merge pull request #53 from shivam096/yasovar-kpi-tests
Browse files Browse the repository at this point in the history
Yasovar kpi tests
  • Loading branch information
shivam096 authored Mar 13, 2024
2 parents 0f145af + 60b859b commit 796a38d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
4 changes: 1 addition & 3 deletions dinero/backend/kpi_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
Relative Strength Index (RSI), Rate of Change (ROC), and Bollinger Bands Percent (BBP)
by utilizing the technical_indicators module.
"""
import os
import pandas as pd
import backend.technical_indicators as ti

from backend.stock_data_manager import get_stock_data


# Fetches Technical Indicator
def get_technical_indicator(ticker_symbol, length, indicator):
"""Fetches technical indicators from the technical_indicators module"""
Expand Down
4 changes: 4 additions & 0 deletions dinero/backend/technical_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def calculate_bollinger_bands_percent(data, length=20, num_std_dev=2):

def _formatted_dataframe(data, indicator, name):
"""Formats the resulting DataFrame by appending the date and the calculated indicator."""
if not isinstance(data, pd.DataFrame):
raise TypeError("data must be a pandas DataFrame")
if not isinstance(name, str):
raise TypeError("name must be a string")
result_df = pd.DataFrame()
result_df['Date'] = data['Date']
result_df[name] = indicator
Expand Down
Binary file added dinero/tests/.DS_Store
Binary file not shown.
34 changes: 32 additions & 2 deletions dinero/tests/test_kpi.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Unit tests for the KPI (Key Performance Indicators) Manager module.
This module contains unittests for testing the functionality of the KPI Manager, particularly focusing on reading stock data and calculating various technical indicators.
This module contains unittests for testing the functionality of the KPI Manager, particularly
focusing on reading stock data and calculating various technical indicators.
"""
import unittest
from unittest.mock import patch
import pandas as pd
from backend.technical_indicators import _formatted_dataframe
from backend.kpi_manager import get_technical_indicator


class TestKPIManager(unittest.TestCase):
"""Tests for KPI manager and Technical indicators"""

Expand Down Expand Up @@ -68,6 +69,35 @@ def test_get_technical_indicator_unsupported(self):
with self.assertRaises(ValueError):
get_technical_indicator("AAPL", 10, "UNKNOWN")

def test_formatted_dataframe(self):
"""Test the _formatted_dataframe function to ensure it formats the DataFrame correctly."""
mock_data = pd.DataFrame({
'Date': ['2023-01-01', '2023-01-02', '2023-01-03'],
'Dummy': [1, 2, 3] # This column won't be used, just to simulate structure
})
mock_indicator = pd.Series([0.1, 0.5, 0.9], name='BBP')
result_df = _formatted_dataframe(mock_data, mock_indicator, 'BBP')

self.assertIsInstance(result_df, pd.DataFrame)
self.assertListEqual(list(result_df['Date']), list(mock_data['Date']))

def test_formatted_dataframe_with_invalid_data_type(self):
"""Test _formatted_dataframe with invalid data type for data parameter."""
mock_data = "not a dataframe"
mock_indicator = pd.Series([1, 2, 3])
with self.assertRaises(TypeError) as context:
_formatted_dataframe(mock_data, mock_indicator, 'TestName')
self.assertTrue("data must be a pandas DataFrame" in str(context.exception))

def test_formatted_dataframe_with_invalid_name_type(self):
"""Test _formatted_dataframe with invalid data type for name parameter."""
mock_data = pd.DataFrame({'Date': ['2023-01-01', '2023-01-02'], 'Close': [100, 200]})
mock_indicator = pd.Series([1, 2])
with self.assertRaises(TypeError) as context:
_formatted_dataframe(mock_data, mock_indicator, 123)
self.assertTrue("name must be a string" in str(context.exception))


# This allows the test suite to be run from the command line
if __name__ == "__main__":
unittest.main()

0 comments on commit 796a38d

Please sign in to comment.