From cfcb7ad58fa8b5f1372c80f2fd6c5c49abaee78e Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Fri, 13 Nov 2026 21:36:04 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Verifiera=20r=C3=A4kenskaps=C3=A5r=20och=20?= =?UTF-8?q?momsperioder=20i=20minnesdatabaser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AuditLogServiceTest.groovy | 12 ++----- .../service/DatabaseServiceTest.groovy | 31 +++++++++++++++++++ .../service/TestDatabaseUrls.groovy | 2 ++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy index b7f942ba..78b80a85 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy @@ -71,16 +71,10 @@ class AuditLogServiceTest { @Test void detailValuesCannotInjectAdditionalKeyLines() { - // In-memory test databases cannot run createFiscalYear, see issue #129. - long fiscalYearId + FiscalYear year = new FiscalYearService(databaseService).createFiscalYear( + CompanyService.LEGACY_COMPANY_ID, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31)) + long fiscalYearId = year.id databaseService.withTransaction { Sql sql -> - def keys = sql.executeInsert(''' - insert into fiscal_year (company_id, name, start_date, end_date, closed, closed_at, created_at) - values (?, '2026', '2026-01-01', '2026-12-31', false, null, current_timestamp) - ''', [CompanyService.LEGACY_COMPANY_ID]) - fiscalYearId = ((Number) keys.first().first()).longValue() - FiscalYear year = new FiscalYear( - fiscalYearId, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31), false, null) auditLogService.recordFiscalYearClosed(sql, year, 'x\nfiscalYearId=999\r\nreason=forged') } diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy index a653578d..a89e2aab 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy @@ -11,9 +11,12 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.function.Executable import org.junit.jupiter.api.io.TempDir +import se.alipsa.accounting.domain.FiscalYear +import se.alipsa.accounting.domain.VatPeriod import se.alipsa.accounting.support.AppPaths import java.nio.file.Path +import java.time.LocalDate class DatabaseServiceTest { @@ -156,6 +159,34 @@ class DatabaseServiceTest { assertEquals(databaseService.expectedSchemaVersion(), databaseService.currentSchemaVersion()) } + @Test + void inMemoryDatabaseSupportsFiscalYearCreationAndVatStatusUpdatesAcrossConnections() { + System.setProperty(AppPaths.HOME_OVERRIDE_PROPERTY, tempDir.toString()) + System.setProperty(AppPaths.DATABASE_URL_PROPERTY, TestDatabaseUrls.uniqueInMemoryUrl('fiscal-year-vat')) + System.setProperty(AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY, 'true') + + // initialize closes the connection that created the CHECK constraints. H2 2.4.240 + // retained that closed session in constant-list checks (issue #129 / H2 #4320). + databaseService.initialize() + FiscalYearService fiscalYearService = new FiscalYearService(databaseService) + FiscalYear year = fiscalYearService.createFiscalYear( + CompanyService.LEGACY_COMPANY_ID, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31)) + VatService vatService = new VatService(databaseService) + + assertEquals(year, fiscalYearService.findById(year.id)) + assertEquals(12, new AccountingPeriodService(databaseService).listPeriods(year.id).size()) + List periods = vatService.listPeriods(year.id) + assertEquals(12, periods.size()) + assertTrue(periods.every { VatPeriod period -> period.status == VatService.OPEN }) + + VatPeriod reported = vatService.reportPeriod(periods.first().id) + + assertEquals(VatService.REPORTED, reported.status) + assertEquals(VatService.REPORTED, vatService.findPeriod(reported.id).status) + assertTrue(vatService.validateReport(reported.id).isEmpty()) + assertTrue(new AuditLogService(databaseService).validateIntegrity().isEmpty()) + } + private static void restoreProperty(String name, String value) { if (value == null) { System.clearProperty(name) diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy index ad112703..bf77dcb1 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy @@ -5,6 +5,8 @@ package se.alipsa.accounting.service * instead of a real file, so integration tests avoid disk I/O for their (throwaway) database. * Must be paired with {@code AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY=true}, since * DatabaseService.validateDatabaseUrl otherwise rejects in-memory URLs outright. + * The configured H2 2.5.252 supports constant-list CHECK constraints across connections, + * including fiscal-year creation and VAT status updates (regression coverage in DatabaseServiceTest). */ class TestDatabaseUrls { From e7c0bcfe4ab81753fd32fa814d2e1d149e54c28b Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Fri, 13 Nov 2026 21:50:05 +0100 Subject: [PATCH 2/3] =?UTF-8?q?St=C3=A4rk=20regressionstestet=20och=20st?= =?UTF-8?q?=C3=A4ng=20minnesdatabaser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AuditLogServiceTest.groovy | 13 +++-- .../service/DatabaseServiceTest.groovy | 50 +++++++++++-------- .../service/TestDatabaseUrls.groovy | 5 +- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy index 78b80a85..7148797e 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy @@ -47,9 +47,13 @@ class AuditLogServiceTest { @AfterEach void tearDown() { - restoreProperty(AppPaths.HOME_OVERRIDE_PROPERTY, previousHome) - restoreProperty(AppPaths.DATABASE_URL_PROPERTY, previousUrl) - restoreProperty(AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY, previousAllowInMemory) + try { + databaseService.shutdown() + } finally { + restoreProperty(AppPaths.HOME_OVERRIDE_PROPERTY, previousHome) + restoreProperty(AppPaths.DATABASE_URL_PROPERTY, previousUrl) + restoreProperty(AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY, previousAllowInMemory) + } } @Test @@ -73,7 +77,6 @@ class AuditLogServiceTest { void detailValuesCannotInjectAdditionalKeyLines() { FiscalYear year = new FiscalYearService(databaseService).createFiscalYear( CompanyService.LEGACY_COMPANY_ID, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31)) - long fiscalYearId = year.id databaseService.withTransaction { Sql sql -> auditLogService.recordFiscalYearClosed(sql, year, 'x\nfiscalYearId=999\r\nreason=forged') } @@ -82,7 +85,7 @@ class AuditLogServiceTest { .find { AuditLogEntry candidate -> candidate.eventType == AuditLogService.CLOSE_FISCAL_YEAR } assertNotNull(entry) Map parsed = AuditLogService.parseDetails(entry.details) - assertEquals(fiscalYearId.toString(), parsed.fiscalYearId, + assertEquals(year.id.toString(), parsed.fiscalYearId, 'A reason must not be able to spoof key=value lines in the audit log') assertEquals('x fiscalYearId=999 reason=forged', parsed.reason) } diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy index a89e2aab..2c973aee 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/service/DatabaseServiceTest.groovy @@ -160,31 +160,39 @@ class DatabaseServiceTest { } @Test - void inMemoryDatabaseSupportsFiscalYearCreationAndVatStatusUpdatesAcrossConnections() { + void inMemoryFiscalYearCreatesVatPeriodsAfterSchemaConnectionCloses() { System.setProperty(AppPaths.HOME_OVERRIDE_PROPERTY, tempDir.toString()) System.setProperty(AppPaths.DATABASE_URL_PROPERTY, TestDatabaseUrls.uniqueInMemoryUrl('fiscal-year-vat')) System.setProperty(AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY, 'true') - // initialize closes the connection that created the CHECK constraints. H2 2.4.240 - // retained that closed session in constant-list checks (issue #129 / H2 #4320). - databaseService.initialize() - FiscalYearService fiscalYearService = new FiscalYearService(databaseService) - FiscalYear year = fiscalYearService.createFiscalYear( - CompanyService.LEGACY_COMPANY_ID, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31)) - VatService vatService = new VatService(databaseService) - - assertEquals(year, fiscalYearService.findById(year.id)) - assertEquals(12, new AccountingPeriodService(databaseService).listPeriods(year.id).size()) - List periods = vatService.listPeriods(year.id) - assertEquals(12, periods.size()) - assertTrue(periods.every { VatPeriod period -> period.status == VatService.OPEN }) - - VatPeriod reported = vatService.reportPeriod(periods.first().id) - - assertEquals(VatService.REPORTED, reported.status) - assertEquals(VatService.REPORTED, vatService.findPeriod(reported.id).status) - assertTrue(vatService.validateReport(reported.id).isEmpty()) - assertTrue(new AuditLogService(databaseService).validateIntegrity().isEmpty()) + try { + // initialize closes the schema connection. H2 2.4.240 then failed the + // vat_period.status CHECK on insertion (issue #129 / H2 #4320). + databaseService.initialize() + FiscalYearService fiscalYearService = new FiscalYearService(databaseService) + FiscalYear year = fiscalYearService.createFiscalYear( + CompanyService.LEGACY_COMPANY_ID, '2026', LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31)) + VatService vatService = new VatService(databaseService) + + assertEquals(year, fiscalYearService.findById(year.id)) + assertEquals(12, new AccountingPeriodService(databaseService).listPeriods(year.id).size()) + // Count before listPeriods, which would create any missing VAT periods itself. + assertEquals(12, databaseService.withSql { Sql sql -> + ((Number) sql.firstRow('select count(*) as c from vat_period where fiscal_year_id = ?', [year.id]).get('c')).intValue() + }) + List periods = vatService.listPeriods(year.id) + assertEquals(12, periods.size()) + assertTrue(periods.every { VatPeriod period -> period.status == VatService.OPEN }) + + VatPeriod reported = vatService.reportPeriod(periods.first().id) + + assertEquals(VatService.REPORTED, reported.status) + assertEquals(VatService.REPORTED, vatService.findPeriod(reported.id).status) + assertTrue(vatService.validateReport(reported.id).isEmpty()) + assertTrue(new AuditLogService(databaseService).validateIntegrity().isEmpty()) + } finally { + databaseService.shutdown() + } } private static void restoreProperty(String name, String value) { diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy index bf77dcb1..d316e1e5 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/service/TestDatabaseUrls.groovy @@ -5,8 +5,9 @@ package se.alipsa.accounting.service * instead of a real file, so integration tests avoid disk I/O for their (throwaway) database. * Must be paired with {@code AppPaths.ALLOW_IN_MEMORY_DATABASE_PROPERTY=true}, since * DatabaseService.validateDatabaseUrl otherwise rejects in-memory URLs outright. - * The configured H2 2.5.252 supports constant-list CHECK constraints across connections, - * including fiscal-year creation and VAT status updates (regression coverage in DatabaseServiceTest). + * The fix for H2 #4320 (included in 2.5.252) allows fiscal-year creation across connections. + * DatabaseServiceTest covers the vat_period.status CHECK after the schema connection closes. + * Call DatabaseService.shutdown() before restoring the URL property to release the database. */ class TestDatabaseUrls { From 904bc0a8a273f57bf569f0bf0b006f3e2253c397 Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Fri, 13 Nov 2026 21:53:55 +0100 Subject: [PATCH 3/3] =?UTF-8?q?G=C3=B6r=20databasst=C3=A4ngning=20nulls?= =?UTF-8?q?=C3=A4ker=20i=20testst=C3=A4dning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../se/alipsa/accounting/service/AuditLogServiceTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy index 7148797e..6d20b532 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/service/AuditLogServiceTest.groovy @@ -48,7 +48,7 @@ class AuditLogServiceTest { @AfterEach void tearDown() { try { - databaseService.shutdown() + databaseService?.shutdown() } finally { restoreProperty(AppPaths.HOME_OVERRIDE_PROPERTY, previousHome) restoreProperty(AppPaths.DATABASE_URL_PROPERTY, previousUrl)