-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_financebuddy.py
66 lines (53 loc) · 2.16 KB
/
test_financebuddy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import pytest
from StreamlitApp import (
calculate_months_to_goal,
calculate_budget_per_person,
calculate_volatility,
calculate_sharpe_ratio,
)
# Test function for calculate_months_to_goal
@pytest.mark.parametrize("savings, savings_goal, expected_months", [
(1000, 5000, 5),
(2000, 10000, 5),
(0, 1000, float('inf')),
])
def test_calculate_months_to_goal(savings, savings_goal, expected_months):
# Calculate the result using the function
result = calculate_months_to_goal(savings, savings_goal)
# Assert that the result matches the expected value
assert result == expected_months
# Test function for calculate_budget_per_person
@pytest.mark.parametrize("monthly_expenses, num_people, expected_budget", [
(1000, 2, 500),
(1500, 3, 500),
(0, 1, 0),
])
def test_calculate_budget_per_person(monthly_expenses, num_people, expected_budget):
# Calculate the result using the function
result = calculate_budget_per_person(monthly_expenses, num_people)
# Assert that the result matches the expected value
assert result == expected_budget
# Test function for calculate_volatility
def test_calculate_volatility():
# Sample returns data
returns = [0.02, 0.05, -0.03, 0.01, 0.03]
# Calculate the result using the function
result = calculate_volatility(returns)
# Calculate the expected value using numpy's std function
expected = np.std(returns)
# Assert that the result matches the expected value using pytest.approx
assert result == pytest.approx(expected)
# Test function for calculate_sharpe_ratio
def test_calculate_sharpe_ratio():
# Sample returns data
returns = [0.02, 0.05, -0.03, 0.01, 0.03]
# Calculate the result using the function
result = calculate_sharpe_ratio(returns)
# Calculate the expected Sharpe ratio manually
average_return = np.mean(returns)
volatility = np.std(returns)
risk_free_rate = 0.01
expected = (average_return - risk_free_rate) / volatility if volatility != 0 else 0
# Assert that the result matches the expected value using pytest.approx
assert result == pytest.approx(expected)