Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
chore: add unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatFrogDev committed Feb 19, 2024
1 parent 7291bbd commit e2acf4f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 32 deletions.
86 changes: 54 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ inquire = "0.6.2"
crossterm = "0.27.0"
rusqlite = { version = "0.30.0", features = ["bundled"] }
termimad = "0.29.1"
tempfile = "3.10.0"
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod note;
mod prompts;
mod return_to_main;
mod utilities;
mod tests;

use crate::{
note::Note,
Expand Down
72 changes: 72 additions & 0 deletions src/tests/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#[cfg(test)]
use crate::{api, note::Note};
use chrono::prelude::Local;
use async_std::path::PathBuf;
use tempfile::tempdir;

#[test]
fn test_db_init() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempdir()?;
let data_directory = PathBuf::from(dir.path());
let db_file = data_directory.join("notes.db");
assert!(api::init_db(&data_directory, &db_file).is_ok());
Ok(())
}

#[test]
fn test_save_note() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempdir()?;
let data_directory = PathBuf::from(dir.path());
let db_file: PathBuf = data_directory.join("notes.db");
api::init_db(&data_directory, &db_file)?;

let note = Note {
id: 0,
name: "Test".to_string(),
content: "Test".to_string(),
created: format!("{}", Local::now().format("%A %e %B, %H:%M")),
};

assert!(api::save_note(&note, &db_file).is_ok());
Ok(())
}

#[test]
fn test_delete_notes() {
let dir = tempdir().unwrap();
let data_directory = PathBuf::from(dir.path());
let db_file = data_directory.join("notes.db");
api::init_db(&data_directory, &db_file).unwrap();

let note = Note {
id: 0,
name: "Test".to_string(),
content: "Test".to_string(),
created: format!("{}", Local::now().format("%A %e %B, %H:%M")),
};

api::save_note(&note, &db_file).unwrap();
let notes = api::get_notes(&db_file).unwrap();
let ids: Vec<usize> = notes.iter().map(|note| note.id).collect();
api::delete_notes(ids, &db_file).unwrap();
assert!(api::get_notes(&db_file).unwrap().is_empty());
}

#[test]
fn test_get_notes() -> Result<(), Box<dyn std::error::Error>> {
let dir = tempdir()?;
let data_directory = PathBuf::from(dir.path());
let db_file = data_directory.join("notes.db");
api::init_db(&data_directory, &db_file)?;

let note = Note {
id: 0,
name: "Test".to_string(),
content: "Test".to_string(),
created: format!("{}", Local::now().format("%A %e %B, %H:%M")),
};

api::save_note(&note, &db_file)?;
assert!(!api::get_notes(&db_file)?.is_empty());
Ok(())
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod api;

0 comments on commit e2acf4f

Please sign in to comment.