Skip to content

Commit

Permalink
Merge pull request #39 from OpenSRP/38-refactor-check-has-initialized…
Browse files Browse the repository at this point in the history
…-indicators

38 Refactor check has initialized indicators
  • Loading branch information
allan-on authored Oct 4, 2019
2 parents 69f7886 + 2bdaa9a commit a92fd08
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=0.0.5-SNAPSHOT
VERSION_NAME=0.0.6-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Reporting Library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,21 @@ private boolean hasInitializedIndicators(SQLiteDatabase sqLiteDatabase) {
if (!appOnDebugMode && (!isAppUpdated() || isIndicatorsInitialized())) {
return true;
}
truncateIndicatorDefinitionTables(sqLiteDatabase);
return false;
}

public void truncateIndicatorDefinitionTables(SQLiteDatabase sqLiteDatabase) {
if (sqLiteDatabase != null) {
indicatorRepository.truncateTable(sqLiteDatabase);
indicatorQueryRepository.truncateTable(sqLiteDatabase);
} else {
indicatorRepository.truncateTable();
indicatorQueryRepository.truncateTable();
}
return false;
}

private void readConfigFile(String configFilePath, SQLiteDatabase sqLiteDatabase) {
public void readConfigFile(String configFilePath, SQLiteDatabase sqLiteDatabase) {
initYamlIndicatorConfig();
Iterable<Object> indicatorsFromFile = null;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package org.smartregister.reporting;

import net.sqlcipher.database.SQLiteDatabase;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.reflect.Whitebox;
import org.smartregister.Context;
import org.smartregister.commonregistry.CommonFtsObject;
import org.smartregister.reporting.repository.IndicatorQueryRepository;
import org.smartregister.reporting.repository.IndicatorRepository;
import org.smartregister.repository.Repository;

import static org.junit.Assert.assertNotNull;

@RunWith(PowerMockRunner.class)
@RunWith(MockitoJUnitRunner.class)
public class ReportingLibraryTest {

@Mock
private Context context;

@Mock
private Repository repository;

@Mock
private IndicatorRepository indicatorRepository;

@Mock
private IndicatorQueryRepository indicatorQueryRepository;


@Mock
private CommonFtsObject commonFtsObject;

@Mock
private SQLiteDatabase sqliteDatabase;

private int appVersion = 1;
private int dbVersion = 1;

Expand All @@ -46,4 +64,18 @@ public void testThatAllRepositoriesAreInitialized() {
assertNotNull(reportingLibrary.dailyIndicatorCountRepository());

}

@Test
public void truncateIndicatorTablesWithDBInvokesRepositoryTruncate() {
ReportingLibrary.init(context, repository, commonFtsObject, appVersion, dbVersion);
ReportingLibrary reportingLibrary = ReportingLibrary.getInstance();
ReportingLibrary reportingLibrarySpy = Mockito.spy(reportingLibrary);
// Magic
Whitebox.setInternalState(reportingLibrarySpy, "indicatorRepository", indicatorRepository);
Whitebox.setInternalState(reportingLibrarySpy, "indicatorQueryRepository", indicatorQueryRepository);

reportingLibrarySpy.truncateIndicatorDefinitionTables(sqliteDatabase);
Mockito.verify(indicatorRepository, Mockito.times(1)).truncateTable(sqliteDatabase);
Mockito.verify(indicatorQueryRepository, Mockito.times(1)).truncateTable(sqliteDatabase);
}
}

0 comments on commit a92fd08

Please sign in to comment.