Lines
97.7 %
Functions
91.3 %
Branches
100 %
use super::account::CreateAccount;
use super::account::SetAccountTag;
use super::account_edit::EditAccount;
use super::{CmdError, CmdResult, FinanceEntity};
use crate::account_state::StoredAccount;
use crate::{db::install_test_pool, hierarchy::lock_hierarchy, user::User};
use finance::tag::Tag;
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use tokio::sync::OnceCell;
use uuid::Uuid;
mod concurrency;
mod edits;
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 })
fn tag(name: &str, value: &str) -> Tag {
Tag {
id: Uuid::new_v4(),
tag_name: name.to_string(),
tag_value: value.to_string(),
description: None,
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 set_tag(&self, account: Uuid, name: &str, value: &str) -> anyhow::Result<()> {
SetAccountTag::new()
.user_id(self.user.id)
.account_id(account)
.tag_name(name.to_string())
.tag_value(value.to_string())
.run()
.await?;
Ok(())
async fn stored(&self, account: Uuid) -> anyhow::Result<StoredAccount> {
self.user
.account_snapshot(account)
.await?
.ok_or_else(|| anyhow::anyhow!("account {account} is gone"))
async fn revision(&self, account: Uuid) -> anyhow::Result<String> {
Ok(self.stored(account).await?.revision())
async fn edit(
&self,
account: Uuid,
name: &str,
parent: Option<Uuid>,
tags: Vec<Tag>,
revision: String,
) -> Result<Option<CmdResult>, CmdError> {
let mut cmd = EditAccount::new()
.tags(tags)
.revision(revision);
cmd.run().await
/// The stored `(name, value)` pairs of the account's tags.
fn pairs(stored: &StoredAccount) -> Vec<(&str, &str)> {
stored
.tags
.iter()
.map(|tag| (tag.tag_name.as_str(), tag.tag_value.as_str()))
.collect()
/// Link a tag row whose name is stored with its padding.
async fn link_raw(pool: &PgPool, owner: Uuid, name: &str, value: &str) -> anyhow::Result<()> {
let tag = Uuid::new_v4();
sqlx::query("INSERT INTO tags (id, tag_name, tag_value) VALUES ($1, $2, $3)")
.bind(tag)
.bind(name)
.bind(value)
.execute(pool)
sqlx::query("INSERT INTO account_tags (account_id, tag_id) VALUES ($1, $2)")
.bind(owner)