From 018f7dd19fdae11f8f560a5ea96588802528c1d2 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sat, 9 Oct 2021 21:14:08 +0800 Subject: [PATCH] Streaming for MockConnection --- src/database/mock.rs | 31 +++++++++++++++++++++++++++++++ src/executor/select.rs | 6 ------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/database/mock.rs b/src/database/mock.rs index b6e96a79..d0fce2cd 100644 --- a/src/database/mock.rs +++ b/src/database/mock.rs @@ -315,4 +315,35 @@ mod tests { assert_eq!(db.into_transaction_log(), vec![]); } + + #[smol_potat::test] + async fn test_stream_1() -> Result<(), DbErr> { + use futures::TryStreamExt; + + let apple = fruit::Model { + id: 1, + name: "Apple".to_owned(), + cake_id: Some(1), + }; + + let orange = fruit::Model { + id: 2, + name: "orange".to_owned(), + cake_id: None, + }; + + let db = MockDatabase::new(DbBackend::Postgres) + .append_query_results(vec![vec![apple.clone(), orange.clone()]]) + .into_connection(); + + let mut stream = fruit::Entity::find().stream(&db).await?; + + assert_eq!(stream.try_next().await?, Some(apple)); + + assert_eq!(stream.try_next().await?, Some(orange)); + + assert_eq!(stream.try_next().await?, None); + + Ok(()) + } } diff --git a/src/executor/select.rs b/src/executor/select.rs index 1c1825cc..a95fe463 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -3,11 +3,9 @@ use crate::{ ModelTrait, Paginator, PrimaryKeyToColumn, QueryResult, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, Statement, TryGetableMany, }; -#[cfg(feature = "sqlx-dep")] use futures::{Stream, TryStreamExt}; use sea_query::SelectStatement; use std::marker::PhantomData; -#[cfg(feature = "sqlx-dep")] use std::pin::Pin; #[derive(Clone, Debug)] @@ -252,7 +250,6 @@ where self.into_model().all(db).await } - #[cfg(feature = "sqlx-dep")] pub async fn stream<'a: 'b, 'b, C>( self, db: &'a C, @@ -320,7 +317,6 @@ where self.into_model().all(db).await } - #[cfg(feature = "sqlx-dep")] pub async fn stream<'a: 'b, 'b, C>( self, db: &'a C, @@ -381,7 +377,6 @@ where self.into_model().one(db).await } - #[cfg(feature = "sqlx-dep")] pub async fn stream<'a: 'b, 'b, C>( self, db: &'a C, @@ -456,7 +451,6 @@ where Ok(models) } - #[cfg(feature = "sqlx-dep")] pub async fn stream<'a: 'b, 'b, C>( self, db: &'a C,