Skip to content

Commit

Permalink
test: create cursorexecutor tests scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Mar 30, 2024
1 parent 0c6949f commit 9ea9614
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Expand Up @@ -76,6 +76,7 @@ private static CriteriaCondition condition(SelectQuery query, PageRequest.Cursor
CriteriaCondition condition = null;
CriteriaCondition previousCondition = null;
List<Sort<?>> sorts = query.sorts();
checkCursorKeySizes(cursor, sorts);
for (int index = 0; index < sorts.size(); index++) {
Sort<?> sort = sorts.get(index);
Object key = cursor.get(index);
Expand All @@ -90,6 +91,8 @@ private static CriteriaCondition condition(SelectQuery query, PageRequest.Cursor
}
return condition;
}


}, CURSOR_PREVIOUS {
@SuppressWarnings("unchecked")
@Override
Expand Down Expand Up @@ -117,6 +120,7 @@ private static CriteriaCondition condition(SelectQuery query, PageRequest.Cursor
CriteriaCondition condition = null;
CriteriaCondition previousCondition = null;
List<Sort<?>> sorts = query.sorts();
checkCursorKeySizes(cursor, sorts);
for (int index = 0; index < sorts.size(); index++) {
Sort<?> sort = sorts.get(index);
Object key = cursor.get(index);
Expand Down Expand Up @@ -161,4 +165,11 @@ private static DefaultSelectQuery updateQuery(int pageRequest, SelectQuery query
query.condition().map(c -> CriteriaCondition.and(c, condition))
.orElse(condition));
}

private static void checkCursorKeySizes(PageRequest.Cursor cursor, List<Sort<?>> sorts) {
if(sorts.size() != cursor.size()) {
throw new IllegalArgumentException("The cursor size is different from the sort size. Cursor: "
+ cursor.size() + " Sort: " + sorts.size());
}
}
}
Expand Up @@ -71,6 +71,28 @@ void shouldReturnPaginationOffSet() {
});
}

@Test
void shouldReturnPaginationOffSetWhenReturnEmpty() {
SelectQuery query = SelectQuery.select().from("person")
.orderBy("name").asc().build();

Mockito.when(databaseManager.select(Mockito.any(SelectQuery.class)))
.thenReturn(Stream.empty());

CursoredPage<CommunicationEntity> entities = databaseManager.selectCursor(query,
PageRequest.ofSize(10));

assertSoftly(soft -> {
PageRequest<CommunicationEntity> pageRequest = entities.pageRequest();
soft.assertThat(entities.hasNext()).isFalse();
soft.assertThat(entities.hasPrevious()).isFalse();

soft.assertThat(entities).hasSize(0);
soft.assertThat(pageRequest.mode())
.isEqualTo(PageRequest.Mode.OFFSET);
});
}

@Test
void shouldReturnPaginationOffSet2() {
SelectQuery query = SelectQuery.select().from("person")
Expand Down Expand Up @@ -386,6 +408,28 @@ void shouldReturnPaginationBeforeKeyAndReturnEmpty() {
});
}

@Test
void shouldReturnErrorSortSizeDifferentFromOrderSizeBeforeKey() {
SelectQuery query = SelectQuery.select().from("person")
.orderBy("name").asc()
.orderBy("age").asc()
.orderBy("id").asc().build();

assertThrows(IllegalArgumentException.class, () -> databaseManager.selectCursor(query,
PageRequest.ofSize(10).beforeKey("Ada", 20)));
}

@Test
void shouldReturnErrorSortSizeDifferentFromOrderSizeAfterKey() {
SelectQuery query = SelectQuery.select().from("person")
.orderBy("name").asc()
.orderBy("age").asc()
.orderBy("id").asc().build();

assertThrows(IllegalArgumentException.class, () -> databaseManager.selectCursor(query,
PageRequest.ofSize(10).afterKey("Ada", 20)));
}


private Stream<CommunicationEntity> stream() {
var entity = CommunicationEntity.of("name");
Expand Down

0 comments on commit 9ea9614

Please sign in to comment.