Skip to content

Commit

Permalink
samples: Add sample snippets for IN, NOT_EQUALS, and NOT_IN (#749)
Browse files Browse the repository at this point in the history
* test: add samples for IN, NOT_IN, and NOT_EQUALS.

* Set up another Datastore client for tests that require a real backend.

* Add a method for setting up a test entity. Fix linting errors.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
jlara310 and gcf-owl-bot[bot] authored Jun 14, 2022
1 parent 9a779d2 commit 3efd61c
Showing 1 changed file with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.cloud.datastore.Cursor;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.EntityQuery;
import com.google.cloud.datastore.FullEntity;
Expand Down Expand Up @@ -83,6 +84,7 @@ public class ConceptsTest {
private static final FullEntity<IncompleteKey> TEST_FULL_ENTITY = FullEntity.newBuilder().build();

private Datastore datastore;
private Datastore datastoreRealBackend;
private KeyFactory keyFactory;
private Key taskKey;
private Entity testEntity;
Expand Down Expand Up @@ -123,6 +125,8 @@ public void setUp() {
endDate = Timestamp.of(calendar.getTime());
calendar.set(1999, DECEMBER, 31);
includedDate = Timestamp.of(calendar.getTime());
// Create a client for tests that require a real backend
datastoreRealBackend = DatastoreOptions.getDefaultInstance().getService();
}

/**
Expand Down Expand Up @@ -384,7 +388,7 @@ private void setUpQueryTests() {
.set(
"description",
StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
.set("tag", "fun", "l", "programming")
.set("tag", "fun", "l", "programming", "learn")
.build());
}

Expand Down Expand Up @@ -1044,4 +1048,101 @@ public void testPropertyFilteringRunQuery() {
ImmutableMap.of("Task", ImmutableSet.of("priority", "tag"));
assertEquals(expected, propertiesByKind);
}

@Test
public void testEqQuerySorted() {
setUpQueryTests();
// [START datastore_eq_query_sorted]
Query<Entity> query =
Query.newEntityQueryBuilder()
.setKind("Task")
.setFilter(PropertyFilter.eq("tag", "learn"))
.setOrderBy(OrderBy.asc("tag"))
.build();
// [END datastore_eq_query_sorted]
assertValidQuery(query);
}

/** Start tests using a real backend. */
private <V> V assertValidQueryRealBackend(Query<V> query) {
QueryResults<V> results = datastoreRealBackend.run(query);
V result = results.next();
// assertFalse(results.hasNext());
return result;
}

private void setUpQueryTestsRealBackend() {
Key taskKey =
datastoreRealBackend
.newKeyFactory()
.setKind("Task")
.addAncestors(PathElement.of("TaskList", "default"))
.newKey("someTask");
datastoreRealBackend.put(
Entity.newBuilder(taskKey)
.set("category", "Personal")
.set("done", false)
.set("completed", false)
.set("priority", 4)
.set("created", includedDate)
.set("percent_complete", 10.0)
.set(
"description",
StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
.set("tag", "fun", "l", "programming", "learn")
.build());
}

@Test
public void testInQuery() {
setUpQueryTestsRealBackend();
// [START datastore_in_query]
Query<Entity> query =
Query.newEntityQueryBuilder()
.setKind("Task")
.setFilter(PropertyFilter.in("tag", ListValue.of("learn", "study")))
.build();
// [END datastore_in_query]
assertValidQueryRealBackend(query);
}

@Test
public void testNotEqualsQuery() {
setUpQueryTestsRealBackend();
// [START datastore_not_equals_query]
Query<Entity> query =
Query.newEntityQueryBuilder()
.setKind("Task")
.setFilter(PropertyFilter.neq("category", "Work"))
.build();
// [END datastore_not_equals_query]
assertValidQueryRealBackend(query);
}

@Test
public void testNotInQuery() {
setUpQueryTestsRealBackend();
// [START datastore_not_in_query]
Query<Entity> query =
Query.newEntityQueryBuilder()
.setKind("Task")
.setFilter(PropertyFilter.not_in("category", ListValue.of("Work", "Chores", "School")))
.build();
// [END datastore_not_in_query]
assertValidQueryRealBackend(query);
}

@Test
public void testInQuerySorted() {
setUpQueryTestsRealBackend();
// [START datastore_in_query_sorted]
Query<Entity> query =
Query.newEntityQueryBuilder()
.setKind("Task")
.setFilter(PropertyFilter.in("tag", ListValue.of("learn", "study")))
.setOrderBy(OrderBy.asc("tag"))
.build();
// [END datastore_in_query_sorted]
assertValidQueryRealBackend(query);
}
}

0 comments on commit 3efd61c

Please sign in to comment.