summary refs log tree commit diff stats
path: root/src/db.rs
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-28 03:31:43 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-28 03:31:43 -0400
commite4856f6ba83af8fc61c1eda1a2842ff353cd5b9d (patch)
tree4a1df9091030496560ac429ff11451d16472e9d2 /src/db.rs
parentbb327d381e5626d96942a805e6a5f4d4a5a771d5 (diff)
downloadclinte-e4856f6ba83af8fc61c1eda1a2842ff353cd5b9d.tar.gz
more testing
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs52
1 files changed, 41 insertions, 11 deletions
diff --git a/src/db.rs b/src/db.rs
index 90ff548..02cd776 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -13,7 +13,7 @@ pub const PATH: &str = "clinte.json";
 #[cfg(not(test))]
 pub const PATH: &str = "/usr/local/clinte/clinte.json";
 
-#[derive(Debug, Deserialize, Serialize)]
+#[derive(Clone, Debug, Deserialize, Serialize)]
 pub struct Post {
     pub title: String,
     pub author: String,
@@ -62,8 +62,8 @@ impl Posts {
         self.posts[n] = post;
     }
 
-    pub fn get(&self, n: usize) -> &Post {
-        &self.posts[n]
+    pub fn get(&self, n: usize) -> Post {
+        self.posts[n].clone()
     }
 
     pub fn append(&mut self, post: Post) {
@@ -95,21 +95,51 @@ impl Posts {
 #[cfg(test)]
 mod tests {
     use super::*;
+    use crate::user;
 
     #[test]
-    fn test_init() {
-        let mut conn = Conn::init(PATH);
-        conn.conn.try_lock().unwrap();
-    }
-
-    #[test]
-    fn retrieve_posts_and_examine() {
-        let all = Posts::get_all(PATH);
+    fn retrieve_posts_and_crud() {
+        let mut all = Posts::get_all(PATH);
         assert_eq!(all.posts.len(), 1);
 
         let post = all.get(0);
         assert_eq!(post.title, "Welcome to CLI NoTEs!");
         assert_eq!(post.author, "clinte!");
         assert_eq!(post.body, "Welcome to clinte! For usage, run 'clinte -h'");
+
+        let user = &*user::NAME;
+
+        all.append(Post {
+            author: user.into(),
+            title: String::from("TITLE_HERE"),
+            body: String::from("BODY_HERE"),
+        });
+
+        all.write();
+        let mut all = Posts::get_all(PATH);
+
+        let post = all.get(1);
+        assert_eq!(post.title, "TITLE_HERE");
+        assert_eq!(post.author, *user);
+        assert_eq!(post.body, "BODY_HERE");
+
+        let post = Post {
+            author: user.into(),
+            title: "TITLE_GOES_HERE".into(),
+            body: "BODY_GOES_HERE".into(),
+        };
+
+        all.replace(1, post);
+
+        all.write();
+        let mut all = Posts::get_all(PATH);
+
+        let post = all.get(1);
+        assert_eq!(post.title, "TITLE_GOES_HERE");
+        assert_eq!(post.author, *user);
+        assert_eq!(post.body, "BODY_GOES_HERE");
+
+        all.delete(1);
+        all.write();
     }
 }