Skip to content

Commit b68f9de

Browse files
committed
Add test cases
1 parent 7170c57 commit b68f9de

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

tests/cursor_tests.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod common;
22

33
pub use common::{features::*, setup::*, TestContext};
44
use pretty_assertions::assert_eq;
5-
use sea_orm::{entity::prelude::*, FromQueryResult};
5+
use sea_orm::{entity::prelude::*, DerivePartialModel, FromQueryResult};
66
use serde_json::json;
77

88
#[sea_orm_macros::test]
@@ -219,6 +219,45 @@ pub async fn cursor_pagination(db: &DatabaseConnection) -> Result<(), DbErr> {
219219
[Row { id: 6 }, Row { id: 7 }]
220220
);
221221

222+
#[derive(DerivePartialModel, FromQueryResult, Debug, PartialEq)]
223+
#[sea_orm(entity = "Entity")]
224+
struct PartialRow {
225+
#[sea_orm(from_col = "id")]
226+
id: i32,
227+
#[sea_orm(from_expr = "sea_query::Expr::col(Column::Id).add(1000)")]
228+
id_shifted: i32,
229+
}
230+
231+
let mut cursor = cursor.into_partial_model::<PartialRow>();
232+
233+
assert_eq!(
234+
cursor.first(2).all(db).await?,
235+
[
236+
PartialRow {
237+
id: 6,
238+
id_shifted: 1006,
239+
},
240+
PartialRow {
241+
id: 7,
242+
id_shifted: 1007,
243+
}
244+
]
245+
);
246+
247+
assert_eq!(
248+
cursor.first(3).all(db).await?,
249+
[
250+
PartialRow {
251+
id: 6,
252+
id_shifted: 1006,
253+
},
254+
PartialRow {
255+
id: 7,
256+
id_shifted: 1007,
257+
}
258+
]
259+
);
260+
222261
// Fetch JSON value
223262

224263
let mut cursor = cursor.into_json();

tests/relational_tests.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ pub use chrono::offset::Utc;
44
pub use common::{bakery_chain::*, setup::*, TestContext};
55
pub use rust_decimal::prelude::*;
66
pub use rust_decimal_macros::dec;
7-
pub use sea_orm::{entity::*, query::*, DbErr, FromQueryResult};
7+
pub use sea_orm::{entity::*, query::*, DbErr, DerivePartialModel, FromQueryResult};
8+
pub use sea_query::{Alias, Expr, Func, SimpleExpr};
89
pub use uuid::Uuid;
910

1011
// Run the test locally:
@@ -66,6 +67,7 @@ pub async fn left_join() {
6667
.filter(baker::Column::Name.contains("Baker 1"));
6768

6869
let result = select
70+
.clone()
6971
.into_model::<SelectResult>()
7072
.one(&ctx.db)
7173
.await
@@ -74,6 +76,28 @@ pub async fn left_join() {
7476
assert_eq!(result.name.as_str(), "Baker 1");
7577
assert_eq!(result.bakery_name, Some("SeaSide Bakery".to_string()));
7678

79+
#[derive(DerivePartialModel, FromQueryResult, Debug, PartialEq)]
80+
#[sea_orm(entity = "Baker")]
81+
struct PartialSelectResult {
82+
name: String,
83+
#[sea_orm(from_expr = "Expr::col((bakery::Entity, bakery::Column::Name))")]
84+
bakery_name: Option<String>,
85+
#[sea_orm(
86+
from_expr = r#"SimpleExpr::FunctionCall(Func::upper(Expr::col((bakery::Entity, bakery::Column::Name))))"#
87+
)]
88+
bakery_name_upper: Option<String>,
89+
}
90+
91+
let result = select
92+
.into_partial_model::<PartialSelectResult>()
93+
.one(&ctx.db)
94+
.await
95+
.unwrap()
96+
.unwrap();
97+
assert_eq!(result.name.as_str(), "Baker 1");
98+
assert_eq!(result.bakery_name, Some("SeaSide Bakery".to_string()));
99+
assert_eq!(result.bakery_name_upper, Some("SEASIDE BAKERY".to_string()));
100+
77101
let select = baker::Entity::find()
78102
.left_join(bakery::Entity)
79103
.select_only()

0 commit comments

Comments
 (0)