Skip to content

Commit 392160f

Browse files
committed
test: use test utils to test file_store and sqlite
1 parent 00aca25 commit 392160f

File tree

3 files changed

+284
-0
lines changed

3 files changed

+284
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#![cfg(feature = "rusqlite")]
2+
use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
3+
use bdk_testenv::persist_test_utils::{
4+
persist_anchors, persist_first_seen, persist_indexer_changeset, persist_last_evicted,
5+
persist_last_revealed, persist_last_seen, persist_local_chain_changeset, persist_spk_cache,
6+
persist_txgraph_changeset, persist_txouts, persist_txs,
7+
};
8+
9+
fn tx_graph_changeset_init(
10+
db: &mut rusqlite::Connection,
11+
) -> rusqlite::Result<tx_graph::ChangeSet<ConfirmationBlockTime>> {
12+
let db_tx = db.transaction()?;
13+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
14+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
15+
db_tx.commit()?;
16+
Ok(changeset)
17+
}
18+
19+
fn tx_graph_changeset_persist(
20+
db: &mut rusqlite::Connection,
21+
changeset: &tx_graph::ChangeSet<ConfirmationBlockTime>,
22+
) -> rusqlite::Result<()> {
23+
let db_tx = db.transaction()?;
24+
changeset.persist_to_sqlite(&db_tx)?;
25+
db_tx.commit()
26+
}
27+
28+
fn keychain_txout_changeset_init(
29+
db: &mut rusqlite::Connection,
30+
) -> rusqlite::Result<keychain_txout::ChangeSet> {
31+
let db_tx = db.transaction()?;
32+
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
33+
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
34+
db_tx.commit()?;
35+
Ok(changeset)
36+
}
37+
38+
fn keychain_txout_changeset_persist(
39+
db: &mut rusqlite::Connection,
40+
changeset: &keychain_txout::ChangeSet,
41+
) -> rusqlite::Result<()> {
42+
let db_tx = db.transaction()?;
43+
changeset.persist_to_sqlite(&db_tx)?;
44+
db_tx.commit()
45+
}
46+
47+
#[test]
48+
fn txgraph_is_persisted() {
49+
persist_txgraph_changeset::<rusqlite::Connection, _, _, _>(
50+
"wallet.sqlite",
51+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
52+
|db| Ok(tx_graph_changeset_init(db)?),
53+
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
54+
);
55+
}
56+
57+
#[test]
58+
fn indexer_is_persisted() {
59+
persist_indexer_changeset::<rusqlite::Connection, _, _, _>(
60+
"wallet.sqlite",
61+
|path| Ok(rusqlite::Connection::open(path)?),
62+
|db| Ok(keychain_txout_changeset_init(db)?),
63+
|db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?),
64+
);
65+
}
66+
67+
#[test]
68+
fn local_chain_is_persisted() {
69+
persist_local_chain_changeset::<rusqlite::Connection, _, _, _>(
70+
"wallet.sqlite",
71+
|path| Ok(rusqlite::Connection::open(path)?),
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+
Ok(db_tx.commit()?)
83+
},
84+
);
85+
}
86+
87+
#[test]
88+
fn txouts_are_persisted() {
89+
persist_txouts::<rusqlite::Connection, _, _, _>(
90+
"wallet.sqlite",
91+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
92+
|db| Ok(tx_graph_changeset_init(db)?),
93+
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
94+
);
95+
}
96+
97+
#[test]
98+
fn txs_are_persisted() {
99+
persist_txs::<rusqlite::Connection, _, _, _>(
100+
"wallet.sqlite",
101+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
102+
|db| Ok(tx_graph_changeset_init(db)?),
103+
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
104+
);
105+
}
106+
107+
#[test]
108+
fn anchors_are_persisted() {
109+
persist_anchors::<rusqlite::Connection, _, _, _>(
110+
"wallet.sqlite",
111+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
112+
|db| Ok(tx_graph_changeset_init(db)?),
113+
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
114+
);
115+
}
116+
117+
#[test]
118+
fn last_seen_is_persisted() {
119+
persist_last_seen::<rusqlite::Connection, _, _, _>(
120+
"wallet.sqlite",
121+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
122+
|db| Ok(tx_graph_changeset_init(db)?),
123+
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
124+
);
125+
}
126+
127+
#[test]
128+
fn last_evicted_is_persisted() {
129+
persist_last_evicted::<rusqlite::Connection, _, _, _>(
130+
"wallet.sqlite",
131+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
132+
|db| Ok(tx_graph_changeset_init(db)?),
133+
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
134+
);
135+
}
136+
137+
#[test]
138+
fn first_seen_is_persisted() {
139+
persist_first_seen::<rusqlite::Connection, _, _, _>(
140+
"wallet.sqlite",
141+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
142+
|db| Ok(tx_graph_changeset_init(db)?),
143+
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
144+
);
145+
}
146+
147+
#[test]
148+
fn last_revealed_is_persisted() {
149+
persist_last_revealed::<rusqlite::Connection, _, _, _>(
150+
"wallet.sqlite",
151+
|path| Ok(rusqlite::Connection::open(path)?),
152+
|db| Ok(keychain_txout_changeset_init(db)?),
153+
|db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?),
154+
);
155+
}
156+
157+
#[test]
158+
fn spk_cache_is_persisted() {
159+
persist_spk_cache::<rusqlite::Connection, _, _, _>(
160+
"wallet.sqlite",
161+
|path| Ok(rusqlite::Connection::open(path)?),
162+
|db| Ok(keychain_txout_changeset_init(db)?),
163+
|db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?),
164+
);
165+
}

