DATACOUCH-214 - Adding case insensitive sorting

Closes #105
This commit is contained in:
James Thomson
2016-03-10 19:31:52 +00:00
committed by Simon Baslé
parent ee58b4d6e4
commit 0149144bc0
4 changed files with 38 additions and 2 deletions

View File

@@ -84,12 +84,25 @@ public class N1qlCouchbaseRepositoryTests {
assertNotNull("Expected to find several parties", previousDesc);
}
@Test
public void shouldSortWithoutCaseSensitivity() {
Iterable<Party> parties = repository.findAll(new Sort(new Sort.Order(Sort.Direction.DESC, "desc").ignoreCase()));
String previousDesc = null;
for (Party party : parties) {
if (previousDesc != null) {
assertTrue(party.getDescription().compareToIgnoreCase(previousDesc) <= 0);
}
previousDesc = party.getDescription();
}
assertNotNull("Expected to find several parties", previousDesc);
}
@Test
public void shouldPageThroughEntities() {
Pageable pageable = new PageRequest(0, 8);
Page<Party> page1 = repository.findAll(pageable);
assertEquals(13, page1.getTotalElements()); //12 generated parties + 1 specifically crafted party
assertEquals(15, page1.getTotalElements()); //12 generated parties + 3 specifically crafted party
assertEquals(8, page1.getNumberOfElements());
}
}

View File

@@ -55,6 +55,8 @@ public class PartyPopulatorListener extends DependencyInjectionTestExecutionList
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 01);
template.save(new Party("aTestParty", "New Year's Eve 90", "Happy New Year", cal.getTime(), 1230000, new Point(100, 100)));
template.save(new Party("lowercaseParty", "lowercase party", "lowercase party", cal.getTime(), 1000, new Point(100, 100)));
template.save(new Party("uppercaseParty", "Uppercase party", "Uppercase party", cal.getTime(), 1000, new Point(100, 100)));
}
private void createAndWaitForDesignDocs(Bucket client) {

View File

@@ -22,12 +22,14 @@ import static com.couchbase.client.java.query.dsl.Expression.path;
import static com.couchbase.client.java.query.dsl.Expression.s;
import static com.couchbase.client.java.query.dsl.functions.AggregateFunctions.count;
import static com.couchbase.client.java.query.dsl.functions.MetaFunctions.meta;
import static com.couchbase.client.java.query.dsl.functions.StringFunctions.lower;
import java.util.ArrayList;
import java.util.List;
import com.couchbase.client.java.query.Statement;
import com.couchbase.client.java.query.dsl.Expression;
import com.couchbase.client.java.query.dsl.functions.TypeFunctions;
import com.couchbase.client.java.query.dsl.path.FromPath;
import com.couchbase.client.java.query.dsl.path.WherePath;
import com.couchbase.client.java.repository.annotation.Field;
@@ -145,8 +147,11 @@ public class N1qlUtils {
List<com.couchbase.client.java.query.dsl.Sort> cbSortList = new ArrayList<com.couchbase.client.java.query.dsl.Sort>();
for (Sort.Order order : sort) {
String orderProperty = order.getProperty();
//FIXME the order property should be converted to its corresponding fieldName
//FIXME the order property should be converted to its corresponding fieldName
Expression orderFieldName = i(orderProperty);
if (order.isIgnoreCase()) {
orderFieldName = lower(TypeFunctions.toString(orderFieldName));
}
if (order.isAscending()) {
cbSortList.add(com.couchbase.client.java.query.dsl.Sort.asc(orderFieldName));
} else {

View File

@@ -101,6 +101,22 @@ public class N1qlUtilsTest {
verifyZeroInteractions(converter);
}
@Test
public void testCreateSortIgnoresCaseWhenSpecified() throws Exception {
CouchbaseConverter converter = mock(CouchbaseConverter.class);
Sort sortDescription = new Sort(
new Sort.Order(Sort.Direction.ASC, "description").ignoreCase(),
new Sort.Order(Sort.Direction.ASC, "attendees")
);
com.couchbase.client.java.query.dsl.Sort[] realSort = N1qlUtils.createSort(sortDescription, converter);
assertEquals(2, realSort.length);
assertEquals(com.couchbase.client.java.query.dsl.Sort.asc("LOWER(TOSTRING(`description`))").toString(), realSort[0].toString());
assertEquals(com.couchbase.client.java.query.dsl.Sort.asc("`attendees`").toString(), realSort[1].toString());
verifyZeroInteractions(converter);
}
@Test
public void testCreateCountQueryForEntity() throws Exception {
CouchbaseConverter converter = mock(CouchbaseConverter.class);