Lines
91.18 %
Functions
62.5 %
Branches
100 %
use super::*;
use crate::test_support::{CountingPool, ErrorMarks};
use scripting::nomiscript::{Compiler, Reader, SymbolTable};
/// Spins until the executor's epoch deadline stops it.
const SPIN: &str = "(do () (nil nil))";
fn compile(source: &str) -> anyhow::Result<Vec<u8>> {
let program = Reader::parse(source).map_err(|err| anyhow::anyhow!("parse: {err}"))?;
let mut symbols = SymbolTable::with_builtins();
Compiler::new()
.compile(&program, &mut symbols)
.map_err(|err| anyhow::anyhow!("compile: {err}"))
}
async fn account(user: &User, name: &str) -> anyhow::Result<Uuid> {
match CreateAccount::new()
.name(name.to_string())
.user_id(user.id)
.run()
.await?
{
Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
other => anyhow::bail!("account: {other:?}"),
fn splits(tx_id: Uuid, commodity_id: Uuid, accounts: [Uuid; 2]) -> Vec<FinanceEntity> {
[(accounts[0], -100), (accounts[1], 100)]
.into_iter()
.map(|(account_id, value_num)| {
FinanceEntity::Split(Split {
id: Uuid::new_v4(),
tx_id,
account_id,
commodity_id,
value_num,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
})
.collect()
/// The script-failure ERROR is logged once the script ends, and by then the
/// pool's only connection has been returned exactly once (the scripts read);
/// the store takes and returns it afterwards.
#[local_db_sqlx_test]
async fn test_create_with_a_slow_script_releases_the_connection_between_phases(
pool: PgPool,
) -> anyhow::Result<()> {
let user = USER.get().ok_or_else(|| anyhow::anyhow!("no user"))?;
user.commit().await?;
user.create_script(compile(SPIN)?, None).await?;
let Some(CmdResult::String(commodity)) = CreateCommodity::new()
.symbol("TST".to_string())
.name("Test".to_string())
else {
anyhow::bail!("commodity");
};
let commodity = Uuid::parse_str(&commodity)?;
let accounts = [account(user, "A1").await?, account(user, "A2").await?];
let single = CountingPool::single(&pool).await?;
let _guard = install_test_pool(single.pool.clone());
let errors = ErrorMarks::of(&single);
let _logs = tracing::subscriber::set_default(errors.subscriber());
let tx_id = Uuid::new_v4();
CreateTransaction::new()
.id(tx_id)
.post_date(Utc::now())
.enter_date(Utc::now())
.splits(splits(tx_id, commodity, accounts))
.await?;
assert_eq!(errors.marks(), [1]);
assert_eq!(single.checkouts().await?, 2);