Skip to content

Commit 88fc227

Browse files
committed
Updated TestCases code
1 parent 17af31c commit 88fc227

File tree

6 files changed

+64
-89
lines changed

6 files changed

+64
-89
lines changed

financepy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cr = "\n"
22

33
s = "####################################################################" + cr
4-
s += "# FINANCEPY BETA Version " + str('0.370') + " - This build: 28 Oct 2024 at 20:29 #" + cr
4+
s += "# FINANCEPY BETA Version " + str('1.01') + " - This build: 12 Aug 2025 at 13:34 #" + cr
55
s += "# This software is distributed FREE AND WITHOUT ANY WARRANTY #" + cr
66
s += "# Report bugs as issues at https://github.com/domokane/FinancePy #" + cr
77
s += "####################################################################"

golden_tests/FinTestCases.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ def __init__(self, module_name, mode):
128128

129129
if exists(self._golden_file_name) and self._careful_mode:
130130
overwrite = input(
131-
"File "
132-
+ self._golden_file_name
133-
+ " exists. Overwrite (Y/N) ?"
131+
"File " + self._golden_file_name + " exists. Overwrite (Y/N) ?"
134132
)
135133
if overwrite == "N":
136134
print("Not overwriting. Saving test cases failed.")
@@ -151,9 +149,7 @@ def __init__(self, module_name, mode):
151149

152150
if exists(self._compare_file_name) and self._careful_mode:
153151
overwrite = input(
154-
"File "
155-
+ self._compare_file_name
156-
+ " exists. Overwrite (Y/N) ?"
152+
"File " + self._compare_file_name + " exists. Overwrite (Y/N) ?"
157153
)
158154
if overwrite == "N":
159155
print("Not overwriting. Saving test cases failed.")
@@ -248,17 +244,13 @@ def header(self, *args):
248244
return
249245

250246
if not self._foldersExist:
251-
self.print_log(
252-
"Cannot print as GOLDEN and COMPARE folders do not exist"
253-
)
247+
self.print_log("Cannot print as GOLDEN and COMPARE folders do not exist")
254248
return
255249

256250
self._header_fields = args
257251

258252
if len(self._header_fields) == 0:
259-
self.print_log(
260-
"ERROR: Number of header fields must be greater than 0"
261-
)
253+
self.print_log("ERROR: Number of header fields must be greater than 0")
262254

263255
if self._mode == FinTestCaseMode.SAVE_TEST_CASES:
264256
file_name = self._golden_file_name
@@ -371,8 +363,7 @@ def compare_rows(self, golden_row, compare_row, row_num):
371363
self.print_log(
372364
"Row# ",
373365
row_num,
374-
" WARNING: Calculation time has changed by %5.2f"
375-
% change,
366+
" WARNING: Calculation time has changed by %5.2f" % change,
376367
" percent.",
377368
)
378369

@@ -461,30 +452,22 @@ def compareTestCases(self):
461452
self._header_fields[0:-1],
462453
)
463454

464-
self.print_log(
465-
"Row# ", row_num, " GOLDEN : ==>", golden_row[:-2]
466-
)
467-
self.print_log(
468-
"Row# ", row_num, " COMPARE: ==>", compare_row[:-2]
469-
)
455+
self.print_log("Row# ", row_num, " GOLDEN : ==>", golden_row[:-2])
456+
self.print_log("Row# ", row_num, " COMPARE: ==>", compare_row[:-2])
470457
self.print_log("")
471458

472459
totalnum_warnings += num_warnings
473460
totalnum_errors += num_errors
474461

475462
if num_golden_lines == minNumLines and num_compare_lines > minNumLines:
476-
self.print_log(
477-
"ERROR:The COMPARE file is longer than the GOLDEN file"
478-
)
463+
self.print_log("ERROR:The COMPARE file is longer than the GOLDEN file")
479464

480465
# for row_num in range(minNumLines,maxNumLines):
481466
# num_warnings, num_errors = compareContents[row_num]
482467
# print(row_num,"COMPARE: ==>",compare_row)
483468

484469
if num_compare_lines == minNumLines and num_golden_lines > minNumLines:
485-
self.print_log(
486-
"ERROR:The GOLDEN file is longer than the COMPARE file"
487-
)
470+
self.print_log("ERROR:The GOLDEN file is longer than the COMPARE file")
488471

489472
# for row_num in range(minNumLines,maxNumLines):
490473
# num_warnings, num_errors = compareContents[row_num]

golden_tests/TestFinBondYieldCurve.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from financepy.utils.day_count import DayCountTypes
2323
from financepy.utils.frequency import FrequencyTypes
2424

25-
2625
test_cases = FinTestCases(__file__, globalTestCaseMode)
2726

2827
###############################################################################
@@ -33,13 +32,9 @@ def test_BondYieldCurve():
3332

3433
###########################################################################
3534

36-
path = os.path.join(
37-
os.path.dirname(__file__), "./data/gilt_bond_prices.txt"
38-
)
35+
path = os.path.join(os.path.dirname(__file__), "./data/gilt_bond_prices.txt")
3936
bond_dataframe = pd.read_csv(path, sep="\t")
40-
bond_dataframe["mid"] = 0.5 * (
41-
bond_dataframe["bid"] + bond_dataframe["ask"]
42-
)
37+
bond_dataframe["mid"] = 0.5 * (bond_dataframe["bid"] + bond_dataframe["ask"])
4338

