aboutsummaryrefslogtreecommitdiff
path: root/src/db/test.rs
blob: 31fea1f02b783f7fa4f5256a3fd86a2e6cce1ab3 (plain) (blame)
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
use crate::*;

use crate::lmdb_adapter::LmdbDb;
use crate::sled_adapter::SledDb;
use crate::sqlite_adapter::SqliteDb;

fn test_suite(db: Db) {
	let tree = db.open_tree("tree").unwrap();

	let ka: &[u8] = &b"test"[..];
	let kb: &[u8] = &b"zwello"[..];
	let kint: &[u8] = &b"tz"[..];
	let va: &[u8] = &b"plop"[..];
	let vb: &[u8] = &b"plip"[..];
	let vc: &[u8] = &b"plup"[..];

	tree.insert(ka, va).unwrap();
	assert_eq!(tree.get(ka).unwrap().unwrap(), va);

	let res = db.transaction::<_, (), _>(|mut tx| {
		assert_eq!(tx.get(&tree, ka).unwrap().unwrap(), va);

		tx.insert(&tree, ka, vb).unwrap();

		assert_eq!(tx.get(&tree, ka).unwrap().unwrap(), vb);

		tx.commit(12)
	});
	assert!(matches!(res, Ok(12)));
	assert_eq!(tree.get(ka).unwrap().unwrap(), vb);

	let res = db.transaction::<(), _, _>(|mut tx| {
		assert_eq!(tx.get(&tree, ka).unwrap().unwrap(), vb);

		tx.insert(&tree, ka, vc).unwrap();

		assert_eq!(tx.get(&tree, ka).unwrap().unwrap(), vc);

		tx.abort(42)
	});
	assert!(matches!(res, Err(TxError::Abort(42))));
	assert_eq!(tree.get(ka).unwrap().unwrap(), vb);

	let mut iter = tree.iter().unwrap();
	let next = iter.next().unwrap().unwrap();
	assert_eq!((next.0.as_ref(), next.1.as_ref()), (ka, vb));
	assert!(iter.next().is_none());
	drop(iter);

	tree.insert(kb, vc).unwrap();
	assert_eq!(tree.get(kb).unwrap().unwrap(), vc);

	let mut iter = tree.iter().unwrap();
	let next = iter.next().unwrap().unwrap();
	assert_eq!((next.0.as_ref(), next.1.as_ref()), (ka, vb));
	let next = iter.next().unwrap().unwrap();
	assert_eq!((next.0.as_ref(), next.1.as_ref()), (kb, vc));
	assert!(iter.next().is_none());
	drop(iter);

	let mut iter = tree.range(kint..).unwrap();
	let next = iter.next().unwrap().unwrap();
	assert_eq!((next.0.as_ref(), next.1.as_ref()), (kb, vc));
	assert!(iter.next().is_none());
	drop(iter);

	let mut iter = tree.range_rev(..kint).unwrap();
	let next = iter.next().unwrap().unwrap();
	assert_eq!((next.0.as_ref(), next.1.as_ref()), (ka, vb));
	assert!(iter.next().is_none());
	drop(iter);

	let mut iter = tree.iter_rev().unwrap();
	let next = iter.next().unwrap().unwrap();
	assert_eq!((next.0.as_ref(), next.1.as_ref()), (kb, vc));
	let next = iter.next().unwrap().unwrap();
	assert_eq!((next.0.as_ref(), next.1.as_ref()), (ka, vb));
	assert!(iter.next().is_none());
	drop(iter);
}

#[test]
fn test_lmdb_db() {
	let path = mktemp::Temp::new_dir().unwrap();
	let db = heed::EnvOpenOptions::new()
		.max_dbs(100)
		.open(&path)
		.unwrap();
	let db = LmdbDb::init(db);
	test_suite(db);
	drop(path);
}

#[test]
fn test_sled_db() {
	let path = mktemp::Temp::new_dir().unwrap();
	let db = SledDb::init(sled::open(path.to_path_buf()).unwrap());
	test_suite(db);
	drop(path);
}

#[test]
fn test_sqlite_db() {
	let db = SqliteDb::init(rusqlite::Connection::open_in_memory().unwrap());
	test_suite(db);
}