|
1 | 1 | #![cfg(feature = "rusqlite")] |
2 | 2 | use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime}; |
3 | 3 | use bdk_testenv::persist_test_utils::{ |
4 | | - persist_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset, |
| 4 | + assert_persist_changesets, keychain_txout_changesets, local_chain_changesets, |
| 5 | + tx_graph_changesets, |
5 | 6 | }; |
6 | 7 |
|
7 | | -fn tx_graph_changeset_init( |
8 | | - db: &mut rusqlite::Connection, |
9 | | -) -> rusqlite::Result<tx_graph::ChangeSet<ConfirmationBlockTime>> { |
10 | | - let db_tx = db.transaction()?; |
11 | | - tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?; |
12 | | - let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?; |
13 | | - db_tx.commit()?; |
14 | | - Ok(changeset) |
15 | | -} |
16 | | - |
17 | | -fn tx_graph_changeset_persist( |
18 | | - db: &mut rusqlite::Connection, |
19 | | - changeset: &tx_graph::ChangeSet<ConfirmationBlockTime>, |
20 | | -) -> rusqlite::Result<()> { |
21 | | - let db_tx = db.transaction()?; |
22 | | - changeset.persist_to_sqlite(&db_tx)?; |
23 | | - db_tx.commit() |
24 | | -} |
25 | | - |
26 | | -fn keychain_txout_changeset_init( |
27 | | - db: &mut rusqlite::Connection, |
28 | | -) -> rusqlite::Result<keychain_txout::ChangeSet> { |
29 | | - let db_tx = db.transaction()?; |
30 | | - keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?; |
31 | | - let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?; |
32 | | - db_tx.commit()?; |
33 | | - Ok(changeset) |
34 | | -} |
35 | | - |
36 | | -fn keychain_txout_changeset_persist( |
37 | | - db: &mut rusqlite::Connection, |
38 | | - changeset: &keychain_txout::ChangeSet, |
39 | | -) -> rusqlite::Result<()> { |
40 | | - let db_tx = db.transaction()?; |
41 | | - changeset.persist_to_sqlite(&db_tx)?; |
42 | | - db_tx.commit() |
43 | | -} |
44 | | - |
45 | | -fn local_chain_changeset_init( |
46 | | - db: &mut rusqlite::Connection, |
47 | | -) -> rusqlite::Result<local_chain::ChangeSet> { |
48 | | - let db_tx = db.transaction()?; |
49 | | - local_chain::ChangeSet::init_sqlite_tables(&db_tx)?; |
50 | | - let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?; |
51 | | - db_tx.commit()?; |
52 | | - Ok(changeset) |
53 | | -} |
54 | | - |
55 | | -fn local_chain_changeset_persist( |
56 | | - db: &mut rusqlite::Connection, |
57 | | - changeset: &local_chain::ChangeSet, |
58 | | -) -> rusqlite::Result<()> { |
59 | | - let db_tx = db.transaction()?; |
60 | | - changeset.persist_to_sqlite(&db_tx)?; |
61 | | - db_tx.commit() |
62 | | -} |
63 | | - |
64 | 8 | #[test] |
65 | 9 | fn txgraph_is_persisted() -> anyhow::Result<()> { |
66 | 10 | let temp_dir = tempfile::tempdir().unwrap(); |
67 | | - Ok(persist_txgraph_changeset::<rusqlite::Connection, _, _, _>( |
| 11 | + let changesets = tx_graph_changesets(); |
| 12 | + Ok(assert_persist_changesets( |
68 | 13 | || { |
69 | 14 | Ok(rusqlite::Connection::open( |
70 | 15 | temp_dir.path().join("wallet.sqlite"), |
71 | 16 | )?) |
72 | 17 | }, |
73 | | - |db| Ok(tx_graph_changeset_init(db)?), |
74 | | - |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 18 | + |db| { |
| 19 | + let db_tx = db.transaction()?; |
| 20 | + tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?; |
| 21 | + let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?; |
| 22 | + db_tx.commit()?; |
| 23 | + Ok(changeset) |
| 24 | + }, |
| 25 | + |db, changeset| { |
| 26 | + let db_tx = db.transaction()?; |
| 27 | + changeset.persist_to_sqlite(&db_tx)?; |
| 28 | + db_tx.commit()?; |
| 29 | + Ok(()) |
| 30 | + }, |
| 31 | + &changesets, |
75 | 32 | )?) |
76 | 33 | } |
77 | 34 |
|
78 | 35 | #[test] |
79 | 36 | fn indexer_is_persisted() -> anyhow::Result<()> { |
80 | 37 | let temp_dir = tempfile::tempdir().unwrap(); |
81 | | - Ok(persist_indexer_changeset::<rusqlite::Connection, _, _, _>( |
| 38 | + let changesets = keychain_txout_changesets(); |
| 39 | + Ok(assert_persist_changesets( |
82 | 40 | || { |
83 | 41 | Ok(rusqlite::Connection::open( |
84 | 42 | temp_dir.path().join("wallet.sqlite"), |
85 | 43 | )?) |
86 | 44 | }, |
87 | | - |db| Ok(keychain_txout_changeset_init(db)?), |
88 | | - |db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?), |
| 45 | + |db| { |
| 46 | + let db_tx = db.transaction()?; |
| 47 | + keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?; |
| 48 | + let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?; |
| 49 | + db_tx.commit()?; |
| 50 | + Ok(changeset) |
| 51 | + }, |
| 52 | + |db, changeset| { |
| 53 | + let db_tx = db.transaction()?; |
| 54 | + changeset.persist_to_sqlite(&db_tx)?; |
| 55 | + db_tx.commit()?; |
| 56 | + Ok(()) |
| 57 | + }, |
| 58 | + &changesets, |
89 | 59 | )?) |
90 | 60 | } |
91 | 61 |
|
92 | 62 | #[test] |
93 | 63 | fn local_chain_is_persisted() -> anyhow::Result<()> { |
94 | 64 | let temp_dir = tempfile::tempdir().unwrap(); |
95 | | - Ok(persist_local_chain_changeset::< |
96 | | - rusqlite::Connection, |
97 | | - _, |
98 | | - _, |
99 | | - _, |
100 | | - >( |
| 65 | + let changesets = local_chain_changesets(); |
| 66 | + Ok(assert_persist_changesets( |
101 | 67 | || { |
102 | 68 | Ok(rusqlite::Connection::open( |
103 | 69 | temp_dir.path().join("wallet.sqlite"), |
104 | 70 | )?) |
105 | 71 | }, |
106 | | - |db| Ok(local_chain_changeset_init(db)?), |
107 | | - |db, changeset| Ok(local_chain_changeset_persist(db, changeset)?), |
| 72 | + |db| { |
| 73 | + let db_tx = db.transaction()?; |
| 74 | + local_chain::ChangeSet::init_sqlite_tables(&db_tx)?; |
| 75 | + let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?; |
| 76 | + db_tx.commit()?; |
| 77 | + Ok(changeset) |
| 78 | + }, |
| 79 | + |db, changeset| { |
| 80 | + let db_tx = db.transaction()?; |
| 81 | + changeset.persist_to_sqlite(&db_tx)?; |
| 82 | + db_tx.commit()?; |
| 83 | + Ok(()) |
| 84 | + }, |
| 85 | + &changesets, |
108 | 86 | )?) |
109 | 87 | } |
0 commit comments