Added tests.

This commit is contained in:
didier amyot committed 2023-04-11 09:32:48 -04:00
1 parent 509794f02d
commit 0a31f89501
2 files changed
+44 -2

No files matched your search

+1
View File
@@ -56,3 +56,4 @@ nix develop
* [x] Hack https://github.com/bigskysoftware/htmx/blob/master/src/ext/json-enc.js to all embedded fields. * [x] Hack https://github.com/bigskysoftware/htmx/blob/master/src/ext/json-enc.js to all embedded fields.
* [x] Create UI to create user * [x] Create UI to create user
* [x] Create UI to create security * [x] Create UI to create security
* [ ] Find a "nice" solution for chart.js
+43 -2
View File
@@ -59,6 +59,7 @@ impl Clone for AccountType {
} }
} }
#[derive(Debug)]
pub struct Account { pub struct Account {
id: i64, id: i64,
amount: i64, amount: i64,
@@ -413,7 +414,7 @@ pub fn get_number_of_accounts(tx: &Connection,
pub fn get_security_market_cap(tx: &Connection, pub fn get_security_market_cap(tx: &Connection,
sec: &SecurityId) -> CustomResult<i64> { sec: &SecurityId) -> CustomResult<i64> {
let q = " let q = "
SELECT -1 * SUM(amount) AS count SELECT COALESCE(-1 * SUM(amount), 0) AS count
FROM accounts FROM accounts
WHERE security = ? WHERE security = ?
AND account_type = ? AND account_type = ?
@@ -424,6 +425,7 @@ pub fn get_security_market_cap(tx: &Connection,
#[cfg(test)] #[cfg(test)]
mod test_accounts { mod test_accounts {
use apache_avro::Schema::String;
use rusqlite::{Connection, Transaction}; use rusqlite::{Connection, Transaction};
use serde_json::to_string; use serde_json::to_string;
@@ -445,7 +447,7 @@ mod test_accounts {
} }
fn create_all_accounts( fn create_all_accounts(
tx: &Transaction, tx: &Connection,
users: &Vec<User>, users: &Vec<User>,
secs: &Vec<Security>, secs: &Vec<Security>,
) -> CustomResult<Vec<Account>> { ) -> CustomResult<Vec<Account>> {
@@ -608,4 +610,43 @@ mod test_accounts {
assert_eq!(res.len(), 2); assert_eq!(res.len(), 2);
Ok(()) Ok(())
} }
#[test]
fn test_get_number_of_accounts() -> CustomResult<()> {
let (mut conn, users, secs) = setup();
create_all_accounts(&conn, &users, &secs)?;
let n = get_number_of_accounts(&conn, &secs.get(0).unwrap().id(), AccountType::Checking)?;
assert_eq!(n, 2);
let n = get_number_of_accounts(&conn, &secs.get(0).unwrap().id(), AccountType::SecurityEmitter)?;
assert_eq!(n, 0);
create_security_emitter_account(&conn, &secs.get(0).unwrap().id(), &users.get(0).unwrap().id(), &"".to_string())?;
let n = get_number_of_accounts(&conn, &secs.get(0).unwrap().id(), AccountType::SecurityEmitter)?;
assert_eq!(n, 1);
Ok(())
}
#[test]
fn test_get_security_market_cap() -> CustomResult<()> {
let (mut conn, users, secs) = setup();
let accounts = create_all_accounts(&conn, &users, &secs.get(0..1).unwrap().to_vec())?;
let sec_id = &secs.get(0).unwrap().id();
let sec_emitter = create_security_emitter_account(&conn, sec_id, &users.get(0).unwrap().id(), &"".to_string())?;
let n = get_security_market_cap(&conn, sec_id)?;
assert_eq!(n, 0);
add_to_account(&conn, &accounts.get(0).unwrap().id(), 800)?;
// markcap is ONLY based on security emitter accounts
let n = get_security_market_cap(&conn, sec_id)?;
assert_eq!(n, 0);
add_to_account(&conn, &sec_emitter.id(), -800)?;
let n = get_security_market_cap(&conn, sec_id)?;
assert_eq!(n, 800);
Ok(())
}
} }