|
| 1 | +package com.marklogic.client.test.rows; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.marklogic.client.expression.PlanBuilder; |
| 6 | +import com.marklogic.client.io.StringHandle; |
| 7 | +import com.marklogic.client.row.RawPlanDefinition; |
| 8 | +import com.marklogic.client.row.RawQueryDSLPlan; |
| 9 | +import com.marklogic.client.row.RowManager; |
| 10 | +import com.marklogic.client.test.Common; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 14 | +import org.springframework.core.io.ClassPathResource; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 18 | + |
| 19 | +public class ColumnInfoTest { |
| 20 | + |
| 21 | + private RowManager rowManager; |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + void beforeEach() { |
| 25 | + rowManager = Common.newClient().newRowManager(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void allTypesWithDSLPlan() { |
| 30 | + String query = "op.fromView('javaClient', 'allTypes')"; |
| 31 | + RawQueryDSLPlan plan = rowManager.newRawQueryDSLPlan(new StringHandle(query)); |
| 32 | + verifyColumnInfo(plan); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void allTypesWithSerializedPlan() { |
| 37 | + String serializedQuery = "{\n" + |
| 38 | + " \"$optic\": {\n" + |
| 39 | + " \"ns\": \"op\",\n" + |
| 40 | + " \"fn\": \"operators\",\n" + |
| 41 | + " \"args\": [\n" + |
| 42 | + " {\n" + |
| 43 | + " \"ns\": \"op\",\n" + |
| 44 | + " \"fn\": \"from-view\",\n" + |
| 45 | + " \"args\": [\n" + |
| 46 | + " \"javaClient\",\n" + |
| 47 | + " \"allTypes\"\n" + |
| 48 | + " ]\n" + |
| 49 | + " }\n" + |
| 50 | + " ]\n" + |
| 51 | + " }\n" + |
| 52 | + "}"; |
| 53 | + |
| 54 | + RawPlanDefinition plan = rowManager.newRawPlanDefinition(new StringHandle(serializedQuery)); |
| 55 | + verifyColumnInfo(plan); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * The expected JSON for all the column infos is based on the results from the 20230417 build. See the internal |
| 60 | + * DBQ-296 ticket for an explanation on why some of the columns have an expected type of "none". |
| 61 | + * |
| 62 | + * @param plan |
| 63 | + */ |
| 64 | + private void verifyColumnInfo(PlanBuilder.Plan plan) { |
| 65 | + StringHandle output = Common.client.newRowManager().columnInfo(plan, new StringHandle()); |
| 66 | + |
| 67 | + assertTrue(output.getServerTimestamp() > 0, "The server timestamp should be present so that a client, such as " + |
| 68 | + "the Spark connector, can both get column info and identify a timestamp for future point-in-time queries."); |
| 69 | + |
| 70 | + String[] columnInfos = output.get().split("\n"); |
| 71 | + assertEquals(36, columnInfos.length, "There are 35 column definitions in the allTypes TDE, and then a 36th " + |
| 72 | + "column info is added for the rowid column."); |
| 73 | + |
| 74 | + ObjectMapper mapper = new ObjectMapper(); |
| 75 | + try { |
| 76 | + JsonNode actualColumnInfo = mapper.readTree("[" + String.join(",", columnInfos) + "]"); |
| 77 | + JsonNode expectedColumnInfo = mapper.readTree(new ClassPathResource("allTypes-columnInfo.json").getInputStream()); |
| 78 | + JSONAssert.assertEquals(expectedColumnInfo.toString(), actualColumnInfo.toString(), true); |
| 79 | + } catch (Exception ex) { |
| 80 | + throw new RuntimeException(ex); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments