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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::conn::DbSyncLevel;
use crate::db::DbKind;
use crate::db::DbWrite;
use holochain_zome_types::test_utils::fake_cell_id;
use shrinkwraprs::Shrinkwrap;
use std::sync::Arc;
use tempdir::TempDir;
pub fn test_cell_db() -> TestDb {
let cell_id = fake_cell_id(1);
test_db(DbKind::Cell(cell_id))
}
fn test_db(kind: DbKind) -> TestDb {
let tmpdir = TempDir::new("holochain-test-environments").unwrap();
TestDb {
db: DbWrite::new(tmpdir.path(), kind, crate::conn::DbSyncLevel::default())
.expect("Couldn't create test database"),
tmpdir,
}
}
pub fn test_dbs() -> TestDbs {
let tempdir = TempDir::new("holochain-test-environments").unwrap();
TestDbs::new(tempdir)
}
#[derive(Shrinkwrap)]
pub struct TestDb {
#[shrinkwrap(main_field)]
db: DbWrite,
tmpdir: TempDir,
}
impl TestDb {
pub fn db(&self) -> DbWrite {
self.db.clone()
}
pub fn into_tempdir(self) -> TempDir {
self.tmpdir
}
}
pub struct TestDbs {
conductor: DbWrite,
wasm: DbWrite,
p2p_agent_store: DbWrite,
p2p_metrics: DbWrite,
tempdir: TempDir,
}
#[allow(missing_docs)]
impl TestDbs {
pub fn new(tempdir: TempDir) -> Self {
use DbKind::*;
let conductor = DbWrite::new(tempdir.path(), Conductor, DbSyncLevel::default()).unwrap();
let wasm = DbWrite::new(tempdir.path(), Wasm, DbSyncLevel::default()).unwrap();
let space = Arc::new(kitsune_p2p::KitsuneSpace(vec![0; 36]));
let p2p_agent_store = DbWrite::new(
tempdir.path(),
P2pAgentStore(space.clone()),
DbSyncLevel::default(),
)
.unwrap();
let p2p_metrics =
DbWrite::new(tempdir.path(), P2pMetrics(space), DbSyncLevel::default()).unwrap();
Self {
conductor,
wasm,
p2p_agent_store,
p2p_metrics,
tempdir,
}
}
pub fn conductor(&self) -> DbWrite {
self.conductor.clone()
}
pub fn wasm(&self) -> DbWrite {
self.wasm.clone()
}
pub fn p2p_agent_store(&self) -> DbWrite {
self.p2p_agent_store.clone()
}
pub fn p2p_metrics(&self) -> DbWrite {
self.p2p_metrics.clone()
}
pub fn into_tempdir(self) -> TempDir {
self.tempdir
}
}
#[macro_export]
macro_rules! fresh_reader_test {
($env: expr, $f: expr) => {{
let mut conn = $env.conn().unwrap();
$crate::db::ReadManager::with_reader(&mut conn, |r| {
$crate::error::DatabaseResult::Ok($f(r))
})
.unwrap()
}};
}
#[macro_export]
macro_rules! print_stmts_test {
($env: expr, $f: expr) => {{
let mut conn = $env.conn().unwrap();
conn.trace(Some(|s| println!("{}", s)));
let r = $crate::db::ReadManager::with_reader(&mut conn, |r| {
$crate::error::DatabaseResult::Ok($f(r))
})
.unwrap();
conn.trace(None);
r
}};
}