Lines
97.65 %
Functions
91.3 %
Branches
100 %
use super::commodity::CreateCommodity;
use super::commodity_edit::EditCommodity;
use super::commodity_tag::SetCommodityTag;
use super::{CmdError, CmdResult};
use crate::commodity_state::StoredCommodity;
use crate::{db::install_test_pool, 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 commodity(&self, symbol: &str, name: &str) -> anyhow::Result<Uuid> {
let created = CreateCommodity::new()
.symbol(symbol.to_string())
.name(name.to_string())
.user_id(self.user.id)
.run()
.await?;
match created {
Some(CmdResult::String(id)) => Ok(Uuid::parse_str(&id)?),
other => anyhow::bail!("unexpected CreateCommodity result {other:?}"),
async fn set_tag(&self, commodity: Uuid, name: &str, value: &str) -> anyhow::Result<()> {
SetCommodityTag::new()
.commodity_id(commodity)
.tag_name(name.to_string())
.tag_value(value.to_string())
Ok(())
async fn stored(&self, commodity: Uuid) -> anyhow::Result<StoredCommodity> {
self.user
.commodity_snapshot(commodity)
.await?
.ok_or_else(|| anyhow::anyhow!("commodity {commodity} is gone"))
async fn revision(&self, commodity: Uuid) -> anyhow::Result<String> {
Ok(self.stored(commodity).await?.revision())
async fn edit(
&self,
commodity: Uuid,
(symbol, name): (&str, &str),
tags: Vec<Tag>,
revision: String,
) -> Result<Option<CmdResult>, CmdError> {
EditCommodity::new()
.tags(tags)
.revision(revision)
.await
/// The stored `(name, value)` pairs of the commodity's tags.
fn pairs(stored: &StoredCommodity) -> 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 commodity_tags (commodity_id, tag_id) VALUES ($1, $2)")
.bind(owner)