Skip to content

Add test coverage for total count edge cases #509

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

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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,7 +34,6 @@
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLScalarType;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -118,12 +117,7 @@ public PagedResult<Object> get(DataFetchingEnvironment environment) {
}

if (totalSelection.isPresent() || pagesSelection.isPresent()) {
final var selectResult = pagedResult.getSelect();

final long total = recordsSelection.isEmpty() ||
selectResult.filter(Predicate.not(Collection::isEmpty)).isPresent()
? queryFactory.queryTotalCount(environment, restrictedKeys)
: 0L;
final Long total = queryFactory.queryTotalCount(environment, restrictedKeys);

pagedResult.withTotal(total);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class PagedResult<T> {

Expand Down Expand Up @@ -136,10 +135,6 @@ public Builder<T> withSelect(List<T> select) {
return this;
}

public Optional<List<T>> getSelect() {
return Optional.ofNullable(select);
}

/**
* Builder method for select parameter.
* @param select field to set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,20 @@ public void queryWhereRoot() {
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryWhereRoot2() {
//given:
String query = "query { Humans( page: { start: 4, limit: 2 }) { pages, total, select { name } } }";

String expected = "{Humans={pages=3, total=5, select=[]}}";

//when:
Object result = executor.execute(query).getData();

//then:
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryWhereRootPagedWithVariables() {
//given:
Expand Down Expand Up @@ -427,6 +441,20 @@ public void queryPaginationWithoutRecords() {
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryPaginationWithoutRecords2() {
//given:
String query = "query { Humans ( page: { start: 4, limit: 2 }) { pages, total } }";

String expected = "{Humans={pages=3, total=5}}";

//when:
Object result = executor.execute(query).getData();

//then:
assertThat(result.toString()).isEqualTo(expected);
}

@Test
public void queryOrderByFields() {
//given:
Expand Down
Loading