1
use chrono::Utc;
2
use finance::{price::Price, split::Split};
3
use num_rational::Rational64;
4
use sqlx::types::Uuid;
5

            
6
use super::raise_transaction;
7
use crate::command::CmdError;
8

            
9
275
fn uuid(n: u128) -> Uuid {
10
275
    Uuid::from_u128(n)
11
275
}
12

            
13
52
fn split(id: u128, commodity: u128, num: i64, denom: i64) -> Split {
14
52
    Split {
15
52
        id: uuid(id),
16
52
        tx_id: uuid(99),
17
52
        account_id: uuid(id + 100),
18
52
        commodity_id: uuid(commodity),
19
52
        reconcile_state: None,
20
52
        reconcile_date: None,
21
52
        value_num: num,
22
52
        value_denom: denom,
23
52
        lot_id: None,
24
52
    }
25
52
}
26

            
27
11
fn price(commodity_split: Option<u128>, currency_split: Option<u128>) -> Price {
28
11
    Price {
29
11
        id: uuid(500),
30
11
        date: Utc::now(),
31
11
        commodity_id: uuid(2),
32
11
        currency_id: uuid(1),
33
11
        commodity_split: commodity_split.map(uuid),
34
11
        currency_split: currency_split.map(uuid),
35
11
        value_num: 5,
36
11
        value_denom: 3,
37
11
    }
38
11
}
39

            
40
14
fn assert_args_error(result: Result<Vec<super::RaisedRow>, CmdError>) {
41
14
    assert!(matches!(result, Err(CmdError::Args(_))), "{result:?}");
42
14
}
43

            
44
#[test]
45
1
fn raises_same_commodity_pair() {
46
1
    let splits = [split(1, 1, -100, 1), split(2, 1, 100, 1)];
47
1
    let rows = raise_transaction(&splits, &[]).unwrap();
48
1
    assert_eq!(rows.len(), 1);
49
1
    assert_eq!(rows[0].from_split, uuid(1));
50
1
    assert_eq!(rows[0].to_split, uuid(2));
51
1
    assert_eq!(rows[0].from_account, uuid(101));
52
1
    assert_eq!(rows[0].to_account, uuid(102));
53
1
    assert_eq!(rows[0].value, Rational64::new(100, 1));
54
1
    assert_eq!(rows[0].to_amount, None);
55
1
}
56

            
57
#[test]
58
1
fn raises_cross_commodity_pair_through_price() {
59
1
    let splits = [split(1, 1, -100, 1), split(2, 2, 60, 1)];
60
1
    let rows = raise_transaction(&splits, &[price(Some(2), Some(1))]).unwrap();
61
1
    assert_eq!(rows.len(), 1);
62
1
    assert_eq!(rows[0].from_commodity, uuid(1));
63
1
    assert_eq!(rows[0].to_commodity, uuid(2));
64
1
    assert_eq!(rows[0].value, Rational64::new(100, 1));
65
1
    assert_eq!(rows[0].to_amount, Some(Rational64::new(60, 1)));
66
1
}
67

            
68
#[test]
69
1
fn raises_mixed_rows_in_input_order() {
70
1
    let splits = [
71
1
        split(3, 1, -5, 1),
72
1
        split(1, 1, -100, 1),
73
1
        split(4, 1, 5, 1),
74
1
        split(2, 2, 60, 1),
75
1
    ];
76
1
    let rows = raise_transaction(&splits, &[price(Some(2), Some(1))]).unwrap();
77
2
    let from: Vec<Uuid> = rows.iter().map(|r| r.from_split).collect();
78
1
    assert_eq!(from, vec![uuid(3), uuid(1)]);
79
1
    assert_eq!(rows[0].to_amount, None);
80
1
    assert_eq!(rows[1].to_amount, Some(Rational64::new(60, 1)));
81
1
}
82

            
83
#[test]
84
1
fn row_order_follows_input_not_ids() {
85
1
    let splits = [
86
1
        split(9, 1, -5, 1),
87
1
        split(8, 1, 5, 1),
88
1
        split(1, 1, -7, 1),
89
1
        split(2, 1, 7, 1),
90
1
    ];
91
1
    let rows = raise_transaction(&splits, &[]).unwrap();
92
2
    let from: Vec<Uuid> = rows.iter().map(|r| r.from_split).collect();
93
1
    assert_eq!(from, vec![uuid(9), uuid(1)]);
94
1
}
95

            
96
#[test]
97
1
fn rejects_three_way_split() {
98
1
    let splits = [split(1, 1, -100, 1), split(2, 1, 60, 1), split(3, 1, 40, 1)];
99
1
    assert_args_error(raise_transaction(&splits, &[]));
100
1
}
101

            
102
#[test]
103
1
fn rejects_equal_magnitude_pairs() {
104
1
    let splits = [
105
1
        split(1, 1, -100, 1),
106
1
        split(2, 1, -100, 1),
107
1
        split(3, 1, 100, 1),
108
1
        split(4, 1, 100, 1),
109
1
    ];
110
1
    assert_args_error(raise_transaction(&splits, &[]));
111
1
}
112

            
113
#[test]
114
1
fn rejects_zero_split() {
115
1
    let splits = [split(1, 1, 0, 1), split(2, 1, 0, 1)];
116
1
    assert_args_error(raise_transaction(&splits, &[]));
117
1
}
118

            
119
#[test]
120
1
fn rejects_invalid_denominator() {
121
1
    let splits = [split(1, 1, -1, 0), split(2, 1, 1, 1)];
122
1
    assert_args_error(raise_transaction(&splits, &[]));
123
1
}
124

            
125
#[test]
126
1
fn rejects_min_numerator() {
127
1
    let splits = [split(1, 1, i64::MIN, 1), split(2, 1, 1, 1)];
128
1
    assert_args_error(raise_transaction(&splits, &[]));
129
1
}
130

            
131
#[test]
132
1
fn rejects_price_with_unknown_split() {
133
1
    let splits = [split(1, 1, -100, 1), split(2, 2, 60, 1)];
134
1
    assert_args_error(raise_transaction(&splits, &[price(Some(2), Some(7))]));
135
1
    assert_args_error(raise_transaction(&splits, &[price(None, Some(1))]));
136
1
}
137

            
138
#[test]
139
1
fn rejects_split_claimed_twice() {
140
1
    let splits = [split(1, 1, -100, 1), split(2, 2, 60, 1), split(3, 2, 60, 1)];
141
1
    let prices = [price(Some(2), Some(1)), price(Some(3), Some(1))];
142
1
    assert_args_error(raise_transaction(&splits, &prices));
143
1
}
144

            
145
#[test]
146
1
fn rejects_wrong_signed_price_legs() {
147
1
    let splits = [split(1, 1, -100, 1), split(2, 2, 60, 1)];
148
1
    assert_args_error(raise_transaction(&splits, &[price(Some(1), Some(2))]));
149
1
}
150

            
151
#[test]
152
1
fn rejects_duplicate_split_ids() {
153
1
    let splits = [split(1, 1, -100, 1), split(1, 1, 100, 1)];
154
1
    assert_args_error(raise_transaction(&splits, &[]));
155
1
}
156

            
157
#[test]
158
1
fn rejects_unpaired_split() {
159
1
    let splits = [split(1, 1, -100, 1), split(2, 2, 100, 1)];
160
1
    assert_args_error(raise_transaction(&splits, &[]));
161
1
}
162

            
163
3
fn assert_refuses_kept_data(edit: impl FnOnce(&mut Split)) {
164
3
    let mut splits = [split(1, 1, -100, 1), split(2, 1, 100, 1)];
165
3
    edit(&mut splits[1]);
166
3
    assert_args_error(raise_transaction(&splits, &[]));
167
3
}
168

            
169
#[test]
170
1
fn refuses_reconciled_split() {
171
1
    assert_refuses_kept_data(|s| s.reconcile_state = Some(true));
172
1
}
173

            
174
#[test]
175
1
fn refuses_split_with_reconcile_date() {
176
1
    assert_refuses_kept_data(|s| s.reconcile_date = Some(Utc::now()));
177
1
}
178

            
179
#[test]
180
1
fn refuses_split_with_lot() {
181
1
    assert_refuses_kept_data(|s| s.lot_id = Some(uuid(7)));
182
1
}
183

            
184
#[test]
185
1
fn carries_explicitly_unreconciled_split() {
186
1
    let mut splits = [split(1, 1, -100, 1), split(2, 1, 100, 1)];
187
1
    splits[0].reconcile_state = Some(false);
188
1
    let rows = raise_transaction(&splits, &[]).unwrap();
189
1
    assert_eq!(rows.len(), 1);
190
1
    assert_eq!(rows[0].from_reconcile, Some(false));
191
1
    assert_eq!(rows[0].to_reconcile, None);
192
1
}
193

            
194
3
fn assert_price_mismatch(edit: impl FnOnce(&mut Price)) {
195
3
    let splits = [split(1, 1, -100, 1), split(2, 2, 60, 1)];
196
3
    let mut stored = price(Some(2), Some(1));
197
3
    edit(&mut stored);
198
3
    let result = raise_transaction(&splits, &[stored]);
199
3
    assert!(
200
3
        matches!(&result, Err(CmdError::Args(m)) if m.contains("exchange rate")),
201
        "{result:?}"
202
    );
203
3
}
204

            
205
#[test]
206
1
fn rejects_price_with_other_rate() {
207
1
    assert_price_mismatch(|p| {
208
1
        p.value_num = 1;
209
1
        p.value_denom = 1;
210
1
    });
211
1
}
212

            
213
#[test]
214
1
fn accepts_unreduced_price_rate_of_the_same_value() -> Result<(), CmdError> {
215
1
    let splits = [split(1, 1, -100, 1), split(2, 2, 60, 1)];
216
1
    let mut stored = price(Some(2), Some(1));
217
1
    (stored.value_num, stored.value_denom) = (10, 6);
218
1
    let rows = raise_transaction(&splits, &[stored])?;
219
1
    assert_eq!(rows.len(), 1);
220
1
    Ok(())
221
1
}
222

            
223
#[test]
224
1
fn rejects_price_with_other_commodity() {
225
1
    assert_price_mismatch(|p| p.commodity_id = uuid(3));
226
1
}
227

            
228
#[test]
229
1
fn rejects_price_with_other_currency() {
230
1
    assert_price_mismatch(|p| p.currency_id = uuid(3));
231
1
}