Skip to content

Commit

Permalink
more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
carderne committed Apr 9, 2024
1 parent d19660e commit dfd8f1a
Show file tree
Hide file tree
Showing 13 changed files with 500 additions and 104 deletions.
126 changes: 126 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ rust_decimal = "1.33.1"
[[bin]]
name = "bean-rs"
path = "src/main.rs"

[dev-dependencies]
assert_cmd = "2.0"
predicates = "2.1"
25 changes: 15 additions & 10 deletions example.bean
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
** Random metadata stuff
option "operating_currency" "GBP"
2000-01-01 custom "fava-option" "language" "en"
2000-01-01 commodity GBP
2000-01-02 commodity GBP
name: "British Pound"

** Prices
2022-01-01 price GOOG 50 GBP

** Open accounts
2023-01-01 open Equity:Bals
2023-01-01 open Assets:Bank GBP
2023-01-02 open Assets:Invest GOO "FIFO"
2023-01-02 open Assets:Bank GBP
2023-01-03 open Assets:Invest GOO "FIFO"
portfolio: "all"
2023-01-03 open Expenses:Food GBP, USD
2023-01-04 open Income:Job GBP
2023-01-04 open Expenses:Food GBP, USD
2023-01-05 open Income:Job GBP

** Transactions
2023-02-01 * "Salary" ; comment
Expand All @@ -25,7 +28,7 @@ option "operating_currency" "GBP"
; comment
Expenses:Food 100 GBP

2023-02-05 * "Shop" "More food" #tag ^link
2023-02-03 * "Shop" "More food" #tag ^link
Assets:Bank -40.00 GBP
Expenses:Food 40.00 USD

Expand All @@ -34,8 +37,10 @@ option "operating_currency" "GBP"
2023-03-02 balance Assets:Invest 111 GOO
2023-03-03 balance Assets:Bank 860 GBP

** Close an account
2023-12-01 close Assets:Bank
** Other
2023-04-01 document Assets:Bank "./doc.pdf"
2023-04-02 note Assets:Bank "a note"
2023-04-03 query "name" "a bad query"

** Prices
2023-01-01 price GOOG 50 GBP
** Close an account
2023-05-01 close Assets:Bank
2 changes: 1 addition & 1 deletion grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ price = { date ~ space+ ~ "price" ~ space+ ~ ccy ~ space+ ~ amount ~ m
balance = { date ~ space+ ~ "balance" ~ space+ ~ account ~ space+ ~ amount ~ metadata_added* }
pad = { date ~ space+ ~ "pad" ~ space+ ~ account ~ space+ ~ account ~ metadata_added* }

document = { date ~ space+ ~ "document" ~ account ~ space+ ~ path ~ metadata_added* }
document = { date ~ space+ ~ "document" ~ space+ ~ account ~ space+ ~ path ~ metadata_added* }
note = { date ~ space+ ~ "note" ~ space+ ~ account ~ space+ ~ quoted ~ metadata_added* }

transaction = { date ~ space+ ~ txn_type ~ space+ ~ payee ~ (space* ~ narration)? ~ (space+ ~ (tag | link))* ~ (posting_added | metadata_added)* }
Expand Down
99 changes: 99 additions & 0 deletions src/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,102 @@ pub fn get_balances(dirs: &mut Vec<Directive>) -> (AccBal, Vec<BeanError>) {
loader::sort(dirs);
(bals, errs)
}

#[cfg(test)]
mod tests {
use chrono::NaiveDate;

use crate::data::{DebugLine, DATE_FMT};

use super::*;

#[test]
fn test_bad_transaction() {
let p1 = Posting {
account: "Assets:Bank".to_string(),
amount: None,
debug: None,
};
let p2 = p1.clone();
let date = NaiveDate::parse_from_str("2023-01-01", DATE_FMT).unwrap();
let mut tx = Transaction {
date,
ty: "*".to_string(),
payee: None,
narration: "".to_string(),
tag: None,
link: None,
postings: vec![p1, p2],
meta: vec![],
debug: DebugLine { line: 0 },
};
let errs = complete_postings(&mut tx);
assert!(errs.first().unwrap().ty == ErrorType::MultipleEmptyPostings);
}

#[test]
fn test_bad_ccy() {
let p1 = Posting {
account: "Assets:Bank".to_string(),
amount: Some(Amount::new(Decimal::new(100, 1), "USD".to_string())),
debug: None,
};
let p2 = Posting {
account: "Income:Job".to_string(),
amount: Some(Amount::new(Decimal::new(-100, 1), "USD".to_string())),
debug: None,
};
let date = NaiveDate::parse_from_str("2023-01-01", DATE_FMT).unwrap();
let tx = Transaction {
date,
ty: "*".to_string(),
payee: None,
narration: "".to_string(),
tag: None,
link: None,
postings: vec![p1, p2],
meta: vec![],
debug: DebugLine { line: 0 },
};
let mut bals: AccBal = HashMap::new();
let mut accs: AccStatuses = HashMap::new();
accs.insert("Assets:Bank".to_string(), (true, vec!["FOO".to_string()]));
accs.insert("Income:Job".to_string(), (true, vec!["FOO".to_string()]));
let mut errs: Vec<BeanError> = vec![];
proc_tx(&tx, &mut bals, &mut accs, &mut errs);
assert!(errs.first().unwrap().ty == ErrorType::InvalidCcy);
}

#[test]
fn test_closed_acc() {
let p1 = Posting {
account: "Assets:Bank".to_string(),
amount: Some(Amount::new(Decimal::new(100, 1), "USD".to_string())),
debug: None,
};
let p2 = Posting {
account: "Income:Job".to_string(),
amount: Some(Amount::new(Decimal::new(-100, 1), "USD".to_string())),
debug: None,
};
let date = NaiveDate::parse_from_str("2023-01-01", DATE_FMT).unwrap();
let tx = Transaction {
date,
ty: "*".to_string(),
payee: None,
narration: "".to_string(),
tag: None,
link: None,
postings: vec![p1, p2],
meta: vec![],
debug: DebugLine { line: 0 },
};
let mut bals: AccBal = HashMap::new();
let mut accs: AccStatuses = HashMap::new();
accs.insert("Assets:Bank".to_string(), (false, vec!["USD".to_string()]));
accs.insert("Income:Job".to_string(), (false, vec!["USD".to_string()]));
let mut errs: Vec<BeanError> = vec![];
proc_tx(&tx, &mut bals, &mut accs, &mut errs);
assert!(errs.first().unwrap().ty == ErrorType::ClosedAccount);
}
}

0 comments on commit dfd8f1a

Please sign in to comment.