Lines
100 %
Functions
Branches
use forms::ACCOUNT_EDIT;
use sqlx::PgPool;
use super::test_account_edit::{loaded, rebuilt, seeded, set_tag, tag_row};
use super::test_seed::seed_account;
use crate::forms::submit;
use crate::resolve::Directory;
async fn path_of(user: uuid::Uuid, account: uuid::Uuid) -> anyhow::Result<String> {
Ok(Directory::load("test", user).await?.account_path(account))
}
async fn tag_pairs(user: uuid::Uuid, account: uuid::Uuid) -> anyhow::Result<Vec<(String, String)>> {
let values = loaded(user, account).await?;
Ok(values
.rows("tags")
.unwrap_or_default()
.iter()
.map(|row| (row["name"].clone(), row["value"].clone()))
.collect())
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn rename_changes_the_name_and_keeps_the_tags(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fx = seeded(&pool).await?;
let mut values = loaded(fx.user, fx.account).await?;
values.set("name", " Wallet ");
let submitted = submit(fx.user, &ACCOUNT_EDIT, &values).await?;
assert_eq!(submitted.id, fx.account);
assert_eq!(path_of(fx.user, fx.account).await?, "Assets:Wallet");
assert_eq!(tag_pairs(fx.user, fx.account).await?.len(), 2);
Ok(())
async fn reparent_moves_to_another_parent_and_to_the_root(pool: PgPool) -> anyhow::Result<()> {
let other = seed_account(fx.user, "Other").await?;
values.set("parent", &other.to_string());
submit(fx.user, &ACCOUNT_EDIT, &values).await?;
assert_eq!(path_of(fx.user, fx.account).await?, "Other:Cash");
values.set("parent", "");
assert_eq!(path_of(fx.user, fx.account).await?, "Cash");
async fn tags_are_added_changed_and_removed(pool: PgPool) -> anyhow::Result<()> {
let values = loaded(fx.user, fx.account).await?;
let edited = rebuilt(
&values,
vec![tag_row("type", "bank"), tag_row("iban", "X1")],
);
submit(fx.user, &ACCOUNT_EDIT, &edited).await?;
let pairs = tag_pairs(fx.user, fx.account).await?;
let expected = [("iban", "X1"), ("type", "bank")]
.map(|(name, value)| (name.to_string(), value.to_string()));
assert_eq!(pairs, expected);
async fn an_empty_tags_list_clears_every_tag(pool: PgPool) -> anyhow::Result<()> {
submit(fx.user, &ACCOUNT_EDIT, &rebuilt(&values, vec![])).await?;
assert!(tag_pairs(fx.user, fx.account).await?.is_empty());
assert_eq!(path_of(fx.user, fx.account).await?, "Assets:Cash");
async fn a_second_submit_with_the_same_revision_is_stale_after_a_change(
pool: PgPool,
) -> anyhow::Result<()> {
set_tag(fx.user, fx.account, "type", "other").await?;
let again = super::test_account_edit::invalid_fields(fx.user, &values).await?;
assert_eq!(again, ["revision"]);