Skip to content

Commit

Permalink
Restructered the test for scenarios and implemented the command all.
Browse files Browse the repository at this point in the history
  • Loading branch information
gitkeeper committed Apr 19, 2024
1 parent 0fbbf59 commit c405d02
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 37 deletions.
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub enum Commands {
Active,
/// Adds a new pending task to the task list.
Add {
/// The name of the task.
name: String,
/// The name or multiple names for tasks to add.
names: Vec<String>,
},
/// Shows all tasks.
All,
Expand Down Expand Up @@ -76,7 +76,8 @@ impl Cli {
let args = Cli::parse();

match args.command {
Some(Commands::Add { name }) => add_task(name).await?,
Some(Commands::Add { names }) => add_task(names).await?,
Some(Commands::All) => list_all_tasks().await?,
None => exit_no_subcommand(),
_ => exit_unknown_subcommand(),
}
Expand Down
48 changes: 42 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
use surrealdb::engine::local::Db;
use surrealdb::Surreal;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Task {
number: i64,
name: String,
Expand All @@ -48,14 +48,16 @@ pub struct Task {
/// # Errors
/// The function will return an error if the database connection fails.
///
pub async fn add_task(name: String) -> Result<(), Error> {
pub async fn add_task(names: Vec<String>) -> Result<(), Error> {
let db = Database::initialize().await?;

let number = next_task_number(&db).await?;
let created: Vec<Task> = db.create("tasks").content(Task { number, name }).await?;
let task = created.first().unwrap();
for name in names {
let number = next_task_number(&db).await?;
let created: Vec<Task> = db.create("tasks").content(Task { number, name }).await?;
let task = created.first().unwrap();

println!("Created task {}: {}", task.number, task.name);
println!("Created task {}: {}", task.number, task.name);
}

Ok(())
}
Expand Down Expand Up @@ -84,3 +86,37 @@ async fn next_task_number(db: &Surreal<Db>) -> Result<i64, Error> {
let next_number = tasks.iter().map(|t| t.number).max().unwrap_or(0) + 1;
Ok(next_number)
}

/// Lists all the tasks in the task list.
///
/// The `list_all_tasks` function initializes the SurrealDB database and queries
/// the database for all the tasks. It then prints the task number and name for
/// each task found in the database.
///
/// # Parameters
///
/// There are no parameters for this function.
///
/// # Returns
///
/// The function returns `Ok(())` if the operation is successful.
///
/// # Errors
///
/// The function will return an error if the database query fails.
///
async fn list_all_tasks() -> Result<(), Error> {
let db = Database::initialize().await?;
let mut response = db.query("SELECT * FROM tasks ORDER BY number ASC").await?;
let tasks: Vec<Task> = response.take(0)?;

if tasks.is_empty() {
println!("No tasks found.");
} else {
for task in tasks {
println!("{}: {}", task.number, task.name);
}
}

Ok(())
}
20 changes: 0 additions & 20 deletions tests/commands/add.md

This file was deleted.

16 changes: 16 additions & 0 deletions tests/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! # Misc
//!
//! These are miscellaneous integration tests for the application's command-line
//! interface (CLI).
//!
//! `trycmd::TestCases::new()` creates a new set of test cases. Each `.case()`
//! method appends a new test case. The paths in the arguments represent test
//! files which contain the definition of each test case. The library `trycmd`
//! reads these `.md` files and evaluates the results for their correctness.
//!
#[test]
fn use_case_help() {
trycmd::TestCases::new()
.env("PILUM_MODE", "test")
.case("tests/misc/*.md");
}
2 changes: 1 addition & 1 deletion tests/commands/unknown.md → tests/misc/errors.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Test Command: Unknown
# Use Case: Errors

`pilum unknown` is an unknown command and therefore returns an error with exitcode `2`:

Expand Down
File renamed without changes.
13 changes: 6 additions & 7 deletions tests/cli_tests.rs → tests/scenarios.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
//! # CLI Tests
//! # Scenarios
//!
//! This is a test operation for the application's command-line interface (CLI).
//! These are test scenarios for the application's command-line interface (CLI).
//!
//! `trycmd::TestCases::new()` creates a new set of test cases. Each `.case()`
//! method appends a new test case. The paths in the arguments represent test
//! files which contain the definition of each test case. The library `trycmd`
//! reads these `.md` files and the CLI tests will appraise different sets of
//! commands based on these definitions.
//! reads these `.md` files and evaluates the results for their correctness.
//!
use pilum::database::Database;

#[tokio::test]
async fn commands() {
#[test]
fn scenarios() {
Database::cleanup_test().expect("Testing cleanup failed.");

trycmd::TestCases::new()
.env("PILUM_MODE", "test")
.case("tests/commands/*.md");
.case("tests/scenarios/cake.md");
}
49 changes: 49 additions & 0 deletions tests/scenarios/cake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Use Case: Cake

`pilum all` has no tasks to show initially:

```console
$ pilum all
No tasks found.

```

`pilum add "Buy milk"` adds a new pending task to the task list:

```console
$ pilum add "Buy flour"
Created task 1: Buy flour

```

`pilum add "Buy eggs"` after `pilum add "Buy milk` adds a second pending task to the task list:

```console
$ pilum add "Buy milk"
Created task 2: Buy milk

$ pilum add "Buy eggs"
Created task 3: Buy eggs

```

`pilum add "Buy sugar" "Bake cake"` adds two pending tasks to the task list:

```console
$ pilum add "Buy sugar" "Bake cake"
Created task 4: Buy sugar
Created task 5: Bake cake

```

`pilum all` shows all tasks.

```console
$ pilum all
1: Buy flour
2: Buy milk
3: Buy eggs
4: Buy sugar
5: Bake cake

```

0 comments on commit c405d02

Please sign in to comment.