Skip to content

Commit

Permalink
docs: Refresh view query language section (#144)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Peter Vlugter <[email protected]>
  • Loading branch information
johanandren and pvlugter authored Jan 16, 2025
1 parent cafcb58 commit d28fb66
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import org.junit.jupiter.api.extension.ExtendWith;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.HOURS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -173,7 +175,9 @@ public void verifyAllTheFieldTypesView() throws Exception {
// see that we can persist and read a row with all fields, no indexed columns
var id = newId();
var row = new AllTheTypesKvEntity.AllTheTypes(1, 2L, 3F, 4D, true, "text", 5, 6L, 7F, 8D, false,
Instant.now(), ZonedDateTime.now(),
Instant.now(),
// Note: we turn it into a timestamp internally, so the specific TZ is lost (but the exact point in time stays the same)
ZonedDateTime.now().withZoneSameInstant(ZoneId.of("Z")),
Optional.of("optional"), List.of("text1", "text2"),
new AllTheTypesKvEntity.ByEmail("[email protected]"),
AllTheTypesKvEntity.AnEnum.THREE, new AllTheTypesKvEntity.Recursive(new AllTheTypesKvEntity.Recursive(null, "level2"), "level1"));
Expand Down Expand Up @@ -221,6 +225,85 @@ public void verifyAllTheFieldTypesView() throws Exception {

assertThat(rows).hasSize(1);
});

Awaitility.await()
.ignoreExceptions()
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> {
var result = await(componentClient.forView()
.method(AllTheTypesView::countRows)
.invokeAsync());

assertThat(result.count()).isEqualTo(1);
});


Awaitility.await()
.ignoreExceptions()
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> {
var rows = await(componentClient.forView()
.stream(AllTheTypesView::compareInstant)
.source(new AllTheTypesView.InstantRequest(Instant.now().minus(3, DAYS)))
.runWith(Sink.seq(), testKit.getMaterializer()));

assertThat(rows).hasSize(1);
});

Awaitility.await()
.ignoreExceptions()
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> {
var rows = await(componentClient.forView()
.stream(AllTheTypesView::groupQuery)
.source()
.runWith(Sink.seq(), testKit.getMaterializer()));

assertThat(rows).hasSize(1);
assertThat(rows.getFirst().grouped()).hasSize(1);
assertThat(rows.getFirst().grouped().getFirst()).isEqualTo(row);
assertThat(rows.getFirst().totalCount()).isEqualTo(1L);
});

Awaitility.await()
.ignoreExceptions()
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> {
var rows = await(componentClient.forView()
.stream(AllTheTypesView::projectedGroupQuery)
.source()
.runWith(Sink.seq(), testKit.getMaterializer()));

assertThat(rows).hasSize(1);
assertThat(rows.getFirst().groupedStringValues()).hasSize(1);
assertThat(rows.getFirst().groupedStringValues().getFirst()).isEqualTo(row.stringValue());
assertThat(rows.getFirst().totalCount()).isEqualTo(1L);
});


Awaitility.await()
.ignoreExceptions()
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> {
var rows = await(componentClient.forView()
.stream(AllTheTypesView::nullableQuery)
.source()
.runWith(Sink.seq(), testKit.getMaterializer()));

assertThat(rows).hasSize(1);
});

Awaitility.await()
.ignoreExceptions()
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> {
var page = await(componentClient.forView()
.method(AllTheTypesView::paging)
.invokeAsync(new AllTheTypesView.PageRequest("")));

assertThat(page.entries()).hasSize(1);
assertThat(page.hasMore()).isFalse();
});
}

@Disabled // pending primitive query parameters working
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,43 @@ public QueryStreamEffect<AllTheTypesKvEntity.AllTheTypes> allRows() {
return queryStreamResult();
}

public record CountResult(long count) {}
@Query("SELECT COUNT(*) FROM events")
public QueryEffect<CountResult> countRows() {
return queryResult();
}

public record InstantRequest(Instant instant) {}
@Query("SELECT * FROM events WHERE instant > :instant")
public QueryStreamEffect<AllTheTypesKvEntity.AllTheTypes> compareInstant(InstantRequest request) { return queryStreamResult(); }

public record GroupResult(List<AllTheTypesKvEntity.AllTheTypes> grouped, long totalCount) {}
@Query("SELECT collect(*) AS grouped, total_count() FROM events GROUP BY intValue")
public QueryStreamEffect<GroupResult> groupQuery() { return queryStreamResult(); }

public record ProjectedGroupResult(int intValue, List<String> groupedStringValues, long totalCount) {}
@Query("SELECT intValue, stringValue AS groupedStringValues, total_count() FROM events GROUP BY intValue")
public QueryStreamEffect<ProjectedGroupResult> projectedGroupQuery() { return queryStreamResult(); }


@Query("SELECT * FROM events WHERE optionalString IS NOT NULL AND nestedMessage.email IS NOT NULL")
public QueryStreamEffect<AllTheTypesKvEntity.AllTheTypes> nullableQuery() {
return queryStreamResult();
}

public record PageRequest(String pageToken) {}
public record Page(List<AllTheTypesKvEntity.AllTheTypes> entries, String nextPageToken, boolean hasMore) { }

@Query("""
SELECT * AS entries, next_page_token() AS nextPageToken, has_more() AS hasMore
FROM events
OFFSET page_token_offset(:pageToken)
LIMIT 10
""")
public QueryEffect<Page> paging(PageRequest request) {
return queryResult();
}

public record BeforeRequest(Instant instant) {}

@Query("SELECT * FROM events WHERE zonedDateTime < :instant")
Expand Down
Loading

0 comments on commit d28fb66

Please sign in to comment.