From 6b6e8139289b90bf9632b77fe865821fdfd04b54 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 21 Mar 2026 22:20:19 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20unit=20tests=20for=20=5Fsa?= =?UTF-8?q?fe=5Ffmt=20in=20finnhub=5Fscanner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement comprehensive unit tests for the `_safe_fmt` utility function to ensure robust handling of numeric formatting and edge cases. - Test None values and custom fallbacks - Test various numeric types (int, float, string) - Test custom format strings - Test error handling for invalid types Co-authored-by: aguzererler <6199053+aguzererler@users.noreply.github.com> --- tests/unit/test_finnhub_scanner_utils.py | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/unit/test_finnhub_scanner_utils.py diff --git a/tests/unit/test_finnhub_scanner_utils.py b/tests/unit/test_finnhub_scanner_utils.py new file mode 100644 index 00000000..d248c5e7 --- /dev/null +++ b/tests/unit/test_finnhub_scanner_utils.py @@ -0,0 +1,35 @@ +"""Unit tests for utility functions in finnhub_scanner.py.""" + +from tradingagents.dataflows.finnhub_scanner import _safe_fmt + +def test_safe_fmt_none_returns_default_fallback(): + assert _safe_fmt(None) == "N/A" + +def test_safe_fmt_none_returns_custom_fallback(): + assert _safe_fmt(None, fallback="Missing") == "Missing" + +def test_safe_fmt_valid_float_returns_default_format(): + assert _safe_fmt(123.456) == "$123.46" + +def test_safe_fmt_valid_int_returns_default_format(): + assert _safe_fmt(100) == "$100.00" + +def test_safe_fmt_numeric_string_returns_default_format(): + assert _safe_fmt("45.678") == "$45.68" + +def test_safe_fmt_custom_format(): + assert _safe_fmt(123.456, fmt="{:.3f}") == "123.456" + +def test_safe_fmt_non_numeric_string_returns_original_string(): + # float("abc") raises ValueError, should return "abc" + assert _safe_fmt("abc") == "abc" + +def test_safe_fmt_unsupported_type_returns_str_representation(): + # float([]) raises TypeError, should return "[]" + assert _safe_fmt([]) == "[]" + +def test_safe_fmt_zero_returns_formatted_zero(): + assert _safe_fmt(0) == "$0.00" + +def test_safe_fmt_negative_number(): + assert _safe_fmt(-1.23) == "$-1.23"