Skip to content

Commit e641e99

Browse files
committed
Add more tests
Address comments Remove incorrect tests
1 parent 36811c1 commit e641e99

2 files changed

Lines changed: 76 additions & 7 deletions

File tree

diesel/src/query_dsl/having_dsl.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use crate::dsl::Having;
2-
use crate::query_source::*;
1+
use crate::dsl;
2+
use crate::expression::Expression;
3+
use crate::expression::TypedExpressionType;
4+
use crate::expression::ValidGrouping;
5+
use crate::query_builder::{AsQuery, SelectStatement};
6+
use crate::query_source::Table;
37

48
/// The `having` method
59
///
@@ -13,17 +17,19 @@ pub trait HavingDsl<Predicate> {
1317
type Output;
1418

1519
/// See the trait documentation.
16-
fn having(self, predicate: Predicate) -> Self::Output;
20+
fn having(self, predicate: Predicate) -> dsl::Having<Self, Predicate>;
1721
}
1822

1923
impl<T, Predicate> HavingDsl<Predicate> for T
2024
where
21-
T: Table,
22-
T::Query: HavingDsl<Predicate>,
25+
T: Table + AsQuery<Query = SelectStatement<T>>,
26+
SelectStatement<T>: HavingDsl<Predicate>,
27+
T::DefaultSelection: Expression<SqlType = T::SqlType> + ValidGrouping<()>,
28+
T::SqlType: TypedExpressionType,
2329
{
24-
type Output = Having<T::Query, Predicate>;
30+
type Output = dsl::Having<SelectStatement<T>, Predicate>;
2531

26-
fn having(self, predicate: Predicate) -> Self::Output {
32+
fn having(self, predicate: Predicate) -> dsl::Having<Self, Predicate> {
2733
self.as_query().having(predicate)
2834
}
2935
}

diesel_tests/tests/having.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ fn simple_having_with_group_by() {
5959
assert_eq!(expected_data, data);
6060
}
6161

62+
#[test]
63+
fn boxed_simple_having_with_group_by() {
64+
let connection = connection();
65+
connection
66+
.execute("INSERT INTO users (id, name) VALUES (1, 'Sean'), (2, 'Tess')")
67+
.unwrap();
68+
connection
69+
.execute(
70+
"INSERT INTO posts (id, user_id, title) VALUES (1, 1, 'Hi Sean'), (2, 2, 'Hi Tess')",
71+
)
72+
.unwrap();
73+
connection
74+
.execute(
75+
"INSERT INTO comments (id, post_id, text) VALUES (1, 1, 'Comment for Hi Sean'), \
76+
(2, 2, 'Comment for Hi Tess'), (3, 2, 'Another comment for Hi Tess')",
77+
)
78+
.unwrap();
79+
80+
let source = users::table
81+
.inner_join(posts::table.inner_join(comments::table))
82+
.group_by((users::id, posts::id))
83+
.having(diesel::dsl::count(comments::id).eq(2))
84+
.select((users::name, posts::title))
85+
.into_boxed();
86+
87+
let expected_data = vec![("Tess".to_string(), "Hi Tess".to_string())];
88+
let data: Vec<(String, String)> = source.load(&connection).unwrap();
89+
90+
assert_eq!(expected_data, data);
91+
}
92+
6293
#[test]
6394
fn multi_condition_having_with_group_by() {
6495
let connection = connection();
@@ -89,3 +120,35 @@ fn multi_condition_having_with_group_by() {
89120

90121
assert_eq!(expected_data, data);
91122
}
123+
124+
#[test]
125+
fn boxed_multi_condition_having_with_group_by() {
126+
let connection = connection();
127+
connection
128+
.execute("INSERT INTO users (id, name) VALUES (1, 'Sean'), (2, 'Tess'), (3, 'Nick')")
129+
.unwrap();
130+
connection
131+
.execute(
132+
"INSERT INTO posts (id, user_id, title) VALUES (1, 1, 'Hi Sean'), (2, 2, 'Hi Tess'), (3, 3, 'Hi Nick')",
133+
)
134+
.unwrap();
135+
connection
136+
.execute(
137+
"INSERT INTO comments (id, post_id, text) VALUES (1, 1, 'Comment for Hi Sean'), \
138+
(2, 2, 'Comment for Hi Tess'), (3, 2, 'Another comment for Hi Tess'), \
139+
(4, 3, 'Comment for Hi Nick'), (5, 3, 'Another comment for Hi Nick')",
140+
)
141+
.unwrap();
142+
143+
let source = users::table
144+
.inner_join(posts::table.inner_join(comments::table))
145+
.group_by((users::id, posts::id))
146+
.having(diesel::dsl::count(comments::id).eq(2).and(users::id.eq(3)))
147+
.select((users::id, users::name, posts::title))
148+
.into_boxed();
149+
150+
let expected_data = vec![(3, "Nick".to_string(), "Hi Nick".to_string())];
151+
let data: Vec<(i32, String, String)> = source.load(&connection).unwrap();
152+
153+
assert_eq!(expected_data, data);
154+
}

0 commit comments

Comments
 (0)