1
use std::collections::HashMap;
2

            
3
use finance::tag::Tag;
4
use server::command::FinanceEntity;
5
use sqlx::types::Uuid;
6

            
7
use super::commodity_labels;
8

            
9
6
fn tags(pairs: &[(&str, &str)]) -> HashMap<String, FinanceEntity> {
10
6
    pairs
11
6
        .iter()
12
8
        .map(|(name, value)| {
13
8
            let tag = Tag {
14
8
                id: Uuid::new_v4(),
15
8
                tag_name: (*name).to_owned(),
16
8
                tag_value: (*value).to_owned(),
17
8
                description: None,
18
8
            };
19
8
            ((*name).to_owned(), FinanceEntity::Tag(tag))
20
8
        })
21
        .collect()
22
6
}
23

            
24
#[test]
25
2
fn both_tags_present_are_used_verbatim() {
26
2
    let labels = commodity_labels(
27
2
        Uuid::new_v4(),
28
2
        &tags(&[("symbol", "USD"), ("name", "Dollar")]),
29
2
    );
30
2
    assert_eq!(labels, ("USD".to_owned(), "Dollar".to_owned()));
31
2
}
32

            
33
#[test]
34
2
fn a_missing_symbol_falls_back_to_the_name() {
35
2
    let labels = commodity_labels(Uuid::new_v4(), &tags(&[("name", "Dollar")]));
36
2
    assert_eq!(labels, ("Dollar".to_owned(), "Dollar".to_owned()));
37
2
}
38

            
39
#[test]
40
2
fn a_missing_name_falls_back_to_the_symbol() {
41
2
    let labels = commodity_labels(Uuid::new_v4(), &tags(&[("symbol", "USD")]));
42
2
    assert_eq!(labels, ("USD".to_owned(), "USD".to_owned()));
43
2
}
44

            
45
#[test]
46
2
fn no_tags_fall_back_to_the_id() {
47
2
    let id = Uuid::new_v4();
48
2
    let labels = commodity_labels(id, &HashMap::new());
49
2
    assert_eq!(labels, (id.to_string(), id.to_string()));
50
2
}