Add new task endpoint

This commit is contained in:
2022-01-13 21:47:19 +01:00
parent b359b5123e
commit 46b29c28e4
7 changed files with 109 additions and 31 deletions

View File

@@ -1,27 +1,53 @@
pub mod test_helper;
use contextswitch_api::taskwarrior;
use contextswitch_api::{taskwarrior, TaskDefinition};
use contextswitch_types::Task;
#[tokio::test]
async fn list_tasks() {
let address = test_helper::spawn_app();
let task_data_path = test_helper::setup_tasks();
let client = reqwest::Client::new();
taskwarrior::add(vec!["test1", "contextswitch:'{\"test\": 1}'"]).unwrap();
let task_id =
taskwarrior::add(vec!["test", "list_tasks", "contextswitch:'{\"test\": 1}'"]).unwrap();
let response: reqwest::Response = client
.get(&format!("{}/tasks", &address))
let tasks: Vec<Task> = reqwest::Client::new()
.get(&format!("{}/tasks?filter={}", &address, task_id))
.send()
.await
.expect("Failed to execute request.");
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
test_helper::clear_tasks(task_data_path);
let text_body = response.text_with_charset("utf-8").await.unwrap();
let tasks: Vec<Task> = serde_json::from_str(&text_body).unwrap();
assert_eq!(tasks.len(), 1);
assert_eq!(tasks[0].description, "test1");
assert_eq!(tasks[0].description, "test list_tasks");
let cs_metadata = tasks[0].contextswitch.as_ref().unwrap();
assert_eq!(cs_metadata.test, 1);
}
#[tokio::test]
async fn add_task() {
let address = test_helper::spawn_app();
println!("add_task address: {}", address);
let response: serde_json::Value = reqwest::Client::new()
.post(&format!("{}/tasks", &address))
.json(&TaskDefinition {
definition: "test add_task contextswitch:{\"test\":1}".to_string(),
})
.send()
.await
.expect("Failed to execute request")
.json()
.await
.expect("Cannot parse JSON result");
let new_task_id = response["id"].as_u64().unwrap();
let tasks = taskwarrior::export(vec![&new_task_id.to_string()]).unwrap();
assert_eq!(tasks.len(), 1);
assert_eq!(tasks[0].id, new_task_id);
assert_eq!(tasks[0].description, "test add_task");
assert_eq!(
tasks[0].contextswitch.as_ref().unwrap(),
&"{\"test\":1}".to_string()
);
}