Skip to content

Commit

Permalink
Added several test cases 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 4a8af85 commit a980772
Show file tree
Hide file tree
Showing 2 changed files with 260 additions and 0 deletions.
130 changes: 130 additions & 0 deletions tests/src/com/activeandroid/test/query/CountTest.java
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);
}
}
130 changes: 130 additions & 0 deletions tests/src/com/activeandroid/test/query/ExistsTest.java
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);
}
}

0 comments on commit a980772

Please sign in to comment.