Friendly app-level validation for a RANGE with end-before-start (#678) #706
@@ -38,7 +38,10 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DataJpaTest
|
||||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
|
||||
@@ -635,6 +638,25 @@ class DocumentRepositoryTest {
|
||||
assertThat(found.getMetaDatePrecision()).isEqualTo(DatePrecision.RANGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void save_rejectsRange_whenEndBeforeStart_atDbLevel() {
|
||||
// The app guard normally intercepts this, so the DB CHECK never fires in practice.
|
||||
// Persisting directly proves chk_meta_date_end_after_start actually rejects end < start
|
||||
// (H2 would not) — if the app guard ever regresses, a bad row still can't reach the table,
|
||||
// and this is exactly the violation the GlobalExceptionHandler backstop turns into a 400.
|
||||
Document doc = Document.builder()
|
||||
.title("Verdrehte Spanne")
|
||||
.originalFilename("verdreht.pdf")
|
||||
.status(DocumentStatus.UPLOADED)
|
||||
.documentDate(LocalDate.of(1917, 1, 11))
|
||||
.metaDatePrecision(DatePrecision.RANGE)
|
||||
.metaDateEnd(LocalDate.of(1917, 1, 10))
|
||||
.build();
|
||||
|
||||
assertThatThrownBy(() -> documentRepository.saveAndFlush(doc))
|
||||
.isInstanceOf(DataIntegrityViolationException.class);
|
||||
}
|
||||
|
||||
// ─── seeding helpers ─────────────────────────────────────────────────────
|
||||
|
||||
private Document uploaded(String title) {
|
||||
|
||||
@@ -283,6 +283,7 @@ class DocumentServiceTest {
|
||||
documentService.updateDocument(id,
|
||||
rangeDto(LocalDate.of(1917, 1, 10), LocalDate.of(1917, 1, 11)), null, null);
|
||||
|
||||
assertThat(doc.getMetaDateEnd()).isEqualTo(LocalDate.of(1917, 1, 11));
|
||||
verify(documentRepository, atLeastOnce()).save(any());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user