Lines
98.46 %
Functions
100 %
Branches
use forms::{COMMODITY_EDIT, FormValues, Row};
use server::command::commodity_tag::SetCommodityTag;
use sqlx::PgPool;
use uuid::Uuid;
use super::test_seed::{seed_commodity, seed_user};
use crate::forms::{Failure, load, submit};
pub(super) async fn loaded(user: Uuid, commodity: Uuid) -> anyhow::Result<FormValues> {
Ok(load(user, &COMMODITY_EDIT, &commodity.to_string()).await?)
}
pub(super) async fn set_tag(
user: Uuid,
commodity: Uuid,
name: &str,
value: &str,
) -> anyhow::Result<()> {
SetCommodityTag::new()
.user_id(user)
.commodity_id(commodity)
.tag_name(name.to_string())
.tag_value(value.to_string())
.run()
.await?;
Ok(())
pub(super) fn tag_row(name: &str, value: &str) -> Row {
Row::from([
("name".to_string(), name.to_string()),
("value".to_string(), value.to_string()),
])
/// The values with `tags` replaced by `rows`; `set_rows` keeps old rows, so
/// rebuild from the scalar fields.
pub(super) fn rebuilt(values: &FormValues, rows: Vec<Row>) -> FormValues {
let mut edited = FormValues::default();
for key in ["id", "revision", "symbol", "name"] {
edited.set(key, values.text(key).unwrap_or_default());
edited.set_rows("tags");
for row in rows {
edited.push_row("tags", row);
edited
pub(super) async fn invalid_fields(user: Uuid, values: &FormValues) -> anyhow::Result<Vec<String>> {
match submit(user, &COMMODITY_EDIT, values).await {
Err(Failure::Invalid(errors)) => Ok(errors.into_iter().map(|e| e.field).collect()),
other => anyhow::bail!("expected field errors, got {other:?}"),
pub(super) struct Seeded {
pub(super) user: Uuid,
pub(super) commodity: Uuid,
pub(super) async fn seeded(pool: &PgPool) -> anyhow::Result<Seeded> {
let user = seed_user(pool).await?;
let commodity = seed_commodity(user, "USD").await?;
set_tag(user, commodity, "kind", "fiat").await?;
set_tag(user, commodity, "color", "green").await?;
Ok(Seeded { user, commodity })
pub(super) async fn link_raw_tag(
pool: &PgPool,
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 commodity_tags (commodity_id, tag_id) VALUES ($1, $2)")
.bind(commodity)