More DAO tests + fixes for discovered bugs.

This commit is contained in:
neviyn 2018-09-23 21:22:48 +01:00
parent 876a09bc85
commit 3dc6e6e10b
7 changed files with 122 additions and 10 deletions

View File

@ -12,5 +12,9 @@ database:
server:
rootPath: /api/
requestLog:
appenders:
- type: console
queueSize: 2048
adminPassword: "testPassword"

View File

@ -37,9 +37,4 @@ public class Tutor extends BaseEntity implements Serializable {
inverseJoinColumns = { @JoinColumn(name = "observation_id")}
)
private Set<Observation> observations;
public Tutor(long id, String name) {
super(id);
this.name = name;
}
}

View File

@ -16,7 +16,7 @@ public class TutorDao extends AbstractDAO<Tutor> {
}
public List<Tutor> listAll(){
return list(criteriaQuery());
return currentSession().createQuery("from Tutor", Tutor.class).list();
}
public Tutor persist(Tutor tutor){

View File

@ -0,0 +1,12 @@
package uk.co.neviyn.Observations.dao;
import io.dropwizard.testing.junit.DAOTestRule;
import org.junit.Rule;
import uk.co.neviyn.Observations.core.Observation;
import uk.co.neviyn.Observations.core.Site;
import uk.co.neviyn.Observations.core.Tutor;
public abstract class DaoTestBase {
@Rule
public DAOTestRule testRule = DAOTestRule.newBuilder().addEntityClass(Observation.class).addEntityClass(Site.class).addEntityClass(Tutor.class).build();
}

View File

@ -15,10 +15,7 @@ import java.util.List;
import static org.junit.Assert.*;
public class ObservationDaoTest {
@Rule
public DAOTestRule testRule = DAOTestRule.newBuilder().addEntityClass(Observation.class).addEntityClass(Site.class).addEntityClass(Tutor.class).build();
public class ObservationDaoTest extends DaoTestBase {
private ObservationDao dao;
private SiteDao siteDao;
@ -41,6 +38,7 @@ public class ObservationDaoTest {
Observation insert = testRule.inTransaction(() -> dao.persist(observation));
assertNotNull(insert);
assertTrue(insert.getId() > 0);
assertNotNull(dao.get(insert.getId()));
}
@Test
@ -57,6 +55,13 @@ public class ObservationDaoTest {
assertEquals(numberOfObservations, observations.size());
}
@Test
public void listAllWithNoObservations() {
List<Observation> observations = dao.listAll();
assertNotNull(observations);
assertEquals(0, observations.size());
}
@Test
public void averageStatsForAll() {
Observation otherEntry = SerializationUtils.clone(observation);

View File

@ -0,0 +1,45 @@
package uk.co.neviyn.Observations.dao;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import uk.co.neviyn.Observations.core.Site;
import static org.junit.Assert.*;
public class SiteDaoTest extends DaoTestBase{
private SiteDao dao;
@Before
public void setUp() {
dao = new SiteDao(testRule.getSessionFactory());
}
@Test
public void listAll() {
testRule.inTransaction(() -> {
dao.persist(new Site("Site A"));
dao.persist(new Site("Site B"));
dao.persist(new Site("Site C"));
});
List<Site> result = dao.listAll();
assertNotNull(result);
assertEquals(3, result.size());
}
@Test
public void listAllWithNoObservations() {
List<Site> sites = dao.listAll();
assertNotNull(sites);
assertEquals(0, sites.size());
}
@Test
public void persistAndGet() {
Site insert = testRule.inTransaction(() -> dao.persist(new Site("Test Site")));
assertNotNull(insert);
assertTrue(insert.getId() > 0);
assertNotNull(dao.get(insert.getId()));
}
}

View File

@ -0,0 +1,51 @@
package uk.co.neviyn.Observations.dao;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import uk.co.neviyn.Observations.core.Observation;
import uk.co.neviyn.Observations.core.Site;
import uk.co.neviyn.Observations.core.Tutor;
import static org.junit.Assert.*;
public class TutorDaoTest extends DaoTestBase{
private TutorDao dao;
private SiteDao siteDao;
private final Site site = new Site("Test site");
@Before
public void setUp() {
dao = new TutorDao(testRule.getSessionFactory());
siteDao = new SiteDao(testRule.getSessionFactory());
testRule.inTransaction(() -> siteDao.persist(site));
}
@Test
public void listAll() {
testRule.inTransaction(() -> {
dao.persist(new Tutor("Mr Test", site, null));
dao.persist(new Tutor("Mr Test2", site, null));
dao.persist(new Tutor("Mr Test3", site, null));
});
List<Tutor> result = dao.listAll();
assertNotNull(result);
assertEquals(3, result.size());
}
@Test
public void listAllWithNoObservations() {
List<Tutor> tutors = dao.listAll();
assertNotNull(tutors);
assertEquals(0, tutors.size());
}
@Test
public void persistAndGet() {
Tutor insert = testRule.inTransaction(() -> dao.persist(new Tutor("Mr Test", site, null)));
assertNotNull(insert);
assertTrue(insert.getId() > 0);
assertNotNull(dao.get(insert.getId()));
}
}