-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added several test cases for count and exists.
- Loading branch information
1 parent
4a8af85
commit a980772
Showing
2 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
|
||
package com.activeandroid.test.query; | ||
|
||
import com.activeandroid.query.Delete; | ||
import com.activeandroid.query.From; | ||
import com.activeandroid.query.Select; | ||
import com.activeandroid.test.MockModel; | ||
|
||
import java.util.List; | ||
|
||
|
||
public class CountTest extends SqlableTestCase { | ||
|
||
private void cleanTable() { | ||
new Delete().from(MockModel.class).execute(); | ||
} | ||
|
||
private void populateTable() { | ||
MockModel m1 = new MockModel(); | ||
MockModel m2 = new MockModel(); | ||
MockModel m3 = new MockModel(); | ||
|
||
m1.intField = 1; | ||
m2.intField = 1; | ||
m3.intField = 2; | ||
|
||
m1.save(); | ||
m2.save(); | ||
m3.save(); | ||
} | ||
|
||
/** | ||
* Should be a simple count for the entire table. | ||
*/ | ||
public void testCountTableSql() { | ||
final String expected = "SELECT COUNT(*) FROM MockModel"; | ||
|
||
String actual = new Select() | ||
.from(MockModel.class) | ||
.toCountSql(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
/** | ||
* Should be a count with the specified where-clause. | ||
*/ | ||
public void testCountWhereClauseSql() { | ||
final String expected = "SELECT COUNT(*) FROM MockModel WHERE intField = ?"; | ||
|
||
String actual = new Select() | ||
.from(MockModel.class) | ||
.where("intField = ?", 1) | ||
.toCountSql(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
/** | ||
* Shouldn't include <i>order by</i> as it has no influence on the result of <i>count</i> and | ||
* should improve performance. | ||
*/ | ||
public void testCountOrderBySql() { | ||
final String expected = "SELECT COUNT(*) FROM MockModel WHERE intField <> ? GROUP BY intField"; | ||
|
||
String actual = new Select() | ||
.from(MockModel.class) | ||
.groupBy("intField") | ||
.orderBy("intField") | ||
.where("intField <> ?", 0) | ||
.toCountSql(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
/** | ||
* Should return the same count as there are entries in the result set/table. | ||
*/ | ||
public void testCountTable() { | ||
cleanTable(); | ||
populateTable(); | ||
|
||
From from = new Select() | ||
.from(MockModel.class); | ||
|
||
final List<MockModel> list = from.execute(); | ||
final int count = from.count(); | ||
|
||
assertEquals(3, count); | ||
assertEquals(list.size(), count); | ||
} | ||
|
||
/** | ||
* Should return the same count as there are entries in the result set if the where-clause | ||
* matches several entries. | ||
*/ | ||
public void testCountWhereClause() { | ||
cleanTable(); | ||
populateTable(); | ||
|
||
From from = new Select() | ||
.from(MockModel.class) | ||
.where("intField = ?", 1); | ||
|
||
final List<MockModel> list = from.execute(); | ||
final int count = from.count(); | ||
|
||
assertEquals(2, count); | ||
assertEquals(list.size(), count); | ||
} | ||
|
||
/** | ||
* Should return the same count as there are entries in the result set if the where-clause | ||
* matches zero entries. | ||
*/ | ||
public void testCountEmptyResult() { | ||
cleanTable(); | ||
populateTable(); | ||
|
||
From from = new Select() | ||
.from(MockModel.class) | ||
.where("intField = ?", 3); | ||
|
||
final List<MockModel> list = from.execute(); | ||
final int count = from.count(); | ||
|
||
assertEquals(0, count); | ||
assertEquals(list.size(), count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
|
||
package com.activeandroid.test.query; | ||
|
||
import com.activeandroid.query.Delete; | ||
import com.activeandroid.query.From; | ||
import com.activeandroid.query.Select; | ||
import com.activeandroid.test.MockModel; | ||
|
||
import java.util.List; | ||
|
||
|
||
public class ExistsTest extends SqlableTestCase { | ||
|
||
private void cleanTable() { | ||
new Delete().from(MockModel.class).execute(); | ||
} | ||
|
||
private void populateTable() { | ||
MockModel m1 = new MockModel(); | ||
MockModel m2 = new MockModel(); | ||
MockModel m3 = new MockModel(); | ||
|
||
m1.intField = 1; | ||
m2.intField = 1; | ||
m3.intField = 2; | ||
|
||
m1.save(); | ||
m2.save(); | ||
m3.save(); | ||
} | ||
|
||
/** | ||
* Should return {@code true} since the result set/table isn't empty. | ||
*/ | ||
public void testExistsTable() { | ||
cleanTable(); | ||
populateTable(); | ||
|
||
From from = new Select() | ||
.from(MockModel.class); | ||
|
||
final List<MockModel> list = from.execute(); | ||
final boolean exists = from.exists(); | ||
|
||
assertTrue(exists); | ||
assertTrue(list.size() > 0); | ||
} | ||
|
||
/** | ||
* Should be a simple exists for the entire table. | ||
*/ | ||
public void testCountTableSql() { | ||
final String expected = "SELECT EXISTS(SELECT 1 FROM MockModel )"; | ||
|
||
String actual = new Select() | ||
.from(MockModel.class) | ||
.toExistsSql(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
/** | ||
* Should be an exists with the specified where-clause. | ||
*/ | ||
public void testCountWhereClauseSql() { | ||
final String expected = "SELECT EXISTS(SELECT 1 FROM MockModel WHERE intField = ? )"; | ||
|
||
String actual = new Select() | ||
.from(MockModel.class) | ||
.where("intField = ?", 1) | ||
.toExistsSql(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
/** | ||
* Shouldn't include <i>group by</i> and <i>order by</i> as they has no influence on the result | ||
* of <i>exists</i> and should improve performance. | ||
*/ | ||
public void testCountOrderBySql() { | ||
final String expected = "SELECT EXISTS(SELECT 1 FROM MockModel WHERE intField <> ? )"; | ||
|
||
String actual = new Select() | ||
.from(MockModel.class) | ||
.groupBy("intField") | ||
.orderBy("intField") | ||
.where("intField <> ?", 0) | ||
.toExistsSql(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
/** | ||
* Should return {@code true} since the where-clause matches rows and thus the result set isn't | ||
* empty. | ||
*/ | ||
public void testExistsWhereClause() { | ||
cleanTable(); | ||
populateTable(); | ||
|
||
From from = new Select() | ||
.from(MockModel.class) | ||
.where("intField = ?", 1); | ||
|
||
final List<MockModel> list = from.execute(); | ||
final boolean exists = from.exists(); | ||
|
||
assertTrue(exists); | ||
assertTrue(list.size() > 0); | ||
} | ||
|
||
/** | ||
* Should return {@code false} since the where-clause matches zero rows and thus the result set | ||
* is empty. | ||
*/ | ||
public void testExistsEmptyResult() { | ||
cleanTable(); | ||
populateTable(); | ||
|
||
From from = new Select() | ||
.from(MockModel.class) | ||
.where("intField = ?", 3); | ||
|
||
final List<MockModel> list = from.execute(); | ||
final boolean exists = from.exists(); | ||
|
||
assertFalse(exists); | ||
assertFalse(list.size() > 0); | ||
} | ||
} |