Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsSR committed Nov 28, 2023
1 parent 2171ec9 commit 3cb26ae
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/database/model_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl ModelContext {
ModelContext { db_context }
}
}

#[async_trait]
impl EntityContextTrait<model::Model> for ModelContext {
/// Used for creating a model::Model entity
Expand Down
4 changes: 4 additions & 0 deletions src/tests/database/access_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
21 changes: 3 additions & 18 deletions src/tests/database/in_use_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 4 additions & 2 deletions src/tests/database/model_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 3cb26ae

Please sign in to comment.