Skip to content

Commit

Permalink
improve count feature
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-thorel-of committed Feb 7, 2024
1 parent 31a12f5 commit ff86e5d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ default Page<T> search(Object criteria) {
}


/**
* Count all items
*
* @return count result
*/
default long count() {
return count(null);
}

/**
* Count from criteria object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public void visit(SelectFilter filter) {
* @param filter select filter
*/
public void visit(CountFilter filter) {
builder.append("count()");
if(filter.getGroupBy() != null) {
builder.append(COMA).append(filter.getGroupBy());
}
builder.append(filter.getMethod());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package fr.ouestfrance.querydsl.postgrest.model;

import lombok.*;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashMap;

@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class CountItem extends HashMap<String, String> {

public static CountItem of(int count) {
CountItem countItem = new CountItem();
countItem.put("count", String.valueOf(count));
return countItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,12 @@
*/
@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class CountFilter implements Filter, FilterVisitor {

/**
* Default query param key for selection
*/
private static final String KEY_PARAMETER = "select";
/**
* list of fields
*/
private String groupBy;


/**
* Create select filter from embedded resources
* @return select filter
*/
public static Filter groupBy(String field) {
return new CountFilter(field);
}

/**
* Create select filter from embedded resources
Expand All @@ -53,21 +39,7 @@ public String getKey() {
return KEY_PARAMETER;
}


/**
* Attribute name
*/
@Getter
@RequiredArgsConstructor
public static class Attribute {
/**
* alias
*/
private final String alias;
/**
* value selected
*/
private final String value;

public String getMethod() {
return "count()";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package fr.ouestfrance.querydsl.postgrest;

import fr.ouestfrance.querydsl.postgrest.app.Post;
import fr.ouestfrance.querydsl.postgrest.app.PostRepository;
import fr.ouestfrance.querydsl.postgrest.app.PostRequest;
import fr.ouestfrance.querydsl.postgrest.app.PostRequestWithSize;
import fr.ouestfrance.querydsl.postgrest.model.CountItem;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;


class PostgrestRepositoryCountMockTest extends AbstractRepositoryMockTest {

@Mock
private PostgrestClient postgrestClient;

private PostgrestRepository<Post> repository;

@BeforeEach
void beforeEach() {
repository = new PostRepository(postgrestClient);
}


@Test
void shouldCountWhithoutCriteriaOrNull() {
when(postgrestClient.count(anyString(), any())).thenReturn(List.of(CountItem.of(1)));
assertEquals(1, repository.count(null));
assertEquals(1, repository.count());
}

@Test
void shouldCountWhithCriteria() {
PostRequest request = new PostRequest();
request.setUserId(1);
request.setId(1);
request.setTitle("Test*");
request.setCodes(List.of("a", "b", "c"));
request.setExcludes(List.of("z"));
request.setValidDate(LocalDate.of(2023, 11, 10));
ArgumentCaptor<Map<String, List<String>>> queryArgs = multiMapCaptor();
when(postgrestClient.count(anyString(), queryArgs.capture())).thenReturn(List.of(CountItem.of(1)));

assertEquals(1, repository.count(request));
Map<String, List<String>> queries = queryArgs.getValue();
assertEquals("eq.1", queries.get("userId").stream().findFirst().orElseThrow());
assertEquals("neq.1", queries.get("id").stream().findFirst().orElseThrow());
assertEquals("lte.2023-11-10", queries.get("startDate").stream().findFirst().orElseThrow());
assertEquals("(endDate.gte.2023-11-10,endDate.is.null)", queries.get("or").stream().findFirst().orElseThrow());
assertEquals("like.Test*", queries.get("title").stream().findFirst().orElseThrow());
assertEquals("count()", queries.get("select").stream().findFirst().orElseThrow());
}

@Test
void shouldNotUseAnotherSelect() {
PostRequestWithSize request = new PostRequestWithSize();
request.setSize("25");
ArgumentCaptor<Map<String, List<String>>> queryArgs = multiMapCaptor();
when(postgrestClient.count(anyString(), queryArgs.capture())).thenReturn(List.of(CountItem.of(1)));
assertEquals(1, repository.count(request));
Map<String, List<String>> queries = queryArgs.getValue();
assertEquals(1, queries.get("select").size());
assertEquals("count()", queries.get("select").stream().findFirst().orElseThrow());
}
}

0 comments on commit ff86e5d

Please sign in to comment.