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
use db_layer::PersistentItem;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
pub use rusqlite::Error as dbError;
use rusqlite::Result;
use std::{
process,
time::{Instant, SystemTime},
};
pub mod db_layer;
mod initialize_db;
extern crate r2d2;
extern crate r2d2_sqlite;
extern crate rusqlite;
pub struct DbSession {
pub db_name: String,
pub pool: Pool<SqliteConnectionManager>,
}
pub fn init() -> Result<DbSession, rusqlite::Error> {
let manager = SqliteConnectionManager::file("dbs/demo.sqlite3")
.with_init(|c| c.execute_batch("PRAGMA foreign_keys=1;"));
let pool = r2d2::Pool::new(manager).unwrap();
match initialize_db::init(&pool) {
Ok(e) => e,
Err(e) => {
eprintln!("{}", e);
process::exit(1);
}
}
let bar = DbSession {
db_name: "HelloDBName".to_owned(),
pool,
};
Ok(bar)
}
#[test]
fn set_and_get_by_hash_test() -> Result<(), rusqlite::Error> {
let result = init()?;
let bar = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n,
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
};
let hash = &bar.as_nanos().to_string();
let test_item: &PersistentItem = &PersistentItem {
hash: String::from(hash),
key: String::from("testing:test"),
tree_hash: String::from(hash),
parent_hash: String::from(hash),
hash_if_deleted: String::from(hash),
lvl: 456835687,
creator: String::from(hash),
created: 567445672,
importance: 234235675,
content: String::from(hash),
deleted: false,
last_checked: 2141235,
reading_errors: 235235,
extras: String::from(hash),
};
let _ = db_layer::insert(&result.pool, &test_item)?;
let results = db_layer::get_by_hash(&result.pool, &test_item.hash)?;
let result = &results[0];
assert_eq!(String::from(hash), result.hash);
assert_eq!(String::from(hash), result.content);
assert_eq!(2141235, result.last_checked);
assert_eq!(String::from(hash), result.extras);
Ok(())
}
#[test]
fn set_and_get_by_half_key_test() -> Result<(), rusqlite::Error> {
let result = init()?;
let bar = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n,
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
};
let hash = &bar.as_nanos().to_string();
let mut key = String::from(hash);
key.push_str(":test:test:test:test");
let mut search_key = String::from(hash);
search_key.push_str(":test:%");
let test_item: &PersistentItem = &PersistentItem {
hash: String::from(hash),
key: String::from(&key),
tree_hash: String::from(hash),
parent_hash: String::from(hash),
hash_if_deleted: String::from(hash),
lvl: 456835687,
creator: String::from(hash),
created: 567445672,
importance: 234235675,
content: String::from(hash),
deleted: false,
last_checked: 2141235,
reading_errors: 235235,
extras: String::from(hash),
};
db_layer::insert(&result.pool, &test_item)?;
let results = db_layer::get_by_key(&result.pool, &String::from(&search_key))?;
let result = &results[0];
assert_eq!(key, result.key);
assert_eq!(String::from(hash), result.content);
assert_eq!(2141235, result.last_checked);
assert_eq!(String::from(hash), result.extras);
Ok(())
}