|
31 | 31 | import static org.hamcrest.MatcherAssert.assertThat; |
32 | 32 | import static org.hamcrest.Matchers.containsString; |
33 | 33 | import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.hamcrest.Matchers.hasItem; |
34 | 35 | import static org.hamcrest.Matchers.hasSize; |
35 | 36 | import static org.hamcrest.Matchers.is; |
| 37 | +import static org.hamcrest.Matchers.not; |
36 | 38 | import static org.hamcrest.Matchers.notNullValue; |
37 | 39 | import static org.hisp.dhis.DhisConvenienceTest.createCategory; |
38 | 40 | import static org.hisp.dhis.DhisConvenienceTest.createCategoryCombo; |
@@ -1168,6 +1170,80 @@ void verifyTeaTypeOrgUnitFetchesOuNameWhenPopulatingEventAnalyticsTable() { |
1168 | 1170 | assertThat(sql.getValue(), containsString(String.format(ouQuery, "name", quote(tea.getUid())))); |
1169 | 1171 | } |
1170 | 1172 |
|
| 1173 | + @Test |
| 1174 | + @DisplayName("removeUpdatedData deletes from the live table, not the staging table") |
| 1175 | + void removeUpdatedDataTargetsLiveTable() { |
| 1176 | + ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class); |
| 1177 | + Program program = createProgram('A'); |
| 1178 | + |
| 1179 | + Date lastFullTableUpdate = new DateTime(2019, 3, 1, 2, 0).toDate(); |
| 1180 | + Date lastLatestPartitionUpdate = new DateTime(2019, 3, 1, 9, 0).toDate(); |
| 1181 | + Date startTime = new DateTime(2019, 3, 1, 10, 0).toDate(); |
| 1182 | + |
| 1183 | + AnalyticsTableUpdateParams params = |
| 1184 | + AnalyticsTableUpdateParams.newBuilder() |
| 1185 | + .withStartTime(startTime) |
| 1186 | + .withLatestPartition() |
| 1187 | + .build(); |
| 1188 | + |
| 1189 | + List<Map<String, Object>> queryResp = new ArrayList<>(); |
| 1190 | + queryResp.add(Map.of("eventid", 1)); |
| 1191 | + |
| 1192 | + when(systemSettingManager.getDateSetting(SettingKey.LAST_SUCCESSFUL_ANALYTICS_TABLES_UPDATE)) |
| 1193 | + .thenReturn(lastFullTableUpdate); |
| 1194 | + when(systemSettingManager.getDateSetting( |
| 1195 | + SettingKey.LAST_SUCCESSFUL_LATEST_ANALYTICS_PARTITION_UPDATE)) |
| 1196 | + .thenReturn(lastLatestPartitionUpdate); |
| 1197 | + when(jdbcTemplate.queryForList(Mockito.anyString())).thenReturn(queryResp); |
| 1198 | + when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(program)); |
| 1199 | + |
| 1200 | + List<AnalyticsTable> tables = subject.getAnalyticsTables(params); |
| 1201 | + assertThat(tables, hasSize(1)); |
| 1202 | + |
| 1203 | + subject.removeUpdatedData(tables); |
| 1204 | + |
| 1205 | + verify(jdbcTemplate).execute(sql.capture()); |
| 1206 | + |
| 1207 | + String mainTableName = TABLE_PREFIX + program.getUid().toLowerCase(); |
| 1208 | + assertThat(sql.getValue(), containsString(quote(mainTableName))); |
| 1209 | + assertThat(sql.getValue(), not(containsString(quote(mainTableName + STAGING_TABLE_SUFFIX)))); |
| 1210 | + } |
| 1211 | + |
| 1212 | + @Test |
| 1213 | + @DisplayName("getRegularAnalyticsTables excludes programs listed in skipPrograms") |
| 1214 | + void getRegularAnalyticsTablesExcludesSkippedPrograms() { |
| 1215 | + Program prA = createProgram('A'); |
| 1216 | + Program prB = createProgram('B'); |
| 1217 | + Program prC = createProgram('C'); |
| 1218 | + Program prD = createProgram('D'); |
| 1219 | + |
| 1220 | + Set<String> skipPrograms = new HashSet<>(); |
| 1221 | + skipPrograms.add(prC.getUid()); |
| 1222 | + skipPrograms.add(prD.getUid()); |
| 1223 | + |
| 1224 | + AnalyticsTableUpdateParams params = |
| 1225 | + AnalyticsTableUpdateParams.newBuilder() |
| 1226 | + .withLastYears(2) |
| 1227 | + .withStartTime(START_TIME) |
| 1228 | + .withToday(today) |
| 1229 | + .withSkipPrograms(skipPrograms) |
| 1230 | + .build(); |
| 1231 | + |
| 1232 | + when(idObjectManager.getAllNoAcl(Program.class)).thenReturn(List.of(prA, prB, prC, prD)); |
| 1233 | + when(periodDataProvider.getAvailableYears(DATABASE)) |
| 1234 | + .thenReturn(List.of(2018, 2019, now().getYear())); |
| 1235 | + when(jdbcTemplate.queryForList(Mockito.anyString(), Mockito.eq(Integer.class))) |
| 1236 | + .thenReturn(List.of(2018, 2019)); |
| 1237 | + |
| 1238 | + List<AnalyticsTable> tables = subject.getAnalyticsTables(params); |
| 1239 | + |
| 1240 | + assertThat(tables, hasSize(2)); |
| 1241 | + |
| 1242 | + List<String> programUids = tables.stream().map(t -> t.getProgram().getUid()).toList(); |
| 1243 | + assertThat(programUids, not(hasItem(prC.getUid()))); |
| 1244 | + assertThat(programUids, not(hasItem(prD.getUid()))); |
| 1245 | + } |
| 1246 | + |
1171 | 1247 | private String toSelectExpression(String template, String uid) { |
1172 | 1248 | return String.format(template, uid, uid, uid); |
1173 | 1249 | } |
|
0 commit comments