-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_from_db.rs
74 lines (59 loc) · 1.97 KB
/
read_from_db.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use ledgerstats::ledger::{self, LedgerError, ParseTxError, Transaction, Transactions};
#[test]
fn read_sample_db() {
const DATABASE: &str = include_str!("../database.txt");
let mut transactions = Transactions::new();
transactions.insert(2, Transaction::new(1, 1, 0));
transactions.insert(3, Transaction::new(1, 2, 0));
transactions.insert(4, Transaction::new(2, 2, 1));
transactions.insert(5, Transaction::new(3, 3, 2));
transactions.insert(6, Transaction::new(3, 4, 3));
assert_eq!(ledger::read_txs_from_db(DATABASE).unwrap(), transactions);
}
#[test]
fn read_db_with_empty_line_in_the_end() {
const DATABASE: &str = "2\n\
1 1 0\n\
1 2 0\n";
let mut transactions = Transactions::new();
transactions.insert(2, Transaction::new(1, 1, 0));
transactions.insert(3, Transaction::new(1, 2, 0));
assert_eq!(ledger::read_txs_from_db(DATABASE).unwrap(), transactions);
}
#[test]
fn read_empty_db() {
assert_eq!(
ledger::read_txs_from_db("").unwrap_err(),
LedgerError::EmptyDatabase
);
}
#[test]
fn read_db_with_broken_transactions_number() {
const DATABASE: &str = "🦀\n\
1 1 0\n\
1 2 0";
assert!(matches!(
ledger::read_txs_from_db(DATABASE).unwrap_err(),
LedgerError::ParseIntError(_)
));
}
#[test]
fn read_db_with_broken_transaction() {
const DATABASE: &str = "2\n\
1 1 0\n\
1 🦀 0";
assert!(matches!(
ledger::read_txs_from_db(DATABASE).unwrap_err(),
LedgerError::ParseTxError(ParseTxError::ParseIntError(_))
));
}
#[test]
fn read_db_with_transactions_number_mismatch() {
const DATABASE: &str = "5\n\
1 1 0\n\
1 2 0";
assert_eq!(
ledger::read_txs_from_db(DATABASE).unwrap_err(),
LedgerError::WrongTxNumberError(5, 2)
);
}