Lines
100 %
Functions
Branches
//! The list page, its lazy detail and delete, served on their production
//! paths.
use axum::{
Router,
body::Body,
http::{Method, Request},
routing::{get, post},
};
use sqlx::types::Uuid;
use tower::ServiceExt;
use web::model::User;
use web::pages::transaction::browse::{LIST_PATH, transaction_detail, transaction_list_page};
use web::pages::transaction::delete::submit::transaction_delete_submit;
use super::new_page::{Reply, created_id, post_form};
use super::seed::{account_id, commodity_id};
use super::{authenticated, create_test_app_state};
async fn send(user: &User, method: Method, uri: &str) -> Reply {
let router = Router::new()
.route(LIST_PATH, get(transaction_list_page))
.route("/api/transaction/detail/{id}", get(transaction_detail))
.route(
"/api/transaction/delete/{id}",
post(transaction_delete_submit),
);
let request = Request::builder()
.method(method)
.uri(uri)
.body(Body::empty())
.expect("request");
let response = authenticated(router, create_test_app_state().await, user.clone())
.oneshot(request)
.await
.expect("response");
let (parts, body) = response.into_parts();
let bytes = axum::body::to_bytes(body, usize::MAX).await.expect("body");
Reply {
status: parts.status,
headers: parts.headers,
body: String::from_utf8_lossy(&bytes).into_owned(),
}
pub async fn list(user: &User, query: &str) -> Reply {
send(user, Method::GET, &format!("{LIST_PATH}?{query}")).await
pub async fn detail(user: &User, id: &str) -> Reply {
send(user, Method::GET, &format!("/api/transaction/detail/{id}")).await
pub async fn delete(user: &User, id: &str) -> Reply {
send(user, Method::POST, &format!("/api/transaction/delete/{id}")).await
/// Create `Assets:Checking` → `to` for `value` USD at `date`.
pub async fn add(user: &User, date: &str, note: &str, to: &str, value: &str) -> Uuid {
let (checking, to) = (
account_id(user.id, "Assets:Checking").await,
account_id(user.id, to).await,
let usd = commodity_id(user.id, "USD").await;
let pairs = [
("date", date),
("note", note),
("tags", ""),
("splits", ""),
("splits.0.key", "n1"),
("splits.0.from", checking.as_str()),
("splits.0.to", to.as_str()),
("splits.0.from-commodity", usd.as_str()),
("splits.0.value", value),
("splits.0.hidden", ""),
("split-tags", ""),
("split-tags.0.row", "n1"),
("split-tags.0.name", "memo"),
("split-tags.0.value", "lunch"),
];
created_id(&post_form(user, &pairs).await)
/// The note cells of the listed rows, in order of appearance.
pub fn listed_notes(body: &str) -> Vec<&str> {
body.split("<summary>")
.skip(1)
.filter_map(|tail| tail.split_once('>')?.1.split('<').next())
.collect()