Lines
94.44 %
Functions
91.67 %
Branches
100 %
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use tokio::sync::OnceCell;
use uuid::Uuid;
use super::ListTemplates;
use crate::artifact_mgmt::user::TemplateInfo;
use crate::command::CmdResult;
use crate::db::install_test_pool;
use crate::user::User;
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 listed(user: &User) -> anyhow::Result<Vec<(Uuid, Option<String>)>> {
let templates = templates(user).await?;
Ok(templates.into_iter().map(|t| (t.id, t.name)).collect())
async fn templates(user: &User) -> anyhow::Result<Vec<TemplateInfo>> {
match ListTemplates::new().user_id(user.id).run().await? {
Some(CmdResult::Templates(listed)) => Ok(listed),
other => anyhow::bail!("unexpected result {other:?}"),
#[local_db_sqlx_test]
async fn lists_templates_by_name_with_unnamed_last(pool: PgPool) -> anyhow::Result<()> {
let user = user().await?;
let unnamed = user.create_template("(a)".into(), None).await?;
let rent = user
.create_template("(b)".into(), Some("rent".into()))
.await?;
let food = user
.create_template("(c)".into(), Some("food".into()))
assert_eq!(
listed(user).await?,
vec![
(food, Some("food".into())),
(rent, Some("rent".into())),
(unnamed, None),
]
);
async fn lists_nothing_without_templates(pool: PgPool) -> anyhow::Result<()> {
assert!(listed(user).await?.is_empty());