going somewhere.

This commit is contained in:
didier amyot committed 2022-12-20 10:18:17 -05:00
1 parent 9ccaeaf410
commit d262f0d2f8
6 files changed
+121 -28

No files matched your search

+74 -8
View File
@@ -6,11 +6,29 @@ use crate::db::_securities::Security;
use crate::db::_users::User;
use crate::db::external_id::SecurityId;
#[derive(PartialEq, Debug)]
pub enum AccountType {
Checking,
SecurityEmitter,
}
impl AccountType {
pub fn from_row(row: &Row) -> Result<AccountType> {
let s: String = row.get("acc_account_type")?;
if s == "security_emitter" {
Ok(AccountType::SecurityEmitter)
} else {
Ok(AccountType::Checking)
}
}
}
pub struct Account {
id: u32,
amount: u32,
timestamp: i32,
security: Security,
account_type: AccountType,
}
impl Account {
@@ -24,6 +42,7 @@ impl Account {
amount: row.get("acc_amount")?,
timestamp: row.get("acc_timestamp")?,
security: Security::from_row(row)?,
account_type: AccountType::from_row(row)?,
});
}
@@ -36,6 +55,7 @@ pub fn list_accounts<'a>(conn: &Connection) -> Result<Vec<Account>> {
let q = "SELECT acc.id AS acc_id,
acc.amount AS acc_amount,
UNIXEPOCH(acc.timestamp) AS acc_timestamp,
account_type AS acc_account_type,
sec.id AS sec_id,
sec.symbol AS sec_symbol,
@@ -56,6 +76,7 @@ pub fn get_account_by_id(tx: &Transaction, id: &AccountId) -> Result<Account> {
let q = "SELECT acc.id AS acc_id,
acc.amount AS acc_amount,
UNIXEPOCH(acc.timestamp) AS acc_timestamp,
account_type AS acc_account_type,
sec.id AS sec_id,
sec.symbol AS sec_symbol,
@@ -66,14 +87,26 @@ pub fn get_account_by_id(tx: &Transaction, id: &AccountId) -> Result<Account> {
return tx.query_row(q, params![id], Account::from_row);
}
pub fn create_account(tx: &Transaction, sec: &SecurityId, user: &UserId) -> Result<Account> {
let q = "INSERT INTO accounts(security, owner, amount, timestamp)
VALUES (?1, ?2, 0, CURRENT_TIMESTAMP)";
tx.execute(q, (sec, user))?;
fn create_account(tx: &Transaction, sec: &SecurityId, user: &UserId, account_type: AccountType) -> Result<Account> {
let q = "INSERT INTO accounts(security, owner, amount, timestamp, account_type)
VALUES (?1, ?2, 0, CURRENT_TIMESTAMP, ?3)";
let account_type = match account_type {
AccountType::Checking => "checking",
AccountType::SecurityEmitter => "security_emitter",
};
tx.execute(q, (sec, user, account_type))?;
let id = AccountId::from_id(tx.last_insert_rowid() as u32);
return get_account_by_id(tx, &id);
}
pub fn create_checking_account(tx: &Transaction, sec: &SecurityId, user: &UserId) -> Result<Account> {
create_account(tx, sec, user, AccountType::Checking)
}
pub fn create_security_emitter_account(tx: &Transaction, sec: &SecurityId, user: &UserId) -> Result<Account> {
create_account(tx, sec, user, AccountType::SecurityEmitter)
}
pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> Result<bool> {
let q = "SELECT id FROM accounts WHERE id = ?1 AND owner = ?2";
let params = params![account_id, user_id];
@@ -83,7 +116,7 @@ pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> R
}
#[cfg(test)]
mod test {
mod test_accounts {
use rusqlite::Connection;
use crate::db::_test_utils::{setup_users_and_securities};
@@ -108,12 +141,46 @@ mod test {
let mut ret: Vec<Account> = Vec::new();
for sec in secs.iter() {
for user in users.iter() {
ret.push(create_account(&tx, &sec.id(), &user.id())?);
ret.push(create_checking_account(&tx, &sec.id(), &user.id())?);
}
}
return Ok(ret);
}
#[test]
fn test_simplest_checking_account() -> Result<()> {
let (mut conn, users, secs) = setup();
let user = users.get(0).unwrap();
let not_owner = users.get(1).unwrap();
let sec = secs.get(0).unwrap();
let tx = conn.transaction()?;
let acc = create_checking_account(&tx, &sec.id(), &user.id())?;
assert_eq!(acc.account_type, AccountType::Checking);
assert_eq!(0, acc.raw_amount());
assert!(is_owner(& tx, & acc.id(), &user.id())?);
assert!(!is_owner(& tx, & acc.id(), &not_owner.id())?);
Ok(())
}
#[test]
fn test_simplest_security_emitter_account() -> Result<()> {
let (mut conn, users, secs) = setup();
let user = users.get(0).unwrap();
let not_owner = users.get(1).unwrap();
let sec = secs.get(0).unwrap();
let tx = conn.transaction()?;
let acc = create_security_emitter_account(&tx, &sec.id(), &user.id())?;
assert_eq!(acc.account_type, AccountType::SecurityEmitter);
assert_eq!(0, acc.raw_amount());
assert!(is_owner(& tx, & acc.id(), &user.id())?);
assert!(!is_owner(& tx, & acc.id(), &not_owner.id())?);
Ok(())
}
#[test]
fn test_happy_path() -> Result<()> {
let (mut conn, users, secs) = setup();
@@ -123,7 +190,6 @@ mod test {
let accs = list_accounts(&conn)?;
assert_eq!(4, accs.len());
assert_eq!(1, accs.get(0).unwrap().id);
assert_eq!(4, accs.get(3).unwrap().id);
return Ok(());
}
@@ -145,7 +211,7 @@ mod test {
let user_2 = users.get(1).unwrap();
let sec = secs.get(0).unwrap();
let acc = create_account(&tx, &sec.id(), &user_1.id())?;
let acc = create_checking_account(&tx, &sec.id(), &user_1.id())?;
assert!(is_owner(tx, &acc.id(), &user_1.id())?);
assert!(!is_owner(tx, &acc.id(), &user_2.id())?);