2020-09-29 00:33:43 +00:00
|
|
|
mod errors;
|
|
|
|
mod store;
|
|
|
|
mod structs;
|
|
|
|
|
|
|
|
pub use crate::errors::NoteStoreError;
|
2020-09-29 15:08:30 +00:00
|
|
|
pub use crate::store::NoteStore;
|
2020-09-29 00:33:43 +00:00
|
|
|
|
2020-09-27 12:35:37 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-09-29 15:08:30 +00:00
|
|
|
use super::*;
|
|
|
|
use tokio;
|
2020-09-29 00:33:43 +00:00
|
|
|
|
2020-09-29 15:08:30 +00:00
|
|
|
async fn fresh_inmemory_database() -> NoteStore {
|
|
|
|
let storagepool = NoteStore::new("sqlite://:memory:").await;
|
2020-09-30 15:53:45 +00:00
|
|
|
assert!(storagepool.is_ok(), "{:?}", storagepool);
|
2020-09-29 15:08:30 +00:00
|
|
|
let storagepool = storagepool.unwrap();
|
2020-09-30 15:53:45 +00:00
|
|
|
let reset = storagepool.reset_database().await;
|
2020-09-30 14:37:18 +00:00
|
|
|
assert!(reset.is_ok(), "{:?}", reset);
|
2020-09-29 15:08:30 +00:00
|
|
|
storagepool
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(threaded_scheduler)]
|
2020-09-29 14:44:43 +00:00
|
|
|
async fn fetching_unfound_page_works() {
|
2020-09-29 15:08:30 +00:00
|
|
|
let storagepool = fresh_inmemory_database().await;
|
2020-09-30 14:37:18 +00:00
|
|
|
let unfoundpage = storagepool.fetch_raw_page("nonexistent-page").await;
|
2020-09-29 15:08:30 +00:00
|
|
|
assert!(unfoundpage.is_err());
|
2020-09-27 12:35:37 +00:00
|
|
|
}
|
2020-09-29 14:44:43 +00:00
|
|
|
|
2020-09-29 15:08:30 +00:00
|
|
|
#[tokio::test(threaded_scheduler)]
|
2020-09-29 14:44:43 +00:00
|
|
|
async fn fetching_unfound_note_works() {
|
2020-09-29 15:08:30 +00:00
|
|
|
let storagepool = fresh_inmemory_database().await;
|
2020-09-30 14:37:18 +00:00
|
|
|
let unfoundnote = storagepool.fetch_raw_note("nonexistent-note").await;
|
2020-09-29 15:08:30 +00:00
|
|
|
assert!(unfoundnote.is_err());
|
2020-09-29 14:44:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 15:08:30 +00:00
|
|
|
#[tokio::test(threaded_scheduler)]
|
|
|
|
async fn cloning_storagepool_is_ok() {
|
|
|
|
let storagepool = fresh_inmemory_database().await;
|
|
|
|
let storagepool2 = storagepool.clone();
|
2020-09-30 14:37:18 +00:00
|
|
|
let unfoundnote = storagepool2.fetch_raw_note("nonexistent-note").await;
|
2020-09-29 15:08:30 +00:00
|
|
|
assert!(unfoundnote.is_err());
|
2020-09-30 14:37:18 +00:00
|
|
|
let unfoundnote = storagepool.fetch_raw_note("nonexistent-note").await;
|
2020-09-29 15:08:30 +00:00
|
|
|
assert!(unfoundnote.is_err());
|
|
|
|
}
|
2020-09-29 21:27:11 +00:00
|
|
|
|
2020-09-30 01:24:34 +00:00
|
|
|
#[tokio::test(threaded_scheduler)]
|
|
|
|
async fn can_save_a_note() {
|
2020-09-30 15:53:45 +00:00
|
|
|
let storagepool = fresh_inmemory_database().await;
|
|
|
|
let note_id = storagepool.insert_note("noteid", "notecontent", "note").await;
|
|
|
|
assert!(note_id.is_ok(), "{:?}", note_id);
|
|
|
|
let note_id = note_id.unwrap();
|
|
|
|
assert!(note_id > 0);
|
2020-09-30 01:24:34 +00:00
|
|
|
|
2020-09-30 15:53:45 +00:00
|
|
|
let foundnote = storagepool.fetch_raw_note("noteid").await;
|
|
|
|
assert!(foundnote.is_ok(), "{:?}", foundnote);
|
|
|
|
let foundnote = foundnote.unwrap();
|
|
|
|
assert_eq!(foundnote.content, "notecontent");
|
|
|
|
assert_eq!(foundnote.notetype, "note");
|
|
|
|
}
|
2020-09-30 14:37:18 +00:00
|
|
|
|
|
|
|
#[tokio::test(threaded_scheduler)]
|
|
|
|
async fn can_save_a_page() {
|
2020-09-30 15:53:45 +00:00
|
|
|
let storagepool = fresh_inmemory_database().await;
|
|
|
|
let page_id = storagepool.insert_page("pageid", "Test page").await;
|
|
|
|
assert!(page_id.is_ok(), "{:?}", page_id);
|
2020-09-30 14:37:18 +00:00
|
|
|
|
2020-09-30 15:53:45 +00:00
|
|
|
let page = storagepool.fetch_raw_page("pageid").await;
|
|
|
|
assert!(page.is_ok(), "{:?}", page);
|
|
|
|
let page = page.unwrap();
|
|
|
|
assert_eq!(page.title, "Test page");
|
|
|
|
assert!(page.note_id > 0);
|
|
|
|
}
|
2020-09-30 16:17:37 +00:00
|
|
|
|
|
|
|
#[tokio::test(threaded_scheduler)]
|
|
|
|
async fn reports_note_update_failure() {
|
|
|
|
let storagepool = fresh_inmemory_database().await;
|
|
|
|
let note_id = storagepool.insert_note("noteid", "notecontent", "note").await;
|
|
|
|
assert!(note_id.is_ok(), "{:?}", note_id);
|
|
|
|
|
|
|
|
let update = storagepool.update_raw_note("badnote", "Bad Note Content").await;
|
|
|
|
assert!(update.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test(threaded_scheduler)]
|
|
|
|
async fn can_update_a_note() {
|
|
|
|
let storagepool = fresh_inmemory_database().await;
|
|
|
|
let note_id = storagepool.insert_note("noteid", "notecontent", "note").await;
|
|
|
|
assert!(note_id.is_ok(), "{:?}", note_id);
|
|
|
|
|
|
|
|
let update = storagepool.update_raw_note("noteid", "Good Note Content").await;
|
|
|
|
assert!(update.is_ok(), "{:?}", update);
|
|
|
|
|
|
|
|
let note = storagepool.fetch_raw_note("noteid").await;
|
|
|
|
assert!(note.is_ok(), "{:?}", note);
|
|
|
|
let note = note.unwrap();
|
|
|
|
assert_eq!(note.content, "Good Note Content");
|
|
|
|
}
|
2020-09-27 12:35:37 +00:00
|
|
|
}
|