Lines
97.3 %
Functions
91.67 %
Branches
100 %
use super::tag::{CreateTag, DeleteTag, EditTag, ListTags};
use super::{CmdError, CmdResult, FinanceEntity};
use crate::db::install_test_pool;
use crate::error::ServerError;
use crate::tag_state::{STALE_TAG, tag_revision};
use crate::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 create(user: &User, name: &str, value: &str) -> anyhow::Result<Uuid> {
match CreateTag::new()
.user_id(user.id)
.name(name.to_string())
.value(value.to_string())
.run()
.await?
{
Some(CmdResult::Uuid(id)) => Ok(id),
other => anyhow::bail!("unexpected CreateTag result {other:?}"),
async fn edit(
user: &User,
tag: Uuid,
revision: &str,
description: Option<&str>,
) -> Result<Option<CmdResult>, CmdError> {
let mut command = EditTag::new()
.tag_id(tag)
.name("kind".to_string())
.value("fiat".to_string())
.revision(revision.to_string());
if let Some(text) = description {
command = command.description(text.to_string());
command.run().await
#[local_db_sqlx_test]
async fn test_edit_with_the_loaded_revision_applies(pool: PgPool) -> Result<(), anyhow::Error> {
let user = user().await?;
let tag = create(user, "kind", "fiat").await?;
let revision = tag_revision(&user.stored_tag(tag).await?);
edit(user, tag, &revision, Some("note")).await?;
assert_eq!(
user.stored_tag(tag).await?.description.as_deref(),
Some("note")
);
async fn test_edit_with_an_old_revision_is_stale(pool: PgPool) -> Result<(), anyhow::Error> {
edit(user, tag, &revision, Some("first")).await?;
let result = edit(user, tag, &revision, Some("second")).await;
assert!(
matches!(&result, Err(CmdError::Args(m)) if m == STALE_TAG),
"{result:?}"
Some("first")
async fn test_edit_onto_another_tag_is_a_clash(pool: PgPool) -> Result<(), anyhow::Error> {
let tag = create(user, "kind", "coin").await?;
create(user, "kind", "fiat").await?;
let result = edit(user, tag, &revision, None).await;
matches!(&result, Err(CmdError::Server(ServerError::TagClash { .. }))),
async fn test_edit_of_a_deleted_tag_is_not_found(pool: PgPool) -> Result<(), anyhow::Error> {
user.delete_tag(tag).await?;
let edited = edit(user, tag, &revision, None).await;
let updated = user
.rename_tag(tag, "kind".into(), "fiat".into(), None)
matches!(&edited, Err(err) if err.is_missing_row()),
"{edited:?}"
matches!(&updated, Err(err) if err.is_missing_row()),
"{updated:?}"
async fn test_delete_removes_the_tag_and_a_second_delete_is_not_found(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let delete = || DeleteTag::new().user_id(user.id).tag_id(tag).run();
let first = delete().await?;
let second = delete().await;
assert!(matches!(first, Some(CmdResult::Bool(true))), "{first:?}");
assert!(user.find_tag(tag).await?.is_none());
assert!(matches!(second, Err(CmdError::NotFound(_))), "{second:?}");
async fn test_list_returns_the_users_tags_with_descriptions(
let listed = ListTags::new().user_id(user.id).run().await?;
let Some(CmdResult::Entities(entities)) = listed else {
anyhow::bail!("unexpected ListTags result {listed:?}");
};
let [FinanceEntity::Tag(found)] = entities.as_slice() else {
anyhow::bail!("expected one tag, got {entities:?}");
assert_eq!(found.id, tag);
assert_eq!(found.description.as_deref(), Some("note"));
async fn test_list_is_empty_for_a_user_without_tags(pool: PgPool) -> Result<(), anyhow::Error> {
matches!(&listed, Some(CmdResult::Entities(entities)) if entities.is_empty()),
"{listed:?}"