@@ -74,11 +74,11 @@ public void testCountWhereClauseSql() {
7474 }
7575
7676 /**
77- * Shouldn't include <i>group by</i> and <i> order by</i> as they has no influence on the result
78- * of <i>exists</i> and should improve performance.
77+ * Shouldn't include <i>order by</i> as it has no influence on the result of <i>exists</i> and
78+ * should improve performance.
7979 */
8080 public void testCountOrderBySql () {
81- final String expected = "SELECT EXISTS(SELECT 1 FROM MockModel WHERE intField <> ? )" ;
81+ final String expected = "SELECT EXISTS(SELECT 1 FROM MockModel WHERE intField <> ? GROUP BY intField )" ;
8282
8383 String actual = new Select ()
8484 .from (MockModel .class )
@@ -127,4 +127,61 @@ public void testExistsEmptyResult() {
127127 assertFalse (exists );
128128 assertFalse (list .size () > 0 );
129129 }
130+
131+ /**
132+ * Should not change the result if order by is used.
133+ */
134+ public void testCountOrderBy () {
135+ cleanTable ();
136+ populateTable ();
137+
138+ From from = new Select ()
139+ .from (MockModel .class )
140+ .where ("intField = ?" , 1 )
141+ .orderBy ("intField ASC" );
142+
143+ final List <MockModel > list = from .execute ();
144+ final boolean exists = from .exists ();
145+
146+ assertTrue (exists );
147+ assertTrue (list .size () > 0 );
148+ }
149+
150+ /**
151+ * Should not change the result if group by is used.
152+ */
153+ public void testCountGroupBy () {
154+ cleanTable ();
155+ populateTable ();
156+
157+ From from = new Select ()
158+ .from (MockModel .class )
159+ .groupBy ("intField" )
160+ .having ("intField = 1" );
161+
162+ final List <MockModel > list = from .execute ();
163+ final boolean exists = from .exists ();
164+
165+ assertTrue (exists );
166+ assertTrue (list .size () > 0 );
167+ }
168+
169+ /**
170+ * Should not exist if group by eliminates all rows.
171+ */
172+ public void testCountGroupByEmpty () {
173+ cleanTable ();
174+ populateTable ();
175+
176+ From from = new Select ()
177+ .from (MockModel .class )
178+ .groupBy ("intField" )
179+ .having ("intField = 3" );
180+
181+ final List <MockModel > list = from .execute ();
182+ final boolean exists = from .exists ();
183+
184+ assertFalse (exists );
185+ assertFalse (list .size () > 0 );
186+ }
130187}
0 commit comments