|
| 1 | +package org.hypertrace.core.documentstore.query; |
| 2 | + |
| 3 | +import static java.util.Collections.emptyList; |
| 4 | +import static org.hypertrace.core.documentstore.expression.operators.SortOrder.DESC; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | + |
| 7 | +import org.hypertrace.core.documentstore.expression.impl.ConstantExpression; |
| 8 | +import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; |
| 9 | +import org.hypertrace.core.documentstore.expression.impl.UnnestExpression; |
| 10 | +import org.hypertrace.core.documentstore.query.Query.QueryBuilder; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.TestInstance; |
| 14 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 15 | + |
| 16 | +@TestInstance(Lifecycle.PER_CLASS) |
| 17 | +class QueryBuilderTest { |
| 18 | + |
| 19 | + private QueryBuilder queryBuilder; |
| 20 | + |
| 21 | + @BeforeEach |
| 22 | + void setUp() { |
| 23 | + queryBuilder = Query.builder(); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testSetAndClearSelections() { |
| 28 | + final Query queryWithSelections = |
| 29 | + queryBuilder.addSelection(ConstantExpression.of("Something")).build(); |
| 30 | + assertEquals(1, queryWithSelections.getSelections().size()); |
| 31 | + |
| 32 | + final Query queryWithoutSelections = queryBuilder.setSelections(emptyList()).build(); |
| 33 | + assertEquals(0, queryWithoutSelections.getSelections().size()); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testSetAndClearSorts() { |
| 38 | + final Query queryWithSorts = |
| 39 | + queryBuilder.addSort(IdentifierExpression.of("Something"), DESC).build(); |
| 40 | + assertEquals(1, queryWithSorts.getSorts().size()); |
| 41 | + |
| 42 | + final Query queryWithoutSorts = queryBuilder.setSorts(emptyList()).build(); |
| 43 | + assertEquals(0, queryWithoutSorts.getSorts().size()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testSetAndClearAggregations() { |
| 48 | + final Query queryWithAggregations = |
| 49 | + queryBuilder.addAggregation(IdentifierExpression.of("Something")).build(); |
| 50 | + assertEquals(1, queryWithAggregations.getAggregations().size()); |
| 51 | + |
| 52 | + final Query queryWithoutAggregations = queryBuilder.setAggregations(emptyList()).build(); |
| 53 | + assertEquals(0, queryWithoutAggregations.getAggregations().size()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testSetAndClearFromClauses() { |
| 58 | + final Query queryWithFromClauses = |
| 59 | + queryBuilder |
| 60 | + .addFromClause(UnnestExpression.of(IdentifierExpression.of("Something"), false)) |
| 61 | + .build(); |
| 62 | + assertEquals(1, queryWithFromClauses.getFromTypeExpressions().size()); |
| 63 | + |
| 64 | + final Query queryWithoutFromClauses = queryBuilder.setFromClauses(emptyList()).build(); |
| 65 | + assertEquals(0, queryWithoutFromClauses.getFromTypeExpressions().size()); |
| 66 | + } |
| 67 | +} |
0 commit comments