Lines
100 %
Functions
Branches
//! Saving a script compiles its source and stores source and bytecode together.
use chrono::Local;
use finance::split::Split;
use finance::tag::Tag;
use finance::transaction::TransactionBuilder;
use rpc::{ScriptError, compile_script, create_script, update_script_source};
use scripting::ScriptExecutor;
use server::command::FinanceEntity;
use server::script::TransactionState;
use server::user::User;
use sqlx::PgPool;
use uuid::Uuid;
const TAG_SYNC: &str = include_str!("../../doc/scripts/tag-sync.nms");
async fn seed_user(pool: &PgPool) -> anyhow::Result<User> {
let id = Uuid::new_v4();
sqlx::query(
"INSERT INTO users \
(id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
VALUES ($1, 'Test', 'test@test.com', 'x', false, 'pw', 'user', 'db', NOW())",
)
.bind(id)
.execute(pool)
.await?;
Ok(User { id })
}
fn split(tx_id: Uuid, value_num: i64) -> Split {
Split {
id: Uuid::new_v4(),
tx_id,
account_id: Uuid::new_v4(),
commodity_id: Uuid::new_v4(),
value_num,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
/// Two splits, a category on the first, none on the transaction.
fn tagged_split_state() -> anyhow::Result<TransactionState> {
let tx_id = Uuid::new_v4();
let first = split(tx_id, -100);
let first_id = first.id;
let tag = Tag {
tag_name: "category".to_string(),
tag_value: "food".to_string(),
description: None,
};
let tx = TransactionBuilder::new()
.id(tx_id)
.post_date(Local::now().into())
.enter_date(Local::now().into())
.build()?;
Ok(TransactionState::new(tx)
.with(vec![
FinanceEntity::Split(first),
FinanceEntity::Split(split(tx_id, 100)),
])
.with_split_tags(vec![(first_id, tag)]))
fn transaction_tag_names(bytecode: &[u8]) -> anyhow::Result<Vec<String>> {
let executor = ScriptExecutor::try_new()?;
let report =
tagged_split_state()?.run_scripts(&executor, &[(Uuid::new_v4(), bytecode.to_vec())])?;
anyhow::ensure!(report.failures.is_empty(), "{:?}", report.failures);
Ok(report
.state
.transaction_tags
.into_iter()
.map(|tag| tag.tag_name)
.collect())
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn create_from_source_runs_like_uploaded_bytecode(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let compiled = compile_script(TAG_SYNC)?;
let from_bytecode = user.create_script(compiled.clone(), None).await?;
let from_source = create_script(&user, TAG_SYNC.to_string(), None).await?;
let uploaded = user.get_script(from_bytecode).await?;
let saved = user.get_script(from_source).await?;
assert_eq!(saved.source.as_deref(), Some(TAG_SYNC));
assert_eq!(saved.bytecode, uploaded.bytecode);
assert_eq!(
transaction_tag_names(&saved.bytecode)?,
transaction_tag_names(&uploaded.bytecode)?
);
assert_eq!(transaction_tag_names(&saved.bytecode)?, ["category"]);
Ok(())
async fn update_source_recompiles_and_stores_both(pool: PgPool) -> anyhow::Result<()> {
let id = create_script(&user, "(+ 1 2)".to_string(), None).await?;
let first = user.get_script(id).await?;
update_script_source(&user, id, TAG_SYNC.to_string()).await?;
let second = user.get_script(id).await?;
assert_eq!(second.source.as_deref(), Some(TAG_SYNC));
assert_eq!(second.bytecode, compile_script(TAG_SYNC)?);
assert_ne!(second.bytecode, first.bytecode);
async fn a_compile_error_refuses_create_and_stores_nothing(pool: PgPool) -> anyhow::Result<()> {
let err = create_script(&user, "(unclosed".to_string(), None)
.await
.expect_err("does not compile");
assert!(matches!(err, ScriptError::Compile(_)), "{err:?}");
assert!(user.list_scripts().await?.is_empty());
async fn a_compile_error_leaves_the_script_as_it_was(pool: PgPool) -> anyhow::Result<()> {
let before = user.get_script(id).await?;
let err = update_script_source(&user, id, "(unclosed".to_string())
let after = user.get_script(id).await?;
assert_eq!(after.source, before.source);
assert_eq!(after.bytecode, before.bytecode);
async fn a_bytecode_only_script_lists_without_source_and_still_runs(
pool: PgPool,
) -> anyhow::Result<()> {
let old = user.create_script(compile_script(TAG_SYNC)?, None).await?;
let new = create_script(&user, TAG_SYNC.to_string(), None).await?;
let listed = user.list_scripts().await?;
let has_source = |id| {
listed
.iter()
.find(|info| info.id == id)
.map(|info| info.has_source)
assert_eq!(has_source(old), Some(false));
assert_eq!(has_source(new), Some(true));
let detail = user.get_script(old).await?;
assert_eq!(detail.source, None);
assert_eq!(transaction_tag_names(&detail.bytecode)?, ["category"]);