going somewhere.

This commit is contained in:
didier amyot committed 2022-12-21 08:35:41 -05:00
1 parent d06264401d
commit 2715dc20fa
5 files changed
+40 -24

No files matched your search

+12 -8
View File
@@ -5,6 +5,7 @@ use crate::db::_external_id::{AccountId, Id, UserId};
use crate::db::_securities::Security;
use crate::db::_users::User;
use crate::db::external_id::SecurityId;
use crate::util::{CustomResult, TransactionHelper};
#[derive(PartialEq, Debug)]
pub enum AccountType {
@@ -114,7 +115,7 @@ pub fn create_security_emitter_account(tx: &Transaction, sec: &SecurityId, user:
pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> Result<bool> {
let q = "SELECT id FROM account_user_permissions WHERE account_id = ?1 AND user_id = ?2";
let params = params![account_id, user_id];
let ite = |row: &Row| row.get::<usize, u32>(0);
let ite = |row: &Row| row.get::<usize, i64>(0);
let option = tx.query_row(q, params, ite).optional()?;
Ok(option.is_some())
}
@@ -135,7 +136,9 @@ impl Receipt {
}
}
pub fn add_to_account(tx: &Transaction, account_id: &AccountId, amount: i64) -> Result<Receipt> {
pub fn add_to_account(tx: &TransactionHelper,
account_id: &AccountId,
amount: i64) -> CustomResult<Receipt> {
let q = "
UPDATE accounts SET amount = amount + ?1,
timestamp = CURRENT_TIMESTAMP
@@ -251,19 +254,20 @@ mod test_accounts {
}
#[test]
fn test_add_to_account() -> Result<()> {
fn test_add_to_account() -> CustomResult<()> {
let (mut conn, users, secs) = setup();
let tx = conn.transaction().unwrap();
let mtx = TransactionHelper::new(&tx);
let sec = secs.get(0).unwrap().id();
let user = users.get(0).unwrap().id();
let acc = create_checking_account(&tx, &sec, &user)?;
let receipt = add_to_account(& tx, & acc.id(), 33)?;
let acc = create_checking_account(&tx, &sec, &user).unwrap();
let receipt = add_to_account(&mtx, &acc.id(), 33)?;
assert_eq!(receipt.account_id, acc.id());
assert_eq!(receipt.amount, 33);
tx.commit()?;
tx.commit().unwrap();
let tx = conn.transaction().unwrap();
let acc = get_account_by_id(&tx, & acc.id())?;
let tx2 = conn.transaction().unwrap();
let acc = get_account_by_id(&tx2, &acc.id()).unwrap();
assert_eq!(acc.timestamp, receipt.timestamp);
assert_eq!(acc.amount, 33);