Test count, min, max with group_by

This commit is contained in:
Sam Samai 2021-07-16 15:19:56 +10:00
parent 0e4840c98f
commit 8d9dc5ce1c

View File

@ -296,14 +296,20 @@ pub async fn test_group_by() {
#[derive(Debug, FromQueryResult)] #[derive(Debug, FromQueryResult)]
struct SelectResult { struct SelectResult {
name: String, name: String,
number_orders: Option<i32>,
total_spent: Option<Decimal>, total_spent: Option<Decimal>,
min_spent: Option<Decimal>,
max_spent: Option<Decimal>,
} }
let select = customer::Entity::find() let select = customer::Entity::find()
.left_join(order::Entity) .left_join(order::Entity)
.select_only() .select_only()
.column(customer::Column::Name) .column(customer::Column::Name)
.column_as(order::Column::Total.count(), "number_orders")
.column_as(order::Column::Total.sum(), "total_spent") .column_as(order::Column::Total.sum(), "total_spent")
.column_as(order::Column::Total.min(), "min_spent")
.column_as(order::Column::Total.max(), "max_spent")
.group_by(customer::Column::Name); .group_by(customer::Column::Name);
let result = select let result = select
@ -313,8 +319,29 @@ pub async fn test_group_by() {
.unwrap() .unwrap()
.unwrap(); .unwrap();
assert_eq!(result.number_orders, Some(2));
assert_eq!( assert_eq!(
result.total_spent, result.total_spent,
Some(kate_order_1.total.unwrap() + kate_order_2.total.unwrap()) Some(kate_order_1.total.clone().unwrap() + kate_order_2.total.clone().unwrap())
);
assert_eq!(
result.min_spent,
Some(
kate_order_1
.total
.clone()
.unwrap()
.min(kate_order_2.total.clone().unwrap())
)
);
assert_eq!(
result.max_spent,
Some(
kate_order_1
.total
.clone()
.unwrap()
.max(kate_order_2.total.clone().unwrap())
)
); );
} }