@Test public void testSetFilterList() { // [0,50,90], [1,51,91], [2,52,92] Query query = new Query(kindName, rootKey); List<Filter> filterList = new ArrayList<>(); filterList.add(Query.FilterOperator.LESS_THAN.of("intList", 5)); filterList.add(Query.FilterOperator.GREATER_THAN.of("intList", 90)); Filter filter1 = Query.CompositeFilterOperator.OR.of(filterList); Filter filter2 = Query.FilterOperator.EQUAL.of("intList", 52); query.setFilter(Query.CompositeFilterOperator.AND.of(filter1, filter2)); assertEquals(1, service.prepare(query).countEntities(fo)); }
@Test public void testPropertyMetadata() { NamespaceManager.set(namespaceDat[2]); // sort by kind/property, kindDat[1] < kindDat[0] < kindDat[2] Query q = new Query("__property__").addSort(Entity.KEY_RESERVED_PROPERTY).setKeysOnly(); // filter out properties for kind "testing" Key key1 = Entities.createPropertyKey(kindDat[0], "urlData"); Key key2 = Entities.createPropertyKey(kindDat[2], "urlData"); q.setFilter(CompositeFilterOperator.and( new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, key1), new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, key2))); List<Entity> el = service.prepare(q).asList(fo); // un-indexed property, textData, will not be returned in __property__ queries. assertEquals(13, el.size()); for (int i = 0; i < el.size(); i++) { assertEquals(namespaceDat[2], el.get(i).getKey().getNamespace()); assertEquals(kindDat[2], el.get(i).getKey().getParent().getName()); if (i == 0) { assertEquals("adressData", el.get(0).getKey().getName()); } else if (i == el.size() - 1) { assertEquals("urlData", el.get(el.size() - 1).getKey().getName()); } } }
@Test public void testWithPropertyProjection() { Query query = new Query(kindName, rootKey); query.addProjection(new PropertyProjection("geoptData", GeoPt.class)); Filter filter1 = Query.CompositeFilterOperator.or( Query.FilterOperator.LESS_THAN.of("intList", 5), Query.FilterOperator.GREATER_THAN.of("intList", 90)); Filter filter2 = Query.FilterOperator.EQUAL.of("intList", 52); query.setFilter(Query.CompositeFilterOperator.and(filter1, filter2)); // sql statement String sql = "SELECT geoptData FROM " + kindName; sql += " WHERE ((intList < 5 or intList > 90) AND intList = 52)"; sql += " AND __ancestor__ is " + rootKey; assertEquals(sql.toLowerCase(), query.toString().toLowerCase()); // check query result List<Entity> results = service.prepare(query).asList(fo); Assert.assertTrue(results.size() > 0); assertEquals(new GeoPt((float) (2.12), (float) (2.98)), results.get(0).getProperty("geoptData")); for (Entity e : results) { assertEquals(1, e.getProperties().size()); assertTrue(e.getProperties().containsKey("geoptData")); } }
@Test public void testMultipleFilters() throws Exception { Key parentKey = createQueryBasicsTestParent("testMultipleFilters"); Entity johnDoe = createEntity("Person", parentKey) .withProperty("name", "John") .withProperty("lastName", "Doe") .store(); Entity johnBooks = createEntity("Person", parentKey) .withProperty("name", "John") .withProperty("lastName", "Books") .store(); Entity janeDoe = createEntity("Person", parentKey) .withProperty("name", "Jane") .withProperty("lastName", "Doe") .store(); Query query = new Query("Person") .setAncestor(parentKey) .setFilter(and( new Query.FilterPredicate("name", EQUAL, "John"), new Query.FilterPredicate("lastName", EQUAL, "Doe"))); assertSingleResult(johnDoe, query); }
@Ignore("According to the docs, ordering of query results is undefined when no sort order is specified. They are " + "currently ordered according to the index, but this may change in the future and so it shouldn't be tested by the TCK.") @Test public void testDefaultSortOrderIsDefinedByIndexDefinition() throws Exception { Key parentKey = createQueryBasicsTestParent("testDefaultSortOrderIsDefinedByIndexDefinition"); Entity aaa = createEntity("Product", parentKey) .withProperty("name", "aaa") .withProperty("price", 10) .store(); Entity ccc = createEntity("Product", parentKey) .withProperty("name", "ccc") .withProperty("price", 10) .store(); Entity bbb = createEntity("Product", parentKey) .withProperty("name", "bbb") .withProperty("price", 10) .store(); Query query = new Query("Product") .setAncestor(parentKey) .setFilter(and( new Query.FilterPredicate("name", GREATER_THAN_OR_EQUAL, "aaa"), new Query.FilterPredicate("price", EQUAL, 10) )); assertEquals(Arrays.asList(aaa, bbb, ccc), service.prepare(query).asList(withDefaults())); }
@Test public void queryWithNotEqualFilter() throws Exception { String testMethodName = "queryWithNotEqualFilter"; Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, testMethodName); Key parentKey = parentEntity.getKey(); Entity entity12 = storeTestEntityWithSingleProperty(parentKey, Arrays.asList(1, 2)); Entity entity123 = storeTestEntityWithSingleProperty(parentKey, Arrays.asList(1, 2, 3)); // The NOT_EQUAL operator works as a "value is other than" test. assertSet( queryReturns(entity12, entity123), whenFilteringWith(new FilterPredicate(SINGLE_PROPERTY_NAME, NOT_EQUAL, 1), parentKey)); assertSet( queryReturns(entity123), whenFilteringWith(and( new FilterPredicate(SINGLE_PROPERTY_NAME, NOT_EQUAL, 1), new FilterPredicate(SINGLE_PROPERTY_NAME, NOT_EQUAL, 2)), parentKey) ); // NOTE: should only match entity123, but not entity12 }
@Test public void testCompositeFilter() { Query query = new Query(kindName, rootKey); Filter filter = Query.CompositeFilterOperator.and( Query.FilterOperator.LESS_THAN_OR_EQUAL.of("intData", 40), Query.FilterOperator.GREATER_THAN.of("intData", 0)); query.setFilter(filter); query.addSort("intData", Query.SortDirection.DESCENDING); List<Entity> es = service.prepare(query).asList(fo); assertEquals("check return count", 2, es.size()); assertEquals("check query filter", filter, query.getFilter()); assertEquals("check query key only", false, query.isKeysOnly()); Query.CompositeFilter cf = (Query.CompositeFilter) query.getFilter(); assertEquals(2, cf.getSubFilters().size()); assertEquals(Query.CompositeFilterOperator.AND, cf.getOperator()); }
@Test public void testAncestorQueryWithEqualityAndInequalityFiltersAndSortOnASinglePropertyDoesRequireConfiguredIndex() throws Exception { try { executeQuery( new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter( and(new Query.FilterPredicate("someProperty", GREATER_THAN, "a"), new Query.FilterPredicate("someProperty", EQUAL, "b"))) .addSort("someProperty")); } catch (Exception e) { assertTrue(EXPECTED_DS_INDEX_MSG + " instead got " + e.toString(), e instanceof DatastoreNeedIndexException); return; } fail(EXPECTED_DS_INDEX_MSG); }
@Test public void testQueryWithEqualityAndInequalityFiltersAndSortOnASinglePropertyDoesRequireConfiguredIndex() throws Exception { try { executeQuery( new Query("Unindexed") .setFilter( and(new Query.FilterPredicate("someProperty", GREATER_THAN, "a"), new Query.FilterPredicate("someProperty", EQUAL, "b"))) .addSort("someProperty")); } catch (Exception e) { assertTrue(EXPECTED_DS_INDEX_MSG + " instead got " + e.toString(), e instanceof DatastoreNeedIndexException); return; } fail(EXPECTED_DS_INDEX_MSG); }
@Test public void testStrFilter() { Query q = new Query(kindName); q.setAncestor(rootKey); Query.Filter filter = Query.CompositeFilterOperator.and( new FilterPredicate("stringData", Query.FilterOperator.LESS_THAN, "qqq"), new FilterPredicate("stringData", Query.FilterOperator.GREATER_THAN, "mmm")); q.setFilter(filter); q.addSort("stringData", Query.SortDirection.ASCENDING); assertEquals(2, service.prepare(q).countEntities(fo)); List<Entity> elist = service.prepare(q).asList(fo); assertEquals(Arrays.asList("abc", "xyz", "mno"), elist.get(0).getProperty("stringData")); assertEquals(Arrays.asList("ppp", "iii", "ddd"), elist.get(1).getProperty("stringData")); }
/** * Google issueId:1458158 */ @Test public void testIntFilter() { Query q = new Query(kindName); Query.Filter filter = Query.CompositeFilterOperator.and( new FilterPredicate("intData1", Query.FilterOperator.LESS_THAN, 20), new FilterPredicate("intData1", Query.FilterOperator.GREATER_THAN, 1), new FilterPredicate("intData1", Query.FilterOperator.EQUAL, null)); q.setFilter(filter); q.addSort("intData1", Query.SortDirection.ASCENDING); q.setAncestor(rootKey); assertEquals(1, service.prepare(q).countEntities(fo)); List<Entity> elist = service.prepare(q).asList(fo); assertEquals(Arrays.asList(1L, 10L, null), elist.get(0).getProperty("intData1")); }
@Test public void queryWithMultipleEqualityFiltersOnMultivaluedPropertyReturnsEntityIfAllFiltersMatch() throws Exception { String testMethodName = "queryWithMultipleInequalityFiltersOnMultivaluedPropertyReturnsNothing"; Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, testMethodName); Key parentKey = parentEntity.getKey(); Entity entity = storeTestEntityWithSingleProperty(parentKey, Arrays.asList(1, 2)); assertSet( queryReturns(entity), whenFilteringWith(and( new FilterPredicate(SINGLE_PROPERTY_NAME, EQUAL, 1), new FilterPredicate(SINGLE_PROPERTY_NAME, EQUAL, 2)), parentKey) ); }
@Test public void queryWithMultipleInequalityFiltersOnMultivaluedPropertyReturnsNothing() throws Exception { String testMethodName = "queryWithMultipleEqualityFiltersOnMultivaluedPropertyReturnsEntityIfAllFiltersMatch"; Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, testMethodName); Key parentKey = parentEntity.getKey(); storeTestEntityWithSingleProperty(parentKey, Arrays.asList(1, 2)); assertSet( queryReturnsNothing(), whenFilteringWith(and( new FilterPredicate(SINGLE_PROPERTY_NAME, GREATER_THAN, 1), new FilterPredicate(SINGLE_PROPERTY_NAME, LESS_THAN, 2)), parentKey) ); }
private CompositeFilter getTestMethodFilter(String testMethodTag) { FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId); FilterPredicate method = new FilterPredicate(TEST_METHOD_TAG, FilterOperator.EQUAL, testMethodTag); return CompositeFilterOperator.and(testRunFilter, method); }
@Test public void testQueryUsingOnlyAncestorFiltersAndEqualityFiltersOnPropertiesAndInequalityFiltersOnKeysDoesNotRequireConfiguredIndex() throws Exception { executeQuery(new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter( and(new Query.FilterPredicate("someProperty", EQUAL, "foo"), new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, KeyFactory.createKey("Unindexed", 1))))); }
@Test(expected = DatastoreNeedIndexException.class) public void testAncestorQueryWithInequalityFilterOnSomePropertyAndEqualityFilterOnSamePropertyRequiresConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter( and(new Query.FilterPredicate("someProperty", EQUAL, "b"), new Query.FilterPredicate("someProperty", GREATER_THAN, "a")))); }
@Test(expected = DatastoreNeedIndexException.class) public void testQueryWithInequalityFilterOnSomePropertyAndEqualityFilterOnOtherPropertyRequiresConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setFilter( and(new Query.FilterPredicate("someProperty", GREATER_THAN, "a"), new Query.FilterPredicate("otherProperty", EQUAL, "b")))); }
@Test public void testQueryUsingOnlyAncestorAndEqualityFiltersDoesNotRequireConfiguredIndex() throws Exception { executeQuery(new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter(new Query.FilterPredicate("someProperty", EQUAL, "foo"))); executeQuery(new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter( and(new Query.FilterPredicate("someProperty", EQUAL, "foo"), new Query.FilterPredicate("otherProperty", EQUAL, "bar")))); }
@Test public void testQueryWithInequalityFiltersOnMultiplePropertiesThrowsIllegalArgumentException() throws Exception { Query query = createQuery() .setFilter(and( new Query.FilterPredicate("weight", GREATER_THAN, 3), new Query.FilterPredicate("size", GREATER_THAN, 5))); assertIAEWhenAccessingResult(service.prepare(query)); }