diff options
author | Ben Morrison <ben@gbmor.dev> | 2020-05-28 18:18:32 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2020-05-28 18:18:32 -0400 |
commit | 2cb85a4f5922be57a43470bde828d8f92138de40 (patch) | |
tree | 687f5e80d8b0fb46948ed9f3eec22d49eda39658 /src/posts.rs | |
parent | ec59c5f5d846b15a81629dc670d2e13a1a15b5f4 (diff) | |
download | clinte-2cb85a4f5922be57a43470bde828d8f92138de40.tar.gz |
testing create/delete
Diffstat (limited to 'src/posts.rs')
-rw-r--r-- | src/posts.rs | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/posts.rs b/src/posts.rs index c38c84f..4553b3e 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -1,10 +1,12 @@ use std::io; use crate::db; -use crate::ed; use crate::error; use crate::user; +#[cfg(not(test))] +use crate::ed; + // Make sure nobody encodes narsty characters // into a message to negatively affect other // users @@ -22,7 +24,13 @@ pub fn create() -> error::Result<()> { println!(); println!("Title of the new post: "); + #[cfg(test)] + let title = String::from("TEST_TITLE"); + + #[cfg(not(test))] let mut title = String::new(); + + #[cfg(not(test))] io::stdin().read_line(&mut title)?; let title = str_to_utf8(title.trim()); @@ -34,14 +42,20 @@ pub fn create() -> error::Result<()> { println!(); + #[cfg(not(test))] let body_raw = str_to_utf8(&ed::call("")); + + #[cfg(not(test))] let body = if body_raw.len() > 500 { &body_raw[..500] } else { &body_raw }; - let trimmed_body = body.trim(); + #[cfg(test)] + let body = String::from("TEST_BODY"); + + let trimmed_body = body.trim(); let user = &*user::NAME; let mut all = db::Posts::get_all(db::PATH); @@ -216,4 +230,19 @@ mod tests { assert_eq!(post.body, "TEST_BODY"); fs::rename("clinte_bak.json", db::PATH).unwrap(); } + + #[test] + fn test_create_delete() { + fs::copy(db::PATH, "clinte_bak.json").unwrap(); + create().unwrap(); + let all = db::Posts::get_all(db::PATH); + let post = all.get(1); + + assert_eq!(post.title, "TEST_TITLE"); + assert_eq!(post.body, "TEST_BODY"); + + delete_handler(2).unwrap(); + + fs::rename("clinte_bak.json", db::PATH).unwrap(); + } } |