|
| 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 com.teaql.tracechainservice.payment.Payment; |
| 7 | +import io.teaql.core.Entity; |
| 8 | +import io.teaql.core.ExecutionMetadata; |
| 9 | +import io.teaql.core.SmartList; |
| 10 | +import io.teaql.core.TraceKind; |
| 11 | +import io.teaql.core.TraceNode; |
| 12 | +import io.teaql.runtime.LogPrivacy; |
| 13 | +import java.util.List; |
| 14 | +import org.junit.Test; |
| 15 | +import static org.junit.Assert.*; |
| 16 | + |
| 17 | +/** TC-SQL-09: check returned nested metadata, not just successful facet SQL. */ |
| 18 | +public class GeneratedFacetTraceExampleTest { |
| 19 | + @Test public void nestedFacetMetadataRemainsAttachedToTheReturnedFacetCollection() throws Exception { |
| 20 | + var fixture = new GeneratedTraceChainExampleTest.Fixture(); |
| 21 | + var graph = fixture.saveNormativeGraph(); |
| 22 | + fixture.clear(); |
| 23 | + var rows = Q.paymentAttempts().withIdIs(graph.attempt().getId()).limit(1) |
| 24 | + .facetByPaymentAs("payments", Q.payments().withIdIs(graph.payment().getId()).limit(1) |
| 25 | + .facetByCustomerOrderAs("orders", Q.customerOrders().withIdIs(graph.order().getId()).limit(1))) |
| 26 | + .comment("load nested facet metadata").purpose("verify the returned collection, not only SQL").executeForList(fixture.context); |
| 27 | + assertEquals(1, rows.size()); |
| 28 | + var payments = rows.getFacet("payments"); |
| 29 | + assertNotNull(payments); assertEquals(1, payments.size()); |
| 30 | + var orders = payments.getFacet("orders"); |
| 31 | + assertNotNull("nested Facet metadata must survive materialization", orders); |
| 32 | + assertEquals(1, orders.size()); |
| 33 | + assertEquals(graph.order().getId(), E.customerOrder((CustomerOrder) orders.get(0)).getId().eval()); |
| 34 | + assertEquals(1, count((CustomerOrder) orders.get(0))); |
| 35 | + System.out.println("JAVA_NESTED_FACET_CARRIER returned nested metadata and count verified"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test public void nestedFacetCountsSurviveMaterializationBeyondTheVisiblePage() throws Exception { |
| 39 | + for (boolean logging : List.of(false, true)) { |
| 40 | + for (boolean includeAll : List.of(false, true)) { |
| 41 | + var fixture = new GeneratedTraceChainExampleTest.Fixture(logging); |
| 42 | + var graph = fixture.saveNormativeGraph(); |
| 43 | + var payment = Q.payments().comment("prepare another facet member") |
| 44 | + .purpose("verify full membership beyond one visible row").newEntity(fixture.context); |
| 45 | + payment.updateReferenceCode("FACET-PAYMENT-" + fixture.base); |
| 46 | + for (int i = 0; i < 2; i++) { |
| 47 | + var attempt = Q.paymentAttempts().comment("prepare a counted attempt") |
| 48 | + .purpose("verify full facet membership").newEntity(fixture.context); |
| 49 | + attempt.updateReferenceCode("FACET-ATTEMPT-" + fixture.base + "-" + i); |
| 50 | + payment.addPaymentAttempt(attempt); |
| 51 | + } |
| 52 | + graph.order().addPayment(payment); |
| 53 | + graph.order().auditAs("seed nested facet membership").save(fixture.context); |
| 54 | + long orderId = E.customerOrder(graph.order()).getId().eval(); |
| 55 | + long paymentId = E.payment(payment).getId().eval(); |
| 56 | + fixture.clear(); fixture.queryResults.clear(); |
| 57 | + String comment = "load bounded nested payment facets"; |
| 58 | + String purpose = "verify returned full counts and original query ancestry"; |
| 59 | + var rows = Q.paymentAttempts() |
| 60 | + .withPaymentMatching(Q.payments().filterByCustomerOrder(orderId)) |
| 61 | + .orderByIdDescending().limit(1) |
| 62 | + .facetByPaymentAs("payments", Q.payments().filterByCustomerOrder(orderId) |
| 63 | + .orderByIdDescending().limit(1) |
| 64 | + .facetByCustomerOrderAs("orders", Q.customerOrders().withIdIs(orderId).limit(1), includeAll), includeAll) |
| 65 | + .comment(comment).purpose(purpose).executeForList(fixture.context); |
| 66 | + assertEquals("bounded visible attempt page", 1, rows.size()); |
| 67 | + var payments = rows.getFacet("payments"); |
| 68 | + assertNotNull("first-level facet metadata", payments); |
| 69 | + assertEquals(1, payments.size()); |
| 70 | + var selected = (Payment) payments.get(0); |
| 71 | + assertEquals(paymentId, E.payment(selected).getId().eval().longValue()); |
| 72 | + assertEquals("first-level count is not the one-row visible page", 2, count(selected)); |
| 73 | + var orders = payments.getFacet("orders"); |
| 74 | + assertNotNull("nested Facet metadata must survive materialization", orders); |
| 75 | + assertEquals(1, orders.size()); |
| 76 | + var order = (CustomerOrder) orders.get(0); |
| 77 | + assertEquals(orderId, E.customerOrder(order).getId().eval().longValue()); |
| 78 | + assertEquals("nested count uses both filtered payments, not the visible payment page", 2, count(order)); |
| 79 | + var raw = fixture.queryResults.get(fixture.queryResults.size() - 1).statements(); |
| 80 | + // The page and its COUNT each materialize the predicate. Both |
| 81 | + // real SELECTs must belong to the originating collector. |
| 82 | + var routes = List.of(List.of("payment"), List.<String>of(), List.of("payment"), |
| 83 | + List.<String>of(), List.of("payment"), List.of("payment"), |
| 84 | + List.of("payment", "customerOrder")); |
| 85 | + assertEquals(routes.size(), raw.size()); |
| 86 | + for (int i = 0; i < raw.size(); i++) { |
| 87 | + assertPath(raw.get(i), routes.get(i), comment, purpose); |
| 88 | + assertEquals("real SELECT completed", "success", raw.get(i).getExecutionOutcome()); |
| 89 | + var safe = LogPrivacy.sql(raw.get(i), false); |
| 90 | + assertPath(safe, routes.get(i), comment, purpose); |
| 91 | + if (i == 3 || i == 5) { |
| 92 | + assertTrue("physical membership COUNT", raw.get(i).getParameterizedQuery().toUpperCase().contains("COUNT(")); |
| 93 | + assertFalse("membership count must not reuse the visible page limit", |
| 94 | + raw.get(i).getParameterizedQuery().toUpperCase().contains("LIMIT 1")); |
| 95 | + } |
| 96 | + } |
| 97 | + assertEquals(logging ? raw.size() : 0, fixture.sql.size()); |
| 98 | + for (int i = 0; i < fixture.sql.size(); i++) assertPath(fixture.sql.get(i), routes.get(i), comment, purpose); |
| 99 | + assertTrue(fixture.context.getTraceChain().isEmpty()); |
| 100 | + System.out.printf("JAVA_NESTED_FACET {\"logging\":%s,\"includeAll\":%s,\"visible\":1,\"paymentCount\":2,\"orderCount\":2,\"physicalStatements\":7,\"safeSinkStatements\":%d}%n", |
| 101 | + logging, includeAll, fixture.sql.size()); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static int count(Entity entity) { |
| 107 | + return ((Number) entity.getDynamicProperty("count")).intValue(); |
| 108 | + } |
| 109 | + |
| 110 | + private static void assertPath(ExecutionMetadata fact, List<String> route, String comment, String purpose) { |
| 111 | + var expected = new java.util.ArrayList<>(List.of( |
| 112 | + new TraceNode(TraceKind.OPERATION, "PaymentAttempt", null, "query"), |
| 113 | + new TraceNode(TraceKind.REQUEST, "PaymentAttempt", null, ""))); |
| 114 | + String owner = "PaymentAttempt"; |
| 115 | + for (String edge : route) { |
| 116 | + expected.add(new TraceNode(TraceKind.RELATION, edge, null, owner + "." + edge)); |
| 117 | + owner = edge.equals("payment") ? "Payment" : "CustomerOrder"; |
| 118 | + } |
| 119 | + expected.add(new TraceNode(TraceKind.PROVIDER, "sqlite", null, "")); |
| 120 | + expected.add(new TraceNode(TraceKind.SQL, "select", null, "")); |
| 121 | + assertEquals("complete canonical physical path", expected, fact.getTraceChain()); |
| 122 | + assertEquals(comment, fact.getComment()); |
| 123 | + assertEquals(purpose, fact.getPurpose()); |
| 124 | + } |
| 125 | +} |
0 commit comments