2020-09-29 00:33:43 +00:00
|
|
|
mod errors;
|
2020-10-06 15:01:25 +00:00
|
|
|
mod row_structs;
|
2020-09-29 00:33:43 +00:00
|
|
|
mod store;
|
|
|
|
mod structs;
|
2020-10-14 00:23:24 +00:00
|
|
|
mod reference_parser;
|
2020-09-29 00:33:43 +00:00
|
|
|
|
|
|
|
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::*;
|
2020-10-09 16:57:43 +00:00
|
|
|
use chrono;
|
2020-09-29 15:08:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:01:25 +00:00
|
|
|
// Request for the page by slug.
|
|
|
|
// If the page exists, return it. If the page doesn't, return NotFound
|
|
|
|
|
2020-09-29 15:08:30 +00:00
|
|
|
#[tokio::test(threaded_scheduler)]
|
2020-10-06 15:01:25 +00:00
|
|
|
async fn fetching_unfound_page_by_slug_works() {
|
2020-09-29 15:08:30 +00:00
|
|
|
let storagepool = fresh_inmemory_database().await;
|
2020-10-06 15:01:25 +00:00
|
|
|
let unfoundpage = storagepool.get_page_by_slug("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-10-06 15:01:25 +00:00
|
|
|
// 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.
|
2020-09-29 14:44:43 +00:00
|
|
|
|
2020-09-29 15:08:30 +00:00
|
|
|
#[tokio::test(threaded_scheduler)]
|
2020-10-06 15:01:25 +00:00
|
|
|
async fn fetching_unfound_page_by_title_works() {
|
|
|
|
let title = "Nonexistent Page";
|
2020-10-13 03:43:49 +00:00
|
|
|
let now = chrono::Utc::now();
|
2020-09-29 15:08:30 +00:00
|
|
|
let storagepool = fresh_inmemory_database().await;
|
2020-10-06 15:01:25 +00:00
|
|
|
let newpageresult = storagepool.get_page_by_title(&title).await;
|
2020-09-29 21:27:11 +00:00
|
|
|
|
2020-10-08 19:18:08 +00:00
|
|
|
assert!(newpageresult.is_ok(), "{:?}", newpageresult);
|
2020-10-13 03:43:49 +00:00
|
|
|
let (newpage, newnotes) = newpageresult.unwrap();
|
2020-09-30 14:37:18 +00:00
|
|
|
|
2020-10-06 15:01:25 +00:00
|
|
|
assert_eq!(newpage.title, title, "{:?}", newpage.title);
|
|
|
|
assert_eq!(newpage.slug, "nonexistent-page");
|
2020-10-13 03:43:49 +00:00
|
|
|
|
|
|
|
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());
|
2020-09-30 15:53:45 +00:00
|
|
|
}
|
2020-09-30 16:17:37 +00:00
|
|
|
|
2020-10-13 03:43:49 +00:00
|
|
|
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(¬e1, &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(¬e2, &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(¬e3, ¬e1_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(¬e4, ¬e2_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);
|
|
|
|
}
|
2020-09-27 12:35:37 +00:00
|
|
|
}
|