crates/file_store/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ serde = { version = "1", features = ["derive"] }
2121

2222
[dev-dependencies]
2323
tempfile = "3"
24+
bdk_testenv = {path = "../testenv"}
25+
bdk_chain = { path = "../chain", version = "0.23.1", default-features = false, features = ["serde"]}

crates/file_store/src/store.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ mod test {
295295
const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] =
296296
[98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49];
297297

298+
use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
299+
use bdk_testenv::persist_test_utils::{
300+
persist_anchors, persist_first_seen, persist_indexer_changeset, persist_last_evicted,
301+
persist_last_revealed, persist_last_seen, persist_local_chain_changeset, persist_spk_cache,
302+
persist_txgraph_changeset, persist_txouts, persist_txs,
303+
};
304+
298305
type TestChangeSet = BTreeSet<String>;
299306

300307
/// Check behavior of [`Store::create`] and [`Store::load`].
@@ -599,4 +606,114 @@ mod test {
599606
// current position matches EOF
600607
assert_eq!(current_pointer, expected_pointer);
601608
}
609+
610+
#[test]
611+
fn txgraph_is_persisted() {
612+
persist_txgraph_changeset::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
613+
"wallet.db",
614+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
615+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
616+
|db, changeset| Ok(db.append(changeset)?),
617+
);
618+
}
619+
620+
#[test]
621+
fn indexer_is_persisted() {
622+
persist_indexer_changeset::<Store<keychain_txout::ChangeSet>, _, _, _>(
623+
"wallet.db",
624+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
625+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
626+
|db, changeset| Ok(db.append(changeset)?),
627+
);
628+
}
629+
630+
#[test]
631+
fn local_chain_is_persisted() {
632+
persist_local_chain_changeset::<Store<local_chain::ChangeSet>, _, _, _>(
633+
"wallet.db",
634+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
635+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
636+
|db, changeset| Ok(db.append(changeset)?),
637+
);
638+
}
639+
640+
#[test]
641+
fn txouts_are_persisted() {
642+
persist_txouts::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
643+
"wallet.db",
644+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
645+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
646+
|db, changeset| Ok(db.append(changeset)?),
647+
);
648+
}
649+
650+
#[test]
651+
fn txs_are_persisted() {
652+
persist_txs::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
653+
"wallet.db",
654+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
655+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
656+
|db, changeset| Ok(db.append(changeset)?),
657+
);
658+
}
659+
660+
#[test]
661+
fn anchors_are_persisted() {
662+
persist_anchors::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
663+
"wallet.db",
664+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
665+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
666+
|db, changeset| Ok(db.append(changeset)?),
667+
);
668+
}
669+
670+
#[test]
671+
fn last_seen_is_persisted() {
672+
persist_last_seen::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
673+
"wallet.db",
674+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
675+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
676+
|db, changeset| Ok(db.append(changeset)?),
677+
);
678+
}
679+
680+
#[test]
681+
fn last_evicted_is_persisted() {
682+
persist_last_evicted::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
683+
"wallet.db",
684+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
685+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
686+
|db, changeset| Ok(db.append(changeset)?),
687+
);
688+
}
689+
690+
#[test]
691+
fn first_seen_is_persisted() {
692+
persist_first_seen::<Store<tx_graph::ChangeSet<ConfirmationBlockTime>>, _, _, _>(
693+
"wallet.db",
694+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
695+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
696+
|db, changeset| Ok(db.append(changeset)?),
697+
);
698+
}
699+
700+
#[test]
701+
fn last_revealed_is_persisted() {
702+
persist_last_revealed::<Store<keychain_txout::ChangeSet>, _, _, _>(
703+
"wallet.db",
704+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
705+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
706+
|db, changeset| Ok(db.append(changeset)?),
707+
);
708+
}
709+
710+
#[test]
711+
fn spk_cache_is_persisted() {
712+
persist_spk_cache::<Store<keychain_txout::ChangeSet>, _, _, _>(
713+
"wallet.db",
714+
|path| Ok(Store::create(&TEST_MAGIC_BYTES, path)?),
715+
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
716+
|db, changeset| Ok(db.append(changeset)?),
717+
);
718+
}
602719
}

0 commit comments

Comments
 (0)