Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano-fiandesio committed Jan 17, 2025
1 parent 4ca0d4b commit 02811d9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ protected String getWhereClause(EventQueryParams params) {
sql += "and ps = '" + params.getProgramStage().getUid() + "' ";
}

// ---------------------------------------------------------------------
// Query items and filters
// ---------------------------------------------------------------------
if (!useExperimentalAnalyticsQueryEngine()) {
sql += getQueryItemsAndFiltersWhereClause(params, hlp);
}

// ---------------------------------------------------------------------
// Filter expression
// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.hisp.dhis.program.ProgramIndicator;
import org.hisp.dhis.program.ProgramIndicatorService;
import org.hisp.dhis.relationship.RelationshipType;
import org.hisp.dhis.setting.SystemSettingsService;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -58,6 +59,7 @@ public class DefaultProgramIndicatorSubqueryBuilder implements ProgramIndicatorS
private static final String SUBQUERY_TABLE_ALIAS = "subax";

private final ProgramIndicatorService programIndicatorService;
private final SystemSettingsService settingsService;

@Override
public String getAggregateClauseForProgramIndicator(
Expand Down Expand Up @@ -247,14 +249,12 @@ private String getWhere(
RelationshipTypeJoinGenerator.generate(
SUBQUERY_TABLE_ALIAS, relationshipType, programIndicator.getAnalyticsType());
} else {
// Remove the reference to the outer query's enrollment
// We'll handle the join in the main query
if (AnalyticsType.ENROLLMENT == programIndicator.getAnalyticsType()) {
// No condition needed, we'll join on enrollment in the main query
condition = "";
} else if (AnalyticsType.EVENT == programIndicator.getAnalyticsType()) {
// Handle event type if needed
condition = "";
if (AnalyticsType.ENROLLMENT == outerSqlEntity) {
condition = useExperimentalAnalyticsQueryEngine() ? "" : "enrollment = ax.enrollment";
} else {
if (AnalyticsType.EVENT == programIndicator.getAnalyticsType()) {
condition = useExperimentalAnalyticsQueryEngine() ? "" : "event = ax.event";
}
}
}

Expand Down Expand Up @@ -285,4 +285,8 @@ private String getProgramIndicatorSql(
latestDate,
SUBQUERY_TABLE_ALIAS);
}

protected boolean useExperimentalAnalyticsQueryEngine() {
return this.settingsService.getCurrentSettings().getUseExperimentalAnalyticsQueryEngine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramIndicator;
import org.hisp.dhis.program.ProgramIndicatorService;
import org.hisp.dhis.setting.SystemSettingsService;
import org.hisp.dhis.system.grid.ListGrid;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -129,9 +130,11 @@ class AbstractJdbcEventAnalyticsManagerTest extends EventAnalyticsTest {

@Mock private OrganisationUnitService organisationUnitService;

@Mock private SystemSettingsService systemSettingsService;

@Spy
private ProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder =
new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService);
new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService, systemSettingsService);

@Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.hisp.dhis.relationship.RelationshipConstraint;
import org.hisp.dhis.relationship.RelationshipEntity;
import org.hisp.dhis.relationship.RelationshipType;
import org.hisp.dhis.setting.SystemSettings;
import org.hisp.dhis.setting.SystemSettingsService;
import org.hisp.dhis.system.grid.ListGrid;
import org.hisp.dhis.test.random.BeanRandomizer;
Expand Down Expand Up @@ -117,6 +118,8 @@ class EnrollmentAnalyticsManagerTest extends EventAnalyticsTest {
private EnrollmentTimeFieldSqlRenderer enrollmentTimeFieldSqlRenderer =
new EnrollmentTimeFieldSqlRenderer(sqlBuilder);

@Spy private SystemSettings systemSettings;

@Captor private ArgumentCaptor<String> sql;

private String DEFAULT_COLUMNS =
Expand All @@ -131,9 +134,9 @@ class EnrollmentAnalyticsManagerTest extends EventAnalyticsTest {
@BeforeEach
public void setUp() {
when(jdbcTemplate.queryForRowSet(anyString())).thenReturn(this.rowSet);

when(systemSettingsService.getCurrentSettings()).thenReturn(systemSettings);
DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder =
new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService);
new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService, systemSettingsService);

subject =
new JdbcEnrollmentAnalyticsManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void setUp() {
EventTimeFieldSqlRenderer timeCoordinateSelector = new EventTimeFieldSqlRenderer(sqlBuilder);
ProgramIndicatorService programIndicatorService = mock(ProgramIndicatorService.class);
DefaultProgramIndicatorSubqueryBuilder programIndicatorSubqueryBuilder =
new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService);
new DefaultProgramIndicatorSubqueryBuilder(programIndicatorService, systemSettingsService);

subject =
new JdbcEventAnalyticsManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,24 @@
import org.hisp.dhis.program.ProgramIndicatorService;
import org.hisp.dhis.relationship.RelationshipEntity;
import org.hisp.dhis.relationship.RelationshipType;
import org.hisp.dhis.setting.SystemSettings;
import org.hisp.dhis.setting.SystemSettingsService;
import org.hisp.dhis.test.random.BeanRandomizer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

/**
* @author Luciano Fiandesio
*/
@ExtendWith(MockitoExtension.class)
@ExtendWith({MockitoExtension.class})
@MockitoSettings(strictness = Strictness.LENIENT)
class ProgramIndicatorSubqueryBuilderTest {
private static final String DUMMY_EXPRESSION = "#{1234567}";

Expand All @@ -70,13 +76,18 @@ class ProgramIndicatorSubqueryBuilderTest {

@Mock private ProgramIndicatorService programIndicatorService;

@Mock private SystemSettingsService systemSettingsService;

@InjectMocks private DefaultProgramIndicatorSubqueryBuilder subject;

@Spy private SystemSettings systemSettings;

@BeforeEach
public void setUp() {
program = createProgram('A');
startDate = getDate(2018, 1, 1);
endDate = getDate(2018, 6, 30);
when(systemSettingsService.getCurrentSettings()).thenReturn(systemSettings);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ void testIsTranslatable() {
@Test
void testKeysWithDefaults() {
Set<String> keys = SystemSettings.keysWithDefaults();
assertEquals(139, keys.size());
assertEquals(140, keys.size());
// just check some at random
assertTrue(keys.contains("syncSkipSyncForDataChangedBefore"));
assertTrue(keys.contains("keyTrackerDashboardLayout"));
assertTrue(keys.contains("experimentalAnalyticsSqlEngineEnabled"));
}

@Test
Expand Down

0 comments on commit 02811d9

Please sign in to comment.