Skip to content

Commit 9292ac7

Browse files
authored
Add test coverage for total count edge cases (#509)
* Revert "Skip total count if select result is empty" This reverts commit 7ca1394. * chore: Add test coverage for total count edge case
1 parent d57f9f7 commit 9292ac7

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaQueryDataFetcher.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import graphql.schema.DataFetcher;
3535
import graphql.schema.DataFetchingEnvironment;
3636
import graphql.schema.GraphQLScalarType;
37-
import java.util.Collection;
3837
import java.util.LinkedHashMap;
3938
import java.util.List;
4039
import java.util.Map;
@@ -118,12 +117,7 @@ public PagedResult<Object> get(DataFetchingEnvironment environment) {
118117
}
119118

120119
if (totalSelection.isPresent() || pagesSelection.isPresent()) {
121-
final var selectResult = pagedResult.getSelect();
122-
123-
final long total = recordsSelection.isEmpty() ||
124-
selectResult.filter(Predicate.not(Collection::isEmpty)).isPresent()
125-
? queryFactory.queryTotalCount(environment, restrictedKeys)
126-
: 0L;
120+
final Long total = queryFactory.queryTotalCount(environment, restrictedKeys);
127121

128122
pagedResult.withTotal(total);
129123
}

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/PagedResult.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.LinkedHashMap;
2121
import java.util.List;
2222
import java.util.Map;
23-
import java.util.Optional;
2423

2524
public class PagedResult<T> {
2625

@@ -136,10 +135,6 @@ public Builder<T> withSelect(List<T> select) {
136135
return this;
137136
}
138137

139-
public Optional<List<T>> getSelect() {
140-
return Optional.ofNullable(select);
141-
}
142-
143138
/**
144139
* Builder method for select parameter.
145140
* @param select field to set

schema/src/test/java/com/introproventures/graphql/jpa/query/support/StarwarsQueryExecutorTestsSupport.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ public void queryWhereRoot() {
392392
assertThat(result.toString()).isEqualTo(expected);
393393
}
394394

395+
@Test
396+
public void queryWhereRoot2() {
397+
//given:
398+
String query = "query { Humans( page: { start: 4, limit: 2 }) { pages, total, select { name } } }";
399+
400+
String expected = "{Humans={pages=3, total=5, select=[]}}";
401+
402+
//when:
403+
Object result = executor.execute(query).getData();
404+
405+
//then:
406+
assertThat(result.toString()).isEqualTo(expected);
407+
}
408+
395409
@Test
396410
public void queryWhereRootPagedWithVariables() {
397411
//given:
@@ -427,6 +441,20 @@ public void queryPaginationWithoutRecords() {
427441
assertThat(result.toString()).isEqualTo(expected);
428442
}
429443

444+
@Test
445+
public void queryPaginationWithoutRecords2() {
446+
//given:
447+
String query = "query { Humans ( page: { start: 4, limit: 2 }) { pages, total } }";
448+
449+
String expected = "{Humans={pages=3, total=5}}";
450+
451+
//when:
452+
Object result = executor.execute(query).getData();
453+
454+
//then:
455+
assertThat(result.toString()).isEqualTo(expected);
456+
}
457+
430458
@Test
431459
public void queryOrderByFields() {
432460
//given:

0 commit comments

Comments
 (0)