Skip to content

Commit 993dde9

Browse files
committed
test: use test utils to test file_store and sqlite
1 parent fbc7d63 commit 993dde9

File tree

3 files changed

+346
-0
lines changed

3 files changed

+346
-0
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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+
#[test]
10+
fn txgraph_is_persisted() {
11+
persist_txgraph_changeset::<rusqlite::Connection, _, _, _>(
12+
"wallet.sqlite",
13+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
14+
|db| {
15+
let db_tx = db.transaction()?;
16+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
17+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
18+
db_tx.commit()?;
19+
Ok(changeset)
20+
},
21+
|db, changeset| {
22+
let db_tx = db.transaction()?;
23+
changeset.persist_to_sqlite(&db_tx)?;
24+
Ok(db_tx.commit()?)
25+
},
26+
);
27+
}
28+
29+
#[test]
30+
fn indexer_is_persisted() {
31+
persist_indexer_changeset::<rusqlite::Connection, _, _, _>(
32+
"wallet.sqlite",
33+
|path| Ok(rusqlite::Connection::open(path)?),
34+
|db| {
35+
let db_tx = db.transaction()?;
36+
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
37+
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
38+
db_tx.commit()?;
39+
Ok(changeset)
40+
},
41+
|db, changeset| {
42+
let db_tx = db.transaction()?;
43+
changeset.persist_to_sqlite(&db_tx)?;
44+
Ok(db_tx.commit()?)
45+
},
46+
);
47+
}
48+
49+
#[test]
50+
fn local_chain_is_persisted() {
51+
persist_local_chain_changeset::<rusqlite::Connection, _, _, _>(
52+
"wallet.sqlite",
53+
|path| Ok(rusqlite::Connection::open(path)?),
54+
|db| {
55+
let db_tx = db.transaction()?;
56+
local_chain::ChangeSet::init_sqlite_tables(&db_tx)?;
57+
let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?;
58+
db_tx.commit()?;
59+
Ok(changeset)
60+
},
61+
|db, changeset| {
62+
let db_tx = db.transaction()?;
63+
changeset.persist_to_sqlite(&db_tx)?;
64+
Ok(db_tx.commit()?)
65+
},
66+
);
67+
}
68+
69+
#[test]
70+
fn txouts_are_persisted() {
71+
persist_txouts::<rusqlite::Connection, _, _, _>(
72+
"wallet.sqlite",
73+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
74+
|db| {
75+
let db_tx = db.transaction()?;
76+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
77+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
78+
db_tx.commit()?;
79+
Ok(changeset)
80+
},
81+
|db, changeset| {
82+
let db_tx = db.transaction()?;
83+
changeset.persist_to_sqlite(&db_tx)?;
84+
Ok(db_tx.commit()?)
85+
},
86+
);
87+
}
88+
89+
#[test]
90+
fn txs_are_persisted() {
91+
persist_txs::<rusqlite::Connection, _, _, _>(
92+
"wallet.sqlite",
93+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
94+
|db| {
95+
let db_tx = db.transaction()?;
96+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
97+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
98+
db_tx.commit()?;
99+
Ok(changeset)
100+
},
101+
|db, changeset| {
102+
let db_tx = db.transaction()?;
103+
changeset.persist_to_sqlite(&db_tx)?;
104+
Ok(db_tx.commit()?)
105+
},
106+
);
107+
}
108+
109+
#[test]
110+
fn anchors_are_persisted() {
111+
persist_anchors::<rusqlite::Connection, _, _, _>(
112+
"wallet.sqlite",
113+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
114+
|db| {
115+
let db_tx = db.transaction()?;
116+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
117+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
118+
db_tx.commit()?;
119+
Ok(changeset)
120+
},
121+
|db, changeset| {
122+
let db_tx = db.transaction()?;
123+
changeset.persist_to_sqlite(&db_tx)?;
124+
Ok(db_tx.commit()?)
125+
},
126+
);
127+
}
128+
129+
#[test]
130+
fn last_seen_is_persisted() {
131+
persist_last_seen::<rusqlite::Connection, _, _, _>(
132+
"wallet.sqlite",
133+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
134+
|db| {
135+
let db_tx = db.transaction()?;
136+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
137+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
138+
db_tx.commit()?;
139+
Ok(changeset)
140+
},
141+
|db, changeset| {
142+
let db_tx = db.transaction()?;
143+
changeset.persist_to_sqlite(&db_tx)?;
144+
Ok(db_tx.commit()?)
145+
},
146+
);
147+
}
148+
149+
#[test]
150+
fn last_evicted_is_persisted() {
151+
persist_last_evicted::<rusqlite::Connection, _, _, _>(
152+
"wallet.sqlite",
153+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
154+
|db| {
155+
let db_tx = db.transaction()?;
156+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
157+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
158+
db_tx.commit()?;
159+
Ok(changeset)
160+
},
161+
|db, changeset| {
162+
let db_tx = db.transaction()?;
163+
changeset.persist_to_sqlite(&db_tx)?;
164+
Ok(db_tx.commit()?)
165+
},
166+
);
167+
}
168+
169+
#[test]
170+
fn first_seen_is_persisted() {
171+
persist_first_seen::<rusqlite::Connection, _, _, _>(
172+
"wallet.sqlite",
173+
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
174+
|db| {
175+
let db_tx = db.transaction()?;
176+
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
177+
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
178+
db_tx.commit()?;
179+
Ok(changeset)
180+
},
181+
|db, changeset| {
182+
let db_tx = db.transaction()?;
183+
changeset.persist_to_sqlite(&db_tx)?;
184+
Ok(db_tx.commit()?)
185+
},
186+
);
187+
}
188+
189+
#[test]
190+
fn last_revealed_is_persisted() {
191+
persist_last_revealed::<rusqlite::Connection, _, _, _>(
192+
"wallet.sqlite",
193+
|path| Ok(rusqlite::Connection::open(path)?),
194+
|db| {
195+
let db_tx = db.transaction()?;
196+
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
197+
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
198+
db_tx.commit()?;
199+
Ok(changeset)
200+
},
201+
|db, changeset| {
202+
let db_tx = db.transaction()?;
203+
changeset.persist_to_sqlite(&db_tx)?;
204+
Ok(db_tx.commit()?)
205+
},
206+
);
207+
}
208+
209+
#[test]
210+
fn spk_cache_is_persisted() {
211+
persist_spk_cache::<rusqlite::Connection, _, _, _>(
212+
"wallet.sqlite",
213+
|path| Ok(rusqlite::Connection::open(path)?),
214+
|db| {
215+
let db_tx = db.transaction()?;
216+
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
217+
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
218+
db_tx.commit()?;
219+
Ok(changeset)
220+
},
221+
|db, changeset| {
222+
let db_tx = db.transaction()?;
223+
changeset.persist_to_sqlite(&db_tx)?;
224+
Ok(db_tx.commit()?)
225+
},
226+
);
227+
}

crates/file_store/Cargo.toml

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

2121
[dev-dependencies]
2222
tempfile = "3"
23+
bdk_testenv = {path = "../testenv"}
24+
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)