1
//! Seeds for the report pages' tests.
2

            
3
use chrono::{Datelike, Utc};
4
use server::command::account::SetAccountTag;
5
use sqlx::types::Uuid;
6
use web::model::User;
7

            
8
use super::new_page::{created_id, post_form, seeded_ids};
9

            
10
13
pub fn today_string() -> String {
11
13
    Utc::now().date_naive().format("%Y-%m-%d").to_string()
12
13
}
13

            
14
4
pub fn month_start_string() -> String {
15
4
    let today = Utc::now().date_naive();
16
4
    today
17
4
        .with_day(1)
18
4
        .unwrap_or(today)
19
4
        .format("%Y-%m-%d")
20
4
        .to_string()
21
4
}
22

            
23
29
pub async fn spend(user: &User, category: &str, value: &str) {
24
29
    spend_at(user, category, value, "2026-03-10").await;
25
29
}
26

            
27
42
pub async fn spend_at(user: &User, category: &str, value: &str, date: &str) {
28
42
    post_spend(user, Some(category), value, date).await;
29
42
}
30

            
31
/// An expense whose leg carries no `category` tag.
32
6
pub async fn spend_uncategorized(user: &User, value: &str) {
33
6
    post_spend(user, None, value, "2026-03-10").await;
34
6
}
35

            
36
48
async fn post_spend(user: &User, category: Option<&str>, value: &str, date: &str) {
37
48
    let ids = seeded_ids(user).await;
38
48
    let mut pairs = vec![
39
48
        ("date", date),
40
48
        ("note", "spend"),
41
48
        ("tags", ""),
42
48
        ("splits", ""),
43
48
        ("splits.0.key", "n1"),
44
48
        ("splits.0.from", ids.checking.as_str()),
45
48
        ("splits.0.to", ids.food.as_str()),
46
48
        ("splits.0.from-commodity", ids.usd.as_str()),
47
48
        ("splits.0.value", value),
48
48
        ("splits.0.hidden", ""),
49
48
        ("split-tags", ""),
50
    ];
51
48
    if let Some(category) = category {
52
42
        pairs.extend([
53
42
            ("split-tags.0.row", "n1"),
54
42
            ("split-tags.0.name", "category"),
55
42
            ("split-tags.0.value", category),
56
42
            ("split-tags.0.description", ""),
57
42
            ("split-tags.0.side", "to"),
58
42
        ]);
59
6
    }
60
48
    created_id(&post_form(user, &pairs).await);
61
48
}
62

            
63
20
pub async fn typed_expense(user: &User) {
64
20
    let food = seeded_ids(user).await.food;
65
20
    SetAccountTag::new()
66
20
        .user_id(user.id)
67
20
        .account_id(Uuid::parse_str(&food).expect("account id"))
68
20
        .tag_name("type".to_string())
69
20
        .tag_value("expense".to_string())
70
20
        .run()
71
20
        .await
72
20
        .expect("type tag");
73
20
}