From e4856f6ba83af8fc61c1eda1a2842ff353cd5b9d Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 28 May 2020 03:31:43 -0400 Subject: more testing --- src/db.rs | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) (limited to 'src') 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(); } } -- cgit 1.4.1-2-gfad0 akspecs/ranger/tree/.github/workflows?id=65721319e06789763798d0db1a7c24a08fee0bcd'>workflows/doctest.yml
blob: f12cb9264d001284e8c434e4ed8a6abea523b781 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
name: Python doctest and pytest

on:
  push:
    paths:
      - '.github/workflows/doctest.yml'
      - '*.py'

jobs:
  test_py:
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [2.7, 3.5, 3.6]
    steps:
    - uses: actions/checkout@v1
      with:
        fetch-depth: 1
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: doctest
      run: |
        make test_doctest test_other