Skip to content

Commit

Permalink
✅ Add EventDao tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allan-on committed Sep 16, 2021
1 parent 231e053 commit 2ba9736
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void markEventsForReUpload() {
* @return
*/
public static Long getMinimumVerifiedServerVersion() {
String sql = "select max(serverVersion) serverVersion from event where dateCreated <= (select min(dateCreated) minDate from event where ifnull(eventId,'') = '' and syncStatus = 'Synced') ";
String sql = "select max(serverVersion) serverVersion from event where dateCreated <= (select min(dateCreated) minDate from event where ifnull(eventId,'') = '' and syncStatus = 'Synced') ";
DataMap<Long> dataMap = c -> {
return getCursorLongValue(c, "serverVersion");
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.robolectric.RobolectricTestRunner;
import org.smartregister.clientandeventmodel.Event;
import org.smartregister.dao.AbstractDao;
import org.smartregister.repository.BaseRepository;
import org.smartregister.repository.Repository;

import java.util.Arrays;
Expand All @@ -37,7 +38,7 @@ public void setUp() {
}

@Test
public void getEvents() {
public void getEventsReturnsCorrectEvents() {
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"json"});
matrixCursor.addRow(new Object[]{eventJson});
Mockito.doReturn(matrixCursor).when(database).rawQuery(Mockito.any(), Mockito.any());
Expand All @@ -48,13 +49,31 @@ public void getEvents() {
}

@Test
public void getLatestEvent() {
public void getLatestEventReturnsCorrectEvent() {
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"json"});
matrixCursor.addRow(new Object[]{eventJson});
Mockito.doReturn(matrixCursor).when(database).rawQuery(Mockito.any(), Mockito.any());
Event event = EventDao.getLatestEvent("some-base-entity-id", Arrays.asList("some-event-type", "another-event-type"));
Assert.assertNotNull(event);
}

@Test
public void markEventsForReUploadExecutesCorrectUpdateQueries() {
Mockito.doReturn(database).when(repository).getWritableDatabase();
EventDao.markEventsForReUpload();
Mockito.verify(database).rawExecSQL("update client set syncStatus = '" + BaseRepository.TYPE_Unsynced + "' where baseEntityId in (select baseEntityId from event where ifnull(eventId,'') = '') ");
Mockito.verify(database).rawExecSQL("update ImageList set syncStatus = '" + BaseRepository.TYPE_Unsynced + "' where entityId in (select baseEntityId from event where ifnull(eventId,'') = '') ");
Mockito.verify(database).rawExecSQL("update event set syncStatus = '" + BaseRepository.TYPE_Unsynced + "' where ifnull(eventId,'') = '' ");
}

@Test
public void getMinimumVerifiedServerVersionReturnsCorrectVersion() {
MatrixCursor matrixCursor = new MatrixCursor(new String[]{"serverVersion"});
matrixCursor.addRow(new Object[]{12324567L});
Mockito.doReturn(matrixCursor).when(database).rawQuery(Mockito.any(), Mockito.any());
Long serverVersion = EventDao.getMinimumVerifiedServerVersion();
Assert.assertNotNull(serverVersion);
Assert.assertTrue(serverVersion.equals(12324567L));
}

}

0 comments on commit 2ba9736

Please sign in to comment.