Lines
100 %
Functions
Branches
use std::collections::{HashMap, HashSet};
use forms::Source;
use sqlx::PgPool;
use uuid::Uuid;
use super::form_options;
use super::test_seed::{seed_account, seed_commodity, seed_user};
use crate::forms::options;
async fn labels(user: Uuid, source: Source) -> anyhow::Result<HashMap<Uuid, String>> {
Ok(options(user, source)
.await?
.into_iter()
.map(|option| (option.id, option.label))
.collect())
}
fn assert_unique(labels: &HashMap<Uuid, String>, fold: fn(&str) -> String) {
let folded: HashSet<String> = labels.values().map(|label| fold(label)).collect();
assert_eq!(folded.len(), labels.len(), "{labels:?}");
fn prefix(id: Uuid, length: usize) -> String {
id.simple().to_string()[..length].to_string()
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn clashing_names_get_an_id_prefix(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let first = seed_account(user, "Cash").await?;
let second = seed_account(user, "Cash").await?;
let single = seed_account(user, "Bank").await?;
let labels = labels(user, Source::Accounts).await?;
assert_eq!(labels[&single], "Bank");
for id in [first, second] {
assert_eq!(labels[&id], format!("Cash ‹{}›", prefix(id, 4)));
assert_unique(&labels, str::to_string);
Ok(())
async fn a_suffix_never_collides_with_a_literal_label(pool: PgPool) -> anyhow::Result<()> {
let literal_first = seed_account(user, &format!("Cash ‹{}›", prefix(first, 4))).await?;
let literal_second = seed_account(user, &format!("Cash ‹{}›", prefix(second, 4))).await?;
for (id, literal) in [(literal_first, first), (literal_second, second)] {
assert_eq!(labels[&id], format!("Cash ‹{}›", prefix(literal, 4)));
assert!(labels[&id].starts_with("Cash ‹"), "{labels:?}");
assert!(labels[&id].len() > format!("Cash ‹{}›", prefix(id, 4)).len());
async fn pipes_and_line_breaks_are_cell_safe(pool: PgPool) -> anyhow::Result<()> {
let pipe = seed_account(user, "A|B").await?;
let newline = seed_account(user, "Line\nBreak").await?;
let literal = seed_account(user, "A¦B").await?;
assert_eq!(labels[&newline], "Line Break");
assert_eq!(labels[&pipe], format!("A¦B ‹{}›", prefix(pipe, 4)));
assert_eq!(labels[&literal], format!("A¦B ‹{}›", prefix(literal, 4)));
assert!(
labels
.values()
.all(|label| !label.contains(['|', '\n', '\r']))
);
async fn a_case_only_commodity_clash_is_a_clash(pool: PgPool) -> anyhow::Result<()> {
let upper = seed_commodity(user, "EUR").await?;
let lower = seed_commodity(user, "eur").await?;
let usd = seed_commodity(user, "USD").await?;
let labels = labels(user, Source::Commodities).await?;
assert_eq!(labels[&usd], "USD");
assert_eq!(labels[&upper], format!("EUR ‹{}›", prefix(upper, 4)));
assert_eq!(labels[&lower], format!("eur ‹{}›", prefix(lower, 4)));
assert_unique(&labels, str::to_lowercase);
async fn a_uuid_shaped_name_is_reserved(pool: PgPool) -> anyhow::Result<()> {
let other = Uuid::new_v4();
let shaped = seed_account(user, &other.to_string()).await?;
assert_eq!(labels[&shaped], format!("{other} ‹{}›", prefix(shaped, 4)));
async fn every_option_carries_its_bare_name_as_alias(pool: PgPool) -> anyhow::Result<()> {
let root = seed_account(user, "Assets").await?;
let accounts = form_options(user, "accounts").await?;
for (id, name) in [(root, "Assets"), (first, "Cash"), (second, "Cash")] {
let item = accounts
.split("(:id ")
.find(|item| item.starts_with(&format!("\"{id}\"")))
.unwrap_or_default();
assert!(item.contains(&format!(":alias \"{name}\"")), "{accounts}");
let commodities = form_options(user, "commodities").await?;
assert!(commodities.contains(&format!("(:id \"{usd}\" :label \"USD\" :alias \"USD\")")));