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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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,
    };
    // return bar;
    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("tree-hash-test"),
        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_tree_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("tree-hash-test"),
        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_tree_hash(&result.pool, &test_item.tree_hash)?;
    let result = &results[0];

    assert_eq!(String::from("tree-hash-test"), result.tree_hash);

    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("tree-hash-test"),
        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(())
}

#[test]
fn set_and_get_highest_by_tree_hash() -> 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("tree-hash-test"),
        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_highest_by_tree_hash(&result.pool, &test_item.tree_hash)?;
    let result = &results[0];

    assert_eq!(String::from("tree-hash-test"), result.tree_hash);

    Ok(())
}