sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

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

Open mourtisma opened this issue 1 year ago • 1 comments

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

  • sea-orm v0.12.14
  • PostgreSQL
  • Ubuntu 22.04

mourtisma avatar Aug 03 '24 19:08 mourtisma

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();

mynamebvh avatar Aug 09 '24 08:08 mynamebvh