|
| 1 | +package io.teaql.sqlite; |
| 2 | + |
| 3 | +import io.teaql.core.*; |
| 4 | +import io.teaql.runtime.AppAuditEventSink; |
| 5 | +import java.util.List; |
| 6 | +import org.junit.Test; |
| 7 | +import static org.junit.Assert.*; |
| 8 | + |
| 9 | +/** TC-REQ-13 deliberate native diagnostic input, not generated graph proof. */ |
| 10 | +public class BlankRouteTailSqliteTest { |
| 11 | + @Test public void explicitRootCommentSurvivesEveryBlankTypedTailAtRealSinks() throws Exception { |
| 12 | + for (boolean logging : new boolean[]{false, true}) { |
| 13 | + for (TraceKind kind : List.of(TraceKind.ENTITY, TraceKind.PROVIDER, TraceKind.SQL)) { |
| 14 | + var fixture = new GraphTraceSqliteTest.Fixture(); |
| 15 | + fixture.queryLogging = logging; |
| 16 | + fixture.mutationLogging = logging; |
| 17 | + String comment = " explicit owned mutation reason "; |
| 18 | + var entity = fixture.create("CustomerOrder", 810, "native route fixture"); |
| 19 | + String name = kind == TraceKind.PROVIDER ? "sqlite" |
| 20 | + : kind == TraceKind.SQL ? "insert" : "CustomerOrder"; |
| 21 | + var tail = new TraceNode(kind, name, null, ""); |
| 22 | + var source = List.of(new TraceNode(TraceKind.AUDIT_REASON, "CustomerOrder", 810L, comment), tail); |
| 23 | + entity.setTraceChain(source); |
| 24 | + entity.setComment(comment); |
| 25 | + assertEquals("", tail.getComment()); |
| 26 | + // The sink reads through an independent connection. Pending |
| 27 | + // rows inside the mutation transaction cannot satisfy this probe. |
| 28 | + fixture.context.putAttribute(AppAuditEventSink.class.getName(), (AppAuditEventSink) (caller, event) -> { |
| 29 | + try (var connection = fixture.dataSource.getConnection(); |
| 30 | + var select = connection.prepareStatement("SELECT id, version FROM customer_order_data WHERE id = ?")) { |
| 31 | + assertTrue(connection.getAutoCommit()); |
| 32 | + select.setLong(1, 810); |
| 33 | + try (var row = select.executeQuery()) { |
| 34 | + assertTrue("audit escaped before real commit", row.next()); |
| 35 | + assertEquals(810, row.getLong(1)); |
| 36 | + assertEquals(1, row.getLong(2)); |
| 37 | + assertFalse(row.next()); |
| 38 | + } |
| 39 | + } catch (java.sql.SQLException error) { |
| 40 | + throw new AssertionError("independent committed-row probe failed", error); |
| 41 | + } |
| 42 | + assertEquals(comment, event.traceChain().get(0).getComment()); |
| 43 | + fixture.audit.add(event); |
| 44 | + }); |
| 45 | + entity.auditAs(comment).save(fixture.context); |
| 46 | + assertEquals(1, fixture.commands.size()); |
| 47 | + var emitted = fixture.commands.get(0); |
| 48 | + assertEquals(source, emitted.getTraceChain()); |
| 49 | + assertEquals(tail, emitted.getTraceChain().get(emitted.getTraceChain().size() - 1)); |
| 50 | + assertEquals(comment, emitted.comment()); |
| 51 | + assertEquals(comment, emitted.intent().readbackIntent().comment()); |
| 52 | + assertEquals(1, fixture.results.size()); |
| 53 | + var statements = fixture.results.get(0).statements(); |
| 54 | + assertEquals(2, statements.size()); |
| 55 | + for (int index = 0; index < statements.size(); index++) { |
| 56 | + var statement = statements.get(index); |
| 57 | + assertEquals(comment, statement.getAuditReason()); |
| 58 | + assertEquals(index == 0 ? null : comment, statement.getComment()); |
| 59 | + assertEquals(source, statement.getMutationLineage()); |
| 60 | + assertEquals("success", statement.getExecutionOutcome()); |
| 61 | + assertEquals(index == 0 ? "insert" : "select", statement.getStatementOperation()); |
| 62 | + var route = statement.getTraceChain(); |
| 63 | + assertEquals("CustomerOrder", route.get(0).getName()); |
| 64 | + assertEquals("sqlite", route.get(route.size() - 2).getName()); |
| 65 | + assertEquals(statement.getStatementOperation(), route.get(route.size() - 1).getName()); |
| 66 | + } |
| 67 | + assertEquals(1, fixture.audit.size()); |
| 68 | + assertEquals(source, fixture.audit.get(0).traceChain()); |
| 69 | + assertEquals(logging ? 2 : 0, fixture.sql.size()); |
| 70 | + for (var diagnostic : fixture.sql) assertEquals(comment, diagnostic.getAuditReason()); |
| 71 | + assertEquals(comment, source.get(0).getComment()); |
| 72 | + assertTrue(fixture.context.getTraceChain().isEmpty()); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments