notesmachine/server/nm-store/src/lib.rs

108 lines
3.9 KiB
Rust

mod errors;
mod row_structs;
mod store;
mod structs;
pub use crate::errors::NoteStoreError;
pub use crate::store::NoteStore;
#[cfg(test)]
mod tests {
use super::*;
use chrono;
use tokio;
async fn fresh_inmemory_database() -> NoteStore {
let storagepool = NoteStore::new("sqlite://:memory:").await;
assert!(storagepool.is_ok(), "{:?}", storagepool);
let storagepool = storagepool.unwrap();
let reset = storagepool.reset_database().await;
assert!(reset.is_ok(), "{:?}", reset);
storagepool
}
// Request for the page by slug.
// If the page exists, return it. If the page doesn't, return NotFound
#[tokio::test(threaded_scheduler)]
async fn fetching_unfound_page_by_slug_works() {
let storagepool = fresh_inmemory_database().await;
let unfoundpage = storagepool.get_page_by_slug("nonexistent-page").await;
assert!(unfoundpage.is_err());
}
// Request for the page by title. If the page exists, return it.
// If the page doesn't exist, create it then return it anyway.
// There should be at least one note, the root note.
#[tokio::test(threaded_scheduler)]
async fn fetching_unfound_page_by_title_works() {
let title = "Nonexistent Page";
let now = chrono::Utc::now();
let storagepool = fresh_inmemory_database().await;
let newpageresult = storagepool.get_page_by_title(&title).await;
assert!(newpageresult.is_ok(), "{:?}", newpageresult);
let (newpage, newnotes) = newpageresult.unwrap();
assert_eq!(newpage.title, title, "{:?}", newpage.title);
assert_eq!(newpage.slug, "nonexistent-page");
assert_eq!(newnotes.len(), 1);
assert_eq!(newnotes[0].notetype, "root");
assert_eq!(newpage.note_id, newnotes[0].id);
assert!((newpage.creation_date - now).num_minutes() < 1);
assert!((newpage.updated_date - now).num_minutes() < 1);
assert!((newpage.lastview_date - now).num_minutes() < 1);
assert!(newpage.deleted_date.is_none());
}
fn make_new_note(content: &str) -> row_structs::NewNote {
row_structs::NewNoteBuilder::default()
.content(content.to_string())
.build()
.unwrap()
}
#[tokio::test(threaded_scheduler)]
async fn can_nest_notes() {
let title = "Nonexistent Page";
let storagepool = fresh_inmemory_database().await;
let newpageresult = storagepool.get_page_by_title(&title).await;
let (_newpage, newnotes) = newpageresult.unwrap();
let root = &newnotes[0];
let note1 = make_new_note("1");
let note1_uuid = storagepool.insert_nested_note(&note1, &root.uuid, 0).await;
assert!(note1_uuid.is_ok(), "{:?}", note1_uuid);
let note1_uuid = note1_uuid.unwrap();
let note2 = make_new_note("2");
let note2_uuid = storagepool.insert_nested_note(&note2, &root.uuid, 0).await;
assert!(note2_uuid.is_ok(), "{:?}", note2_uuid);
let note2_uuid = note2_uuid.unwrap();
let note3 = make_new_note("3");
let note3_uuid = storagepool.insert_nested_note(&note3, &note1_uuid, 0).await;
assert!(note3_uuid.is_ok(), "{:?}", note3_uuid);
let note3_uuid = note3_uuid.unwrap();
let note4 = make_new_note("4");
let note4_uuid = storagepool.insert_nested_note(&note4, &note2_uuid, 0).await;
assert!(note4_uuid.is_ok(), "{:?}", note4_uuid);
let note4_uuid = note4_uuid.unwrap();
let newpageresult = storagepool.get_page_by_title(&title).await;
let (newpage, newnotes) = newpageresult.unwrap();
assert_eq!(newpage.title, title, "{:?}", newpage.title);
assert_eq!(newpage.slug, "nonexistent-page");
assert_eq!(newnotes.len(), 5);
assert_eq!(newnotes[0].notetype, "root");
assert_eq!(newpage.note_id, newnotes[0].id);
}
}