Lines
98.04 %
Functions
93.75 %
Branches
100 %
use super::account::CreateAccount;
use super::account_merge::MergeAccounts;
use super::account_reparent::ReparentAccount;
use super::{CmdError, CmdResult, FinanceEntity};
use crate::{db::install_test_pool, hierarchy::lock_hierarchy, user::User};
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use tokio::sync::OnceCell;
use uuid::Uuid;
mod concurrency;
mod moves;
mod refusals;
static USER: OnceCell<User> = OnceCell::const_new();
async fn setup() {
USER.get_or_init(|| async { User { id: Uuid::new_v4() } })
.await;
}
struct Fixture {
user: &'static User,
async fn fixture() -> anyhow::Result<Fixture> {
let user = USER.get().ok_or_else(|| anyhow::anyhow!("no user"))?;
user.commit().await?;
Ok(Fixture { user })
impl Fixture {
async fn account(&self, name: &str, parent: Option<Uuid>) -> anyhow::Result<Uuid> {
let mut cmd = CreateAccount::new()
.name(name.to_string())
.user_id(self.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:?}"),
async fn reparent(
&self,
account: Uuid,
parent: Option<Uuid>,
) -> Result<Option<CmdResult>, CmdError> {
let mut cmd = ReparentAccount::new()
.user_id(self.user.id)
.account_id(account);
cmd.run().await
async fn merge(&self, into: Uuid, from: Uuid) -> Result<Option<CmdResult>, CmdError> {
MergeAccounts::new()
.into(into)
.from(from)
.run()
.await
/// `None` when the account is gone, else its stored parent.
async fn parent_of(&self, account: Uuid) -> anyhow::Result<Option<Option<Uuid>>> {
let mut conn = self.user.get_connection().await?;
Ok(
sqlx::query_scalar("SELECT parent_id FROM accounts WHERE id = $1")
.bind(account)
.fetch_optional(&mut *conn)
.await?,
)
fn is_refused(result: &Result<Option<CmdResult>, CmdError>) -> bool {
matches!(result, Err(CmdError::Refused(_)))