Lines
98.15 %
Functions
100 %
Branches
use forms::{ACCOUNT_EDIT, FormValues, Row};
use server::command::account::SetAccountTag;
use sqlx::PgPool;
use uuid::Uuid;
use super::test_seed::{seed_account, seed_child_account, seed_user};
use crate::forms::{Failure, load, submit};
pub(super) async fn loaded(user: Uuid, account: Uuid) -> anyhow::Result<FormValues> {
Ok(load(user, &ACCOUNT_EDIT, &account.to_string()).await?)
}
pub(super) async fn set_tag(
user: Uuid,
account: Uuid,
name: &str,
value: &str,
) -> anyhow::Result<()> {
SetAccountTag::new()
.user_id(user)
.account_id(account)
.tag_name(name.to_string())
.tag_value(value.to_string())
.run()
.await?;
Ok(())
pub(super) fn tag_row(name: &str, value: &str) -> Row {
Row::from([
("name".to_string(), name.to_string()),
("value".to_string(), value.to_string()),
])
pub(super) fn with_tags(values: &FormValues, rows: Vec<Row>) -> FormValues {
let mut edited = values.clone();
edited.set_rows("tags");
for row in rows {
edited.push_row("tags", row);
edited
/// The values with `tags` replaced by `rows`; `set_rows` keeps old rows, so
/// rebuild from the scalar fields.
pub(super) fn rebuilt(values: &FormValues, rows: Vec<Row>) -> FormValues {
let mut edited = FormValues::default();
for key in ["id", "revision", "name", "parent"] {
edited.set(key, values.text(key).unwrap_or_default());
with_tags(&edited, rows)
pub(super) async fn invalid_fields(user: Uuid, values: &FormValues) -> anyhow::Result<Vec<String>> {
match submit(user, &ACCOUNT_EDIT, values).await {
Err(Failure::Invalid(errors)) => Ok(errors.into_iter().map(|e| e.field).collect()),
other => anyhow::bail!("expected field errors, got {other:?}"),
pub(super) struct Seeded {
pub(super) user: Uuid,
pub(super) parent: Uuid,
pub(super) account: Uuid,
pub(super) async fn seeded(pool: &PgPool) -> anyhow::Result<Seeded> {
let user = seed_user(pool).await?;
let parent = seed_account(user, "Assets").await?;
let account = seed_child_account(user, "Cash", parent).await?;
set_tag(user, account, "type", "asset").await?;
set_tag(user, account, "color", "red").await?;
Ok(Seeded {
user,
parent,
account,
})