|
| 1 | +package io.teaql.examples.tracechain; |
| 2 | + |
| 3 | +import com.teaql.tracechainservice.E; |
| 4 | +import com.teaql.tracechainservice.Q; |
| 5 | +import com.teaql.tracechainservice.customerorder.CustomerOrder; |
| 6 | +import io.teaql.core.ExecutionMetadata; |
| 7 | +import io.teaql.core.TraceKind; |
| 8 | +import io.teaql.core.TraceNode; |
| 9 | +import io.teaql.runtime.LogPrivacy; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import org.junit.Test; |
| 13 | +import static org.junit.Assert.*; |
| 14 | + |
| 15 | +/** Application-owned TC-SQL-09 acceptance; no generated-library inspection or edits. */ |
| 16 | +public class GeneratedReverseFacetTraceExampleTest { |
| 17 | + @Test public void generatedReverseCollectionsRetainScopedFacetsFullCountsAndLoadedEmpty() throws Exception { |
| 18 | + for (boolean logging : List.of(false, true)) for (boolean includeAll : List.of(false, true)) { |
| 19 | + var fixture = new GeneratedTraceChainExampleTest.Fixture(logging); |
| 20 | + var graph = fixture.saveNormativeGraph(); |
| 21 | + var extra = Q.payments().comment("prepare second child of first parent") |
| 22 | + .purpose("verify full membership beyond child page").newEntity(fixture.context); |
| 23 | + extra.updateReferenceCode("REVERSE-EXTRA-" + fixture.base); |
| 24 | + graph.order().addPayment(extra); |
| 25 | + graph.order().auditAs("seed two members for first parent").save(fixture.context); |
| 26 | + var second = newOrder(fixture, graph.order(), "SECOND"); |
| 27 | + var payment = Q.payments().comment("prepare child of second parent") |
| 28 | + .purpose("verify independent relation membership").newEntity(fixture.context); |
| 29 | + payment.updateReferenceCode("REVERSE-SECOND-" + fixture.base); |
| 30 | + second.addPayment(payment); |
| 31 | + second.auditAs("seed independently scoped second parent").save(fixture.context); |
| 32 | + var empty = newOrder(fixture, graph.order(), "EMPTY"); |
| 33 | + empty.auditAs("seed parent with no payments").save(fixture.context); |
| 34 | + var ids = new Long[]{graph.order().getId(), second.getId(), empty.getId()}; |
| 35 | + fixture.clear(); fixture.queryResults.clear(); |
| 36 | + String comment = "load independently scoped reverse collection facets"; |
| 37 | + String purpose = "verify generated Q/E, full counts and requested empty"; |
| 38 | + // Exact selector from local field Assist at generator 218785e2. |
| 39 | + var rows = Q.customerOrders().withIdIn(ids).orderByIdAscending().limit(3) |
| 40 | + .selectPaymentListWith(Q.payments().orderByIdAscending().limit(1) |
| 41 | + .facetByCustomerOrderAs("orders", Q.customerOrders().withIdIn(ids).limit(3), includeAll)) |
| 42 | + .comment(comment).purpose(purpose).executeForList(fixture.context); |
| 43 | + assertEquals(3, rows.size()); |
| 44 | + for (var parent : rows) { |
| 45 | + long parentId = E.customerOrder(parent).getId().eval(); |
| 46 | + int fullCount = parentId == ids[0] ? 2 : parentId == ids[1] ? 1 : 0; |
| 47 | + var children = E.customerOrder(parent).getPaymentList().eval(); |
| 48 | + assertNotNull("requested empty reverse collection is loaded, not NotLoaded", children); |
| 49 | + assertEquals(Math.min(1, fullCount), E.customerOrder(parent).getPaymentList().size().eval().intValue()); |
| 50 | + var facets = children.getFacet("orders"); |
| 51 | + assertNotNull("returned generated collection retains requested Facet metadata", facets); |
| 52 | + assertEquals(includeAll ? 3 : fullCount == 0 ? 0 : 1, facets.size()); |
| 53 | + for (var value : facets) { |
| 54 | + var target = (CustomerOrder) value; |
| 55 | + long targetId = E.customerOrder(target).getId().eval(); |
| 56 | + int expected = targetId == parentId ? fullCount : 0; |
| 57 | + assertEquals("counts cover full scoped membership, not the visible child page", |
| 58 | + expected, ((Number) target.getDynamicProperty("count")).intValue()); |
| 59 | + } |
| 60 | + } |
| 61 | + var raw = fixture.queryResults.get(fixture.queryResults.size() - 1).statements(); |
| 62 | + assertEquals("root plus three independently scoped child/count/target queries", 10, raw.size()); |
| 63 | + for (int i = 0; i < raw.size(); i++) { |
| 64 | + var expected = new ArrayList<>(List.of( |
| 65 | + new TraceNode(TraceKind.OPERATION, "CustomerOrder", null, "query"), |
| 66 | + new TraceNode(TraceKind.REQUEST, "CustomerOrder", null, ""))); |
| 67 | + if (i > 0) expected.add(new TraceNode(TraceKind.RELATION, "paymentList", null, "CustomerOrder.paymentList")); |
| 68 | + if (i > 0 && i % 3 == 0) expected.add(new TraceNode(TraceKind.RELATION, "customerOrder", null, "Payment.customerOrder")); |
| 69 | + expected.add(new TraceNode(TraceKind.PROVIDER, "sqlite", null, "")); |
| 70 | + expected.add(new TraceNode(TraceKind.SQL, "select", null, "")); |
| 71 | + assertFact(raw.get(i), expected, comment, purpose); |
| 72 | + assertFact(LogPrivacy.sql(raw.get(i), false), expected, comment, purpose); |
| 73 | + if (logging) assertFact(fixture.sql.get(i), expected, comment, purpose); |
| 74 | + } |
| 75 | + assertEquals(logging ? 10 : 0, fixture.sql.size()); |
| 76 | + assertEquals("query metadata cannot schedule writes", 0, fixture.commands.size()); |
| 77 | + assertEquals(0, fixture.audit.size()); |
| 78 | + assertTrue(fixture.context.getTraceChain().isEmpty()); |
| 79 | + System.out.printf("JAVA_GENERATED_REVERSE_FACET logging=%s includeAll=%s parents=3 fullCounts=true requestedEmpty=true physicalStatements=10 safeSinkStatements=%d mutationCommands=0%n", |
| 80 | + logging, includeAll, fixture.sql.size()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static CustomerOrder newOrder(GeneratedTraceChainExampleTest.Fixture fixture, CustomerOrder first, String suffix) throws Exception { |
| 85 | + var order = Q.customerOrders().comment("prepare distinct reverse collection parent") |
| 86 | + .purpose("verify independently scoped membership").newEntity(fixture.context); |
| 87 | + order.updatePlatform(E.customerOrder(first).getPlatform().eval()); |
| 88 | + order.updateOrderNumber("REVERSE-" + suffix + "-" + fixture.base); |
| 89 | + order.updateDescription("Reverse Facet " + suffix); |
| 90 | + return order; |
| 91 | + } |
| 92 | + |
| 93 | + private static void assertFact(ExecutionMetadata fact, List<TraceNode> path, String comment, String purpose) { |
| 94 | + assertEquals("complete physical ancestry", path, fact.getTraceChain()); |
| 95 | + assertEquals(comment, fact.getComment()); assertEquals(purpose, fact.getPurpose()); |
| 96 | + assertEquals("success", fact.getExecutionOutcome()); |
| 97 | + } |
| 98 | +} |
0 commit comments