Skip to content
This repository was archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
realmResults.distinct(*,...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Mendez committed Mar 24, 2017
1 parent 10b8f4c commit 0a6c49a
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ protected void onCreate(Bundle savedInstanceState) {
* Configurations must be part of a non-Android component class.
* And such dependency must be declared at @PrepareForTest eg. RobolectricTests
*/
RealmDependencies.createConfig();
realm = Realm.getDefaultInstance();
RealmDependencies.createConfig(this);


setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
shouldDoDistinct();
}

void shouldQueryChain(){
Expand Down Expand Up @@ -85,9 +86,66 @@ void shouldShowChangesFromRealmResultsWithAsyncTransactions(){
});
}

public void shouldDoDistinct(){

//lets do this first with realmList
realm = Realm.getDefaultInstance();

realm.beginTransaction();
Dog dog;
dog = realm.createObject(Dog.class);
dog.setAge(6);
dog.setName("Idalgo");
dog.setBirthdate( new Date(2016, 6, 9));


dog = realm.createObject(Dog.class);
dog.setAge(6);
dog.setName("Fido");
dog.setBirthdate( new Date(2016, 6, 9));

dog = realm.createObject(Dog.class);
dog.setAge(2);
dog.setName("Hernan");
dog.setBirthdate( new Date(2015, 6, 10));

dog = realm.createObject(Dog.class);
dog.setAge(5);
dog.setName("Hernan");
dog.setBirthdate( new Date(2012, 2, 1));

dog = realm.createObject(Dog.class);
dog.setAge(2);
dog.setName("Chibi");
dog.setBirthdate( new Date(2015, 2, 1));

dog = realm.createObject(Dog.class);
dog.setAge(3);
dog.setName("Andromeda");
dog.setBirthdate( new Date(2014, 2, 1));

dog = realm.createObject(Dog.class);
dog.setAge(12);
dog.setName("Baxter");
dog.setBirthdate( new Date(2005, 2, 1));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(10);
dog.setName("Beethoven");
dog.setBirthdate( new Date(2007, 2, 1));
realm.commitTransaction();

RealmResults<Dog> dogs = realm.where(Dog.class).distinct("name", "birthdate");


}

