import dao.ContactDao;
import dao.DataAccessException;
import models.DaoManager;
import models.entities.Contact;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.*;
import static play.test.Helpers.*;
/**
* Created by kiryl on 18.08.2014.
*/
public class DaoTest {
@Test
public void baseDaoTest() {
running(fakeApplication(inMemoryDatabase()), new Runnable() {
public void run() {
try {
ContactDao dao = DaoManager.getContactDao();
Contact c1 = new Contact();
c1.setFirstName("Jack");
c1.setLastName("Jones");
dao.persist(c1);
assertNotNull("saved contact id is null", c1.getId());
Contact c2 = dao.findById(c1.getId());
assertEquals("names are not equal", c2.getFirstName(), c1.getFirstName());
dao.remove(c2);
Contact c3 = dao.findById(c1.getId());
assertNull("removed contact is not null", c3);
} catch (DataAccessException e) {
e.printStackTrace();
}
}
});
}
@Test
public void getBatchTest() {
running(fakeApplication(inMemoryDatabase()), new Runnable() {
public void run() {
ContactDao dao = DaoManager.getContactDao();
for (int i = 0; i < 18; i++) {
Contact c = new Contact();
c.setFirstName("Contact" + i);
c.setLastName("Jones" + i);
try {
dao.persist(c);
} catch (DataAccessException e) {
e.printStackTrace();
}
}
List<Contact> batch1 = null;
List<Contact> batch2 = null;
List<Contact> batch3 = null;
try {
batch1 = dao.getBatch(0, 10);
batch2 = dao.getBatch(1, 10);
batch3 = dao.getBatch(4, 10);
} catch (DataAccessException e) {
e.printStackTrace();
}
assertTrue(batch1.size() == 10);
assertTrue(batch2.size() == 8);
assertTrue(batch3.size() == 0);
}
});
}
}