Lines
97.83 %
Functions
100 %
Branches
use forms::FormValues;
use rpc::{ScriptCtx, Session};
use server::command::account::CreateAccount;
use server::command::{CmdResult, FinanceEntity};
use sqlx::PgPool;
use uuid::Uuid;
use super::merge;
use crate::CommandError;
use crate::transaction::eval_text;
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
let user_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, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
)
.bind(user_id)
.bind(format!("u{user_id}"))
.bind(format!("{user_id}@test.com"))
.execute(pool)
.await?;
Ok(user_id)
}
async fn account(user_id: Uuid, 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!("unexpected CreateAccount result: {other:?}"),
fn picks(into: &str, from: &str) -> FormValues {
let mut values = FormValues::default();
values.set("into", into);
values.set("from", from);
values
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn merge_picks_two_and_keeps_into(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let into = account(user, "Cafe").await?;
let from = account(user, "Cafe").await?;
let mut session = Session::new(ScriptCtx::new(user))?;
let kept = merge(&mut session, &picks(&into.to_string(), &from.to_string())).await?;
assert_eq!(kept, into.to_string());
let again = merge(&mut session, &picks(&into.to_string(), &from.to_string())).await;
assert!(
matches!(&again, Err(CommandError::Argument(m)) if m.contains("--from")),
"the merged account is gone: {again:?}"
);
Ok(())
async fn listed(session: &mut Session) -> anyhow::Result<String> {
Ok(eval_text(session, "(form-options \"accounts\")").await?)
async fn merge_maps_an_alias_and_passes_a_uuid(pool: PgPool) -> anyhow::Result<()> {
account(user, "Coffee").await?;
let kept = merge(&mut session, &picks(&into.to_string(), "Coffee")).await?;
assert!(!listed(&mut session).await?.contains("Coffee"));
async fn ambiguous_or_unknown_merge_pick_fails_before_merging(pool: PgPool) -> anyhow::Result<()> {
account(user, "Twin").await?;
account(user, "Solo").await?;
let before = listed(&mut session).await?;
for (values, flag) in [
(picks("Solo", "Twin"), "--from"),
(picks("Twin", "Solo"), "--into"),
(picks("Solo", "Nowhere"), "--from"),
] {
let failed = merge(&mut session, &values).await;
matches!(&failed, Err(CommandError::Argument(m)) if m.contains(flag)),
"{flag}: {failed:?}"
assert_eq!(listed(&mut session).await?, before);
#[test]
fn the_preview_call_carries_the_merge_payload() {
values.set("into", "a");
values.set("from", "b");
assert_eq!(
super::merge_preview_call(&values),
r#"(merge-preview "(:into \"a\" :from \"b\")")"#
fn the_preview_splits_are_read_from_the_reply_plist() -> anyhow::Result<()> {
use crate::render::{WireValue, parse_wire};
let WireValue::Value(plist) = parse_wire(r#"(:id 1 :value (:into "x" :from "y" :splits 4))"#)?;
assert_eq!(super::merge_preview_splits(&plist), Some(4));
assert_eq!(super::merge_preview_splits(&nomiscript::Value::Nil), None);