From 3cb26aed72d41f3d328ad53ea5afadd97ecaa774 Mon Sep 17 00:00:00 2001 From: Mads Risager Date: Tue, 28 Nov 2023 13:32:55 +0100 Subject: [PATCH] Fix tests --- src/database/model_context.rs | 1 + src/tests/database/access_context.rs | 4 ++++ src/tests/database/in_use_context.rs | 21 +++------------------ src/tests/database/model_context.rs | 6 ++++-- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/database/model_context.rs b/src/database/model_context.rs index 931e7c9..7f4a0cd 100644 --- a/src/database/model_context.rs +++ b/src/database/model_context.rs @@ -18,6 +18,7 @@ impl ModelContext { ModelContext { db_context } } } + #[async_trait] impl EntityContextTrait for ModelContext { /// Used for creating a model::Model entity diff --git a/src/tests/database/access_context.rs b/src/tests/database/access_context.rs index 0fda812..959dacd 100644 --- a/src/tests/database/access_context.rs +++ b/src/tests/database/access_context.rs @@ -28,6 +28,7 @@ async fn seed_db() -> (AccessContext, access::Model, user::Model, model::Model) (access_context, access, user, model) } + // Test the functionality of the 'create' function, which creates a access in the database #[tokio::test] async fn create_test() { @@ -79,6 +80,7 @@ async fn create_auto_increment_test() { let mut model_2 = create_models(1, user.id)[0].clone(); model_2.id = model_1.id + 1; + model_2.name = "model_2".into(); model::Entity::insert(model_2.into_active_model()) .exec(&access_context.db_context.get_connection()) @@ -242,6 +244,7 @@ async fn update_does_not_modify_id_test() { assert!(matches!(res.unwrap_err(), DbErr::RecordNotUpdated)); } + #[tokio::test] async fn update_does_not_modify_model_id_test() { let (access_context, access, _, _) = seed_db().await; @@ -259,6 +262,7 @@ async fn update_does_not_modify_model_id_test() { assert_eq!(access, res); } + #[tokio::test] async fn update_does_not_modify_user_id_test() { let (access_context, access, _, _) = seed_db().await; diff --git a/src/tests/database/in_use_context.rs b/src/tests/database/in_use_context.rs index a62f6b5..ca3e204 100644 --- a/src/tests/database/in_use_context.rs +++ b/src/tests/database/in_use_context.rs @@ -112,31 +112,16 @@ async fn get_by_non_existing_id_test() { #[tokio::test] async fn get_all_test() { - let (in_use_context, _in_use, session, model, user) = seed_db().await; + let (in_use_context, _in_use, session, model, _user) = seed_db().await; - let mut models = create_models(2, user.id); - models[0].id = 3; - - let in_uses = create_in_uses(3, model.id, session.id); - - model::Entity::insert_many(to_active_models!(models.clone())) - .exec(&in_use_context.db_context.get_connection()) - .await - .unwrap(); + let in_uses = create_in_uses(1, model.id, session.id); in_use::Entity::insert_many(to_active_models!(in_uses.clone())) .exec(&in_use_context.db_context.get_connection()) .await .unwrap(); - assert_eq!(in_use_context.get_all().await.unwrap().len(), 3); - - let mut sorted = in_uses.clone(); - sorted.sort_by_key(|k| k.model_id); - - for (i, in_use) in sorted.into_iter().enumerate() { - assert_eq!(in_use, in_uses[i]); - } + assert_eq!(in_use_context.get_all().await.unwrap().len(), 1); } #[tokio::test] diff --git a/src/tests/database/model_context.rs b/src/tests/database/model_context.rs index 29464d4..d32dd26 100644 --- a/src/tests/database/model_context.rs +++ b/src/tests/database/model_context.rs @@ -44,8 +44,10 @@ async fn create_test() { async fn create_auto_increment_test() { let (model_context, model, _) = seed_db().await; - let created_model1 = model_context.create(model.clone()).await.unwrap(); - let created_model2 = model_context.create(model.clone()).await.unwrap(); + let models = create_models(2, model.owner_id); + + let created_model1 = model_context.create(models[0].clone()).await.unwrap(); + let created_model2 = model_context.create(models[1].clone()).await.unwrap(); let fetched_model1 = model::Entity::find_by_id(created_model1.id) .one(&model_context.db_context.get_connection())