Skip to content

Commit

Permalink
update assertThrowsExactly tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-super committed Oct 17, 2024
1 parent b9cfd89 commit 9aa86c8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,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.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SuppressWarnings("SqlSourceToSinkFlow")
Expand Down Expand Up @@ -1492,7 +1492,11 @@ void sameValid_at() throws SQLException {
insertExternalSource(a);

// second source should fail
assertThrowsExactly(PSQLException.class, () -> insertExternalSource(b));
final SQLException ex = assertThrows(PSQLException.class, () -> insertExternalSource(b));
if (!ex.getSQLState().equals("23505")
&& !ex.getMessage().contains("duplicate key value violates unique constraint \"dg_unique_valid_at\"")) {
throw ex;
}
}

/**
Expand Down Expand Up @@ -1601,7 +1605,11 @@ void noDuplicateEventsInSameSource() throws SQLException {
insertExternalEvent(e1);

// but fails on collision!
assertThrowsExactly(PSQLException.class, () -> insertExternalEvent(e2));
final SQLException ex = assertThrows(PSQLException.class, () -> insertExternalEvent(e2));
if (!ex.getSQLState().equals("23505")
&& !ex.getMessage().contains("duplicate key value violates unique constraint \"external_event_pkey\"")) {
throw ex;
}
}

/**
Expand Down Expand Up @@ -1649,10 +1657,20 @@ void endTimeGEstartTime() throws SQLException {
insertDerivationGroup(failing.derivation_group_name(), failing.source_type_name());

// if start time > end time, error
assertThrowsExactly(PSQLException.class, () -> insertExternalSource(failing));
final SQLException ex = assertThrows(PSQLException.class, () -> insertExternalSource(failing));
if (!ex.getSQLState().equals("23514")
&& !ex.getMessage().contains("new row for relation \"external_source\" violates check constraint "
+ "\"external_source_check\"")) {
throw ex;
}

// if start time = end time, error
assertThrowsExactly(PSQLException.class, () -> insertExternalSource(failing2));
final SQLException ex2 = assertThrows(PSQLException.class, () -> insertExternalSource(failing2));
if (!ex.getSQLState().equals("23514")
&& !ex.getMessage().contains("new row for relation \"external_source\" violates check constraint "
+ "\"external_source_check\"")) {
throw ex;
}

// else, no error
assertDoesNotThrow(() -> insertExternalSource(succeeding));
Expand Down Expand Up @@ -1699,13 +1717,25 @@ void externalEventSourceBounds() throws SQLException {
insertExternalEvent(legal);

// assert out of bounds failures
assertThrowsExactly(PSQLException.class, () -> insertExternalEvent(completelyBefore));
final SQLException ex = assertThrows(PSQLException.class, () -> insertExternalEvent(completelyBefore));
if (!ex.getMessage().contains("Event " + completelyBefore.key + " out of bounds of source " + A.key + ".")) {
throw ex;
}

assertThrowsExactly(PSQLException.class, () -> insertExternalEvent(beforeIntersect));
final SQLException ex2 = assertThrows(PSQLException.class, () -> insertExternalEvent(beforeIntersect));
if (!ex2.getMessage().contains("Event " + beforeIntersect.key + " out of bounds of source " + A.key + ".")) {
throw ex2;
}

assertThrowsExactly(PSQLException.class, () -> insertExternalEvent(afterIntersect));
final SQLException ex3 = assertThrows(PSQLException.class, () -> insertExternalEvent(afterIntersect));
if (!ex3.getMessage().contains("Event " + afterIntersect.key + " out of bounds of source " + A.key + ".")) {
throw ex3;
}

assertThrowsExactly(PSQLException.class, () -> insertExternalEvent(completelyAfter));
final SQLException ex4 = assertThrows(PSQLException.class, () -> insertExternalEvent(completelyAfter));
if (!ex4.getMessage().contains("Event " + completelyAfter.key + " out of bounds of source " + A.key + ".")) {
throw ex4;
}
}

/**
Expand Down Expand Up @@ -1739,7 +1769,11 @@ void duplicateSource() throws SQLException {
upload_source(dg);

// upload a conflicting source (same name in a given dg)
assertThrowsExactly(PSQLException.class, () -> insertExternalSource(failing));
final SQLException ex = assertThrows(PSQLException.class, () -> insertExternalSource(failing));
if (!ex.getSQLState().equals("23505")
&& !ex.getMessage().contains("duplicate key value violates unique constraint \"external_source_pkey\"")) {
throw ex;
}

// upload a non-conflicting source (same name in a different dg)
insertDerivationGroup(dg + "_2", st);
Expand All @@ -1755,11 +1789,13 @@ void duplicatedDG() throws SQLException {
upload_source(dg);
insertExternalSourceType("New Name");

assertThrowsExactly(PSQLException.class, () -> {
// use the same name as before (Test Default) with a different source type (New Name) - fails
// (This is noteworthy as this is newer behavior.)
insertDerivationGroup(dg, "New Name");
});
// use the same name as before (Test Default) with a different source type (New Name) - fails
// (This is noteworthy as this is newer behavior.)
final SQLException ex = assertThrows(PSQLException.class, () -> insertDerivationGroup(dg, "New Name"));
if (!ex.getSQLState().equals("23505")
&& !ex.getMessage().contains("duplicate key value violates unique constraint \"derivation_group_pkey\"")) {
throw ex;
}
}

/**
Expand Down Expand Up @@ -1792,14 +1828,21 @@ void deleteDGwithRemainingSource() throws SQLException {
insertExternalSource(src);

// delete the DG (expect error)
assertThrowsExactly(PSQLException.class,
final SQLException ex = assertThrows(PSQLException.class,
() -> statement.executeUpdate(
// language=sql
"""
DELETE FROM merlin.derivation_group WHERE name='%s';
""".formatted(dg)
)
);
if (!ex.getSQLState().equals("23503") &&
!ex.getMessage().contains(
"update or delete on table \"derivation_group\" violates foreign key constraint "
+ "\"external_source_type_matches_derivation_group\" on table \"external_source\"")
) {
throw ex;
}
}
}

Expand Down Expand Up @@ -1842,8 +1885,19 @@ void externalSourceTypeMatchDerivationGroup() throws SQLException {
mt
);

// delete the source type (expect error)
assertThrowsExactly(PSQLException.class, () -> insertExternalSource(src_2));
// insert the erroneous source (expect error)
final SQLException ex = assertThrows(
SQLException.class,
() -> insertExternalSource(src_2)
);
if (!ex.getSQLState().equals("23503") &&
!ex.getMessage().contains(
"ERROR: External source " + src_2.key + " is being added to a derivation group " +
src_2.derivation_group_name + " where its type " + src_2.source_type_name +
" does not match the derivation group type " + src.source_type_name + ".")
) {
throw ex;
}
}

/**
Expand Down Expand Up @@ -1876,13 +1930,20 @@ void deleteSourceTypeWithRemainingSource() throws SQLException {
insertExternalSource(src);

// delete the source type (expect error)
assertThrowsExactly(PSQLException.class, () -> statement.executeUpdate(
final SQLException ex = assertThrows(PSQLException.class, () -> statement.executeUpdate(
// language=sql
"""
DELETE FROM merlin.external_source_type WHERE name='%s';
""".formatted(st)
)
);
if (!ex.getSQLState().equals("23503")
&& !ex.getMessage().contains(
"update or delete on table \"external_source_type\" violates foreign key constraint "
+ "\"derivation_group_references_external_source_type\" on table \"derivation_group\"")
) {
throw ex;
}
}
}

Expand Down Expand Up @@ -1917,14 +1978,21 @@ void deleteEventTypeWithRemainingEvent() throws SQLException {
insertExternalEvent(evt);

// delete the event type (expect error)
assertThrowsExactly(PSQLException.class,
final SQLException ex = assertThrows(PSQLException.class,
() -> statement.executeUpdate(
// language=sql
"""
DELETE FROM merlin.external_event_type WHERE name='%s';
""".formatted(et)
)
);
if (!ex.getSQLState().equals("23503")
&& !ex.getMessage().contains(
"update or delete on table \"external_event_type\" violates foreign key constraint "
+ "\"external_event_references_event_type_name\" on table \"external_event\"")
) {
throw ex;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ begin
event_start := new.start_time;
event_end := new.start_time + new.duration;
if event_start < source_start or event_end > source_end then
raise exception 'Event %s out of bounds of source %s', new.key, new.source_key;
raise exception 'Event % out of bounds of source %', new.key, new.source_key;
end if;
return new;
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ begin
event_start := new.start_time;
event_end := new.start_time + new.duration;
if event_start < source_start or event_end > source_end then
raise exception 'Event %s out of bounds of source %s', new.key, new.source_key;
raise exception 'Event % out of bounds of source %.', new.key, new.source_key;
end if;
return new;
end;
Expand Down

0 comments on commit 9aa86c8

Please sign in to comment.