From 9aa86c8a22512b47741fb4338d2ed1dbe4440385 Mon Sep 17 00:00:00 2001 From: pranav-super Date: Thu, 17 Oct 2024 08:15:05 -0700 Subject: [PATCH] update assertThrowsExactly tests --- .../aerie/database/ExternalEventTests.java | 108 ++++++++++++++---- .../Aerie/11_external_events/up.sql | 2 +- .../merlin/external_events/external_event.sql | 2 +- 3 files changed, 90 insertions(+), 22 deletions(-) diff --git a/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java b/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java index 523b9f1347..803b990aeb 100644 --- a/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java +++ b/db-tests/src/test/java/gov/nasa/jpl/aerie/database/ExternalEventTests.java @@ -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") @@ -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; + } } /** @@ -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; + } } /** @@ -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)); @@ -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; + } } /** @@ -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); @@ -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; + } } /** @@ -1792,7 +1828,7 @@ void deleteDGwithRemainingSource() throws SQLException { insertExternalSource(src); // delete the DG (expect error) - assertThrowsExactly(PSQLException.class, + final SQLException ex = assertThrows(PSQLException.class, () -> statement.executeUpdate( // language=sql """ @@ -1800,6 +1836,13 @@ void deleteDGwithRemainingSource() throws SQLException { """.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; + } } } @@ -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; + } } /** @@ -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; + } } } @@ -1917,7 +1978,7 @@ 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 """ @@ -1925,6 +1986,13 @@ void deleteEventTypeWithRemainingEvent() throws SQLException { """.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; + } } } } diff --git a/deployment/hasura/migrations/Aerie/11_external_events/up.sql b/deployment/hasura/migrations/Aerie/11_external_events/up.sql index c83cadbce0..7351648ed6 100644 --- a/deployment/hasura/migrations/Aerie/11_external_events/up.sql +++ b/deployment/hasura/migrations/Aerie/11_external_events/up.sql @@ -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; diff --git a/deployment/postgres-init-db/sql/tables/merlin/external_events/external_event.sql b/deployment/postgres-init-db/sql/tables/merlin/external_events/external_event.sql index 0ecb483167..5be7c9bf0f 100644 --- a/deployment/postgres-init-db/sql/tables/merlin/external_events/external_event.sql +++ b/deployment/postgres-init-db/sql/tables/merlin/external_events/external_event.sql @@ -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;