SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.56k stars 459 forks source link

Mock Auto Increment ID Not Working for Postgres Backend Complaining About Null #2178

Closed NateAGeek closed 2 months ago

NateAGeek commented 3 months ago

Description

When mocking my database I am attempting to mock the creation of a instance.

pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub status: Option<Status>,
    pub date_initiated: DateTime,
    pub date_responded: Option<DateTime>,
}
let mock_db = MockDatabase::new(DatabaseBackend::Postgres)
            .append_query_results(vec![]);
   let request = friendships::ActiveModel {
        status: Set(Some(Status::Pending)),
        date_initiated: Set(Utc::now().naive_utc()),
        ..Default::default()
    };

    request.insert(db).await.map_err(|err| {
        log::error!("{:?}", err); // Assuming log crate is used
    })?;

I keep getting

Type("A null value was encountered while decoding \"id\"")

I am not sure what is the issue and why I keep getting the null. I have tried stack tracing it but not sure where it is happening.

Expected Behavior

Creation of request

Actual Behavior

Null error

Workarounds

I tried creating results and worked around making the id 0 by default.

Versions

β”œβ”€β”€ sea-orm v0.12.12 β”‚ β”œβ”€β”€ sea-orm-macros v0.12.12 (proc-macro) β”‚ β”‚ β”œβ”€β”€ sea-bae v0.2.0 (proc-macro) β”‚ β”œβ”€β”€ sea-query v0.30.7 β”‚ β”œβ”€β”€ sea-query-binder v0.5.0 β”‚ β”‚ β”œβ”€β”€ sea-query v0.30.7 (*)

NateAGeek commented 2 months ago

After revisiting the SeaORM documentation, I've resolved the issue I encountered with the mock database while trying to mock the creation of an instance. The root cause was a misunderstanding of how the MockDatabase is intended to function, specifically regarding the use of append_query_results and append_exec_results.

To address the Type("A null value was encountered while decoding \"id\"") error, it was essential to properly simulate the database operations. For the example provided, both the creation of friendships::ActiveModel and the expected insert operation needed to be reflected in the mock setupβ€”append_query_results for the select operations and append_exec_results for the insert/update/delete operations.

This clarification made it evident that the mock database doesn't act merely as a container but requires explicit setup for each type of database operation to accurately test the behavior of the code under different conditions.

I recommend enhancing the documentation to more clearly explain the interaction between append_query_results and append_exec_results, especially for new users of SeaORM. This guidance could prevent similar misunderstandings in the future. The documentation at https://www.sea-ql.org/SeaORM/docs/write-test/mock/ would be a great place for such detailed examples and explanations.

I'm closing this issue as it was resolved through a better understanding of the documentation and the intended use of SeaORM's mocking capabilities. Thank you.