Skip to content

Commit

Permalink
fix pdg test
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-super committed Oct 21, 2024
1 parent 26ab129 commit b95fb3b
Showing 1 changed file with 81 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -19,7 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -70,7 +69,7 @@ protected record ExternalEvent(String key, String event_type_name, String source
}
protected record ExternalSource(String key, String source_type_name, String derivation_group_name, String valid_at, String start_time, String end_time, String created_at){}
protected record DerivedEvent(String key, String event_type_name, String source_key, String derivation_group_name, String start_time, String duration, String source_range, String valid_at){}
protected record PlanDerivationGroup(int plan_id, String derivation_group_name, boolean acknowledged){}
protected record PlanDerivationGroup(int plan_id, String derivation_group_name, boolean acknowledged, String last_acknowledged_at){}
//endregion


Expand Down Expand Up @@ -2108,9 +2107,45 @@ void deleteEventTypeWithRemainingEvent() throws SQLException {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class PlanDerivationGroupTests {
List<PlanDerivationGroup> getPlanDerivationGroupAssociations() throws SQLException {
List<PlanDerivationGroup> results = new ArrayList<>();

try (final var statement = connection.createStatement()) {
// create the event type
final var res = statement.executeQuery(
// language=sql
"""
SELECT * FROM merlin.plan_derivation_group ORDER BY plan_id, derivation_group_name;
"""
);

while (res.next()) {
results.add(
new PlanDerivationGroup(
res.getInt("plan_id"),
res.getString("derivation_group_name"),
res.getBoolean("acknowledged"),
res.getString("last_acknowledged_at")
)
);
}
}

return results;
}

void assertEqualsAsideFromLastAcknowledged(List<PlanDerivationGroup> expected, List<PlanDerivationGroup> actual) {
assertEquals(expected.size(), actual.size());
for (int i = 0; i < actual.size(); i++) {
assertEquals(expected.get(i).plan_id(), actual.get(i).plan_id());
assertEquals(expected.get(i).derivation_group_name(), actual.get(i).derivation_group_name());
assertEquals(expected.get(i).acknowledged(), actual.get(i).acknowledged());
}
}

/**
* This test ensures that a derivation group can be associated with a plan, specifically checking that when the
* "acknowledged" field is set, "last_acknowledged_at" is updated, but no other derivation groups or plans
* "acknowledged" field is set, "last_acknowledged_at" is not updated, but no other derivation groups or plans
* (and their associations are affected)
*/
@Test
Expand All @@ -2134,36 +2169,16 @@ void associateDerivationGroupWithBasePlan() throws SQLException {
assertDoesNotThrow(() -> associateDerivationGroupWithPlan(planIdDelta, derivationGroupNameControl));
assertDoesNotThrow(() -> associateDerivationGroupWithPlan(planIdDelta, derivationGroupNameDelta));

// check that acknowledged is true for all entries
try (final var statement = connection.createStatement()) {
// create the event type
var res = statement.executeQuery(
// language=sql
"""
SELECT * FROM merlin.plan_derivation_group;
"""
);

List<PlanDerivationGroup> results = new ArrayList<>();
while (res.next()) {
results.add(
new PlanDerivationGroup(
res.getInt("plan_id"),
res.getString("derivation_group_name"),
res.getBoolean("acknowledged")
)
);
}
// check that acknowledged is true for all entries, and save the last_acknowledged_at field results
List<PlanDerivationGroup> expectedResultsInitial = List.of(
new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true, ""),
new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true, ""),
new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, true, "")
);
final List<PlanDerivationGroup> actualResultsInitial = getPlanDerivationGroupAssociations();

List<PlanDerivationGroup> expectedResults = List.of(
new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true),
new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true),
new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, true)
);

assertEquals(expectedResults.size(), results.size());
assertTrue(results.containsAll(expectedResults));
}
// first, check other properties
assertEqualsAsideFromLastAcknowledged(expectedResultsInitial, actualResultsInitial);

// insert a source to the changing derivation group
insertExternalSource(
Expand All @@ -2179,35 +2194,46 @@ void associateDerivationGroupWithBasePlan() throws SQLException {
);

// check that acknowledged is now false for all non control (delta only) entries, true otherwise
List<PlanDerivationGroup> expectedResults = List.of(
new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true, actualResultsInitial.getFirst()
.last_acknowledged_at()),
new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true, actualResultsInitial.get(1)
.last_acknowledged_at()),
new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, false, actualResultsInitial.get(2)
.last_acknowledged_at())
);
final List<PlanDerivationGroup> actualResults = getPlanDerivationGroupAssociations();

// first, check other properties
assertEquals(expectedResults.size(), actualResults.size());
assertTrue(actualResults.containsAll(expectedResults));

// final bit - update acknowledged to true for (planIdDelta, derivationGroupNameDelta) pair, see that
// last_acknowledged_at updates
try (final var statement = connection.createStatement()) {
// create the event type
var res = statement.executeQuery(
// update acknowledged field for (planIdDelta, derivationGroupNameDelta) pair
statement.executeUpdate(
// language=sql
"""
SELECT * FROM merlin.plan_derivation_group;
"""
UPDATE merlin.plan_derivation_group
SET acknowledged = true
WHERE (plan_id, derivation_group_name) = (%d, '%s');
""".formatted(planIdDelta, derivationGroupNameDelta)
);
}

List<PlanDerivationGroup> results = new ArrayList<>();
while (res.next()) {
results.add(
new PlanDerivationGroup(
res.getInt("plan_id"),
res.getString("derivation_group_name"),
res.getBoolean("acknowledged")
)
);
}
final List<PlanDerivationGroup> postUpdateResults = getPlanDerivationGroupAssociations();

List<PlanDerivationGroup> expectedResults = List.of(
new PlanDerivationGroup(planIdControl, derivationGroupNameControl, true),
new PlanDerivationGroup(planIdDelta, derivationGroupNameControl, true),
new PlanDerivationGroup(planIdDelta, derivationGroupNameDelta, false)
);
// check timestamps
assertEquals(postUpdateResults.getFirst(), actualResultsInitial.getFirst());
assertEquals(postUpdateResults.get(1), actualResultsInitial.get(1));

assertEquals(expectedResults.size(), results.size());
assertTrue(results.containsAll(expectedResults));
}
// comparing strings. The post update last_acknowledged_at should be later than the pre update one.
assertTrue(postUpdateResults.get(2).last_acknowledged_at().compareTo(
actualResultsInitial.get(2).last_acknowledged_at()) > 0);

// check the other properties
assertEqualsAsideFromLastAcknowledged(actualResultsInitial, postUpdateResults);
}
}
//endregion
Expand Down

0 comments on commit b95fb3b

Please sign in to comment.