Skip to content

Commit

Permalink
Tests for diff DataCollections
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Malevaniy committed Mar 18, 2015
1 parent 26d4f46 commit f88f8de
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
59 changes: 59 additions & 0 deletions library/src/androidTest/java/io/techery/snapper/model/Company.java
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;
}
}
15 changes: 15 additions & 0 deletions library/src/androidTest/java/io/techery/snapper/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public User(String userId, int age) {
this.age = age;
}

@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

User user = (User) o;

if (userId != null ? !userId.equals(user.userId) : user.userId != null) return false;

return true;
}

@Override public int hashCode() {
return userId != null ? userId.hashCode() : 0;
}

public String getUserId() {
return userId;
}
Expand Down
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));
}

}

0 comments on commit f88f8de

Please sign in to comment.