From a9807723d08f1d31b0dc627fcb6969754d24dea3 Mon Sep 17 00:00:00 2001 From: Markus Pfeiffer Date: Thu, 17 Apr 2014 16:46:18 +0200 Subject: [PATCH] Added several test cases for count and exists. --- .../activeandroid/test/query/CountTest.java | 130 ++++++++++++++++++ .../activeandroid/test/query/ExistsTest.java | 130 ++++++++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 tests/src/com/activeandroid/test/query/CountTest.java create mode 100644 tests/src/com/activeandroid/test/query/ExistsTest.java diff --git a/tests/src/com/activeandroid/test/query/CountTest.java b/tests/src/com/activeandroid/test/query/CountTest.java new file mode 100644 index 000000000..4a21c90f7 --- /dev/null +++ b/tests/src/com/activeandroid/test/query/CountTest.java @@ -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 order by as it has no influence on the result of count 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 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 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 list = from.execute(); + final int count = from.count(); + + assertEquals(0, count); + assertEquals(list.size(), count); + } +} diff --git a/tests/src/com/activeandroid/test/query/ExistsTest.java b/tests/src/com/activeandroid/test/query/ExistsTest.java new file mode 100644 index 000000000..f2a530768 --- /dev/null +++ b/tests/src/com/activeandroid/test/query/ExistsTest.java @@ -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 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 group by and order by as they 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 <> ? )"; + + 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 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 list = from.execute(); + final boolean exists = from.exists(); + + assertFalse(exists); + assertFalse(list.size() > 0); + } +}