1
use super::*;
2
use crate::render::reparse_list;
3

            
4
const A: &str = "00000000-0000-0000-0000-00000000000a";
5
const B: &str = "00000000-0000-0000-0000-00000000000b";
6
const C: &str = "00000000-0000-0000-0000-00000000000c";
7
const D: &str = "00000000-0000-0000-0000-00000000000d";
8

            
9
20
fn account(id: &str, name: &str, parent: &str) -> String {
10
20
    format!(r#"(:account :id "{id}" :name "{name}" :parent "{parent}")"#)
11
20
}
12

            
13
6
fn sorted_ids(accounts: &[String]) -> Vec<String> {
14
6
    let parsed = reparse_list(&format!("({})", accounts.join(" "))).expect("list parses");
15
6
    let mut elements = nomiscript::list_to_vec(&parsed).expect("proper list");
16
6
    sort_by_path(&mut elements);
17
6
    elements.iter().filter_map(row_id).collect()
18
6
}
19

            
20
#[test]
21
1
fn children_follow_their_parent_path_not_their_activity() {
22
1
    let ids = sorted_ids(&[
23
1
        account(D, "Zed", B),
24
1
        account(C, "Bank", ""),
25
1
        account(B, "Assets", ""),
26
1
        account(A, "Cash", B),
27
1
    ]);
28
1
    assert_eq!(ids, [B, A, D, C]);
29
1
}
30

            
31
#[test]
32
1
fn equal_paths_break_the_tie_by_id() {
33
1
    let ids = sorted_ids(&[account(B, "Cash", ""), account(A, "Cash", "")]);
34
1
    assert_eq!(ids, [A, B]);
35
1
}
36

            
37
#[test]
38
1
fn ids_that_are_not_uuids_go_last_by_their_text() {
39
1
    let ids = sorted_ids(&[
40
1
        account("zz", "A", ""),
41
1
        account(B, "Z", ""),
42
1
        account("aa", "A", ""),
43
1
        account(A, "Y", ""),
44
1
    ]);
45
1
    assert_eq!(ids, [A, B, "aa", "zz"]);
46
1
}
47

            
48
#[test]
49
1
fn parent_cycle_terminates() {
50
1
    let ids = sorted_ids(&[account(A, "P", B), account(B, "Q", A)]);
51
1
    assert_eq!(ids, [B, A]);
52
1
}
53

            
54
#[test]
55
1
fn an_empty_name_stays_an_empty_segment_like_the_server_label() {
56
1
    let ids = sorted_ids(&[account(A, "Zed", ""), account(B, "", "")]);
57
1
    assert_eq!(ids, [B, A]);
58
1
}
59

            
60
#[test]
61
1
fn order_equals_the_form_options_order() {
62
1
    let rows = [
63
1
        (A, "Cash", Some(B)),
64
1
        (B, "Assets", None),
65
1
        (C, "", None),
66
1
        (D, "Zed", Some(B)),
67
1
    ];
68
38
    let uuid = |text: &str| Uuid::parse_str(text).expect("uuid");
69
14
    let name_of = |id: Uuid| rows.iter().find(|r| uuid(r.0) == id).map(|r| r.1);
70
6
    let parent_of = |id: Uuid| {
71
6
        rows.iter()
72
14
            .find(|r| uuid(r.0) == id)
73
6
            .and_then(|r| r.2)
74
6
            .map(uuid)
75
6
    };
76
1
    let mut expected: Vec<(String, Uuid)> = rows
77
1
        .iter()
78
4
        .map(|r| (path_label(uuid(r.0), name_of, parent_of), uuid(r.0)))
79
        .collect();
80
4
    expected.sort_by(|a, b| path_then_id((&a.0, a.1), (&b.0, b.1)));
81
1
    let accounts: Vec<String> = rows
82
1
        .iter()
83
1
        .rev()
84
4
        .map(|r| account(r.0, r.1, r.2.unwrap_or("")))
85
        .collect();
86
4
    let expected_ids: Vec<String> = expected.iter().map(|e| e.1.to_string()).collect();
87
1
    assert_eq!(sorted_ids(&accounts), expected_ids);
88
1
}
89

            
90
#[test]
91
1
fn project_rows_orders_the_accounts_schema_and_keeps_ids() {
92
1
    let parsed = reparse_list(&format!(
93
1
        "({} {})",
94
1
        account(B, "B", ""),
95
1
        account(A, "A", "")
96
1
    ))
97
1
    .expect("list parses");
98
1
    let rows = crate::render::schema::project_rows(&parsed, &crate::render::schema::ACCOUNTS);
99
2
    let ids: Vec<_> = rows.iter().filter_map(|row| row.id.as_deref()).collect();
100
1
    assert_eq!(ids, [A, B]);
101
1
}