-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31a12f5
commit ff86e5d
Showing
5 changed files
with
98 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/CountItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...est/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryCountMockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |