Skip to content

Commit

Permalink
feat: update prepare statement
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed May 19, 2024
1 parent 8dd4ecd commit 4a61498
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public <T> Optional<T> singleResult() {
return singleResult.map(converter::toEntity);
}

@Override
public long count() {
return preparedStatement.count();
}

/**
* Optionally returns the underlying {@link SelectQuery} associated with this PreparedStatement,
* if applicable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,29 @@ void shouldFindByAgeNotGreaterThan() {
assertEquals(Element.of("age", 10), condition.element());
}

@Test
void shouldCount() {

PreparedStatement statement = Mockito.mock(PreparedStatement.class);
when(template.prepare(Mockito.anyString(), Mockito.anyString())).thenReturn(statement);

when(template.count(any(SelectQuery.class)))
.thenReturn(10L);

long result = personRepository.count("Ada", 10);

assertEquals(10L, result);

ArgumentCaptor<SelectQuery> captor = ArgumentCaptor.forClass(SelectQuery.class);
verify(template).count(captor.capture());
SelectQuery query = captor.getValue();
CriteriaCondition negate = query.condition().get();
assertEquals(Condition.NOT, negate.condition());
CriteriaCondition condition = negate.element().get(CriteriaCondition.class);
assertEquals(GREATER_THAN, condition.condition());
assertEquals(Element.of("age", 10), condition.element());
}

@Test
void shouldConvertMapAddressRepository() {

Expand Down Expand Up @@ -796,6 +819,9 @@ interface PersonRepository extends NoSQLRepository<Person, Long> {

@Query("FROM Person WHERE age = ?1")
Optional<Person> findByQuery(int age);

@Query("select count(this) FROM Person WHERE name = ?1 and age > ?2")
long count(String name, int age);
}

public interface VendorRepository extends CrudRepository<Vendor, String> {
Expand Down

0 comments on commit 4a61498

Please sign in to comment.