4439
freq_type = FrequencyTypes.SEMI_ANNUAL
4540
dc_type = DayCountTypes.ACT_ACT_ICMA
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
File Created on:20240731_174050
1+
File Created on:20250812_131541
22
HEADER,PARAMETER,VALUE,
3-
RESULTS,values,[ 3.53695541e-07 -5.03221108e-05 2.30547697e-03 -1.54434702e-03],
3+
RESULTS,values,[ 3.53695557e-07 -5.03221118e-05 2.30547696e-03 -1.54434662e-03],
44
HEADER,PARAMETER,VALUE,
5-
RESULTS,values,[-1.44771535e-09 1.78395721e-07 -7.41468170e-06 9.06222841e-05
6-
1.35360732e-03 4.15140112e-07],
5+
RESULTS,values,[-1.44771564e-09 1.78395761e-07 -7.41468364e-06 9.06223223e-05
6+
1.35360703e-03 4.16055803e-07],
77
HEADER,PARAMETER,VALUE,
8-
RESULTS,beta_1,-0.09364839,
9-
RESULTS,beta_2,0.09210325,
10-
RESULTS,beta_3,0.25878568,
11-
RESULTS,tau,35.78538026,
8+
RESULTS,beta_1,-0.09365083,
9+
RESULTS,beta_2,0.09210571,
10+
RESULTS,beta_3,0.25878915,
11+
RESULTS,tau,35.78564564,
1212
HEADER,PARAMETER,VALUE,
1313
RESULTS,beta_1,0.04601881,
14-
RESULTS,beta_2,-0.04327272,
14+
RESULTS,beta_2,-0.04327273,
1515
RESULTS,beta_3,-0.05226095,
16-
RESULTS,beta_4,-0.03756485,
17-
RESULTS,tau_1,3.17744997,
16+
RESULTS,beta_4,-0.03756487,
17+
RESULTS,tau_1,3.17745031,
1818
RESULTS,tau_2,100.00000000,
19-
RESULTS,19-SEP-2030,0.026018581814958648,
19+
RESULTS,19-SEP-2030,0.026018582282125002,

golden_tests/run_all_tests.py

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import glob
66
from os.path import dirname, basename, join
7+
import traceback
78

89
import sys
910

@@ -16,58 +17,54 @@
1617

1718
set_date_format(DateFormatTypes.UK_LONG)
1819

19-
# I put this here to get the library loaded and header printed before loop
2020

21-
print("Looking in folder:", dirname(__file__))
22-
modules = sorted(glob.glob(join(dirname(__file__), "Test*.py")))
23-
num_modules = len(modules)
21+
def main(start_index=0, end_index=None):
22+
"""Loop over test cases"""
2423

25-
""" This is the index of the file - change this to start later in the list """
26-
N = 0
27-
M = num_modules
24+
# I put this here to get the library loaded and header printed before loop
25+
test_folder = dirname(__file__)
26+
print("Looking in folder:", test_folder)
27+
modules = sorted(glob.glob(join(test_folder, "Test*.py")))
28+
num_modules = len(modules)
2829

29-
###############################################################################
30+
if end_index is None or end_index > num_modules:
31+
end_index = num_modules
3032

31-
for module_file_name in modules[N : M + 1]:
33+
for idx in range(start_index, end_index):
3234

33-
try:
35+
module_path = modules[idx]
36+
module_name = basename(module_path)[:-3]
3437

35-
module_text_name = basename(module_file_name[:-3])
3638
print(
37-
"TEST: %3d out of %3d: MODULE: %-35s "
38-
% (N + 1, num_modules, module_text_name),
39+
f"TEST: {idx + 1:3d} out of {num_modules:3d}: MODULE: {module_name:<35} ",
3940
end="",
4041
)
41-
module_name = __import__(module_text_name)
42-
num_errors = module_name.test_cases._global_num_errors
43-
num_warnings = module_name.test_cases._global_num_warnings
4442

45-
print(
46-
"WARNINGS: %3d ERRORS: %3d " % (num_warnings, num_errors), end=""
47-
)
43+
try:
44+
module = __import__(module_name)
45+
46+
num_errors = getattr(module.test_cases, "_global_num_errors", 0)
47+
num_warnings = getattr(module.test_cases, "_global_num_warnings", 0)
48+
49+
print(f"WARNINGS: {num_warnings:3d} ERRORS: {num_errors:3d} ", end="")
50+
51+
if num_errors > 0:
52+
print("*" * num_errors, end="")
53+
54+
print()
55+
56+
except (FinError, ValueError, NameError, TypeError) as e:
57+
# Handle known financepy or Python errors gracefully
58+
print(f"{type(e).__name__}: {e} ************")
59+
except Exception:
60+
# Catch all other unexpected errors, print traceback for debugging
61+
print("Unexpected error:")
62+
traceback.print_exc()
63+
# Continue to next test module regardless of errors
64+
4865

49-
if num_errors > 0:
50-
for i in range(0, num_errors):
51-
print("*", end="")
52-
53-
print("")
54-
N = N + 1
55-
56-
# Want testing to continue even if a module has an exception
57-
except FinError as err:
58-
print("FinError:", err._message, "************")
59-
N = N + 1
60-
except ValueError as err:
61-
print("Value Error:", err.args[0], "************")
62-
N = N + 1
63-
except NameError as err:
64-
print("Name Error:", err.args[0], "************")
65-
N = N + 1
66-
except TypeError as err:
67-
print("Type Error:", err.args[0], "************")
68-
N = N + 1
69-
except Exception:
70-
print("Unexpected error:", sys.exc_info()[0])
71-
N = N + 1
66+
if __name__ == "__main__":
67+
# Optionally customize start and end test indices here
68+
main()
7269

7370
###############################################################################

version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.370"
1+
__version__ = "1.01"

0 commit comments

Comments
 (0)