Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Refresh view query language section #144

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,70 @@ 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::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,38 @@ 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 * AS grouped, total_count() FROM events GROUP BY intValue")
public QueryStreamEffect<GroupResult> groupQuery() { 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
Loading