Lines
95.12 %
Functions
92.86 %
Branches
100 %
use super::template::{CreateTemplate, DeleteTemplate, EditTemplate};
use super::{CmdError, CmdResult};
use crate::artifact_mgmt::user::TemplateDetail;
use crate::db::install_test_pool;
use crate::template_state::STALE_TEMPLATE;
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: Option<&str>, source: &str) -> anyhow::Result<Uuid> {
let mut command = CreateTemplate::new()
.user_id(user.id)
.source(source.to_string());
if let Some(name) = name {
command = command.name(name.to_string());
match command.run().await? {
Some(CmdResult::Uuid(id)) => Ok(id),
other => anyhow::bail!("unexpected CreateTemplate result {other:?}"),
async fn edit(
user: &User,
id: Uuid,
revision: &str,
name: Option<&str>,
source: &str,
) -> Result<Option<CmdResult>, CmdError> {
let mut command = EditTemplate::new()
.template_id(id)
.source(source.to_string())
.revision(revision.to_string());
command.run().await
async fn stored(user: &User, id: Uuid) -> anyhow::Result<TemplateDetail> {
Ok(user.get_template(id).await?)
#[local_db_sqlx_test]
async fn test_create_stores_the_source_and_drops_a_blank_name(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let user = user().await?;
let named = create(user, Some("rent"), "(a)").await?;
let blank = create(user, Some(" "), "(b)").await?;
assert_eq!(stored(user, named).await?.name.as_deref(), Some("rent"));
assert_eq!(stored(user, blank).await?.name, None);
assert_eq!(stored(user, blank).await?.source_text, "(b)");
async fn test_edit_name_only_keeps_the_source(pool: PgPool) -> Result<(), anyhow::Error> {
let id = create(user, Some("old"), "(a)").await?;
let revision = stored(user, id).await?.revision();
edit(user, id, &revision, Some("new"), "(a)").await?;
let template = stored(user, id).await?;
assert_eq!(template.name.as_deref(), Some("new"));
assert_eq!(template.source_text, "(a)");
async fn test_edit_source_only_keeps_the_name_and_a_blank_name_clears_it(
let id = create(user, Some("rent"), "(a)").await?;
edit(user, id, &revision, Some("rent"), "(b)").await?;
let renamed = stored(user, id).await?;
edit(user, id, &renamed.revision(), Some(" "), "(b)").await?;
assert_eq!(renamed.name.as_deref(), Some("rent"));
assert_eq!(renamed.source_text, "(b)");
assert_eq!(stored(user, id).await?.name, None);
async fn test_stale_edit_changes_neither_name_nor_source(
let id = create(user, Some("one"), "(a)").await?;
edit(user, id, &revision, Some("two"), "(b)").await?;
let result = edit(user, id, &revision, Some("three"), "(c)").await;
assert!(
matches!(&result, Err(CmdError::Args(m)) if m == STALE_TEMPLATE),
"{result:?}"
);
assert_eq!(template.name.as_deref(), Some("two"));
assert_eq!(template.source_text, "(b)");
async fn test_edit_of_a_missing_template_is_not_found(pool: PgPool) -> Result<(), anyhow::Error> {
let result = edit(user, Uuid::new_v4(), "any", None, "(a)").await;
matches!(&result, Err(err) if err.is_missing_row()),
async fn test_delete_removes_the_template_and_a_second_delete_is_not_found(
let delete = || DeleteTemplate::new().user_id(user.id).template_id(id).run();
let first = delete().await?;
let second = delete().await;
assert!(matches!(first, Some(CmdResult::Bool(true))), "{first:?}");
assert!(user.find_template(id).await?.is_none());
assert!(matches!(second, Err(CmdError::NotFound(_))), "{second:?}");
async fn test_delete_does_not_touch_a_script(pool: PgPool) -> Result<(), anyhow::Error> {
let script = user.create_script(vec![0, 1], None).await?;
let result = DeleteTemplate::new()
.template_id(script)
.run()
assert!(matches!(result, Err(CmdError::NotFound(_))), "{result:?}");
assert_eq!(user.list_scripts().await?.len(), 1);