1
//! Tests for the logical→physical transaction lowering.
2

            
3
use num_rational::Rational64;
4
use sqlx::types::Uuid;
5

            
6
use crate::command::CmdError;
7

            
8
use super::{
9
    LogicalSplit, PriceRow, SplitField, lower_logical_split, lower_logical_split_located,
10
    lower_logical_transaction,
11
};
12

            
13
5
fn same_currency_ls() -> LogicalSplit {
14
5
    let c = Uuid::new_v4();
15
5
    LogicalSplit {
16
5
        from: Uuid::new_v4(),
17
5
        to: Uuid::new_v4(),
18
5
        from_commodity: c,
19
5
        to_commodity: c,
20
5
        value: Rational64::new(100, 1),
21
5
        to_amount: None,
22
5
        from_reconcile: None,
23
5
        to_reconcile: None,
24
5
    }
25
5
}
26

            
27
10
fn cross_currency_ls(value: Rational64, to_amount: Rational64) -> LogicalSplit {
28
10
    LogicalSplit {
29
10
        from: Uuid::new_v4(),
30
10
        to: Uuid::new_v4(),
31
10
        from_commodity: Uuid::new_v4(),
32
10
        to_commodity: Uuid::new_v4(),
33
10
        value,
34
10
        to_amount: Some(to_amount),
35
10
        from_reconcile: None,
36
10
        to_reconcile: None,
37
10
    }
38
10
}
39

            
40
#[test]
41
1
fn single_currency_produces_no_price() {
42
1
    let ls = same_currency_ls();
43
1
    let (from, to, price) = lower_logical_split(&ls).expect("same-currency lowering");
44
1
    assert!(
45
1
        price.is_none(),
46
        "same-currency must not produce a price row"
47
    );
48
1
    assert_eq!(from.account_id, ls.from);
49
1
    assert_eq!(to.account_id, ls.to);
50
1
    assert_eq!(from.value, -ls.value);
51
1
    assert_eq!(to.value, ls.value);
52
1
}
53

            
54
#[test]
55
1
fn cross_currency_price_num_denom_and_split_linkage() {
56
1
    let value = Rational64::new(100, 1);
57
1
    let to_amount = Rational64::new(15000, 1);
58
1
    let ls = cross_currency_ls(value, to_amount);
59
1
    let (from, to, price) = lower_logical_split(&ls).expect("cross-currency lowering");
60
    let PriceRow {
61
        commodity_id,
62
        currency_id,
63
        commodity_split,
64
        currency_split,
65
        value_num,
66
        value_denom,
67
        ..
68
1
    } = price.expect("cross-currency must produce a price row");
69
    // 100/15000 reduces to 1/150
70
1
    assert_eq!(value_num, 1);
71
1
    assert_eq!(value_denom, 150);
72
1
    assert_eq!(commodity_id, ls.to_commodity);
73
1
    assert_eq!(currency_id, ls.from_commodity);
74
    // to_id is the commodity-split, from_id is the currency-split
75
1
    assert_eq!(commodity_split, to.id);
76
1
    assert_eq!(currency_split, from.id);
77
1
}
78

            
79
#[test]
80
1
fn zero_value_is_error() {
81
1
    let ls = cross_currency_ls(Rational64::new(0, 1), Rational64::new(100, 1));
82
1
    let err = lower_logical_split(&ls).expect_err("zero value must error");
83
1
    assert!(
84
1
        matches!(err, CmdError::Args(ref m) if m.contains("value must be positive")),
85
        "expected positive-value error, got: {err:?}"
86
    );
87
1
}
88

            
89
#[test]
90
1
fn negative_value_is_error() {
91
1
    let ls = cross_currency_ls(Rational64::new(-100, 1), Rational64::new(50, 1));
92
1
    let err = lower_logical_split(&ls).expect_err("negative value must error");
93
1
    assert!(
94
1
        matches!(err, CmdError::Args(ref m) if m.contains("value must be positive")),
95
        "expected positive-value error, got: {err:?}"
96
    );
97
1
}
98

            
99
#[test]
100
1
fn zero_to_amount_is_error() {
101
1
    let ls = cross_currency_ls(Rational64::new(100, 1), Rational64::new(0, 1));
102
1
    let err = lower_logical_split(&ls).expect_err("zero to_amount must error");
103
1
    assert!(
104
1
        matches!(err, CmdError::Args(ref m) if m.contains("to_amount must be positive")),
105
        "expected positive-to_amount error, got: {err:?}"
106
    );
107
1
}
108

            
109
#[test]
110
1
fn negative_to_amount_is_error() {
111
1
    let ls = cross_currency_ls(Rational64::new(100, 1), Rational64::new(-50, 1));
112
1
    let err = lower_logical_split(&ls).expect_err("negative to_amount must error");
113
1
    assert!(
114
1
        matches!(err, CmdError::Args(ref m) if m.contains("to_amount must be positive")),
115
        "expected positive-to_amount error, got: {err:?}"
116
    );
117
1
}
118

            
119
#[test]
120
1
fn same_currency_with_to_amount_is_error() {
121
1
    let mut ls = same_currency_ls();
122
1
    ls.to_amount = Some(Rational64::new(42, 1));
123
1
    let err = lower_logical_split(&ls).expect_err("same-currency to_amount must error");
124
1
    assert!(
125
1
        matches!(err, CmdError::Args(ref m) if m.contains("only valid for cross-currency")),
126
        "expected cross-currency-only error, got: {err:?}"
127
    );
128
1
}
129

            
130
#[test]
131
1
fn missing_to_amount_cross_currency_is_error() {
132
1
    let ls = LogicalSplit {
133
1
        from: Uuid::new_v4(),
134
1
        to: Uuid::new_v4(),
135
1
        from_commodity: Uuid::new_v4(),
136
1
        to_commodity: Uuid::new_v4(),
137
1
        value: Rational64::new(100, 1),
138
1
        to_amount: None,
139
1
        from_reconcile: None,
140
1
        to_reconcile: None,
141
1
    };
142
1
    let err = lower_logical_split(&ls).expect_err("missing to_amount must error");
143
1
    assert!(
144
1
        matches!(err, CmdError::Args(ref m) if m.contains("to_amount")),
145
        "expected to_amount error, got: {err:?}"
146
    );
147
1
}
148

            
149
#[test]
150
1
fn i64_overflow_after_reduction_is_error() {
151
    // 4 * i64::MAX cannot fit i64 after reduction
152
1
    let ls = cross_currency_ls(Rational64::new(i64::MAX, 1), Rational64::new(1, 4));
153
1
    let err = lower_logical_split(&ls).expect_err("overflow must error");
154
1
    assert!(
155
1
        matches!(err, CmdError::Args(ref m) if m.contains("overflow")),
156
        "expected overflow error, got: {err:?}"
157
    );
158
1
}
159

            
160
#[test]
161
1
fn reducible_large_rate_canonicalises() {
162
    // MAX / (MAX/2) = 2/1 after reduction
163
1
    let ls = cross_currency_ls(Rational64::new(i64::MAX, 1), Rational64::new(i64::MAX, 2));
164
1
    let (_, _, price) = lower_logical_split(&ls).expect("reducible large input");
165
1
    let p = price.expect("cross-currency must produce price");
166
1
    assert_eq!(p.value_num, 2);
167
1
    assert_eq!(p.value_denom, 1);
168
1
}
169

            
170
#[test]
171
1
fn lower_logical_transaction_single_currency() {
172
1
    let ls = same_currency_ls();
173
1
    let (splits, prices) = lower_logical_transaction(&[ls]).expect("single-currency multi");
174
1
    assert_eq!(splits.len(), 2);
175
1
    assert!(prices.is_empty());
176
1
}
177

            
178
#[test]
179
1
fn lower_logical_transaction_cross_currency() {
180
1
    let ls = cross_currency_ls(Rational64::new(100, 1), Rational64::new(15000, 1));
181
1
    let (splits, prices) = lower_logical_transaction(&[ls]).expect("cross-currency multi");
182
1
    assert_eq!(splits.len(), 2);
183
1
    assert_eq!(prices.len(), 1);
184
1
}
185

            
186
#[test]
187
1
fn lower_logical_transaction_empty_is_error() {
188
1
    let err = lower_logical_transaction(&[]).expect_err("empty slice must error");
189
1
    assert!(
190
1
        matches!(err, CmdError::Args(ref m) if m.contains("at least one")),
191
        "expected at-least-one error, got: {err:?}"
192
    );
193
1
}
194

            
195
#[test]
196
1
fn located_faults_name_the_rejected_input() {
197
4
    let located = |ls: &LogicalSplit| {
198
4
        lower_logical_split_located(ls)
199
4
            .expect_err("must fail")
200
4
            .field
201
4
    };
202
1
    let mut negative = same_currency_ls();
203
1
    negative.value = Rational64::new(-1, 1);
204
1
    assert_eq!(located(&negative), Some(SplitField::Value));
205

            
206
1
    let mut same_with_amount = same_currency_ls();
207
1
    same_with_amount.to_amount = Some(Rational64::new(1, 1));
208
1
    assert_eq!(located(&same_with_amount), Some(SplitField::ToAmount));
209

            
210
1
    let mut missing = cross_currency_ls(Rational64::new(1, 1), Rational64::new(1, 1));
211
1
    missing.to_amount = None;
212
1
    assert_eq!(located(&missing), Some(SplitField::ToAmount));
213

            
214
1
    let overflow = cross_currency_ls(Rational64::new(i64::MAX, 1), Rational64::new(1, 4));
215
1
    assert_eq!(located(&overflow), None);
216
1
}