Skip to content

Commit

Permalink
Merge pull request #601 from njr-11/458-queries-without-all-clauses
Browse files Browse the repository at this point in the history
TCK tests for queries without all clauses
  • Loading branch information
otaviojava authored Mar 28, 2024
2 parents 1b675d5 + d773e8d commit b72c6e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Set;
import java.util.stream.Stream;

import jakarta.data.Limit;
import jakarta.data.Order;
import jakarta.data.Sort;
import jakarta.data.page.Page;
Expand All @@ -39,6 +40,12 @@
@Repository
public interface AsciiCharacters extends DataRepository<AsciiCharacter, Long>, IdOperations<AsciiCharacter> {

@Query(" ") // it is valid to have a query with no clauses
Stream<AsciiCharacter> all(Limit limit, Sort<?>... sort);

@Query("ORDER BY id ASC")
Stream<AsciiCharacter> alphabetic(Limit limit);

int countByHexadecimalNotNull();

boolean existsByThisCharacter(char ch);
Expand Down Expand Up @@ -78,6 +85,9 @@ Stream<AsciiCharacter> findByHexadecimalIgnoreCaseBetweenAndHexadecimalNotIn(Str
" order by id asc")
char[] getABCDFO();

@Query("SELECT hexadecimal WHERE hexadecimal IS NOT NULL AND thisCharacter = ?1")
Optional<String> hex(char ch);

@Query("WHERE hexadecimal <> ' ORDER BY isn''t a keyword when inside a literal' AND hexadecimal IN ('4a', '4b', '4c', ?1)")
Stream<AsciiCharacter> jklOr(String hex);

Expand All @@ -86,6 +96,9 @@ default Stream<AsciiCharacter> retrieveAlphaNumericIn(long minId, long maxId) {
.filter(c -> Character.isLetterOrDigit(c.getThisCharacter()));
}

@Query("SELECT thisCharacter ORDER BY id DESC")
char[] reverseAlphabetic(Limit limit);

@Save
List<AsciiCharacter> saveAll(List<AsciiCharacter> characters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,15 @@ public void testDescendingSort() {
Arrays.toString(stream.map(number -> number.getId()).toArray()));
}

@Assertion(id = "458", strategy = "Use a repository method with a JDQL query that has no clauses.")
public void testEmptyQuery() {

assertEquals(List.of('a', 'b', 'c', 'd', 'e', 'f'),
characters.all(Limit.range(97, 102), Sort.asc("id"))
.map(AsciiCharacter::getThisCharacter)
.collect(Collectors.toList()));
}

@Assertion(id = "133", strategy = "Use a repository method that returns a single entity value where no result is found. Expect EmptyResultException.")
public void testEmptyResultException() {
try {
Expand Down Expand Up @@ -1341,6 +1350,22 @@ public void testPageOfNothing() {
assertEquals(0L, page.totalPages());
}

@Assertion(id = "458", strategy = "Use a repository method with a JDQL query that consists of only an ORDER BY clause.")
public void testPartialQueryOrderBy() {

assertEquals(List.of('A', 'B', 'C', 'D', 'E', 'F'),
characters.alphabetic(Limit.range(65, 70))
.map(AsciiCharacter::getThisCharacter)
.collect(Collectors.toList()));
}

@Assertion(id = "458", strategy = "Use a repository method with a JDQL query that consists of only the SELECT and ORDER BY clauses.")
public void testPartialQuerySelectAndOrderBy() {

assertEquals("zyxwvuts",
String.valueOf(characters.reverseAlphabetic(Limit.range(6, 13))));
}

@Assertion(id = "133", strategy = "Use count and exists methods where the primary entity class is inferred from the lifecycle methods.")
public void testPrimaryEntityClassDeterminedByLifeCycleMethods() {
assertEquals(4, customRepo.countByIdIn(Set.of(2L, 15L, 37L, -5L, 60L)));
Expand All @@ -1359,6 +1384,13 @@ public void testQueryWithNot() {
assertEquals("ABCDFO", String.valueOf(characters.getABCDFO()));
}

@Assertion(id = "458", strategy = "Use a repository method with a JDQL query that uses the NULL keyword.")
public void testQueryWithNull() {

assertEquals("4a", characters.hex('J').orElseThrow());
assertEquals("44", characters.hex('D').orElseThrow());
}

@Assertion(id = "458", strategy = "Use a repository method with a JDQL query that relies on the OR operator.")
public void testQueryWithOr() {
PageRequest<?> page1Request = PageRequest.ofSize(4).sortBy(Sort.desc("numBitsRequired"), Sort.asc("id"));
Expand Down

0 comments on commit b72c6e3

Please sign in to comment.