diff --git a/src/tests/database/access_context.rs b/src/tests/database/access_context.rs index 23207f1..0e893f7 100644 --- a/src/tests/database/access_context.rs +++ b/src/tests/database/access_context.rs @@ -1,348 +1,346 @@ #[cfg(test)] -mod database_tests { - use crate::tests::database::helpers::{ - create_accesses, create_models, create_users, get_reset_database_context, - }; - use crate::{ - database::{access_context::AccessContext, entity_context::EntityContextTrait}, - entities::{access, model, user}, - to_active_models, - }; - use sea_orm::{entity::prelude::*, IntoActiveModel}; +use crate::tests::database::helpers::{ + create_accesses, create_models, create_users, get_reset_database_context, +}; +use crate::{ + database::{access_context::AccessContext, entity_context::EntityContextTrait}, + entities::{access, model, user}, + to_active_models, +}; +use sea_orm::{entity::prelude::*, IntoActiveModel}; + +async fn seed_db() -> (AccessContext, access::Model, user::Model, model::Model) { + let db_context = get_reset_database_context().await; + + let access_context = AccessContext::new(db_context); + + let user = create_users(1)[0].clone(); + let model = create_models(1, user.id)[0].clone(); + let access = create_accesses(1, user.id, model.id)[0].clone(); + + user::Entity::insert(user.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); + model::Entity::insert(model.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); + + (access_context, access, user, model) +} - async fn seed_db() -> (AccessContext, access::Model, user::Model, model::Model) { - let db_context = get_reset_database_context().await; +// Test the functionality of the 'create' function, which creates a access in the database +#[tokio::test] +async fn create_test() { + let (access_context, access, _, _) = seed_db().await; - let access_context = AccessContext::new(db_context); + let created_access = access_context.create(access.clone()).await.unwrap(); - let user = create_users(1)[0].clone(); - let model = create_models(1, user.id)[0].clone(); - let access = create_accesses(1, user.id, model.id)[0].clone(); + let fetched_access = access::Entity::find_by_id(created_access.id) + .one(&access_context.db_context.get_connection()) + .await + .unwrap() + .unwrap(); - user::Entity::insert(user.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); - model::Entity::insert(model.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + // Assert if the fetched access is the same as the created access + assert_eq!(access, created_access); + assert_eq!(fetched_access, created_access); +} - (access_context, access, user, model) - } +#[tokio::test] +async fn create_check_unique_pair_model_id_user_id_test() { + let (access_context, access, _, _) = seed_db().await; - // Test the functionality of the 'create' function, which creates a access in the database - #[tokio::test] - async fn create_test() { - let (access_context, access, _, _) = seed_db().await; + let _created_access_1 = access_context.create(access.clone()).await.unwrap(); + let _created_access_2 = access_context.create(access.clone()).await; - let created_access = access_context.create(access.clone()).await.unwrap(); + assert!(matches!( + _created_access_2.unwrap_err().sql_err(), + Some(SqlErr::UniqueConstraintViolation(_)) + )); +} - let fetched_access = access::Entity::find_by_id(created_access.id) - .one(&access_context.db_context.get_connection()) - .await - .unwrap() - .unwrap(); +#[tokio::test] +async fn create_invalid_role_test() { + let (access_context, mut access, _, _) = seed_db().await; - // Assert if the fetched access is the same as the created access - assert_eq!(access, created_access); - assert_eq!(fetched_access, created_access); - } + access.role = "abc".into(); - #[tokio::test] - async fn create_check_unique_pair_model_id_user_id_test() { - let (access_context, access, _, _) = seed_db().await; + let created_access = access_context.create(access.clone()).await; - let _created_access_1 = access_context.create(access.clone()).await.unwrap(); - let _created_access_2 = access_context.create(access.clone()).await; + assert!(matches!( + created_access.unwrap_err().sql_err(), + Some(SqlErr::ForeignKeyConstraintViolation(_)) + )); +} - assert!(matches!( - _created_access_2.unwrap_err().sql_err(), - Some(SqlErr::UniqueConstraintViolation(_)) - )); - } +#[tokio::test] +async fn create_auto_increment_test() { + let (access_context, _, user, model_1) = seed_db().await; - #[tokio::test] - async fn create_invalid_role_test() { - let (access_context, mut access, _, _) = seed_db().await; + let mut model_2 = create_models(1, user.id)[0].clone(); + model_2.id = model_1.id + 1; + model_2.name = "model_2".to_string(); - access.role = "abc".into(); + model::Entity::insert(model_2.into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - let created_access = access_context.create(access.clone()).await; + let access_1 = access::Model { + id: 0, + role: "Editor".to_string(), + model_id: 1, + user_id: user.id, + }; - assert!(matches!( - created_access.unwrap_err().sql_err(), - Some(SqlErr::ForeignKeyConstraintViolation(_)) - )); - } + let access_2 = access::Model { + id: 0, + role: "Editor".to_string(), + model_id: 2, + user_id: user.id, + }; - #[tokio::test] - async fn create_auto_increment_test() { - let (access_context, _, user, model_1) = seed_db().await; - - let mut model_2 = create_models(1, user.id)[0].clone(); - model_2.id = model_1.id + 1; - model_2.name = "model_2".to_string(); - - model::Entity::insert(model_2.into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); - - let access_1 = access::Model { - id: 0, - role: "Editor".to_string(), - model_id: 1, - user_id: user.id, - }; - - let access_2 = access::Model { - id: 0, - role: "Editor".to_string(), - model_id: 2, - user_id: user.id, - }; - - let created_access1 = access_context.create(access_1.clone()).await.unwrap(); - let created_access2 = access_context.create(access_2.clone()).await.unwrap(); - - let fetched_access1 = access::Entity::find_by_id(created_access1.id) - .one(&access_context.db_context.get_connection()) - .await - .unwrap() - .unwrap(); - - let fetched_access2 = access::Entity::find_by_id(created_access2.id) - .one(&access_context.db_context.get_connection()) - .await - .unwrap() - .unwrap(); - - assert_ne!(fetched_access1.id, fetched_access2.id); - assert_ne!(created_access1.id, created_access2.id); - assert_eq!(created_access1.id, fetched_access1.id); - assert_eq!(created_access2.id, fetched_access2.id); - } + let created_access1 = access_context.create(access_1.clone()).await.unwrap(); + let created_access2 = access_context.create(access_2.clone()).await.unwrap(); + + let fetched_access1 = access::Entity::find_by_id(created_access1.id) + .one(&access_context.db_context.get_connection()) + .await + .unwrap() + .unwrap(); + + let fetched_access2 = access::Entity::find_by_id(created_access2.id) + .one(&access_context.db_context.get_connection()) + .await + .unwrap() + .unwrap(); + + assert_ne!(fetched_access1.id, fetched_access2.id); + assert_ne!(created_access1.id, created_access2.id); + assert_eq!(created_access1.id, fetched_access1.id); + assert_eq!(created_access2.id, fetched_access2.id); +} - #[tokio::test] - async fn get_by_id_test() { - let (access_context, access, _, _) = seed_db().await; +#[tokio::test] +async fn get_by_id_test() { + let (access_context, access, _, _) = seed_db().await; - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - // Fetches the access created using the 'get_by_id' function - let fetched_access = access_context.get_by_id(access.id).await.unwrap().unwrap(); + // Fetches the access created using the 'get_by_id' function + let fetched_access = access_context.get_by_id(access.id).await.unwrap().unwrap(); - // Assert if the fetched access is the same as the created access - assert_eq!(access, fetched_access); - } + // Assert if the fetched access is the same as the created access + assert_eq!(access, fetched_access); +} - #[tokio::test] - async fn get_by_non_existing_id_test() { - let (access_context, _, _, _) = seed_db().await; +#[tokio::test] +async fn get_by_non_existing_id_test() { + let (access_context, _, _, _) = seed_db().await; - let fetched_access = access_context.get_by_id(1).await.unwrap(); + let fetched_access = access_context.get_by_id(1).await.unwrap(); - assert!(fetched_access.is_none()); - } + assert!(fetched_access.is_none()); +} - #[tokio::test] - async fn get_all_test() { - let (access_context, _, user, model) = seed_db().await; +#[tokio::test] +async fn get_all_test() { + let (access_context, _, user, model) = seed_db().await; - // Creates a model of the access which will be created - let new_accesses = create_accesses(1, user.id, model.id); + // Creates a model of the access which will be created + let new_accesses = create_accesses(1, user.id, model.id); - // Creates the access in the database using the 'create' function - access::Entity::insert_many(to_active_models!(new_accesses.clone())) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + // Creates the access in the database using the 'create' function + access::Entity::insert_many(to_active_models!(new_accesses.clone())) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - assert_eq!(access_context.get_all().await.unwrap().len(), 1); + assert_eq!(access_context.get_all().await.unwrap().len(), 1); - let mut sorted: Vec = new_accesses.clone(); - sorted.sort_by_key(|k| k.id); + let mut sorted: Vec = new_accesses.clone(); + sorted.sort_by_key(|k| k.id); - for (i, access) in sorted.into_iter().enumerate() { - assert_eq!(access, new_accesses[i]); - } + for (i, access) in sorted.into_iter().enumerate() { + assert_eq!(access, new_accesses[i]); } +} - #[tokio::test] - async fn get_all_empty_test() { - let (access_context, _, _, _) = seed_db().await; +#[tokio::test] +async fn get_all_empty_test() { + let (access_context, _, _, _) = seed_db().await; - let result = access_context.get_all().await.unwrap(); - let empty_accesses: Vec = vec![]; + let result = access_context.get_all().await.unwrap(); + let empty_accesses: Vec = vec![]; - assert_eq!(empty_accesses, result); - } + assert_eq!(empty_accesses, result); +} - #[tokio::test] - async fn update_test() { - let (access_context, access, _, _) = seed_db().await; +#[tokio::test] +async fn update_test() { + let (access_context, access, _, _) = seed_db().await; - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - let new_access = access::Model { ..access }; + let new_access = access::Model { ..access }; - let updated_access = access_context.update(new_access.clone()).await.unwrap(); + let updated_access = access_context.update(new_access.clone()).await.unwrap(); - let fetched_access = access::Entity::find_by_id(updated_access.id) - .one(&access_context.db_context.get_connection()) - .await - .unwrap() - .unwrap(); + let fetched_access = access::Entity::find_by_id(updated_access.id) + .one(&access_context.db_context.get_connection()) + .await + .unwrap() + .unwrap(); - assert_eq!(new_access, updated_access); - assert_eq!(updated_access, fetched_access); - } + assert_eq!(new_access, updated_access); + assert_eq!(updated_access, fetched_access); +} - #[tokio::test] - async fn update_modifies_role_test() { - let (access_context, access, _, _) = seed_db().await; +#[tokio::test] +async fn update_modifies_role_test() { + let (access_context, access, _, _) = seed_db().await; - let access = access::Model { - role: "Editor".into(), - ..access - }; + let access = access::Model { + role: "Editor".into(), + ..access + }; - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - let new_access = access::Model { - role: "Commenter".into(), - ..access - }; + let new_access = access::Model { + role: "Commenter".into(), + ..access + }; - let updated_access = access_context.update(new_access.clone()).await.unwrap(); + let updated_access = access_context.update(new_access.clone()).await.unwrap(); - assert_ne!(access, updated_access); - assert_ne!(access, new_access); - } + assert_ne!(access, updated_access); + assert_ne!(access, new_access); +} - #[tokio::test] - async fn update_does_not_modify_id_test() { - let (access_context, access, _, _) = seed_db().await; - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); - - let updated_access = access::Model { - id: &access.id + 1, - ..access.clone() - }; - let res = access_context.update(updated_access.clone()).await; - - assert!(matches!(res.unwrap_err(), DbErr::RecordNotUpdated)); - } +#[tokio::test] +async fn update_does_not_modify_id_test() { + let (access_context, access, _, _) = seed_db().await; + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); + + let updated_access = access::Model { + id: &access.id + 1, + ..access.clone() + }; + let res = access_context.update(updated_access.clone()).await; - #[tokio::test] - async fn update_does_not_modify_model_id_test() { - let (access_context, access, _, _) = seed_db().await; + assert!(matches!(res.unwrap_err(), DbErr::RecordNotUpdated)); +} - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); +#[tokio::test] +async fn update_does_not_modify_model_id_test() { + let (access_context, access, _, _) = seed_db().await; - let updated_access = access::Model { - model_id: &access.model_id + 1, - ..access.clone() - }; - let res = access_context.update(updated_access.clone()).await.unwrap(); + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - assert_eq!(access, res); - } + let updated_access = access::Model { + model_id: &access.model_id + 1, + ..access.clone() + }; + let res = access_context.update(updated_access.clone()).await.unwrap(); + + assert_eq!(access, res); +} - #[tokio::test] - async fn update_does_not_modify_user_id_test() { - let (access_context, access, _, _) = seed_db().await; +#[tokio::test] +async fn update_does_not_modify_user_id_test() { + let (access_context, access, _, _) = seed_db().await; - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - let updated_access = access::Model { - user_id: &access.user_id + 1, - ..access.clone() - }; - let res = access_context.update(updated_access.clone()).await.unwrap(); + let updated_access = access::Model { + user_id: &access.user_id + 1, + ..access.clone() + }; + let res = access_context.update(updated_access.clone()).await.unwrap(); - assert_eq!(access, res); - } + assert_eq!(access, res); +} - #[tokio::test] - async fn update_invalid_role_test() { - let (access_context, mut access, _, _) = seed_db().await; +#[tokio::test] +async fn update_invalid_role_test() { + let (access_context, mut access, _, _) = seed_db().await; - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - access.role = "abc".into(); + access.role = "abc".into(); - let updated_access = access_context.update(access.clone()).await; + let updated_access = access_context.update(access.clone()).await; - assert!(matches!( - updated_access.unwrap_err().sql_err(), - Some(SqlErr::ForeignKeyConstraintViolation(_)) - )); - } + assert!(matches!( + updated_access.unwrap_err().sql_err(), + Some(SqlErr::ForeignKeyConstraintViolation(_)) + )); +} - #[tokio::test] - async fn update_non_existing_id_test() { - let (access_context, access, _, _) = seed_db().await; +#[tokio::test] +async fn update_non_existing_id_test() { + let (access_context, access, _, _) = seed_db().await; - let updated_access = access_context.update(access.clone()).await; + let updated_access = access_context.update(access.clone()).await; - assert!(matches!( - updated_access.unwrap_err(), - DbErr::RecordNotUpdated - )); - } + assert!(matches!( + updated_access.unwrap_err(), + DbErr::RecordNotUpdated + )); +} - #[tokio::test] - async fn delete_test() { - let (access_context, access, _, _) = seed_db().await; +#[tokio::test] +async fn delete_test() { + let (access_context, access, _, _) = seed_db().await; - access::Entity::insert(access.clone().into_active_model()) - .exec(&access_context.db_context.get_connection()) - .await - .unwrap(); + access::Entity::insert(access.clone().into_active_model()) + .exec(&access_context.db_context.get_connection()) + .await + .unwrap(); - let deleted_access = access_context.delete(access.id).await.unwrap(); + let deleted_access = access_context.delete(access.id).await.unwrap(); - let all_accesses = access::Entity::find() - .all(&access_context.db_context.get_connection()) - .await - .unwrap(); + let all_accesses = access::Entity::find() + .all(&access_context.db_context.get_connection()) + .await + .unwrap(); - assert_eq!(access, deleted_access); - assert!(all_accesses.is_empty()); - } + assert_eq!(access, deleted_access); + assert!(all_accesses.is_empty()); +} - #[tokio::test] - async fn delete_non_existing_id_test() { - let (access_context, _, _, _) = seed_db().await; +#[tokio::test] +async fn delete_non_existing_id_test() { + let (access_context, _, _, _) = seed_db().await; - let deleted_access = access_context.delete(1).await; + let deleted_access = access_context.delete(1).await; - assert!(matches!( - deleted_access.unwrap_err(), - DbErr::RecordNotFound(_) - )); - } + assert!(matches!( + deleted_access.unwrap_err(), + DbErr::RecordNotFound(_) + )); } diff --git a/src/tests/database/in_use_context.rs b/src/tests/database/in_use_context.rs index e3da4aa..4b00500 100644 --- a/src/tests/database/in_use_context.rs +++ b/src/tests/database/in_use_context.rs @@ -1,276 +1,274 @@ #[cfg(test)] -mod database_tests { - use crate::tests::database::helpers::*; - use crate::{ - database::{ - entity_context::EntityContextTrait, - in_use_context::{DbErr, InUseContext}, - }, - entities::{in_use, model, session, user}, - to_active_models, - }; - use chrono::{Duration, Utc}; - use sea_orm::{entity::prelude::*, IntoActiveModel}; - use std::matches; - use std::ops::Add; - - async fn seed_db() -> ( - InUseContext, - in_use::Model, - session::Model, - model::Model, - user::Model, - ) { - let db_context = get_reset_database_context().await; +use crate::tests::database::helpers::*; +use crate::{ + database::{ + entity_context::EntityContextTrait, + in_use_context::{DbErr, InUseContext}, + }, + entities::{in_use, model, session, user}, + to_active_models, +}; +use chrono::{Duration, Utc}; +use sea_orm::{entity::prelude::*, IntoActiveModel}; +use std::matches; +use std::ops::Add; + +async fn seed_db() -> ( + InUseContext, + in_use::Model, + session::Model, + model::Model, + user::Model, +) { + let db_context = get_reset_database_context().await; + + let in_use_context = InUseContext::new(db_context); + + let user = create_users(1)[0].clone(); + let model = create_models(1, user.id)[0].clone(); + let session = create_sessions(1, user.id)[0].clone(); + let in_use = create_in_uses(1, model.id, session.id)[0].clone(); + + user::Entity::insert(user.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); + model::Entity::insert(model.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); + session::Entity::insert(session.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); + + (in_use_context, in_use, session, model, user) +} + +#[tokio::test] +async fn create_test() { + let (in_use_context, mut in_use, _, _, _) = seed_db().await; + + let inserted_in_use = in_use_context.create(in_use.clone()).await.unwrap(); + + in_use.latest_activity = inserted_in_use.latest_activity; + + let fetched_in_use = in_use::Entity::find_by_id(inserted_in_use.clone().model_id) + .one(&in_use_context.db_context.get_connection()) + .await + .unwrap() + .unwrap(); - let in_use_context = InUseContext::new(db_context); - - let user = create_users(1)[0].clone(); - let model = create_models(1, user.id)[0].clone(); - let session = create_sessions(1, user.id)[0].clone(); - let in_use = create_in_uses(1, model.id, session.id)[0].clone(); + assert_eq!(in_use, inserted_in_use); + assert_eq!(in_use, fetched_in_use); +} + +#[tokio::test] +async fn create_default_latest_activity_test() { + let t_min = Utc::now().timestamp(); + + let (in_use_context, in_use, _, _, _) = seed_db().await; - user::Entity::insert(user.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); - model::Entity::insert(model.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); - session::Entity::insert(session.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); + let inserted_in_use = in_use_context.create(in_use.clone()).await.unwrap(); - (in_use_context, in_use, session, model, user) - } + let fetched_in_use = in_use::Entity::find_by_id(inserted_in_use.model_id) + .one(&in_use_context.db_context.get_connection()) + .await + .unwrap() + .unwrap(); - #[tokio::test] - async fn create_test() { - let (in_use_context, mut in_use, _, _, _) = seed_db().await; + let t_max = Utc::now().timestamp(); - let inserted_in_use = in_use_context.create(in_use.clone()).await.unwrap(); + let t_actual = fetched_in_use.clone().latest_activity.timestamp(); - in_use.latest_activity = inserted_in_use.latest_activity; + assert!(t_min <= t_actual && t_actual <= t_max) +} + +#[tokio::test] +async fn get_by_id_test() { + let (in_use_context, in_use, _, _, _) = seed_db().await; - let fetched_in_use = in_use::Entity::find_by_id(inserted_in_use.clone().model_id) - .one(&in_use_context.db_context.get_connection()) - .await - .unwrap() - .unwrap(); + in_use::Entity::insert(in_use.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - assert_eq!(in_use, inserted_in_use); - assert_eq!(in_use, fetched_in_use); - } + let fetched_in_use = in_use_context + .get_by_id(in_use.model_id) + .await + .unwrap() + .unwrap(); - #[tokio::test] - async fn create_default_latest_activity_test() { - let t_min = Utc::now().timestamp(); + assert_eq!(fetched_in_use, in_use) +} - let (in_use_context, in_use, _, _, _) = seed_db().await; +#[tokio::test] +async fn get_by_non_existing_id_test() { + let (in_use_context, _in_use, _, _, _) = seed_db().await; - let inserted_in_use = in_use_context.create(in_use.clone()).await.unwrap(); + let in_use = in_use_context.get_by_id(1).await; - let fetched_in_use = in_use::Entity::find_by_id(inserted_in_use.model_id) - .one(&in_use_context.db_context.get_connection()) - .await - .unwrap() - .unwrap(); + assert!(in_use.unwrap().is_none()) +} - let t_max = Utc::now().timestamp(); +#[tokio::test] +async fn get_all_test() { + let (in_use_context, _in_use, session, model, _user) = seed_db().await; - let t_actual = fetched_in_use.clone().latest_activity.timestamp(); + let in_uses = create_in_uses(1, model.id, session.id); - assert!(t_min <= t_actual && t_actual <= t_max) - } + in_use::Entity::insert_many(to_active_models!(in_uses.clone())) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - #[tokio::test] - async fn get_by_id_test() { - let (in_use_context, in_use, _, _, _) = seed_db().await; + assert_eq!(in_use_context.get_all().await.unwrap().len(), 1); +} - in_use::Entity::insert(in_use.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); +#[tokio::test] +async fn get_all_empty_test() { + let (in_use_context, _, _, _, _) = seed_db().await; - let fetched_in_use = in_use_context - .get_by_id(in_use.model_id) - .await - .unwrap() - .unwrap(); + let in_uses = in_use_context.get_all().await.unwrap(); - assert_eq!(fetched_in_use, in_use) - } + assert_eq!(0, in_uses.len()) +} - #[tokio::test] - async fn get_by_non_existing_id_test() { - let (in_use_context, _in_use, _, _, _) = seed_db().await; +#[tokio::test] +async fn update_test() { + let (in_use_context, in_use, _, _, _) = seed_db().await; - let in_use = in_use_context.get_by_id(1).await; + in_use::Entity::insert(in_use.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - assert!(in_use.unwrap().is_none()) - } + let new_in_use = in_use::Model { ..in_use }; - #[tokio::test] - async fn get_all_test() { - let (in_use_context, _in_use, session, model, _user) = seed_db().await; + let updated_in_use = in_use_context.update(new_in_use.clone()).await.unwrap(); - let in_uses = create_in_uses(1, model.id, session.id); + let fetched_in_use = in_use::Entity::find_by_id(updated_in_use.model_id) + .one(&in_use_context.db_context.get_connection()) + .await + .unwrap() + .unwrap(); - in_use::Entity::insert_many(to_active_models!(in_uses.clone())) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); + assert_eq!(new_in_use, updated_in_use); + assert_eq!(updated_in_use, fetched_in_use); +} - assert_eq!(in_use_context.get_all().await.unwrap().len(), 1); - } +#[tokio::test] +async fn update_modifies_latest_activity_test() { + let (in_use_context, in_use, _, _, _) = seed_db().await; - #[tokio::test] - async fn get_all_empty_test() { - let (in_use_context, _, _, _, _) = seed_db().await; + in_use::Entity::insert(in_use.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - let in_uses = in_use_context.get_all().await.unwrap(); + let new_in_use = in_use::Model { + latest_activity: in_use.clone().latest_activity.add(Duration::seconds(1)), + ..in_use + }; - assert_eq!(0, in_uses.len()) - } + let updated_in_use = in_use_context.update(new_in_use.clone()).await.unwrap(); - #[tokio::test] - async fn update_test() { - let (in_use_context, in_use, _, _, _) = seed_db().await; + assert_ne!(in_use, updated_in_use); + assert_ne!(in_use, new_in_use); +} - in_use::Entity::insert(in_use.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); +#[tokio::test] +async fn update_modifies_session_id_test() { + let (in_use_context, in_use, _, _, _) = seed_db().await; - let new_in_use = in_use::Model { ..in_use }; + in_use::Entity::insert(in_use.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - let updated_in_use = in_use_context.update(new_in_use.clone()).await.unwrap(); + let mut session2 = create_sessions(1, in_use.session_id)[0].clone(); + session2.id = in_use.session_id + 1; + session2.refresh_token = "new_refresh_token".to_string(); + session2.access_token = "new_access_token".to_string(); - let fetched_in_use = in_use::Entity::find_by_id(updated_in_use.model_id) - .one(&in_use_context.db_context.get_connection()) - .await - .unwrap() - .unwrap(); + session::Entity::insert(session2.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - assert_eq!(new_in_use, updated_in_use); - assert_eq!(updated_in_use, fetched_in_use); - } + let new_in_use = in_use::Model { + session_id: in_use.session_id + 1, + ..in_use + }; - #[tokio::test] - async fn update_modifies_latest_activity_test() { - let (in_use_context, in_use, _, _, _) = seed_db().await; + let updated_in_use = in_use_context.update(new_in_use.clone()).await.unwrap(); - in_use::Entity::insert(in_use.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); + assert_ne!(in_use, updated_in_use); + assert_ne!(in_use, new_in_use); +} - let new_in_use = in_use::Model { - latest_activity: in_use.clone().latest_activity.add(Duration::seconds(1)), - ..in_use - }; +#[tokio::test] +async fn update_does_not_modify_model_id_test() { + let (in_use_context, in_use, _, _, _) = seed_db().await; - let updated_in_use = in_use_context.update(new_in_use.clone()).await.unwrap(); + in_use::Entity::insert(in_use.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - assert_ne!(in_use, updated_in_use); - assert_ne!(in_use, new_in_use); - } + let updated_in_use = in_use::Model { + model_id: in_use.model_id + 1, + ..in_use.clone() + }; - #[tokio::test] - async fn update_modifies_session_id_test() { - let (in_use_context, in_use, _, _, _) = seed_db().await; + let updated_in_use = in_use_context.update(updated_in_use.clone()).await; - in_use::Entity::insert(in_use.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); + assert!(matches!( + updated_in_use.unwrap_err(), + DbErr::RecordNotUpdated + )); +} - let mut session2 = create_sessions(1, in_use.session_id)[0].clone(); - session2.id = in_use.session_id + 1; - session2.refresh_token = "new_refresh_token".to_string(); - session2.access_token = "new_access_token".to_string(); +#[tokio::test] +async fn update_non_existing_id_test() { + let (in_use_context, in_use, _, _, _) = seed_db().await; - session::Entity::insert(session2.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); + let updated_in_use = in_use_context.update(in_use.clone()).await; - let new_in_use = in_use::Model { - session_id: in_use.session_id + 1, - ..in_use - }; + assert!(matches!( + updated_in_use.unwrap_err(), + DbErr::RecordNotUpdated + )); +} - let updated_in_use = in_use_context.update(new_in_use.clone()).await.unwrap(); +#[tokio::test] +async fn delete_test() { + let (in_use_context, in_use, _, _, _) = seed_db().await; - assert_ne!(in_use, updated_in_use); - assert_ne!(in_use, new_in_use); - } + in_use::Entity::insert(in_use.clone().into_active_model()) + .exec(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - #[tokio::test] - async fn update_does_not_modify_model_id_test() { - let (in_use_context, in_use, _, _, _) = seed_db().await; + let deleted_in_use = in_use_context.delete(in_use.model_id).await.unwrap(); - in_use::Entity::insert(in_use.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); + let all_in_uses = in_use::Entity::find() + .all(&in_use_context.db_context.get_connection()) + .await + .unwrap(); - let updated_in_use = in_use::Model { - model_id: in_use.model_id + 1, - ..in_use.clone() - }; + assert_eq!(in_use, deleted_in_use); + assert!(all_in_uses.is_empty()); +} - let updated_in_use = in_use_context.update(updated_in_use.clone()).await; +#[tokio::test] +async fn delete_non_existing_id_test() { + let (in_use_context, _, _, _, _) = seed_db().await; - assert!(matches!( - updated_in_use.unwrap_err(), - DbErr::RecordNotUpdated - )); - } + let deleted_in_use = in_use_context.delete(1).await; - #[tokio::test] - async fn update_non_existing_id_test() { - let (in_use_context, in_use, _, _, _) = seed_db().await; - - let updated_in_use = in_use_context.update(in_use.clone()).await; - - assert!(matches!( - updated_in_use.unwrap_err(), - DbErr::RecordNotUpdated - )); - } - - #[tokio::test] - async fn delete_test() { - let (in_use_context, in_use, _, _, _) = seed_db().await; - - in_use::Entity::insert(in_use.clone().into_active_model()) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); - - let deleted_in_use = in_use_context.delete(in_use.model_id).await.unwrap(); - - let all_in_uses = in_use::Entity::find() - .all(&in_use_context.db_context.get_connection()) - .await - .unwrap(); - - assert_eq!(in_use, deleted_in_use); - assert!(all_in_uses.is_empty()); - } - - #[tokio::test] - async fn delete_non_existing_id_test() { - let (in_use_context, _, _, _, _) = seed_db().await; - - let deleted_in_use = in_use_context.delete(1).await; - - assert!(matches!( - deleted_in_use.unwrap_err(), - DbErr::RecordNotFound(_) - )) - } + assert!(matches!( + deleted_in_use.unwrap_err(), + DbErr::RecordNotFound(_) + )) }