Lines
92.31 %
Functions
100 %
Branches
use forms::{FormValues, escape_str};
use sqlx::PgPool;
use uuid::Uuid;
use super::test_seed::{seed_commodity, seed_price, seed_user};
use crate::ctx::ScriptCtx;
use crate::forms::{Failure, convert};
use crate::session::Session;
fn values(amount: &str, from: &str, to: &str) -> FormValues {
let mut values = FormValues::default();
values.set("amount", amount);
values.set("from", from);
values.set("to", to);
values
}
struct Rates {
user: Uuid,
usd: Uuid,
eur: Uuid,
async fn rates(pool: &PgPool) -> anyhow::Result<Rates> {
let user = seed_user(pool).await?;
let usd = seed_commodity(user, "USD").await?;
let eur = seed_commodity(user, "EUR").await?;
Ok(Rates { user, usd, eur })
async fn script(session: &mut Session, form: &str) -> String {
session.handle_form(&format!("(:id 1 :form {form})")).await
async fn invalid_fields(user: Uuid, values: &FormValues) -> anyhow::Result<Vec<String>> {
match convert(user, values).await {
Err(Failure::Invalid(errors)) => Ok(errors.into_iter().map(|e| e.field).collect()),
other => anyhow::bail!("expected field errors, got {other:?}"),
async fn refusal(user: Uuid, values: &FormValues) -> anyhow::Result<String> {
Err(Failure::Refused(reason)) => Ok(reason),
other => anyhow::bail!("expected a refusal, got {other:?}"),
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn a_script_receives_the_ratio_as_a_number(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let r = rates(&pool).await?;
seed_price(&pool, r.usd, r.eur, (9, 10)).await?;
let mut session = Session::new(ScriptCtx::new(r.user))?;
let payload = format!("(:amount \"1/3\" :from \"{}\" :to \"{}\")", r.usd, r.eur);
let call = format!("(submit-form \"convert\" {})", escape_str(&payload));
assert_eq!(script(&mut session, &call).await, "(:id 1 :value 3/10)");
assert_eq!(
script(&mut session, &format!("(equal {call} 3/10)")).await,
"(:id 1 :value t)"
);
Ok(())
async fn the_inverse_price_converts_back(pool: PgPool) -> anyhow::Result<()> {
let ratio = convert(r.user, &values("9", &r.eur.to_string(), &r.usd.to_string())).await?;
assert_eq!((*ratio.numer(), *ratio.denom()), (10, 1));
async fn the_same_commodity_is_an_error_at_to(pool: PgPool) -> anyhow::Result<()> {
let usd = r.usd.to_string();
invalid_fields(r.user, &values("1", &usd, &usd)).await?,
["to"]
async fn bad_fields_are_reported_together(pool: PgPool) -> anyhow::Result<()> {
let fields = invalid_fields(r.user, &values("x", &Uuid::new_v4().to_string(), "")).await?;
assert_eq!(fields, ["amount", "to", "from"]);
async fn an_overflowing_conversion_is_a_bad_argument(pool: PgPool) -> anyhow::Result<()> {
seed_price(&pool, r.usd, r.eur, (2, 1)).await?;
let big = i64::MAX.to_string();
let failure = convert(
r.user,
&values(&big, &r.usd.to_string(), &r.eur.to_string()),
)
.await
.expect_err("overflow");
assert_eq!(failure.http_status(), 400, "{failure}");
assert!(failure.to_string().contains("overflow"), "{failure}");
async fn a_missing_price_names_both_commodities(pool: PgPool) -> anyhow::Result<()> {
let reason = refusal(r.user, &values("1", &r.usd.to_string(), &r.eur.to_string())).await?;
assert_eq!(reason, "no exchange rate between USD and EUR");