List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(1)); if (results.size() != 1 || !results.get(0).getProperty("name").toString().contains(backupName)) { System.err.println("BuiltinDatatoreToBigqueryIngesterTask: can't find backupName: " + backupName);
@Test public void testKeyOrder() { Query query = new Query(kindName, rootKey); query.addSort("__key__"); List<Entity> ascRecs = service.prepare(query).asList(withLimit(5)); query = new Query(kindName, rootKey); query.addSort("__key__", Query.SortDirection.DESCENDING); List<Entity> descRecs = service.prepare(query).asList(withLimit(5)); int size = ascRecs.size(); assertEquals(5, size); for (int i = 0; i < size; i++) { assertEquals(ascRecs.get(i).getProperty("pop").toString(), descRecs.get(size - i - 1).getProperty("pop").toString()); } }
@Test public void testLimit() { List<Entity> results = executeQuery(withLimit(2)); assertEquals(asList(foo1, foo2), results); }
opts = withLimit(intExclNo); } else {
@Test public void testWithIneqiAndFilter() { Query query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("loc", Query.FilterOperator.EQUAL, "ae")); Key key = service.prepare(query).asSingleEntity().getKey(); query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("__key__", Query.FilterOperator.LESS_THAN, key)); query.setFilter(new FilterPredicate("loc", Query.FilterOperator.EQUAL, "ac")); query.addSort("__key__"); List<Entity> ascRecs = service.prepare(query).asList(withLimit(5)); query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("__key__", Query.FilterOperator.LESS_THAN, key)); query.setFilter(new FilterPredicate("loc", Query.FilterOperator.EQUAL, "ac")); query.addSort("__key__", Query.SortDirection.DESCENDING); List<Entity> descRecs = service.prepare(query).asList(withLimit(5)); int size = ascRecs.size(); for (int i = 0; i < size; i++) { assertEquals(ascRecs.get(i).getProperty("pop").toString(), descRecs.get(size - i - 1).getProperty("pop").toString()); } }
@Test public void testStartEndCursor() { int limit = total / testDat.length; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit); // fetch 1st page and get cursor1 QueryResultList<Entity> nextBatch = service.prepare(query) .asQueryResultList(fetchOption); Cursor cursor1 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // fetch 2nd page and get cursor2 nextBatch = service.prepare(query).asQueryResultList(fetchOption.startCursor(cursor1)); Cursor cursor2 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // cursor1 as start and cursor2 as end and 15 in limit -- -- should return 2nd page. checkPage(query, cursor1, cursor2, limit, limit, testDat[1], testDat[1]); // cursor1 as start and cursor2 as end and 30 in limit -- should return 2nd page. checkPage(query, cursor1, cursor2, 2 * limit, limit, testDat[1], testDat[1]); // cursor2 as start and cursor1 as end and 15 in limit -- should not return any. checkPage(query, cursor2, cursor1, limit, 0, null, null); }
private Cursor checkPage(Query query, Cursor stCursor, Cursor endCursor, int limit, int exptRet, String chkSt, String chkEnd) { FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit); if (stCursor != null) { fetchOption = fetchOption.startCursor(stCursor); } if (endCursor != null) { fetchOption = fetchOption.endCursor(endCursor); } QueryResultList<Entity> nextBatch = service.prepare(query) .asQueryResultList(fetchOption); assertEquals(exptRet, nextBatch.size()); if (chkSt != null) { assertEquals(chkSt, nextBatch.get(0).getProperty("name")); } if (chkEnd != null) { assertEquals(chkEnd, nextBatch.get(nextBatch.size() - 1).getProperty("name")); } return nextBatch.getCursor(); } }
@Test public void testWithIneqi() { Query query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("loc", Query.FilterOperator.EQUAL, "ae")); Key key = service.prepare(query).asSingleEntity().getKey(); query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("__key__", Query.FilterOperator.GREATER_THAN, key)); query.addSort("__key__"); List<Entity> ascRecs = service.prepare(query).asList(withLimit(5)); query = new Query(kindName, rootKey); query.setFilter(new FilterPredicate("__key__", Query.FilterOperator.GREATER_THAN, key)); query.addSort("__key__", Query.SortDirection.DESCENDING); List<Entity> descRecs = service.prepare(query).asList(withLimit(5)); int size = ascRecs.size(); for (int i = 0; i < size; i++) { assertEquals(ascRecs.get(i).getProperty("pop").toString(), descRecs.get(size - i - 1).getProperty("pop").toString()); } }
private void checkQueryWithLimit(int limit) { FetchOptions fo = FetchOptions.Builder.withLimit(limit); Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("stringData", String.class)); List<Entity> results = service.prepare(query).asList(fo); assertEquals(limit, results.size()); } }
@Test @InSequence(5) public void testStep5IteratorWithOption() { FetchOptions foIterator = FetchOptions.Builder.withLimit(limit); Query q = new Query(kindName, rootKey); Iterator<Entity> eData = service.prepare(q).asIterator(foIterator); assertEquals(limit, getSize(eData)); foIterator = FetchOptions.Builder.withOffset(offset); eData = service.prepare(q).asIterator(foIterator); assertEquals(bigCount - offset, getSize(eData)); }
@Test public void testEndCursorAndLimit() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withEndCursor(cursor).limit(2)); assertEquals(asList(foo1, foo2), results); results = executeQuery(withEndCursor(cursor).limit(5)); // even if limit is past endCursor, endCursor will still apply assertEquals(asList(foo1, foo2, foo3), results); }
@Test public void testEndCursorAndOffset() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withEndCursor(cursor).offset(1)); assertEquals(asList(foo2, foo3), results); }
@Test public void testStartCursor() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withStartCursor(cursor)); assertEquals(asList(foo4, foo5), results); }
@Test @InSequence(7) public void testStep7IterableWithOption() { FetchOptions foIterable = FetchOptions.Builder.withLimit(limit); Query q = new Query(kindName, rootKey); Iterator<Entity> eData = service.prepare(q).asIterator(fo.limit(limit)); assertEquals(limit, getSize(eData)); foIterable = FetchOptions.Builder.withOffset(offset); eData = service.prepare(q).asIterator(foIterable); assertEquals(bigCount - offset, getSize(eData)); }
@Test public void testStartCursorAndOffset() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withStartCursor(cursor).offset(1)); assertEquals(asList(foo5), results); }
@Test public void testStartCursorAndLimit() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withStartCursor(cursor).limit(1)); assertEquals(asList(foo4), results); }
@Test public void testEndCursor() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withEndCursor(cursor)); assertEquals(asList(foo1, foo2, foo3), results); }
@Test public void testEndCursorAndOffsetAndLimit() { QueryResultList<Entity> results = executeQuery(withLimit(3)); Cursor cursor = results.getCursor(); // points to foo4 results = executeQuery(withEndCursor(cursor).offset(1).limit(2)); assertEquals(asList(foo2, foo3), results); results = executeQuery(withEndCursor(cursor).offset(1).limit(5)); assertEquals(asList(foo2, foo3), results); }
@Test public void testEndCursorLessThanOffset() { QueryResultList<Entity> results = executeQuery(withLimit(1)); Cursor cursor = results.getCursor(); // points to foo2 results = executeQuery(withEndCursor(cursor).offset(3)); assertTrue(results.isEmpty()); }
@Test @InSequence(3) public void testStep3ListWithOption() { FetchOptions foList = FetchOptions.Builder.withLimit(limit); Query q = new Query(kindName, rootKey); List<Entity> eData = service.prepare(q).asList(foList); assertEquals(limit, eData.size()); foList = FetchOptions.Builder.withOffset(offset); eData = service.prepare(q).asList(foList); assertEquals(bigCount - offset, eData.size()); }