private void prepareQueryOrder(QueryBuilder<?> builder, Query q) { if (builder.getPreOrders().isEmpty()) { return; } for (QueryOrder order : builder.getPreOrders()) { String string = getActualFieldName(order.getProperty(), builder.getModel().getClazz()); q.addSort(string, getSortDirection(order)); } }
protected void doSort(String kind, String pName, Object fDat, Query.SortDirection direction) { Query query = new Query(kind, rootKey); query.addSort(pName, direction); Object[] result = getResult(query, pName); assertEquals(fDat.toString(), result[0].toString()); }
@Test(expected = DatastoreNeedIndexException.class) public void testQueryWithInequalityFilterAndSortOnAnotherPropertyRequiresConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setFilter(new Query.FilterPredicate("someProperty", GREATER_THAN, "foo")) .addSort("someProperty") .addSort("otherProperty")); }
@Test public void testQueryOrder() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("intData", Integer.class)); query.addSort("stringData", Query.SortDirection.DESCENDING); List<Entity> results = service.prepare(query).asList(fetchOption); assertEquals(count, results.size()); int first = new Integer(results.get(0).getProperty("intData").toString()); int last = new Integer(results.get(count - 1).getProperty("intData").toString()); assertTrue(first > last); }
@Test(expected = DatastoreNeedIndexException.class) public void testQueryWithDescendingSortOrderOnKeysRequiresConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .addSort(Entity.KEY_RESERVED_PROPERTY, Query.SortDirection.DESCENDING)); }
protected void doSort(String kind, String pName, int expDat, Query.SortDirection direction) { Query query = new Query(kind, rootKey); query.addSort(pName, direction); Object[] result = getResult(query, pName); assertEquals(expDat, result.length); }
@Override public void doSort(String kind, String pName, int expDat, Query.SortDirection direction) { Query query = new Query(kind); query.addSort(pName, direction); Object[] result = getResult(query, pName); assertEquals(expDat, result.length); } }
@Test @InSequence(2) public void testStep2BigAsList() { Query q = new Query(kindName, rootKey); q.addSort("count", Query.SortDirection.DESCENDING); List<Entity> eData = service.prepare(q).asList(fo); assertEquals(bigCount, eData.size()); assertEquals(new Integer(bigCount - 1).longValue(), eData.get(0).getProperty("count")); }
@Test public void testQueryFilter() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.setFilter(new FilterPredicate("intData", FilterOperator.NOT_EQUAL, 50)); query.addSort("intData"); List<Entity> results = service.prepare(query).asList(fetchOption); assertEquals(count - 1, results.size()); for (Entity e : results) { assertTrue(e.getProperty("stringData").toString().contains("5") == false); } }
@Test public void testSortPredicates() { Query query = new Query(kindName, rootKey); query.addSort("intData", Query.SortDirection.DESCENDING); List<Entity> es = service.prepare(query).asList(fo); assertEquals((long) 40, es.get(0).getProperty("intData")); List<Query.SortPredicate> qsp = query.getSortPredicates(); assertEquals("check SortPredicate name", "intData", qsp.get(0).getPropertyName()); assertEquals("check SortPredicate direction", Query.SortDirection.DESCENDING, qsp.get(0).getDirection()); }
@Test public void testQueryWithAncestorAndSortOrderOnKeyPropertyDoesNotRequireConfiguredIndex() throws Exception { executeQuery(new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .addSort(Entity.KEY_RESERVED_PROPERTY)); }
@Test public void testEndCursor() { int limit = total / testDat.length; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); // fetch 1st page Cursor cursor = checkPage(query, null, null, limit, limit, testDat[0], testDat[0]); Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString()); // fetch 1st page again since using decodedCursor as end cursor checkPage(query, null, decodedCursor, limit, limit, testDat[0], testDat[0]); }
@Test public void testQueryWithInequalityFilterAndFirstSortOnDifferentPropertyThrowsIllegalArgumentException() throws Exception { Query query = createQuery() .setFilter(new Query.FilterPredicate("foo", GREATER_THAN, 3)) .addSort("bar"); assertIAEWhenAccessingResult(service.prepare(query)); }
protected List<Entity> doQuery(String kind, String pName, Class<?> type, boolean indexed) { FetchOptions fo = FetchOptions.Builder.withDefaults(); Query query = new Query(kind, rootKey); if (indexed) { query.addProjection(new PropertyProjection(pName, type)); query.addSort(pName); } return service.prepare(query).asList(fo); }
@Test public void testDistinctSort() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.addProjection(new PropertyProjection("floatData", Float.class)); query.addSort("stringData", Query.SortDirection.DESCENDING); query.setDistinct(true); assertEquals(7, service.prepare(query).countEntities(fo)); assertTrue(query.getDistinct()); query.addSort("floatData", Query.SortDirection.DESCENDING); assertEquals(7, service.prepare(query).countEntities(fo)); }
@Test public void testIntegerPropertySortingIsNotLexicographic() throws Exception { String methodName = "testIntegerPropertySortingIsNotLexicographic"; Entity parent = createTestEntityWithUniqueMethodNameKey(QUERY_SORTING_ENTITY, methodName); Key key = parent.getKey(); Entity ten = storeTestEntityWithSingleProperty(key, 10); Entity five = storeTestEntityWithSingleProperty(key, 5); Query query = createQuery().setAncestor(key).addSort(SINGLE_PROPERTY_NAME, ASCENDING); List<Entity> results = service.prepare(query).asList(withDefaults()); assertTrue(results.indexOf(five) < results.indexOf(ten)); // if sorting were lexicographic, "10" would come before "5" service.delete(ten.getKey(), five.getKey()); }
@Test public void testAncestorKey() { Key pKey = getParent().getKey(); Query query = new Query(CHILDKIND, pKey); query.addSort("__key__"); assertEquals(2, service.prepare(query) .countEntities(FetchOptions.Builder.withDefaults())); for (Entity cRec : service.prepare(query).asIterable()) { assertEquals(pKey, cRec.getParent()); } }
@Test public void testDistinctFilter() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); query.addProjection(new PropertyProjection("floatData", Float.class)); query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.NOT_EQUAL, "string1")); query.addSort("stringData", Query.SortDirection.DESCENDING); query.setDistinct(true); assertEquals(5, service.prepare(query).countEntities(fo)); assertTrue(query.getDistinct()); }