1
use std::collections::HashMap;
2

            
3
use uuid::Uuid;
4

            
5
use super::Resolution;
6
use crate::resolve::Directory;
7

            
8
42
fn named(name: &str) -> (Uuid, Option<String>) {
9
42
    (Uuid::new_v4(), Some(name.to_string()))
10
42
}
11

            
12
struct Fixture {
13
    directory: Directory,
14
    cafe: Uuid,
15
}
16

            
17
6
fn fixture() -> Fixture {
18
6
    let (expenses, cafe) = (named("Expenses"), named("Cafe"));
19
6
    let (lunch, other_lunch) = (named("Lunch"), named("Lunch"));
20
6
    let mut directory = Directory {
21
6
        accounts: vec![
22
6
            expenses.clone(),
23
6
            cafe.clone(),
24
6
            lunch.clone(),
25
6
            other_lunch.clone(),
26
6
        ],
27
6
        account_paths: Vec::new(),
28
6
        commodities: vec![named("USD"), named("JPY"), named("jpy")],
29
6
        parents: HashMap::from([
30
6
            (cafe.0, expenses.0),
31
6
            (lunch.0, expenses.0),
32
6
            (other_lunch.0, expenses.0),
33
6
        ]),
34
6
    };
35
6
    directory.account_paths = directory
36
6
        .accounts
37
6
        .iter()
38
24
        .map(|(id, _)| (*id, Some(directory.account_path(*id))))
39
        .collect();
40
6
    Fixture {
41
6
        cafe: cafe.0,
42
6
        directory,
43
6
    }
44
6
}
45

            
46
#[test]
47
1
fn an_own_id_is_an_id() {
48
1
    let fx = fixture();
49
1
    let key = fx.cafe.to_string();
50
1
    assert_eq!(fx.directory.classify_account(&key), Resolution::Id);
51
1
}
52

            
53
#[test]
54
1
fn a_root_name_is_its_path_and_a_full_path_is_a_path() {
55
1
    let fx = fixture();
56
1
    assert_eq!(fx.directory.classify_account("Expenses"), Resolution::Path);
57
1
    assert_eq!(
58
1
        fx.directory.classify_account("Expenses:Cafe"),
59
1
        Resolution::Path
60
    );
61
1
}
62

            
63
#[test]
64
1
fn a_nested_name_is_bare_and_carries_its_path() {
65
1
    let fx = fixture();
66
1
    assert_eq!(
67
1
        fx.directory.classify_account("Cafe"),
68
1
        Resolution::Bare("Expenses:Cafe".to_string())
69
    );
70
1
}
71

            
72
#[test]
73
1
fn a_shared_name_or_path_is_ambiguous() {
74
1
    let fx = fixture();
75
1
    assert_eq!(
76
1
        fx.directory.classify_account("Lunch"),
77
1
        Resolution::Ambiguous(2)
78
    );
79
1
    assert_eq!(
80
1
        fx.directory.classify_account("Expenses:Lunch"),
81
1
        Resolution::Ambiguous(2)
82
    );
83
1
}
84

            
85
#[test]
86
1
fn a_missing_key_is_unknown() {
87
1
    let fx = fixture();
88
1
    assert_eq!(fx.directory.classify_account("Nope"), Resolution::Unknown);
89
1
    assert_eq!(
90
1
        fx.directory.classify_account(&Uuid::new_v4().to_string()),
91
1
        Resolution::Unknown
92
    );
93
1
    assert_eq!(fx.directory.classify_commodity("EUR"), Resolution::Unknown);
94
1
}
95

            
96
#[test]
97
1
fn commodity_symbols_match_case_insensitively() {
98
1
    let fx = fixture();
99
1
    assert_eq!(fx.directory.classify_commodity("usd"), Resolution::Symbol);
100
1
    assert_eq!(
101
1
        fx.directory.classify_commodity("JPY"),
102
1
        Resolution::Ambiguous(2)
103
    );
104
1
}