Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/22-create-model' into 21-get-model
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderManich committed Nov 28, 2023
2 parents 854dbc8 + 3110aea commit a0aa8b4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/tests/database/access_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod database_tests {

(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 @@ -81,6 +82,7 @@ mod database_tests {

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())
Expand Down Expand Up @@ -244,6 +246,7 @@ mod database_tests {

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 @@ -261,6 +264,7 @@ mod database_tests {

assert_eq!(access, res);
}

#[tokio::test]
async fn update_does_not_modify_user_id_test() {
let (access_context, access, _, _) = seed_db().await;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/database/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn create_users(amount: i32) -> Vec<user::Model> {
pub fn create_models(amount: i32, user_id: i32) -> Vec<model::Model> {
create_entities(amount, |i| model::Model {
id: i + 1,
name: "name".to_string(),
name: format!("name {}", i),
components_info: "{}".to_owned().parse().unwrap(),
owner_id: user_id,
})
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 @@ -114,31 +114,16 @@ mod database_tests {

#[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 @@ -46,8 +46,10 @@ mod database_tests {
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 a0aa8b4

Please sign in to comment.