Skip to content

Commit

Permalink
Merge pull request #206 from joshuapinter/fix_boolean_type
Browse files Browse the repository at this point in the history
Handle boolean types in where arguments.
  • Loading branch information
SeanPONeil committed Apr 7, 2014
2 parents 27733e7 + 53397c4 commit cb31474
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/com/activeandroid/query/From.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import android.text.TextUtils;

import com.activeandroid.Cache;
import com.activeandroid.Model;
import com.activeandroid.content.ContentProvider;
Expand All @@ -25,7 +26,6 @@
import com.activeandroid.util.SQLiteUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class From implements Sqlable {
Expand Down Expand Up @@ -100,13 +100,13 @@ public From where(String where) {
public From where(String where, Object... args) {
if (mWhere != null) { // Chain conditions if a previous
mWhere = mWhere + " AND " + where; // condition exists.
mArguments.addAll(Arrays.asList(args));

} else {
}
else {
mWhere = where;
mArguments.addAll(Arrays.asList(args));
}

addArguments(args);

return this;
}

Expand Down Expand Up @@ -144,7 +144,12 @@ public From offset(String offset) {
}

void addArguments(Object[] args) {
mArguments.addAll(Arrays.asList(args));
for( Object arg : args ) {
if (arg.getClass() == boolean.class || arg.getClass() == Boolean.class)
arg = ( arg.equals(true) ? 1 : 0 );

mArguments.add(arg);
}
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions tests/src/com/activeandroid/test/MockModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,22 @@
*/

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

import java.util.Date;

@Table(name = "MockModel")
public class MockModel extends Model {
@Column
public Date dateField;

@Column
public double doubleField;

@Column
public int intField;

@Column
public boolean booleanField;
}
44 changes: 44 additions & 0 deletions tests/src/com/activeandroid/test/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package com.activeandroid.test;

import com.activeandroid.Cache;
import com.activeandroid.Model;
import com.activeandroid.TableInfo;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -125,6 +129,46 @@ public void testHashCodeDifferentRows() {
assertEquals(2, set.size());
}

/**
* Column names should default to the field name.
*/
public void testColumnNamesDefaulToFieldNames() {
TableInfo tableInfo = Cache.getTableInfo(MockModel.class);

for ( Field field : tableInfo.getFields() ) {
// Id column is a special case, we'll ignore that one.
if ( field.getName().equals("mId") ) continue;

assertEquals(field.getName(), tableInfo.getColumnName(field));
}
}

/**
* Boolean should handle integer (0/1) and boolean (false/true) values.
*/
public void testBooleanColumnType() {
MockModel mockModel = new MockModel();
mockModel.booleanField = false;
Long id = mockModel.save();

boolean databaseBooleanValue = MockModel.load( MockModel.class, id ).booleanField;

assertEquals( false, databaseBooleanValue );

// Test passing both a integer and a boolean into the where conditional.
assertEquals(
mockModel,
new Select().from(MockModel.class).where("booleanField = ?", 0).executeSingle() );

assertEquals(
mockModel,
new Select().from(MockModel.class).where("booleanField = ?", false).executeSingle() );

assertNull( new Select().from(MockModel.class).where("booleanField = ?", 1).executeSingle() );

assertNull( new Select().from(MockModel.class).where("booleanField = ?", true).executeSingle() );
}

/**
* Mock model as we need 2 different model classes.
*/
Expand Down

0 comments on commit cb31474

Please sign in to comment.