testing create/delete

This commit is contained in:
Ben Morrison 2020-05-28 18:18:32 -04:00
parent ec59c5f5d8
commit 2cb85a4f59
Signed by untrusted user: gbmor
GPG Key ID: 8F192E4720BB0DAC
2 changed files with 38 additions and 3 deletions

View File

@ -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.

View File

@ -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();
}
}