Test mock connection
This commit is contained in:
parent
966f7ff9a8
commit
f4218dec56
@ -82,3 +82,108 @@ async fn exec_update(statement: Statement, db: &DatabaseConnection) -> Result<Up
|
||||
rows_affected: result.rows_affected(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{entity::prelude::*, tests_cfg::*, *};
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[smol_potat::test]
|
||||
async fn update_record_not_found_1() -> Result<(), DbErr> {
|
||||
let db = MockDatabase::new(DbBackend::Postgres)
|
||||
.append_query_results(vec![
|
||||
vec![cake::Model {
|
||||
id: 1,
|
||||
name: "Cheese Cake".to_owned(),
|
||||
}],
|
||||
vec![],
|
||||
vec![],
|
||||
])
|
||||
.append_exec_results(vec![
|
||||
MockExecResult {
|
||||
last_insert_id: 0,
|
||||
rows_affected: 1,
|
||||
},
|
||||
MockExecResult {
|
||||
last_insert_id: 0,
|
||||
rows_affected: 0,
|
||||
},
|
||||
MockExecResult {
|
||||
last_insert_id: 0,
|
||||
rows_affected: 0,
|
||||
},
|
||||
])
|
||||
.into_connection();
|
||||
|
||||
let model = cake::Model {
|
||||
id: 1,
|
||||
name: "New York Cheese".to_owned(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
cake::ActiveModel {
|
||||
name: Set("Cheese Cake".to_owned()),
|
||||
..model.into_active_model()
|
||||
}
|
||||
.update(&db)
|
||||
.await?,
|
||||
cake::Model {
|
||||
id: 1,
|
||||
name: "Cheese Cake".to_owned(),
|
||||
}
|
||||
.into_active_model()
|
||||
);
|
||||
|
||||
let model = cake::Model {
|
||||
id: 2,
|
||||
name: "New York Cheese".to_owned(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
cake::ActiveModel {
|
||||
name: Set("Cheese Cake".to_owned()),
|
||||
..model.clone().into_active_model()
|
||||
}
|
||||
.update(&db)
|
||||
.await,
|
||||
Err(DbErr::RecordNotFound(
|
||||
"None of the database rows are affected".to_owned()
|
||||
))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
cake::Entity::update(cake::ActiveModel {
|
||||
name: Set("Cheese Cake".to_owned()),
|
||||
..model.into_active_model()
|
||||
})
|
||||
.exec(&db)
|
||||
.await,
|
||||
Err(DbErr::RecordNotFound(
|
||||
"None of the database rows are affected".to_owned()
|
||||
))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
db.into_transaction_log(),
|
||||
vec![
|
||||
Transaction::from_sql_and_values(
|
||||
DbBackend::Postgres,
|
||||
r#"UPDATE "cake" SET "name" = $1 WHERE "cake"."id" = $2"#,
|
||||
vec!["Cheese Cake".into(), 1i32.into()]
|
||||
),
|
||||
Transaction::from_sql_and_values(
|
||||
DbBackend::Postgres,
|
||||
r#"UPDATE "cake" SET "name" = $1 WHERE "cake"."id" = $2"#,
|
||||
vec!["Cheese Cake".into(), 2i32.into()]
|
||||
),
|
||||
Transaction::from_sql_and_values(
|
||||
DbBackend::Postgres,
|
||||
r#"UPDATE "cake" SET "name" = $1 WHERE "cake"."id" = $2"#,
|
||||
vec!["Cheese Cake".into(), 2i32.into()]
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user