sea-orm
sea-orm copied to clipboard
[Testing] Error: `A null value was encountered while decoding "num_items"` when testing pagination and counting
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
- Use either
countor pagination when selecting records - Write a test using SeaORM's mock features
- 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
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();