From 66cc09a32339ec376ac0e4141de58b94470203c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Thu, 4 Apr 2024 06:23:15 +0300 Subject: [PATCH] test: :white_check_mark: follow_already, unfollow_non, ban_already, ban_nonban, delete_non, create_already, search non feat: :sparkles: database status in alive json --- README.md | 1 + src/db/db_operations.rs | 5 +- src/db/db_utils.rs | 3 + src/routing.rs | 9 +- src/tests/db_tests.rs | 180 +++++++++++++++++++++++++++++++++------- 5 files changed, 164 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index ee8bbfc..a840052 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ +[![Rust](https://github.com/Acapair/acapair_follow_ban_api/actions/workflows/rust.yml/badge.svg)](https://github.com/Acapair/acapair_follow_ban_api/actions/workflows/rust.yml) # Acapair Follow Ban API diff --git a/src/db/db_operations.rs b/src/db/db_operations.rs index ea4c5bc..ccfe7d6 100644 --- a/src/db/db_operations.rs +++ b/src/db/db_operations.rs @@ -9,7 +9,10 @@ pub async fn connect() -> Option> { } pub async fn create(username: &String, db: &Surreal) -> Option { - create_channel(username, db).await.pop().unwrap() + match create_channel(username, db).await.pop() { + Some(channel) => channel, + None => None, + } } pub async fn search(username: &String, db: &Surreal) -> Option { diff --git a/src/db/db_utils.rs b/src/db/db_utils.rs index c39fcd6..ce7fe73 100644 --- a/src/db/db_utils.rs +++ b/src/db/db_utils.rs @@ -78,6 +78,9 @@ pub async fn create_channel(username: &String, db: &Surreal) -> Vec { + db.query("DEFINE INDEX usernameINDEX ON TABLE channel COLUMNS username UNIQUE") + .await + .unwrap(); let created: Vec> = db .create("channel") .content(Channel { diff --git a/src/routing.rs b/src/routing.rs index 5fdd45d..0a24bf1 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -1,7 +1,7 @@ use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router}; use tower_http::cors::CorsLayer; -use crate::AppState; +use crate::{db::db_operations, AppState}; pub async fn routing(State(state): State) -> Router { Router::new() @@ -11,8 +11,13 @@ pub async fn routing(State(state): State) -> Router { } async fn alive() -> impl IntoResponse { + let ping = match db_operations::connect().await { + Some(_) => "Alive", + None => "Dead", + }; let alive_json = serde_json::json!({ - "status":"Alive", + "server_status":"Alive", + "database_status":ping, }); println!("{}", alive_json); (StatusCode::OK, Json(alive_json)) diff --git a/src/tests/db_tests.rs b/src/tests/db_tests.rs index 6817220..99b09d4 100644 --- a/src/tests/db_tests.rs +++ b/src/tests/db_tests.rs @@ -31,8 +31,9 @@ async fn test_create() { let name = &"Ahmet".to_string(); let created = create(name, &connection).await; - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(created.is_some(), true); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -43,8 +44,9 @@ async fn test_search() { let created = create(name, &connection).await; let searched = search(name, &connection).await; - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(created, searched); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -55,8 +57,10 @@ async fn test_delete() { let created = create(name, &connection).await; let deleted = delete(name, &connection).await; - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(created, deleted); + assert_eq!(search(name, &connection).await.is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -69,9 +73,10 @@ async fn test_change_username() { .await .unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(created.id, changed.clone().id); assert_eq!(changed.username, "Kaan"); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -88,7 +93,6 @@ async fn test_follow() { .unwrap(); let mut followed = search(name_followed, &connection).await.unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!( followed.follower_list.pop().unwrap(), follower.id.unwrap().id @@ -97,6 +101,8 @@ async fn test_follow() { follower.followed_list.pop().unwrap(), followed.id.unwrap().id ); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -127,9 +133,10 @@ async fn test_unfollow() { .unwrap(); followed = search(name_followed, &connection).await.unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(followed.follower_list.pop().is_none(), true); assert_eq!(follower.followed_list.pop().is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -144,9 +151,10 @@ async fn test_ban() { let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); let mut judge = search(name_judge, &connection).await.unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -167,9 +175,10 @@ async fn test_unban() { victim = unban(name_victim, name_judge, &connection).await.unwrap(); judge = search(name_judge, &connection).await.unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(victim.banned_from_list.pop().is_none(), true); assert_eq!(judge.banned_list.pop().is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -198,9 +207,10 @@ async fn test_delete_follower() { follower = delete(name_follower, &connection).await.unwrap(); followed = search(name_followed, &connection).await.unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(followed.follower_list.pop().is_none(), true); assert_eq!(follower.followed_list.pop().is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -229,9 +239,10 @@ async fn test_delete_followed() { followed = delete(name_followed, &connection).await.unwrap(); follower = search(name_follower, &connection).await.unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(followed.follower_list.pop().is_none(), true); assert_eq!(follower.followed_list.pop().is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -243,26 +254,19 @@ async fn test_delete_victim() { let _victim = create(name_victim, &connection).await.unwrap(); let _judge = create(name_judge, &connection).await.unwrap(); - let mut victim = ban(name_victim, name_judge, &connection) - .await - .unwrap(); + let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); let mut judge = search(name_judge, &connection).await.unwrap(); - assert_eq!( - judge.banned_list.pop().unwrap(), - victim.id.unwrap().id - ); - assert_eq!( - victim.banned_from_list.pop().unwrap(), - judge.id.unwrap().id - ); + assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); + assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); victim = delete(name_victim, &connection).await.unwrap(); judge = search(name_judge, &connection).await.unwrap(); - let _cleaning = connection.query("DELETE channel;").await; assert_eq!(judge.banned_list.pop().is_none(), true); assert_eq!(victim.banned_from_list.pop().is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; } #[test] @@ -274,24 +278,138 @@ async fn test_delete_judge() { let _victim = create(name_victim, &connection).await.unwrap(); let _judge = create(name_judge, &connection).await.unwrap(); - let mut victim = ban(name_victim, name_judge, &connection) + let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); + let mut judge = search(name_judge, &connection).await.unwrap(); + + assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); + assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); + + judge = delete(name_judge, &connection).await.unwrap(); + victim = search(name_victim, &connection).await.unwrap(); + + assert_eq!(judge.banned_list.pop().is_none(), true); + assert_eq!(victim.banned_from_list.pop().is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_follow_already_follower() { + let connection = create_connection_for_tests("test_follow_already_follower").await; + let name_follower = &"Ahmet".to_string(); + let name_followed = &"Kaan".to_string(); + + let _follower = create(name_follower, &connection).await.unwrap(); + let _followed = create(name_followed, &connection).await.unwrap(); + + let mut follower = follow(name_follower, name_followed, &connection) .await .unwrap(); + let mut followed = search(name_followed, &connection).await.unwrap(); + + assert_eq!( + followed.follower_list.pop().unwrap(), + follower.id.unwrap().id + ); + assert_eq!( + follower.followed_list.pop().unwrap(), + followed.id.unwrap().id + ); + assert_eq!( + follow(name_follower, name_followed, &connection) + .await + .is_none(), + true + ); + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_unfollow_already_nonfollower() { + let connection = create_connection_for_tests("test_unfollow_already_nonfollower").await; + let name_follower = &"Ahmet".to_string(); + let name_followed = &"Kaan".to_string(); + + let _follower = create(name_follower, &connection).await.unwrap(); + let _followed = create(name_followed, &connection).await.unwrap(); + + assert_eq!( + unfollow(name_follower, name_followed, &connection) + .await + .is_none(), + true + ); + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_ban_already_banned() { + let connection = create_connection_for_tests("test_ban_already_banned").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); + + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); + + let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); let mut judge = search(name_judge, &connection).await.unwrap(); + assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); + assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); assert_eq!( - judge.banned_list.pop().unwrap(), - victim.id.unwrap().id + ban(name_victim, name_judge, &connection).await.is_none(), + true ); + + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_unban_already_nonbanned() { + let connection = create_connection_for_tests("test_unban_already_nonbanned").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); + + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); + assert_eq!( - victim.banned_from_list.pop().unwrap(), - judge.id.unwrap().id + unban(name_victim, name_judge, &connection).await.is_none(), + true ); - judge = delete(name_judge, &connection).await.unwrap(); - victim = search(name_victim, &connection).await.unwrap(); + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_delete_noncreated() { + let connection = create_connection_for_tests("test_delete_noncreated").await; + let name = &"Ahmet".to_string(); + + assert_eq!(delete(name, &connection).await.is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_create_already_created() { + let connection = create_connection_for_tests("test_create_already_created").await; + + let name = &"Ahmet".to_string(); + let created = create(name, &connection).await; + + assert_eq!(created.is_some(), true); + assert_eq!(create(name, &connection).await.is_none(), true); + + let _cleaning = connection.query("DELETE channel;").await; +} + +#[test] +async fn test_search_noncreated() { + let connection = create_connection_for_tests("test_search_noncreated").await; + let name = &"Ahmet".to_string(); + + assert_eq!(search(name, &connection).await.is_none(), true); let _cleaning = connection.query("DELETE channel;").await; - assert_eq!(judge.banned_list.pop().is_none(), true); - assert_eq!(victim.banned_from_list.pop().is_none(), true); }