From 1c8e7d35eed4d445750937761ca18fcb13c852fd Mon Sep 17 00:00:00 2001 From: Markus Pfeiffer Date: Thu, 17 Apr 2014 23:21:29 +0200 Subject: [PATCH] Added additional tests for count and exists. --- .../activeandroid/test/query/CountTest.java | 43 ++++++++++++- .../activeandroid/test/query/ExistsTest.java | 63 ++++++++++++++++++- 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/tests/src/com/activeandroid/test/query/CountTest.java b/tests/src/com/activeandroid/test/query/CountTest.java index 4a21c90f7..f0617ce1e 100644 --- a/tests/src/com/activeandroid/test/query/CountTest.java +++ b/tests/src/com/activeandroid/test/query/CountTest.java @@ -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); @@ -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 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 list = from.execute(); + final int count = from.count(); + + assertEquals(2, count); + assertEquals(1, list.size()); + } } diff --git a/tests/src/com/activeandroid/test/query/ExistsTest.java b/tests/src/com/activeandroid/test/query/ExistsTest.java index f2a530768..e67d609a6 100644 --- a/tests/src/com/activeandroid/test/query/ExistsTest.java +++ b/tests/src/com/activeandroid/test/query/ExistsTest.java @@ -74,11 +74,11 @@ public void testCountWhereClauseSql() { } /** - * Shouldn't include group by and order by as they has no influence on the result - * of exists and should improve performance. + * Shouldn't include order by as it has no influence on the result of exists 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) @@ -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 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 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 list = from.execute(); + final boolean exists = from.exists(); + + assertFalse(exists); + assertFalse(list.size() > 0); + } }