Skip to content

Modernize database tests and cleanup #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
targetSdkVersion targetSdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package com.firebase.ui.database.utils;
package com.firebase.ui.database;

public class Bean {
private int number;
private String text;
private boolean bool;
private int mNumber;
private String mText;
private boolean mBool;

public Bean() {
// Needed for Firebase
}

public Bean(int number, String text, boolean bool) {
this.number = number;
this.text = text;
this.bool = bool;
mNumber = number;
mText = text;
mBool = bool;
}

public Bean(int index) {
this(index, "Text " + index, index % 2 == 0);
}

public int getNumber() {
return number;
return mNumber;
}

public String getText() {
return text;
return mText;
}

public boolean isBool() {
return bool;
return mBool;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

package com.firebase.ui.database;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import com.firebase.ui.database.utils.Bean;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
Expand All @@ -35,54 +33,46 @@
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class FirebaseArrayOfObjectsTest extends InstrumentationTestCase {
public class FirebaseArrayOfObjectsTest {
private static final int INITIAL_SIZE = 3;

private DatabaseReference mRef;
private FirebaseArray mArray;

@Before
public void setUp() throws Exception {
FirebaseApp app = getAppInstance(getInstrumentation().getContext());
mRef = FirebaseDatabase.getInstance(app).getReference()
.child("firebasearray").child("objects");
FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
mRef = FirebaseDatabase.getInstance(app)
.getReference()
.child("firebasearray")
.child("objects");
mArray = new FirebaseArray(mRef);
mRef.removeValue();
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 3; i++) {
mRef.push().setValue(new Bean(i, "Text " + i, i % 2 == 0), i);
}
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getCount() == 3;
}
}
);
@Override
public void run() {
for (int i = 1; i <= INITIAL_SIZE; i++) {
mRef.push().setValue(new Bean(i, "Text " + i, i % 2 == 0), i);
}
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getCount() == INITIAL_SIZE;
}
});
}

@After
public void tearDown() throws Exception {
if (mRef != null) {
mRef.getRoot().removeValue();
}

if (mArray != null) {
mArray.cleanup();
}
}

@Test
public void testSize() throws Exception {
assertEquals(3, mArray.getCount());
mArray.cleanup();
mRef.getRoot().removeValue();
}

@Test
public void testPushIncreasesSize() throws Exception {
assertEquals(3, mArray.getCount());
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mRef.push().setValue(new Bean(4));
}
Expand All @@ -97,6 +87,7 @@ public Boolean call() throws Exception {
@Test
public void testPushAppends() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mRef.push().setValue(new Bean(4), 4);
}
Expand All @@ -111,10 +102,12 @@ public Boolean call() throws Exception {
@Test
public void testAddValueWithPriority() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mRef.push().setValue(new Bean(4), 0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getItem(3).getValue(Bean.class).getNumber() == 3 && mArray.getItem(0)
.getValue(Bean.class)
Expand All @@ -126,10 +119,12 @@ public Boolean call() throws Exception {
@Test
public void testChangePriorities() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mArray.getItem(2).getRef().setPriority(0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return getBean(mArray, 0).getNumber() == 3
&& getBean(mArray, 1).getNumber() == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

package com.firebase.ui.database;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
Expand All @@ -34,50 +33,42 @@
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class FirebaseArrayTest extends InstrumentationTestCase {
public class FirebaseArrayTest {
private static final int INITIAL_SIZE = 3;
private DatabaseReference mRef;
private FirebaseArray mArray;

@Before
public void setUp() throws Exception {
FirebaseApp app = getAppInstance(getInstrumentation().getContext());
FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
mRef = FirebaseDatabase.getInstance(app).getReference().child("firebasearray");
mArray = new FirebaseArray(mRef);
mRef.removeValue();
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 3; i++) {
for (int i = 1; i <= INITIAL_SIZE; i++) {
mRef.push().setValue(i, i);
}
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getCount() == 3;
return mArray.getCount() == INITIAL_SIZE;
}
});
}

@After
public void tearDown() throws Exception {
if (mRef != null) {
mRef.getRoot().removeValue();
}

if (mArray != null) {
mArray.cleanup();
}
}

@Test
public void testSize() throws Exception {
assertEquals(3, mArray.getCount());
mArray.cleanup();
mRef.getRoot().removeValue();
}

@Test
public void testPushIncreasesSize() throws Exception {
assertEquals(3, mArray.getCount());
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mRef.push().setValue(4);
}
Expand All @@ -92,6 +83,7 @@ public Boolean call() throws Exception {
@Test
public void testPushAppends() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mRef.push().setValue(4, 4);
}
Expand All @@ -106,10 +98,12 @@ public Boolean call() throws Exception {
@Test
public void testAddValueWithPriority() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mRef.push().setValue(4, 0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getItem(3).getValue(Integer.class).equals(3)
&& mArray.getItem(0).getValue(Integer.class).equals(4);
Expand All @@ -118,15 +112,32 @@ public Boolean call() throws Exception {
}

@Test
public void testChangePriorities() throws Exception {
public void testChangePriorityBackToFront() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mArray.getItem(2).getRef().setPriority(0.5);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isValuesEqual(mArray, new int[]{3, 1, 2});
}
});
}

@Test
public void testChangePriorityFrontToBack() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
@Override
public void run() {
mArray.getItem(0).getRef().setPriority(4);
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isValuesEqual(mArray, new int[]{2, 3, 1});
}
});
}
}
Loading