SeaQL / sea-orm

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

[Testing] Error: `A null value was encountered while decoding "num_items"` when testing pagination and counting #2309

Open mourtisma opened 1 month ago

mourtisma commented 1 month ago

Description

I am writing a basic CRUD app using SeaORM. At some point, I have a selector, used for either pagination and counting.

let paginator = selector.into_model().paginate(self.connection, page_size);
let num_items_and_pages = paginator.num_items_and_pages().await.unwrap();

Here is the corresponding test:

let connection = MockDatabase::new(DatabaseBackend::Postgres)
            .append_query_results([
                // First query result
                vec![/* My models */]
            ]).append_query_results([vec![maplit::btreemap! {
                "num_items" => Into::<Value>::into(2),   
            }]])
            .into_connection();

When running the test, it panics with the following error:

A null value was encountered while decoding "num_items"

Steps to Reproduce

  1. Use either count or pagination when selecting records
  2. Write a test using SeaORM's mock features
  3. The count or the pagination mocks don't seem to have effect

Expected Behavior

The count or the pagination mocks should produce a result.

Actual Behavior

The count or the pagination mocks don't seem to have effect

Reproduces How Often

Every time

Workarounds

And when using the debugger, in SeaORM's query.rs, there is this piece of code:

#[cfg(feature = "mock")]
                    QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
                        debug_print!("{:#?}", e.to_string());
                        err_null_idx_col(idx)
                    }),

And the error is: No column for ColIdx "num_items"

Reproducible Example

A reproducible example has been added to the description.

Versions

mynamebvh commented 1 month ago

You can try the code below

let connection = MockDatabase::new(DatabaseBackend::Postgres)
    .append_query_results([[maplit::btreemap! {
        "num_items" => Into::<Value>::into(1i64),
    }]])
    .into_connection();