Lines
94.44 %
Functions
88.89 %
Branches
100 %
use super::account::{CreateAccount, SetAccountTag};
use super::account_name::{account_name_notice, duplicate_name_notice};
use super::{CmdResult, FinanceEntity};
use crate::{db::install_test_pool, user::User};
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use tokio::sync::OnceCell;
use uuid::Uuid;
static USER: OnceCell<User> = OnceCell::const_new();
async fn setup() {
USER.get_or_init(|| async { User { id: Uuid::new_v4() } })
.await;
}
async fn user() -> anyhow::Result<&'static User> {
let user = USER.get().ok_or_else(|| anyhow::anyhow!("no user"))?;
user.commit().await?;
Ok(user)
async fn account(user: &User, name: &str, parent: Option<Uuid>) -> anyhow::Result<Uuid> {
let mut cmd = CreateAccount::new().name(name.to_string()).user_id(user.id);
if let Some(parent) = parent {
cmd = cmd.parent(parent);
match cmd.run().await? {
Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
other => anyhow::bail!("unexpected CreateAccount result {other:?}"),
#[local_db_sqlx_test]
async fn test_a_twin_under_the_same_parent_gets_the_notice(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let user = user().await?;
let food = account(user, "Food", None).await?;
let first = account(user, "Cafe", Some(food)).await?;
assert_eq!(account_name_notice(user.id, first).await?, None);
let twin = account(user, "Cafe", Some(food)).await?;
assert_eq!(
account_name_notice(user.id, twin).await?,
Some(duplicate_name_notice("Cafe"))
);
account_name_notice(user.id, first).await?,
async fn test_the_same_name_elsewhere_is_no_twin(pool: PgPool) -> Result<(), anyhow::Error> {
account(user, "Cafe", None).await?;
let nested = account(user, "Cafe", Some(food)).await?;
assert_eq!(account_name_notice(user.id, nested).await?, None);
async fn test_a_rename_into_a_twin_gets_the_notice(pool: PgPool) -> Result<(), anyhow::Error> {
let bar = account(user, "Bar", None).await?;
SetAccountTag::new()
.user_id(user.id)
.account_id(bar)
.tag_name("name".to_string())
.tag_value("Cafe".to_string())
.run()
.await?;
account_name_notice(user.id, bar).await?,
assert_eq!(account_name_notice(user.id, Uuid::new_v4()).await?, None);