portfolio_rs/tests/cov_core_cli.rs
Markus Zoppelt 68beadcab3 test(core): cover portfolio, review and context; test the configured provider
Bring portfolio.rs, review.rs and context.rs to full line coverage with
offline tests: historic lookup errors (now a pure helper), FX price
display, performance since a non-finite balance, review severities and
unknown classes, and the context cash-buffer and risk-flag cases.
Unreachable branches are restructured instead of tested: risk flags skip
weightless entries via filter_map, and Severity orders itself (Ord).

New integration tests run allocation/performance through the binary with
fixture quotes in a temp directory, and exercise the PORTFOLIO_RS_QUOTES
configured provider (AppState::new, handle_position, historic values).
2026-11-03 09:39:48 +01:00

88 lines
2.9 KiB
Rust

//! `allocation` and `performance` print through `Portfolio`'s terminal helpers.
//! Offline: fixture quotes, a throwaway HOME, and a temp working directory
//! for the `./database` balance history.
use assert_cmd::Command;
use predicates::prelude::PredicateBooleanExt;
use predicates::str;
use tempfile::TempDir;
fn bin(home: &TempDir, cwd: &TempDir) -> Command {
let mut cmd = Command::cargo_bin("portfolio_rs").unwrap();
cmd.current_dir(cwd.path())
.env("HOME", home.path())
.env("XDG_CONFIG_HOME", home.path().join(".config"))
.env("XDG_DATA_HOME", home.path().join(".local/share"))
.env(
"PORTFOLIO_RS_QUOTES",
concat!(
"fixture:",
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/quotes_example.json"
),
);
cmd
}
fn example_data() -> &'static str {
concat!(env!("CARGO_MANIFEST_DIR"), "/example_data.json")
}
#[test]
fn allocation_draws_the_pie_chart_and_lists_classes() {
let (home, cwd) = (TempDir::new().unwrap(), TempDir::new().unwrap());
bin(&home, &cwd)
.args(["allocation", example_data()])
.assert()
.success()
.stdout(str::contains("Bitcoin"))
.stdout(str::contains("===================================="))
.stdout(str::is_match(r"\n\s+Cash \| +\d+\.\d\d\n").unwrap());
}
#[test]
fn performance_compares_with_the_stored_balance() {
let (home, cwd) = (TempDir::new().unwrap(), TempDir::new().unwrap());
bin(&home, &cwd)
.args(["balances", example_data()])
.assert()
.success();
assert!(cwd.path().join("database").is_dir(), "balance was stored");
bin(&home, &cwd)
.args(["performance", example_data()])
.assert()
.success()
.stdout(str::contains("Since"))
.stdout(str::contains("Top Gainers"))
// The stored balance is the current total: 0 % since then.
.stdout(str::is_match(r"0\.00%\s*│\s*\n").unwrap());
}
#[test]
fn performance_without_a_database_warns_and_prints_nothing() {
let (home, cwd) = (TempDir::new().unwrap(), TempDir::new().unwrap());
// A regular file where the database directory belongs.
std::fs::write(cwd.path().join("database"), "").unwrap();
bin(&home, &cwd)
.args(["performance", example_data()])
.assert()
.success()
.stdout(str::contains("Since").not())
.stderr(str::contains(
"Warning: failed to open performance database",
));
}
#[test]
fn performance_with_an_empty_database_compares_with_nothing() {
let (home, cwd) = (TempDir::new().unwrap(), TempDir::new().unwrap());
bin(&home, &cwd)
.args(["performance", example_data()])
.assert()
.success()
.stdout(str::contains("Since"));
assert!(
cwd.path().join("database").is_dir(),
"an empty database was opened"
);
}