Skip to content

Commit 1c8e7d3

Browse files
committed
Added additional tests for count and exists.
1 parent 79c80ad commit 1c8e7d3

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

tests/src/com/activeandroid/test/query/CountTest.java

+41-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public void testCountOrderBySql() {
6565

6666
String actual = new Select()
6767
.from(MockModel.class)
68-
.groupBy("intField")
69-
.orderBy("intField")
7068
.where("intField <> ?", 0)
69+
.orderBy("intField")
70+
.groupBy("intField")
7171
.toCountSql();
7272

7373
assertEquals(expected, actual);
@@ -127,4 +127,43 @@ public void testCountEmptyResult() {
127127
assertEquals(0, count);
128128
assertEquals(list.size(), count);
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 int count = from.count();
145+
146+
assertEquals(2, count);
147+
assertEquals(list.size(), count);
148+
}
149+
150+
/**
151+
* Should return the total number of rows, even if the rows are grouped. May seem weird, just
152+
* test it in an SQL explorer.
153+
*/
154+
public void testCountGroupBy() {
155+
cleanTable();
156+
populateTable();
157+
158+
From from = new Select()
159+
.from(MockModel.class)
160+
.groupBy("intField")
161+
.having("intField = 1");
162+
163+
final List<MockModel> list = from.execute();
164+
final int count = from.count();
165+
166+
assertEquals(2, count);
167+
assertEquals(1, list.size());
168+
}
130169
}

tests/src/com/activeandroid/test/query/ExistsTest.java

+60-3
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)