2020-10-06 15:01:25 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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";
|
|
|
|
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-06 15:01:25 +00:00
|
|
|
assert!(newpageresult.is_ok(), "{:?}", newpage);
|
|
|
|
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-09-30 15:53:45 +00:00
|
|
|
}
|
2020-09-30 16:17:37 +00:00
|
|
|
|
2020-10-06 15:01:25 +00:00
|
|
|
// // TODO: This should be 1, not 0
|
|
|
|
// assert_eq!(newnotes.len(), 0);
|
|
|
|
// // assert_eq!(newnotes[0].notetype, "root");
|
|
|
|
// // assert_eq!(newpage.note_id, newnotes[0].id);
|
|
|
|
//
|
|
|
|
// assert!((newpage.creation_date - now).num_minutes() < 1.0);
|
|
|
|
// assert!((newpage.updated_date - now).num_minutes() < 1.0);
|
|
|
|
// assert!((newpage.lastview_date - now).num_minutes() < 1.0);
|
|
|
|
// assert!(newpage.deleted_date.is_none());
|
|
|
|
// }
|
2020-09-27 12:35:37 +00:00
|
|
|
}
|