Skip to content

Commit

Permalink
Added additional tests for count and exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpfeiffermway committed Apr 17, 2014
1 parent 79c80ad commit 1c8e7d3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 5 deletions.
43 changes: 41 additions & 2 deletions tests/src/com/activeandroid/test/query/CountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void testCountOrderBySql() {

String actual = new Select()
.from(MockModel.class)
.groupBy("intField")
.orderBy("intField")
.where("intField <> ?", 0)
.orderBy("intField")
.groupBy("intField")
.toCountSql();

assertEquals(expected, actual);
Expand Down Expand Up @@ -127,4 +127,43 @@ public void testCountEmptyResult() {
assertEquals(0, count);
assertEquals(list.size(), count);
}

/**
* Should not change the result if order by is used.
*/
public void testCountOrderBy() {
cleanTable();
populateTable();

From from = new Select()
.from(MockModel.class)
.where("intField = ?", 1)
.orderBy("intField ASC");

final List<MockModel> list = from.execute();
final int count = from.count();

assertEquals(2, count);
assertEquals(list.size(), count);
}

/**
* Should return the total number of rows, even if the rows are grouped. May seem weird, just
* test it in an SQL explorer.
*/
public void testCountGroupBy() {
cleanTable();
populateTable();

From from = new Select()
.from(MockModel.class)
.groupBy("intField")
.having("intField = 1");

final List<MockModel> list = from.execute();
final int count = from.count();

assertEquals(2, count);
assertEquals(1, list.size());
}
}
63 changes: 60 additions & 3 deletions tests/src/com/activeandroid/test/query/ExistsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public void testCountWhereClauseSql() {
}

/**
* 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.
* Shouldn't include <i>order by</i> as it 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 <> ? )";
final String expected = "SELECT EXISTS(SELECT 1 FROM MockModel WHERE intField <> ? GROUP BY intField )";

String actual = new Select()
.from(MockModel.class)
Expand Down Expand Up @@ -127,4 +127,61 @@ public void testExistsEmptyResult() {
assertFalse(exists);
assertFalse(list.size() > 0);
}

/**
* Should not change the result if order by is used.
*/
public void testCountOrderBy() {
cleanTable();
populateTable();

From from = new Select()
.from(MockModel.class)
.where("intField = ?", 1)
.orderBy("intField ASC");

final List<MockModel> list = from.execute();
final boolean exists = from.exists();

assertTrue(exists);
assertTrue(list.size() > 0);
}

/**
* Should not change the result if group by is used.
*/
public void testCountGroupBy() {
cleanTable();
populateTable();

From from = new Select()
.from(MockModel.class)
.groupBy("intField")
.having("intField = 1");

final List<MockModel> list = from.execute();
final boolean exists = from.exists();

assertTrue(exists);
assertTrue(list.size() > 0);
}

/**
* Should not exist if group by eliminates all rows.
*/
public void testCountGroupByEmpty() {
cleanTable();
populateTable();

From from = new Select()
.from(MockModel.class)
.groupBy("intField")
.having("intField = 3");

final List<MockModel> list = from.execute();
final boolean exists = from.exists();

assertFalse(exists);
assertFalse(list.size() > 0);
}
}

0 comments on commit 1c8e7d3

Please sign in to comment.