Skip to content

Commit

Permalink
test: velocity (#240)
Browse files Browse the repository at this point in the history
* refactor: extract determin_entries_to_enforce

* chore: add AccountValues to velocity context

* refactor: nest determine_window under VelocityBalances

* refactor: extract some fns onto AccountVelocityXxx values

* test: unit test context_for_entry

* fix: check-code

* test: unit test account_control/value

* fix: type_complexity

* refactor: better pair axes
  • Loading branch information
bodymindarts authored Oct 8, 2024
1 parent 3dbef3b commit 210f714
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 153 deletions.
6 changes: 6 additions & 0 deletions cala-cel-interpreter/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ impl From<&Literal> for CelValue {
}
}

impl From<DateTime<Utc>> for CelValue {
fn from(d: DateTime<Utc>) -> Self {
CelValue::Timestamp(d)
}
}

impl TryFrom<&CelValue> for Arc<String> {
type Error = CelError;

Expand Down
19 changes: 19 additions & 0 deletions cala-ledger-core-types/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ pub struct AccountConfig {
pub is_account_set: bool,
pub eventually_consistent: bool,
}

mod cel {
use cel_interpreter::{CelMap, CelValue};

impl From<&super::AccountValues> for CelValue {
fn from(account: &super::AccountValues) -> Self {
let mut map = CelMap::new();
map.insert("id", account.id);
map.insert("code", account.code.clone());
map.insert("name", account.name.clone());
map.insert("externalId", account.code.clone());
map.insert("normalBalanceType", account.normal_balance_type);
if let Some(metadata) = &account.metadata {
map.insert("metadata", metadata.clone());
}
map.into()
}
}
}
2 changes: 1 addition & 1 deletion cala-ledger-core-types/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod cel {
fn from(entry: &super::EntryValues) -> Self {
let mut map = CelMap::new();
map.insert("id", entry.id);
map.insert("entry_type", entry.entry_type.clone());
map.insert("entryType", entry.entry_type.clone());
map.insert("sequence", CelValue::UInt(entry.sequence as u64));
map.insert("layer", entry.layer);
map.insert("direction", entry.direction);
Expand Down
6 changes: 3 additions & 3 deletions cala-ledger-core-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ mod cel {
fn from(tx: &super::TransactionValues) -> Self {
let mut map = CelMap::new();
map.insert("id", tx.id);
map.insert("journal_id", tx.journal_id);
map.insert("tx_template_id", tx.tx_template_id);
map.insert("journalId", tx.journal_id);
map.insert("txTemplateId", tx.tx_template_id);
map.insert("effective", tx.effective);
map.insert("correlation_id", tx.correlation_id.clone());
map.insert("correlationId", tx.correlation_id.clone());
if let Some(metadata) = &tx.metadata {
map.insert("metadata", metadata.clone());
}
Expand Down

This file was deleted.

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

9 changes: 7 additions & 2 deletions cala-ledger/src/velocity/account_control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ use sqlx::PgPool;

use std::collections::HashMap;

use cala_types::{
account::AccountValues,
velocity::{VelocityControlValues, VelocityLimitValues},
};

use crate::{
atomic_operation::*,
param::Params,
primitives::{AccountId, DebitOrCredit, Layer},
};
use cala_types::velocity::{VelocityControlValues, VelocityLimitValues};

use super::error::VelocityError;

Expand Down Expand Up @@ -101,7 +105,8 @@ impl AccountControls {
&self,
op: &mut AtomicOperation<'_>,
account_ids: &[AccountId],
) -> Result<HashMap<AccountId, Vec<AccountVelocityControl>>, VelocityError> {
) -> Result<HashMap<AccountId, (AccountValues, Vec<AccountVelocityControl>)>, VelocityError>
{
self.repo.find_for_enforcement(op.tx(), account_ids).await
}
}
27 changes: 22 additions & 5 deletions cala-ledger/src/velocity/account_control/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use sqlx::{PgPool, Postgres, Transaction};

use std::collections::HashMap;

use cala_types::account::AccountValues;

use crate::primitives::{AccountId, VelocityControlId};

use super::{super::error::*, value::*};
Expand Down Expand Up @@ -39,21 +41,36 @@ impl AccountControlRepo {
&self,
db: &mut Transaction<'_, Postgres>,
account_ids: &[AccountId],
) -> Result<HashMap<AccountId, Vec<AccountVelocityControl>>, VelocityError> {
) -> Result<HashMap<AccountId, (AccountValues, Vec<AccountVelocityControl>)>, VelocityError>
{
let rows = sqlx::query!(
r#"SELECT values FROM cala_velocity_account_controls
WHERE data_source_id = '00000000-0000-0000-0000-000000000000' AND account_id = ANY($1)"#,
r#"SELECT values, latest_values
FROM cala_velocity_account_controls v
JOIN cala_accounts a
ON v.account_id = a.id
AND v.data_source_id = a.data_source_id
WHERE v.data_source_id = '00000000-0000-0000-0000-000000000000'
AND account_id = ANY($1)"#,
account_ids as &[AccountId],
)
.fetch_all(&mut **db)
.await?;

let mut res: HashMap<AccountId, Vec<AccountVelocityControl>> = HashMap::new();
let mut res: HashMap<AccountId, (AccountValues, Vec<_>)> = HashMap::new();

for row in rows {
let values: AccountVelocityControl =
serde_json::from_value(row.values).expect("Failed to deserialize control values");
res.entry(values.account_id).or_default().push(values);
res.entry(values.account_id)
.or_insert_with(|| {
(
serde_json::from_value(row.latest_values)
.expect("Failed to deserialize account values"),
Vec::new(),
)
})
.1
.push(values);
}

Ok(res)
Expand Down
Loading

0 comments on commit 210f714

Please sign in to comment.