|
34 | 34 | import static org.hamcrest.MatcherAssert.assertThat; |
35 | 35 | import static org.hamcrest.Matchers.containsString; |
36 | 36 | import static org.hamcrest.Matchers.equalTo; |
| 37 | +import static org.hamcrest.Matchers.hasItem; |
37 | 38 | import static org.hamcrest.Matchers.hasSize; |
38 | 39 | import static org.hamcrest.Matchers.is; |
| 40 | +import static org.hamcrest.Matchers.not; |
39 | 41 | import static org.hamcrest.Matchers.notNullValue; |
40 | 42 | import static org.hisp.dhis.db.model.DataType.BIGINT; |
41 | 43 | import static org.hisp.dhis.db.model.DataType.CHARACTER_11; |
|
112 | 114 | import org.hisp.dhis.trackedentity.TrackedEntityAttribute; |
113 | 115 | import org.joda.time.DateTime; |
114 | 116 | import org.junit.jupiter.api.BeforeEach; |
| 117 | +import org.junit.jupiter.api.DisplayName; |
115 | 118 | import org.junit.jupiter.api.Test; |
116 | 119 | import org.junit.jupiter.api.extension.ExtendWith; |
117 | 120 | import org.mockito.ArgumentCaptor; |
@@ -1091,6 +1094,77 @@ void verifyTeaTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() { |
1091 | 1094 | assertThat(sql.getValue(), containsString(ouNameQuery)); |
1092 | 1095 | } |
1093 | 1096 |
|
| 1097 | + @Test |
| 1098 | + @DisplayName("removeUpdatedData deletes from the live table, not the staging table") |
| 1099 | + void removeUpdatedDataTargetsLiveTable() { |
| 1100 | + ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class); |
| 1101 | + Program program = createProgram('A'); |
| 1102 | + program.setProgramType(WITH_REGISTRATION); |
| 1103 | + |
| 1104 | + Date lastFullTableUpdate = new DateTime(2019, 3, 1, 2, 0).toDate(); |
| 1105 | + Date lastLatestPartitionUpdate = new DateTime(2019, 3, 1, 9, 0).toDate(); |
| 1106 | + Date startTime = new DateTime(2019, 3, 1, 10, 0).toDate(); |
| 1107 | + |
| 1108 | + AnalyticsTableUpdateParams params = |
| 1109 | + AnalyticsTableUpdateParams.newBuilder().startTime(startTime).build().withLatestPartition(); |
| 1110 | + |
| 1111 | + List<Map<String, Object>> queryResp = new ArrayList<>(); |
| 1112 | + queryResp.add(Map.of("eventid", 1)); |
| 1113 | + |
| 1114 | + when(settings.getLastSuccessfulAnalyticsTablesUpdate()).thenReturn(lastFullTableUpdate); |
| 1115 | + when(settings.getLastSuccessfulLatestAnalyticsPartitionUpdate()) |
| 1116 | + .thenReturn(lastLatestPartitionUpdate); |
| 1117 | + when(jdbcTemplate.queryForList(Mockito.anyString())).thenReturn(queryResp); |
| 1118 | + when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(program)); |
| 1119 | + whenConfigurationPeriodSettings(); |
| 1120 | + |
| 1121 | + List<AnalyticsTable> tables = subject.getAnalyticsTables(params); |
| 1122 | + assertThat(tables, hasSize(1)); |
| 1123 | + |
| 1124 | + subject.removeUpdatedData(tables); |
| 1125 | + |
| 1126 | + verify(jdbcTemplate).execute(sql.capture()); |
| 1127 | + |
| 1128 | + String mainTableName = TABLE_PREFIX + program.getUid().toLowerCase(); |
| 1129 | + assertThat(sql.getValue(), containsString(quote(mainTableName))); |
| 1130 | + assertThat(sql.getValue(), not(containsString(quote(mainTableName + STAGING_TABLE_SUFFIX)))); |
| 1131 | + } |
| 1132 | + |
| 1133 | + @Test |
| 1134 | + @DisplayName("getRegularAnalyticsTables excludes programs listed in skipPrograms") |
| 1135 | + void getRegularAnalyticsTablesExcludesSkippedPrograms() { |
| 1136 | + Program prA = createProgram('A'); |
| 1137 | + Program prB = createProgram('B'); |
| 1138 | + Program prC = createProgram('C'); |
| 1139 | + Program prD = createProgram('D'); |
| 1140 | + |
| 1141 | + Set<String> skipPrograms = new HashSet<>(); |
| 1142 | + skipPrograms.add(prC.getUid()); |
| 1143 | + skipPrograms.add(prD.getUid()); |
| 1144 | + |
| 1145 | + AnalyticsTableUpdateParams params = |
| 1146 | + AnalyticsTableUpdateParams.newBuilder() |
| 1147 | + .lastYears(2) |
| 1148 | + .startTime(START_TIME) |
| 1149 | + .today(today) |
| 1150 | + .skipPrograms(skipPrograms) |
| 1151 | + .build(); |
| 1152 | + |
| 1153 | + when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(prA, prB, prC, prD)); |
| 1154 | + mockPeriodYears(List.of(2018, 2019, now().getYear())); |
| 1155 | + whenConfigurationPeriodSettings(); |
| 1156 | + when(jdbcTemplate.queryForList(Mockito.anyString(), Mockito.eq(Integer.class))) |
| 1157 | + .thenReturn(List.of(2018, 2019)); |
| 1158 | + |
| 1159 | + List<AnalyticsTable> tables = subject.getAnalyticsTables(params); |
| 1160 | + |
| 1161 | + assertThat(tables, hasSize(2)); |
| 1162 | + |
| 1163 | + List<String> programUids = tables.stream().map(t -> t.getProgram().getUid()).toList(); |
| 1164 | + assertThat(programUids, not(hasItem(prC.getUid()))); |
| 1165 | + assertThat(programUids, not(hasItem(prD.getUid()))); |
| 1166 | + } |
| 1167 | + |
1094 | 1168 | private String toSelectExpression(String template, String uid) { |
1095 | 1169 | return String.format(template, uid, uid, uid); |
1096 | 1170 | } |
|
0 commit comments