Skip to content

Commit

Permalink
Fix loader panic on empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Sep 22, 2023
1 parent e61b066 commit 5a6acd6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/executor/cursor.rs
Expand Up @@ -382,7 +382,6 @@ where
#[cfg(test)]
#[cfg(feature = "mock")]
mod tests {
use super::*;
use crate::entity::prelude::*;
use crate::tests_cfg::*;
use crate::{DbBackend, MockDatabase, Statement, Transaction};
Expand Down
31 changes: 17 additions & 14 deletions src/query/loader.rs
Expand Up @@ -143,6 +143,10 @@ where
return Err(query_err("Relation is HasMany instead of HasOne"));
}

if self.is_empty() {
return Ok(Vec::new());
}

let keys: Vec<ValueTuple> = self
.iter()
.map(|model: &M| extract_key(&rel_def.from_col, model))
Expand Down Expand Up @@ -194,6 +198,10 @@ where
return Err(query_err("Relation is HasOne instead of HasMany"));
}

if self.is_empty() {
return Ok(Vec::new());
}

let keys: Vec<ValueTuple> = self
.iter()
.map(|model: &M| extract_key(&rel_def.from_col, model))
Expand Down Expand Up @@ -269,6 +277,10 @@ where
)));
}

if self.is_empty() {
return Ok(Vec::new());
}

let pkeys: Vec<ValueTuple> = self
.iter()
.map(|model: &M| extract_key(&via_rel.from_col, model))
Expand Down Expand Up @@ -587,14 +599,11 @@ mod tests {
);
}

// FIXME: load many with empty vector will panic
// #[tokio::test]
#[tokio::test]
async fn test_load_many_empty() {
use sea_orm::{entity::prelude::*, tests_cfg::*, DbBackend, MockDatabase};

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[fruit_model(1, Some(1)), fruit_model(2, Some(1))]])
.into_connection();
let db = MockDatabase::new(DbBackend::Postgres).into_connection();

let cakes: Vec<cake::Model> = vec![];

Expand All @@ -610,9 +619,7 @@ mod tests {

#[tokio::test]
async fn test_load_many_to_many_base() {
use sea_orm::{
entity::prelude::*, tests_cfg::*, DbBackend, IntoMockRow, LoaderTrait, MockDatabase,
};
use sea_orm::{tests_cfg::*, DbBackend, IntoMockRow, LoaderTrait, MockDatabase};

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([
Expand All @@ -633,9 +640,7 @@ mod tests {

#[tokio::test]
async fn test_load_many_to_many_complex() {
use sea_orm::{
entity::prelude::*, tests_cfg::*, DbBackend, IntoMockRow, LoaderTrait, MockDatabase,
};
use sea_orm::{tests_cfg::*, DbBackend, IntoMockRow, LoaderTrait, MockDatabase};

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([
Expand Down Expand Up @@ -675,9 +680,7 @@ mod tests {

#[tokio::test]
async fn test_load_many_to_many_empty() {
use sea_orm::{
entity::prelude::*, tests_cfg::*, DbBackend, IntoMockRow, LoaderTrait, MockDatabase,
};
use sea_orm::{tests_cfg::*, DbBackend, IntoMockRow, LoaderTrait, MockDatabase};

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([
Expand Down

0 comments on commit 5a6acd6

Please sign in to comment.