Lines
100 %
Functions
Branches
//! A split carrying two values of one tag name reaches a script through
//! `get-split-tags`, while `get-split-tag` stays the lowest value.
#![cfg(feature = "db")]
use rpc::{ScriptCtx, Session};
use server::db::install_test_pool;
use server::user::User;
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use uuid::Uuid;
async fn setup() {}
async fn insert_test_user(pool: &PgPool, id: Uuid) -> anyhow::Result<()> {
sqlx::query(
"INSERT INTO users (
id, user_name, email, photo, verified, user_password,
user_role, database_name, created_at
) VALUES (
$1, 'split-values-user', 'split-values@example.com', 'default.png',
FALSE, 'irrelevant', 'user', 'split-values', NOW()
)",
)
.bind(id)
.execute(pool)
.await?;
Ok(())
}
fn extract_value_uuid(response: &str) -> Option<String> {
let needle = ":value \"";
let start = response.find(needle)? + needle.len();
let end = start + response[start..].find('"')?;
Some(response[start..end].to_string())
#[local_db_sqlx_test]
async fn two_values_of_one_name_reach_a_script(pool: PgPool) -> anyhow::Result<()> {
setup().await;
let user_id = Uuid::new_v4();
insert_test_user(&pool, user_id).await?;
let mut session = Session::new(ScriptCtx::new(user_id)).expect("Session::new");
let usd = extract_value_uuid(
&session
.handle_form("(:id 1 :form (create-commodity \"USD\" \"Dollar\"))")
.await,
.expect("commodity id");
let cash = extract_value_uuid(
.handle_form("(:id 2 :form (create-account \"Cash\" \"\"))")
.expect("cash id");
let food = extract_value_uuid(
.handle_form("(:id 3 :form (create-account \"Food\" \"\"))")
.expect("food id");
let tx = extract_value_uuid(
.handle_form(&format!(
"(:id 4 :form (create-transaction \"(:post-date \\\"2026-02-01T00:00:00Z\\\" \
:note \\\"n\\\" :splits ((:account-id \\\"{cash}\\\" :commodity-id \\\"{usd}\\\" :value -5) \
(:account-id \\\"{food}\\\" :commodity-id \\\"{usd}\\\" :value 5)))\"))"
))
.expect("tx id");
let split =
sqlx::query_scalar::<_, Uuid>("SELECT id FROM splits WHERE tx_id = $1 AND account_id = $2")
.bind(Uuid::parse_str(&tx)?)
.bind(Uuid::parse_str(&food)?)
.fetch_one(&pool)
let user = User { id: user_id };
for value in ["b", "a"] {
user.create_split_tag(split, "dup".to_string(), value.to_string(), None)
let all = session
"(:id 5 :form (get-split-tags \"{split}\" \"dup\"))"
.await;
assert!(!all.contains(":error"), "{all}");
assert!(all.contains(r#":value ("a" "b")"#), "{all}");
let first = session
"(:id 6 :form (get-split-tag \"{split}\" \"dup\"))"
assert!(first.contains(":value \"a\""), "{first}");
let none = session
"(:id 7 :form (length (get-split-tags \"{split}\" \"missing\")))"
assert!(none.contains(":value 0"), "{none}");