2020-09-29 00:33:43 +00:00
|
|
|
mod errors;
|
2020-11-04 20:54:17 +00:00
|
|
|
mod reference_parser;
|
2020-09-29 00:33:43 +00:00
|
|
|
mod store;
|
2020-10-27 02:49:38 +00:00
|
|
|
mod store_private;
|
2020-09-29 00:33:43 +00:00
|
|
|
mod structs;
|
|
|
|
|
|
|
|
pub use crate::errors::NoteStoreError;
|
2020-09-29 15:08:30 +00:00
|
|
|
pub use crate::store::NoteStore;
|
2020-11-04 20:54:17 +00:00
|
|
|
pub use crate::structs::{Note, NoteKind};
|
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-11-04 20:54:17 +00:00
|
|
|
// 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;
|
2020-11-05 01:53:25 +00:00
|
|
|
let foundkasten = storagepool.get_kasten_by_slug("nonexistent-kasten").await;
|
|
|
|
assert!(foundkasten.is_err());
|
2020-11-04 20:54:17 +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-11-03 02:32:01 +00:00
|
|
|
let newpageresult = storagepool.get_kasten_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-11-05 01:53:25 +00:00
|
|
|
let (newpages, _) = newpageresult.unwrap();
|
2020-11-04 20:54:17 +00:00
|
|
|
|
|
|
|
assert_eq!(newpages.len(), 1);
|
|
|
|
let newpage = newpages.iter().next().unwrap();
|
2020-09-30 14:37:18 +00:00
|
|
|
|
2020-11-03 02:32:01 +00:00
|
|
|
assert_eq!(newpage.content, title, "{:?}", newpage.content);
|
|
|
|
assert_eq!(newpage.id, "nonexistent-page");
|
2020-11-04 20:54:17 +00:00
|
|
|
assert_eq!(newpage.kind, NoteKind::Kasten);
|
2020-10-13 03:43:49 +00:00
|
|
|
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-11-04 20:54:17 +00:00
|
|
|
|
|
|
|
fn make_new_note(content: &str) -> structs::NewNote {
|
|
|
|
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_kasten_by_title(&title).await;
|
|
|
|
|
|
|
|
assert!(newpageresult.is_ok(), "{:?}", newpageresult);
|
2020-11-05 01:53:25 +00:00
|
|
|
let (newpages, _) = newpageresult.unwrap();
|
2020-11-04 20:54:17 +00:00
|
|
|
assert_eq!(newpages.len(), 1);
|
|
|
|
let root = &newpages[0];
|
|
|
|
|
|
|
|
// root <- 1 <- 3
|
|
|
|
// <- 2 <- 4
|
|
|
|
|
|
|
|
let note1 = make_new_note("1");
|
|
|
|
let note1_id = storagepool.add_note(¬e1, &root.id, 0).await;
|
|
|
|
assert!(note1_id.is_ok(), "{:?}", note1_id);
|
|
|
|
let note1_id = note1_id.unwrap();
|
|
|
|
|
|
|
|
let note2 = make_new_note("2");
|
|
|
|
let note2_id = storagepool.add_note(¬e2, &root.id, 0).await;
|
|
|
|
assert!(note2_id.is_ok(), "{:?}", note2_id);
|
|
|
|
let note2_id = note2_id.unwrap();
|
|
|
|
|
|
|
|
let note3 = make_new_note("3");
|
|
|
|
let note3_id = storagepool.add_note(¬e3, ¬e1_id, 0).await;
|
|
|
|
assert!(note3_id.is_ok(), "{:?}", note3_id);
|
|
|
|
let _note3_id = note3_id.unwrap();
|
|
|
|
|
|
|
|
let note4 = make_new_note("4");
|
|
|
|
let note4_id = storagepool.add_note(¬e4, ¬e2_id, 0).await;
|
|
|
|
assert!(note4_id.is_ok(), "{:?}", note4_id);
|
|
|
|
let _note4_id = note4_id.unwrap();
|
|
|
|
|
|
|
|
let newpageresult = storagepool.get_kasten_by_title(&title).await;
|
|
|
|
assert!(newpageresult.is_ok(), "{:?}", newpageresult);
|
2020-11-05 01:53:25 +00:00
|
|
|
let (newpages, _) = newpageresult.unwrap();
|
2020-11-04 20:54:17 +00:00
|
|
|
|
|
|
|
assert_eq!(newpages.len(), 5);
|
|
|
|
let newroot = newpages.iter().next().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(newroot.content, title, "{:?}", newroot.content);
|
|
|
|
assert_eq!(newroot.id, "nonexistent-page");
|
|
|
|
|
|
|
|
assert_eq!(newpages[1].parent_id, Some(newroot.id.clone()));
|
|
|
|
assert_eq!(newpages[2].parent_id, Some(newpages[1].id.clone()));
|
|
|
|
}
|
2020-11-05 01:53:25 +00:00
|
|
|
|
|
|
|
|
2020-09-27 12:35:37 +00:00
|
|
|
}
|