-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aleksey Malevaniy
committed
Mar 18, 2015
1 parent
26d4f46
commit f88f8de
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
library/src/androidTest/java/io/techery/snapper/model/Company.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.techery.snapper.model; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
|
||
public class Company implements Indexable { | ||
|
||
int id; | ||
String name; | ||
List<User> users; | ||
|
||
public Company(int id, String name, List<User> users) { | ||
this.id = id; | ||
this.name = name; | ||
this.users = users; | ||
} | ||
|
||
@Override public byte[] index() { | ||
return ByteBuffer.allocate(4).putInt(id).array(); | ||
} | ||
|
||
@Override public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Company company = (Company) o; | ||
|
||
return id == company.id; | ||
|
||
} | ||
|
||
@Override public int hashCode() { | ||
return id; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public List<User> getUsers() { | ||
return users; | ||
} | ||
|
||
public void setUsers(List<User> users) { | ||
this.users = users; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
library/src/androidTest/java/io/techery/snapper/test/ManyCollectionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.techery.snapper.test; | ||
|
||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import com.innahema.collections.query.functions.Predicate; | ||
import com.innahema.collections.query.queriables.Queryable; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.Collections; | ||
|
||
import io.techery.snapper.BaseTestCase; | ||
import io.techery.snapper.DataCollection; | ||
import io.techery.snapper.model.Company; | ||
import io.techery.snapper.model.User; | ||
import io.techery.snapper.view.IDataView; | ||
|
||
import static org.junit.Assert.assertThat; | ||
import static org.junit.matchers.JUnitMatchers.hasItem; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class ManyCollectionsTest extends BaseTestCase { | ||
|
||
DataCollection<User> userStorage; | ||
DataCollection<Company> companyStorage; | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
// Preconditions | ||
/////////////////////////////////////////////////////////////////////////// | ||
|
||
@Before public void createCollectionAndFilter() { | ||
userStorage = db.collection(User.class); | ||
userStorage.insert(new User("10000")); | ||
userStorage.insert(new User("100")); | ||
userStorage.insert(new User("1")); | ||
userStorage.insert(new User("10")); | ||
userStorage.insert(new User("1000")); | ||
|
||
companyStorage = db.collection(Company.class); | ||
companyStorage.insert(new Company(1, "BigCo", Collections.singletonList(new User("100")))); | ||
companyStorage.insert(new Company(2, "SmallCo", Collections.singletonList(new User("1")))); | ||
} | ||
|
||
@After public void releaseCollection() throws Exception { | ||
userStorage.clear(); | ||
companyStorage.clear(); | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
// Tests | ||
/////////////////////////////////////////////////////////////////////////// | ||
|
||
@Test public void storageConsistency() { | ||
assertThat(userStorage.view().build().toList().size(), CoreMatchers.is(5)); | ||
assertThat(companyStorage.view().build().toList().size(), CoreMatchers.is(2)); | ||
} | ||
|
||
@Test public void modelEqualityFromDiffStorages() { | ||
IDataView<Company> smallCoView = companyStorage.view().where(new Predicate<Company>() { | ||
@Override public boolean apply(Company element) { | ||
return element.getName().contains("SmallCo"); | ||
} | ||
}).build(); | ||
IDataView<User> smallCoUserView = userStorage.view().where(new Predicate<User>() { | ||
@Override public boolean apply(User element) { | ||
return element.getUserId().equals("1"); | ||
} | ||
}).build(); | ||
Company company = Queryable.from(smallCoView.toList()).first(); | ||
User user = Queryable.from(smallCoUserView.toList()).first(); | ||
assertThat(company.getUsers(), hasItem(user)); | ||
} | ||
|
||
} |