From da22de87cf8528b2d0f0a7edf7edb66a7eafc646 Mon Sep 17 00:00:00 2001 From: ToBinio Date: Tue, 28 Apr 2026 10:30:00 +0200 Subject: [PATCH] add delete and check tests --- api/tests/common/client.rs | 43 ++++ api/tests/todos.rs | 419 +------------------------------------ api/tests/todos/add.rs | 401 +++++++++++++++++++++++++++++++++++ api/tests/todos/check.rs | 76 +++++++ api/tests/todos/delete.rs | 36 ++++ api/tests/todos/get.rs | 16 ++ 6 files changed, 577 insertions(+), 414 deletions(-) create mode 100644 api/tests/todos/add.rs create mode 100644 api/tests/todos/check.rs create mode 100644 api/tests/todos/delete.rs create mode 100644 api/tests/todos/get.rs diff --git a/api/tests/common/client.rs b/api/tests/common/client.rs index 9e5893b..14d7cf9 100644 --- a/api/tests/common/client.rs +++ b/api/tests/common/client.rs @@ -1,5 +1,6 @@ use reqwest::{Response, header}; use types::Todo; +use uuid::Uuid; pub struct Client { pub api: reqwest::Client, @@ -36,6 +37,48 @@ impl Client { assert_eq!(response.status().as_str(), "200"); response.json().await.unwrap() } + + pub async fn delete_todo(&self, todo_uuid: Uuid) -> Response { + self.api + .delete(&format!("{}/api/todos/{}", self.base_url, todo_uuid)) + .send() + .await + .unwrap() + } + + pub async fn delete_todo_json(&self, todo_uuid: Uuid) -> Todo { + let response = self.delete_todo(todo_uuid).await; + assert_eq!(response.status().as_str(), "200"); + response.json().await.unwrap() + } + + pub async fn check_todo(&self, todo_uuid: Uuid) -> Response { + self.api + .post(&format!("{}/api/todos/check/{}", self.base_url, todo_uuid)) + .send() + .await + .unwrap() + } + + pub async fn check_todo_json(&self, todo_uuid: Uuid) -> Todo { + let response = self.check_todo(todo_uuid).await; + assert_eq!(response.status().as_str(), "200"); + response.json().await.unwrap() + } + + pub async fn remove_check_todo(&self, todo_uuid: Uuid) -> Response { + self.api + .delete(&format!("{}/api/todos/check/{}", self.base_url, todo_uuid)) + .send() + .await + .unwrap() + } + + pub async fn remove_check_todo_json(&self, todo_uuid: Uuid) -> Todo { + let response = self.remove_check_todo(todo_uuid).await; + assert_eq!(response.status().as_str(), "200"); + response.json().await.unwrap() + } } pub fn get_client() -> Client { diff --git a/api/tests/todos.rs b/api/tests/todos.rs index 9157b10..79d4aec 100644 --- a/api/tests/todos.rs +++ b/api/tests/todos.rs @@ -1,417 +1,8 @@ -use crate::common::db::clear_db; -use common::client::get_client; -use serde_json::json; -use serial_test::serial; -use types::Todo; -use uuid::Uuid; - mod common; -#[tokio::test] -#[serial] -async fn get_empty_todos() { - let client = get_client(); - clear_db().await; - - let response = client.get_todos().await; - assert_eq!(response.status().as_str(), "200"); - let json = response.json::>().await.unwrap(); - - assert_eq!(json.len(), 0); -} - -#[tokio::test] -#[serial] -async fn add_todo() { - let client = get_client(); - clear_db().await; - - let todo_uuid = Uuid::new_v4(); - let todo = Todo { - uuid: todo_uuid, - title: "Test Todo".to_string(), - note: "Test Note".to_string(), - time: None, - tags: vec![], - category: None, - checks: vec![], - }; - - let response = client - .add_todo(json!( - { - "data": { - "uuid": todo_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - } - })) - .await; - - assert_eq!(response.status().as_str(), "200"); - - let json = response.json::().await.unwrap(); - assert_eq!(json, todo); - - let todos = client.get_todos_json().await; - assert_eq!(todos.len(), 1); - assert_eq!(todos.first().unwrap(), &todo); -} - -#[tokio::test] -#[serial] -async fn add_todo_with_labels() { - let client = get_client(); - clear_db().await; - - let todo_uuid = Uuid::new_v4(); - let tag_uuid = Uuid::new_v4(); - let category_uuid = Uuid::new_v4(); - - let todo = Todo { - uuid: todo_uuid, - title: "Test Todo".to_string(), - note: "Test Note".to_string(), - time: None, - tags: vec![tag_uuid], - category: Some(category_uuid), - checks: vec![], - }; - - let json = client - .add_todo_json(json!( - { - "data": { - "uuid": todo_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [tag_uuid], - "category": category_uuid, - "checks": [], - } - } - )) - .await; - - assert_eq!(json, todo); -} - -#[tokio::test] -#[serial] -async fn add_mutliple_todos() { - let client = get_client(); - clear_db().await; - - let todo1_uuid = Uuid::new_v4(); - let todo2_uuid = Uuid::new_v4(); - let todo3_uuid = Uuid::new_v4(); - - client - .add_todo_json(json!( - { - "data": { - "uuid": todo1_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - } - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo2_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - } - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo3_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - } - } - )) - .await; - - let todos = client.get_todos_json().await; - - assert_eq!(todos.len(), 3); - assert_eq!(todos[2].uuid, todo1_uuid); - assert_eq!(todos[1].uuid, todo2_uuid); - assert_eq!(todos[0].uuid, todo3_uuid); -} - -#[tokio::test] -#[serial] -async fn add_mutliple_bottom_todos() { - let client = get_client(); - clear_db().await; - - let todo1_uuid = Uuid::new_v4(); - let todo2_uuid = Uuid::new_v4(); - let todo3_uuid = Uuid::new_v4(); - - client - .add_todo_json(json!( - { - "data": { - "uuid": todo1_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "bottom" - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo2_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "bottom" - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo3_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "bottom" - } - )) - .await; - - let todos = client.get_todos_json().await; - - assert_eq!(todos.len(), 3); - assert_eq!(todos[0].uuid, todo1_uuid); - assert_eq!(todos[1].uuid, todo2_uuid); - assert_eq!(todos[2].uuid, todo3_uuid); -} - -#[tokio::test] -#[serial] -async fn add_mutliple_top_todos() { - let client = get_client(); - clear_db().await; - - let todo1_uuid = Uuid::new_v4(); - let todo2_uuid = Uuid::new_v4(); - let todo3_uuid = Uuid::new_v4(); - - client - .add_todo_json(json!( - { - "data": { - "uuid": todo1_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "top" - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo2_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "top" - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo3_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "top" - } - )) - .await; - - let todos = client.get_todos_json().await; - - assert_eq!(todos.len(), 3); - assert_eq!(todos[2].uuid, todo1_uuid); - assert_eq!(todos[1].uuid, todo2_uuid); - assert_eq!(todos[0].uuid, todo3_uuid); -} - -#[tokio::test] -#[serial] -async fn add_mutliple_mixed_todos() { - let client = get_client(); - clear_db().await; - - let todo1_uuid = Uuid::new_v4(); - let todo2_uuid = Uuid::new_v4(); - let todo3_uuid = Uuid::new_v4(); - - client - .add_todo_json(json!( - { - "data": { - "uuid": todo1_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - } - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo2_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "bottom" - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo3_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "top" - } - )) - .await; - - let todos = client.get_todos_json().await; - - assert_eq!(todos.len(), 3); - assert_eq!(todos[0].uuid, todo3_uuid); - assert_eq!(todos[1].uuid, todo1_uuid); - assert_eq!(todos[2].uuid, todo2_uuid); -} - -#[tokio::test] -#[serial] -async fn add_mutliple_reverencing_todos() { - let client = get_client(); - clear_db().await; - - let todo1_uuid = Uuid::new_v4(); - let todo2_uuid = Uuid::new_v4(); - let todo3_uuid = Uuid::new_v4(); - let todo4_uuid = Uuid::new_v4(); - - client - .add_todo_json(json!( - { - "data": { - "uuid": todo1_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - } - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo2_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - "previousId": todo1_uuid - } - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo3_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "top", - "previousId": todo1_uuid - } - )) - .await; - client - .add_todo_json(json!( - { - "data": { - "uuid": todo4_uuid, - "title": "Test Todo", - "note": "Test Note", - "tags": [], - "checks": [], - }, - "position": "bottom", - "previousId": todo3_uuid - } - )) - .await; - - let todos = client.get_todos_json().await; - - assert_eq!(todos.len(), 4); - assert_eq!(todos[0].uuid, todo2_uuid); - assert_eq!(todos[1].uuid, todo3_uuid); - assert_eq!(todos[2].uuid, todo4_uuid); - assert_eq!(todos[3].uuid, todo1_uuid); +mod todos { + mod add; + mod check; + mod delete; + mod get; } diff --git a/api/tests/todos/add.rs b/api/tests/todos/add.rs new file mode 100644 index 0000000..8658e0a --- /dev/null +++ b/api/tests/todos/add.rs @@ -0,0 +1,401 @@ +use crate::common::{client::get_client, db::clear_db}; +use serde_json::json; +use serial_test::serial; +use types::Todo; +use uuid::Uuid; + +#[tokio::test] +#[serial] +async fn add_todo() { + let client = get_client(); + clear_db().await; + + let todo_uuid = Uuid::new_v4(); + let todo = Todo { + uuid: todo_uuid, + title: "Test Todo".to_string(), + note: "Test Note".to_string(), + time: None, + tags: vec![], + category: None, + checks: vec![], + }; + + let response = client + .add_todo(json!( + { + "data": { + "uuid": todo_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + })) + .await; + + assert_eq!(response.status().as_str(), "200"); + + let json = response.json::().await.unwrap(); + assert_eq!(json, todo); + + let todos = client.get_todos_json().await; + assert_eq!(todos.len(), 1); + assert_eq!(todos.first().unwrap(), &todo); +} + +#[tokio::test] +#[serial] +async fn add_todo_with_labels() { + let client = get_client(); + clear_db().await; + + let todo_uuid = Uuid::new_v4(); + let tag_uuid = Uuid::new_v4(); + let category_uuid = Uuid::new_v4(); + + let todo = Todo { + uuid: todo_uuid, + title: "Test Todo".to_string(), + note: "Test Note".to_string(), + time: None, + tags: vec![tag_uuid], + category: Some(category_uuid), + checks: vec![], + }; + + let json = client + .add_todo_json(json!( + { + "data": { + "uuid": todo_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [tag_uuid], + "category": category_uuid, + "checks": [], + } + } + )) + .await; + + assert_eq!(json, todo); +} + +#[tokio::test] +#[serial] +async fn add_mutliple_todos() { + let client = get_client(); + clear_db().await; + + let todo1_uuid = Uuid::new_v4(); + let todo2_uuid = Uuid::new_v4(); + let todo3_uuid = Uuid::new_v4(); + + client + .add_todo_json(json!( + { + "data": { + "uuid": todo1_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo2_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo3_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + } + )) + .await; + + let todos = client.get_todos_json().await; + + assert_eq!(todos.len(), 3); + assert_eq!(todos[2].uuid, todo1_uuid); + assert_eq!(todos[1].uuid, todo2_uuid); + assert_eq!(todos[0].uuid, todo3_uuid); +} + +#[tokio::test] +#[serial] +async fn add_mutliple_bottom_todos() { + let client = get_client(); + clear_db().await; + + let todo1_uuid = Uuid::new_v4(); + let todo2_uuid = Uuid::new_v4(); + let todo3_uuid = Uuid::new_v4(); + + client + .add_todo_json(json!( + { + "data": { + "uuid": todo1_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "bottom" + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo2_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "bottom" + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo3_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "bottom" + } + )) + .await; + + let todos = client.get_todos_json().await; + + assert_eq!(todos.len(), 3); + assert_eq!(todos[0].uuid, todo1_uuid); + assert_eq!(todos[1].uuid, todo2_uuid); + assert_eq!(todos[2].uuid, todo3_uuid); +} + +#[tokio::test] +#[serial] +async fn add_mutliple_top_todos() { + let client = get_client(); + clear_db().await; + + let todo1_uuid = Uuid::new_v4(); + let todo2_uuid = Uuid::new_v4(); + let todo3_uuid = Uuid::new_v4(); + + client + .add_todo_json(json!( + { + "data": { + "uuid": todo1_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "top" + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo2_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "top" + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo3_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "top" + } + )) + .await; + + let todos = client.get_todos_json().await; + + assert_eq!(todos.len(), 3); + assert_eq!(todos[2].uuid, todo1_uuid); + assert_eq!(todos[1].uuid, todo2_uuid); + assert_eq!(todos[0].uuid, todo3_uuid); +} + +#[tokio::test] +#[serial] +async fn add_mutliple_mixed_todos() { + let client = get_client(); + clear_db().await; + + let todo1_uuid = Uuid::new_v4(); + let todo2_uuid = Uuid::new_v4(); + let todo3_uuid = Uuid::new_v4(); + + client + .add_todo_json(json!( + { + "data": { + "uuid": todo1_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo2_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "bottom" + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo3_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "top" + } + )) + .await; + + let todos = client.get_todos_json().await; + + assert_eq!(todos.len(), 3); + assert_eq!(todos[0].uuid, todo3_uuid); + assert_eq!(todos[1].uuid, todo1_uuid); + assert_eq!(todos[2].uuid, todo2_uuid); +} + +#[tokio::test] +#[serial] +async fn add_mutliple_referencing_todos() { + let client = get_client(); + clear_db().await; + + let todo1_uuid = Uuid::new_v4(); + let todo2_uuid = Uuid::new_v4(); + let todo3_uuid = Uuid::new_v4(); + let todo4_uuid = Uuid::new_v4(); + + client + .add_todo_json(json!( + { + "data": { + "uuid": todo1_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo2_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + "previousId": todo1_uuid + } + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo3_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "top", + "previousId": todo1_uuid + } + )) + .await; + client + .add_todo_json(json!( + { + "data": { + "uuid": todo4_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + }, + "position": "bottom", + "previousId": todo3_uuid + } + )) + .await; + + let todos = client.get_todos_json().await; + + assert_eq!(todos.len(), 4); + assert_eq!(todos[0].uuid, todo2_uuid); + assert_eq!(todos[1].uuid, todo3_uuid); + assert_eq!(todos[2].uuid, todo4_uuid); + assert_eq!(todos[3].uuid, todo1_uuid); +} diff --git a/api/tests/todos/check.rs b/api/tests/todos/check.rs new file mode 100644 index 0000000..4282b8f --- /dev/null +++ b/api/tests/todos/check.rs @@ -0,0 +1,76 @@ +use crate::common::{client::get_client, db::clear_db}; +use serde_json::json; +use serial_test::serial; +use types::Todo; +use uuid::Uuid; + +#[tokio::test] +#[serial] +async fn check_todo() { + let client = get_client(); + clear_db().await; + + let todo_uuid = Uuid::new_v4(); + client + .add_todo_json(json!( + { + "data": { + "uuid": todo_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + })) + .await; + + let response = client.check_todo(todo_uuid).await; + + assert_eq!(response.status().as_str(), "200"); + let todo: Todo = response.json().await.unwrap(); + + assert_eq!(todo.uuid, todo_uuid); + assert_eq!(todo.checks.len(), 1); + + let todos = client.get_todos_json().await; + assert_eq!(todos.len(), 1); + assert_eq!(todos.first().unwrap().uuid, todo_uuid); + assert_eq!(todos.first().unwrap().checks.len(), 1); +} + +#[tokio::test] +#[serial] +async fn remove_check_todo() { + let client = get_client(); + clear_db().await; + + let todo_uuid = Uuid::new_v4(); + client + .add_todo_json(json!( + { + "data": { + "uuid": todo_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + })) + .await; + + let response = client.check_todo(todo_uuid).await; + + assert_eq!(response.status().as_str(), "200"); + let todo: Todo = response.json().await.unwrap(); + + assert_eq!(todo.uuid, todo_uuid); + assert_eq!(todo.checks.len(), 1); + + let response = client.remove_check_todo(todo_uuid).await; + + assert_eq!(response.status().as_str(), "200"); + let todo: Todo = response.json().await.unwrap(); + + assert_eq!(todo.uuid, todo_uuid); + assert_eq!(todo.checks.len(), 0); +} diff --git a/api/tests/todos/delete.rs b/api/tests/todos/delete.rs new file mode 100644 index 0000000..16506d9 --- /dev/null +++ b/api/tests/todos/delete.rs @@ -0,0 +1,36 @@ +use crate::common::{client::get_client, db::clear_db}; +use serde_json::json; +use serial_test::serial; +use types::Todo; +use uuid::Uuid; + +#[tokio::test] +#[serial] +async fn delete_todo() { + let client = get_client(); + clear_db().await; + + let todo_uuid = Uuid::new_v4(); + client + .add_todo_json(json!( + { + "data": { + "uuid": todo_uuid, + "title": "Test Todo", + "note": "Test Note", + "tags": [], + "checks": [], + } + })) + .await; + + let response = client.delete_todo(todo_uuid).await; + + assert_eq!(response.status().as_str(), "200"); + let todo: Todo = response.json().await.unwrap(); + + assert_eq!(todo.uuid, todo_uuid); + + let todos = client.get_todos_json().await; + assert_eq!(todos.len(), 0); +} diff --git a/api/tests/todos/get.rs b/api/tests/todos/get.rs new file mode 100644 index 0000000..fd3c313 --- /dev/null +++ b/api/tests/todos/get.rs @@ -0,0 +1,16 @@ +use crate::common::{client::get_client, db::clear_db}; +use serial_test::serial; +use types::Todo; + +#[tokio::test] +#[serial] +async fn get_empty_todos() { + let client = get_client(); + clear_db().await; + + let response = client.get_todos().await; + assert_eq!(response.status().as_str(), "200"); + let json = response.json::>().await.unwrap(); + + assert_eq!(json.len(), 0); +} -- 2.51.2