about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-28 18:18:32 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-28 18:18:32 -0400
commit2cb85a4f5922be57a43470bde828d8f92138de40 (patch)
tree687f5e80d8b0fb46948ed9f3eec22d49eda39658
parentec59c5f5d846b15a81629dc670d2e13a1a15b5f4 (diff)
downloadclinte-2cb85a4f5922be57a43470bde828d8f92138de40.tar.gz
testing create/delete
-rw-r--r--src/ed.rs8
-rw-r--r--src/posts.rs33
2 files changed, 38 insertions, 3 deletions
diff --git a/src/ed.rs b/src/ed.rs
index b87b4a1..468e008 100644
--- a/src/ed.rs
+++ b/src/ed.rs
@@ -1,10 +1,15 @@
-use std::env;
 use std::fs;
+
+#[cfg(not(test))]
+use std::env;
+#[cfg(not(test))]
 use std::process;
 
 use chrono::prelude::*;
 
+#[cfg(not(test))]
 use crate::error;
+
 use crate::user;
 
 fn create_tmp_file() -> Result<String, std::io::Error> {
@@ -16,6 +21,7 @@ fn create_tmp_file() -> Result<String, std::io::Error> {
     }
 }
 
+#[cfg(not(test))]
 pub fn call(body: &str) -> String {
     // If they don't have $EDITOR set, just default to nano
     // instead of assuming vim or emacs.
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();
+    }
 }