@Override
protected void onPause(){
super.onPause();
realm.close();

if( realm != null )
realm.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package info.juanmendez.mockrealmdemo;

import android.content.Context;

import io.realm.Realm;
import io.realm.RealmConfiguration;

Expand All @@ -11,7 +13,8 @@

public class RealmDependencies {

public static void createConfig(){
public static void createConfig(Context context){
Realm.init( context );
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("realmconfiguration.realm")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@

import io.realm.RealmObject;
import io.realm.annotations.Ignore;
import io.realm.annotations.Index;

/**
* Created by musta on 2/10/2017.
*/

public class Dog extends RealmObject{
private int id;

@Index
private String name;

@Index
private int age;

@Index
private Date birthdate;

@Ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,67 @@ public void shouldDoMax(){
assertEquals( "min age is ", 1, realm.where(Dog.class).min("age").intValue());
assertEquals( "average age is ", Double.valueOf((float) 3.5), Double.valueOf(realm.where(Dog.class).average("age")) );
}

@Test
public void shouldDoDistinct(){
RealmStorage.clear();

//lets do this first with realmList
Dog dog;

dog = realm.createObject(Dog.class);
dog.setAge(6);
dog.setName("Idalgo");
dog.setBirthdate( new Date(2016, 6, 9));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(6);
dog.setName("Fido");
dog.setBirthdate( new Date(2016, 6, 9));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(2);
dog.setName("Hernan");
dog.setBirthdate( new Date(2015, 6, 10));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(5);
dog.setName("Hernan");
dog.setBirthdate( new Date(2012, 2, 1));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(2);
dog.setName("Chibi");
dog.setBirthdate( new Date(2015, 2, 1));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(3);
dog.setName("Andromeda");
dog.setBirthdate( new Date(2014, 2, 1));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(12);
dog.setName("Baxter");
dog.setBirthdate( new Date(2005, 2, 1));
realm.copyToRealm( dog );

dog = realm.createObject(Dog.class);
dog.setAge(10);
dog.setName("Beethoven");
dog.setBirthdate( new Date(2007, 2, 1));
realm.copyToRealm( dog );

RealmResults<Dog> dogs = realm.where(Dog.class).findAll().distinct("name", "birthdate");

for( Dog _dog: dogs ){
System.out.println( "dog: " + _dog.getName() + " " + _dog.getBirthdate() );
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static RealmQuery create( QueryTracker queryTracker){
private static void handleCollectionMethods(QueryTracker queryTracker) {
RealmQuery realmQuery = queryTracker.getRealmQuery();

when( realmQuery.findAll() ).thenAnswer(invocationOnMock ->{
when( realmQuery.findAll() ).thenAnswer(invocation ->{
queryTracker.appendQuery( new Query(Compare.endTopGroup));
return queryTracker.rewind();
});
Expand All @@ -62,7 +62,7 @@ private static void handleCollectionMethods(QueryTracker queryTracker) {
return queryTracker.getRealmResults();
});

when( realmQuery.findFirst()).thenAnswer(invocationOnMock -> {
when( realmQuery.findFirst()).thenAnswer(invocation -> {
queryTracker.appendQuery( new Query(Compare.endTopGroup));
RealmResults<RealmModel> realmResults = queryTracker.rewind();

Expand Down Expand Up @@ -268,7 +268,7 @@ private static void handleSearchMethods( QueryTracker queryTracker){
* @return the same args.realmQuery
*/

private static Answer<RealmQuery> createComparison(QueryTracker queryTracker, String condition ){
public static Answer<RealmQuery> createComparison(QueryTracker queryTracker, String condition ){

RealmQuery realmQuery = queryTracker.getRealmQuery();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyVararg;
import static org.powermock.api.mockito.PowerMockito.doAnswer;
import static org.powermock.api.mockito.PowerMockito.when;

Expand All @@ -35,26 +36,27 @@ public class RealmResultsDecorator {
public static RealmResults create(QueryTracker queryTracker ){

RealmResults realmResults = queryTracker.getRealmResults();
RealmList<RealmModel> results = queryTracker.getQueryList();
RealmList<RealmModel> realmList = queryTracker.getQueryList();

doAnswer(new Answer<RealmQuery>() {

@Override
public RealmQuery answer(InvocationOnMock invocationOnMock) throws Throwable {

QueryTracker resultsQueryTracker = queryTracker.clone();
resultsQueryTracker.appendQuery( new Query(Compare.startTopGroup, new Object[]{results}));
resultsQueryTracker.appendQuery( new Query(Compare.startTopGroup, new Object[]{realmList}));

RealmQuery realmQuery = RealmQueryDecorator.create(resultsQueryTracker);

return realmQuery;
}
}).when(realmResults).where();

handleBasicActions( realmResults, results );
handleDeleteMethods( realmResults, results );
handleMathMethods( realmResults, results );
handleBasicActions( realmResults, realmList );
handleDeleteMethods( realmResults, realmList );
handleMathMethods( realmResults, realmList );
handleAsyncMethods( queryTracker );
handleDistinct( queryTracker );

return realmResults;
}
Expand Down Expand Up @@ -289,6 +291,38 @@ private static void handleAsyncMethods( QueryTracker queryTracker){
}).when( realmResults ).asObservable();
}

private static void handleDistinct( QueryTracker queryTracker ) {

RealmResults realmResults = queryTracker.getRealmResults();

doAnswer(invocation -> {
return invocateDistinct( queryTracker, invocation.getArguments() );
}).when(realmResults).distinct( anyString() );

doAnswer(invocation -> {
return invocateDistinct( queryTracker, invocation.getArguments() );
}).when(realmResults).distinct( anyString(), anyVararg());
}

private static RealmResults<RealmModel> invocateDistinct(QueryTracker queryTracker, Object[] arguments ){
RealmList realmList = queryTracker.getQueryList();
QueryTracker resultsQueryTracker = queryTracker.clone();
resultsQueryTracker.appendQuery(new Query(Compare.startTopGroup, new Object[]{realmList}));
resultsQueryTracker.appendQuery(new Query(Compare.endTopGroup));

String field;

System.out.println( "MockingRealm: " + "There seems to be a bug, as in Realm only the first argument is doing all the distincts" );
arguments = new Object[]{arguments[0]};

for (Object argument: arguments ) {
field = (String) argument;
resultsQueryTracker.appendQuery(new Query(Compare.distinct, field, new String[]{field}));
}

return resultsQueryTracker.rewind();
}

public static void removeSubscriptions(){
subscriptionsUtil.removeAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public class Compare{
public static final String endGroup = "endGroup";
public static final String startTopGroup = "startTopGroup";
public static final String endTopGroup = "endTopGroup";


public static final String findAll = "findAll";
public static final String findFirst = "findFirst";
public static final String distinct = "distinct";
public static final String async = "async";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class QuerySearch {
Object[] arguments;
Object needle;
ArrayList<Object> needles;
ArrayList<Object> distinctNeedles;

Class clazz;
Case casing = Case.SENSITIVE;
Expand All @@ -38,22 +39,28 @@ public RealmList<RealmModel> search( String condition, Object[] arguments, Realm
this.arguments = arguments;
this.types = new ArrayList<>(Arrays.asList(((String) arguments[0]).split("\\.")));

this.needle = arguments[1];
this.clazz = RealmModelUtil.getClass(needle);

int argsLen = arguments.length;

if ((clazz == String.class || clazz == String[].class) && argsLen >= 3) {
casing = (Case) arguments[2];
if( argsLen >= 2){
this.needle = arguments[1];
this.clazz = RealmModelUtil.getClass(needle);

if ((clazz == String.class || clazz == String[].class) && argsLen >= 3) {
casing = (Case) arguments[2];
}
}

if (condition == Compare.between) {
this.left = arguments[1];
this.right = arguments[2];
} else if (condition == Compare.in) {
}
else if (condition == Compare.in) {
needles = new ArrayList<>(Arrays.asList((Object[]) needle));
}

else if( condition == Compare.distinct ) {
System.out.println( "MockingRealm: ensure " + ((String) arguments[0]) + " has @index annotation" );
distinctNeedles = new ArrayList<>();
}

if (casing == Case.INSENSITIVE) {

Expand All @@ -65,7 +72,6 @@ public RealmList<RealmModel> search( String condition, Object[] arguments, Realm
} else {
this.needle = ((String) needle).toLowerCase();
}

}

RealmList<RealmModel> queriedList = RealmListDecorator.create();
Expand Down Expand Up @@ -130,6 +136,20 @@ private boolean checkRealmObject(RealmModel realmModel, int level) {
} else if (!needle.equals(value)) {
return true;
}
} else if (condition == Compare.distinct ) {

if (clazz == Date.class) {

long time = ((Date)value).getTime();
if( !distinctNeedles.contains(time) ){
distinctNeedles.add( time );
return true;
}

} else if( !distinctNeedles.contains(value)){
distinctNeedles.add( value );
return true;
}
} else if (condition == Compare.less) {

if (clazz == Date.class && (((Date) value)).compareTo((Date) needle) < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ public RealmResults<RealmModel> rewind(){
for ( Query query: queries ){

if( !executeGroupQuery( query ) ){

searchList = new QuerySearch().search( query.getCondition(), query.getArgs(), getQueryList() );
setQueryList( searchList );
}
Expand Down

0 comments on commit 0a6c49a

Please sign in to comment.