diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Datastore.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Datastore.java index efc56d532751..2d590bdaa730 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Datastore.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Datastore.java @@ -52,6 +52,18 @@ interface TransactionCallable { * Any thrown exception will cause the transaction to rollback and will be propagated * as a {@link DatastoreException} with the original exception as its root cause. * + *

Example of running in a transaction. + *

 {@code
+   * String callableResult = "my_callable_result";
+   * TransactionCallable callable = new TransactionCallable() {
+   *   public String run(DatastoreReaderWriter readerWriter) {
+   *     // use readerWriter to run in transaction
+   *     return callableResult;
+   *   }
+   * };
+   * String result = datastore.runInTransaction(callable);
+   * }
+ * * @param callable the callback to call with a newly created transactional readerWriter * @throws DatastoreException upon failure */ @@ -59,6 +71,21 @@ interface TransactionCallable { /** * Returns a new Batch for processing multiple write operations in one request. + * + *

Example of starting a new batch. + *

 {@code
+   * String keyName1 = "my_key_name_1";
+   * String keyName2 = "my_key_name_2";
+   * Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1);
+   * Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2);
+   * Batch batch = datastore.newBatch();
+   * Entity entity1 = Entity.builder(key1).set("name", "John").build();
+   * Entity entity2 = Entity.builder(key2).set("title", "title").build();
+   * batch.add(entity1);
+   * batch.add(entity2);
+   * batch.submit();
+   * }
+ * */ Batch newBatch(); @@ -67,6 +94,15 @@ interface TransactionCallable { * The returned key will have the same information (projectId, kind, namespace and ancestors) * as the given key and will have a newly assigned id. * + *

Example of allocating an id. + *

 {@code
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
+   * IncompleteKey incompleteKey = keyFactory.newKey();
+   * 
+   * // let cloud datastore automatically assign an id
+   * Key key = datastore.allocateId(incompleteKey);
+   * }
+ * * @throws DatastoreException upon failure */ Key allocateId(IncompleteKey key); @@ -74,6 +110,16 @@ interface TransactionCallable { /** * Returns a list of keys using the allocated ids ordered by the input. * + *

Example of allocating multiple ids in a single batch. + *

 {@code
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
+   * IncompleteKey incompleteKey1 = keyFactory.newKey();
+   * IncompleteKey incompleteKey2 = keyFactory.newKey();
+   * 
+   * // let cloud datastore automatically assign the ids
+   * List keys = datastore.allocateId(incompleteKey1, incompleteKey2);
+   * }
+ * * @throws DatastoreException upon failure * @see #allocateId(IncompleteKey) */ @@ -81,6 +127,24 @@ interface TransactionCallable { /** * {@inheritDoc} + * + *

Example of updating multiple entities. + *

 {@code
+   * String keyName1 = "my_key_name_1";
+   * String keyName2 = "my_key_name_2";
+   * Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1);
+   * Entity.Builder entityBuilder1 = Entity.builder(key1);
+   * entityBuilder1.set("propertyName", "updatedValue1");
+   * Entity entity1 = entityBuilder1.build();
+   * 
+   * Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2);
+   * Entity.Builder entityBuilder2 = Entity.builder(key2);
+   * entityBuilder2.set("propertyName", "updatedValue2");
+   * Entity entity2 = entityBuilder2.build();
+   * 
+   * datastore.update(entity1, entity2);
+   * }
+ * * @throws DatastoreException upon failure */ @Override @@ -88,6 +152,17 @@ interface TransactionCallable { /** * {@inheritDoc} + * + *

Example of putting a single entity. + *

 {@code
+   * String keyName = "my_key_name";
+   * Key key = datastore.newKeyFactory().kind("MyKind").newKey(keyName);
+   * Entity.Builder entityBuilder = Entity.builder(key);
+   * entityBuilder.set("propertyName", "value");
+   * Entity entity = entityBuilder.build();
+   * datastore.put(entity);
+   * }
+ * * @throws DatastoreException upon failure */ @Override @@ -95,6 +170,24 @@ interface TransactionCallable { /** * {@inheritDoc} + * + *

Example of putting multiple entities. + *

 {@code
+   * String keyName1 = "my_key_name1";
+   * String keyName2 = "my_key_name2";
+   * Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1);
+   * Entity.Builder entityBuilder1 = Entity.builder(key1);
+   * entityBuilder1.set("propertyName", "value1");
+   * Entity entity1 = entityBuilder1.build();
+   * 
+   * Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2);
+   * Entity.Builder entityBuilder2 = Entity.builder(key2);
+   * entityBuilder2.set("propertyName", "value2");
+   * Entity entity2 = entityBuilder2.build();
+   * 
+   * datastore.put(entity1, entity2);
+   * }
+ * * @throws DatastoreException upon failure */ @Override @@ -102,6 +195,16 @@ interface TransactionCallable { /** * {@inheritDoc} + * + *

Example of deleting multiple entities. + *

 {@code
+   * String keyName1 = "my_key_name1";
+   * String keyName2 = "my_key_name2";
+   * Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1);
+   * Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2);
+   * datastore.delete(key1, key2);
+   * }
+ * * @throws DatastoreException upon failure */ @Override @@ -109,6 +212,12 @@ interface TransactionCallable { /** * Returns a new KeyFactory for this service + * + *

Example of creating a {@code KeyFactory}. + *

 {@code
+   * KeyFactory keyFactory = datastore.newKeyFactory();
+   * }
+ * */ KeyFactory newKeyFactory(); @@ -116,6 +225,14 @@ interface TransactionCallable { * Returns an {@link Entity} for the given {@link Key} or {@code null} if it doesn't exist. * {@link ReadOption}s can be specified if desired. * + *

Example of getting an entity. + *

 {@code
+   * String keyName = "my_key_name";
+   * Key key = datastore.newKeyFactory().kind("MyKind").newKey(keyName);
+   * Entity entity = datastore.get(key);
+   * // Do something with the entity
+   * }
+ * * @throws DatastoreException upon failure */ Entity get(Key key, ReadOption... options); @@ -127,6 +244,22 @@ interface TransactionCallable { * {@link Iterator#hasNext hasNext} or {@link Iterator#next next} methods. {@link ReadOption}s can * be specified if desired. * + *

Example of getting multiple entity objects. + *

 {@code
+   * String firstKeyName = "my_first_key_name";
+   * String secondKeyName = "my_second_key_name";
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
+   * Key firstKey = keyFactory.newKey(firstKeyName);
+   * Key secondKey = keyFactory.newKey(secondKeyName);
+   * Iterator entitiesIterator = datastore.get(Lists.newArrayList(firstKey, secondKey));
+   * List entities = Lists.newArrayList();
+   * while (entitiesIterator.hasNext()) {
+   *   Entity entity = entitiesIterator.next();
+   *   // do something with the entity
+   *   entities.add(entity);
+   * }
+   * }
+ * * @throws DatastoreException upon failure * @see #get(Key) */ @@ -136,6 +269,20 @@ interface TransactionCallable { * Returns a list with a value for each given key (ordered by input). {@code null} values are * returned for nonexistent keys. When possible prefer using {@link #get(Key...)} to avoid eagerly * loading the results. {@link ReadOption}s can be specified if desired. + * + *

Example of fetching a list of Entity objects. + *

 {@code
+   * String firstKeyName = "my_first_key_name";
+   * String secondKeyName = "my_second_key_name";
+   * KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind");
+   * Key firstKey = keyFactory.newKey(firstKeyName);
+   * Key secondKey = keyFactory.newKey(secondKeyName);
+   * List entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey));
+   * for (Entity entity : entities) {
+   *   // do something with the entity
+   * }
+   * }
+ * */ List fetch(Iterable keys, ReadOption... options); @@ -143,6 +290,39 @@ interface TransactionCallable { * Submits a {@link Query} and returns its result. {@link ReadOption}s can be specified if * desired. * + *

Example of running a query to find all entities of one kind. + *

 {@code
+   * String kind = "my_kind";
+   * StructuredQuery query = Query.entityQueryBuilder()
+   *     .kind(kind)
+   *     .build();
+   * QueryResults results = datastore.run(query);
+   * List entities = Lists.newArrayList();
+   * while (results.hasNext()) {
+   *   Entity result = results.next();
+   *   // do something with result
+   *   entities.add(result);
+   * }
+   * }
+ * + *

Example of running a query to find all entities with a matching property value. + *

 {@code
+   * String kind = "my_kind";
+   * String property = "my_property";
+   * String value = "my_value";
+   * StructuredQuery query = Query.entityQueryBuilder()
+   *     .kind(kind)
+   *     .filter(PropertyFilter.eq(property, value))
+   *     .build();
+   * QueryResults results = datastore.run(query);
+   * List entities = Lists.newArrayList();
+   * while (results.hasNext()) {
+   *   Entity result = results.next();
+   *   // do something with result
+   *   entities.add(result);
+   * }
+   * }
+ * * @throws DatastoreException upon failure */ QueryResults run(Query query, ReadOption... options); diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/DatastoreSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/DatastoreSnippets.java new file mode 100644 index 000000000000..7eee484af92a --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/DatastoreSnippets.java @@ -0,0 +1,308 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file is referenced in Datastore's javadoc. Any change to this file should be reflected in + * Datastore's javadoc. + */ + +package com.google.cloud.examples.datastore.snippets; + +import com.google.cloud.datastore.Batch; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.Datastore.TransactionCallable; +import com.google.cloud.datastore.DatastoreReaderWriter; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.IncompleteKey; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.KeyFactory; +import com.google.cloud.datastore.Query; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.StructuredQuery; +import com.google.cloud.datastore.StructuredQuery.PropertyFilter; +import com.google.common.collect.Lists; + +import java.util.Iterator; +import java.util.List; + +/** + * This class contains a number of snippets for the {@link Datastore} interface. + */ +public class DatastoreSnippets { + + private final Datastore datastore; + + public DatastoreSnippets(Datastore datastore) { + this.datastore = datastore; + } + + /** + * Example of running in a transaction. + */ + // [TARGET runInTransaction(TransactionCallable)] + // [VARIABLE "my_callable_result"] + public String runInTransaction(final String callableResult) { + // [START runInTransaction] + TransactionCallable callable = new TransactionCallable() { + public String run(DatastoreReaderWriter readerWriter) { + // use readerWriter to run in transaction + return callableResult; + } + }; + String result = datastore.runInTransaction(callable); + // [END runInTransaction] + return result; + } + + /** + * Example of starting a new batch. + */ + // [TARGET newBatch()] + // [VARIABLE "my_key_name_1"] + // [VARIABLE "my_key_name_2"] + public Batch newBatch(String keyName1, String keyName2) { + // [START newBatch] + Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1); + Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2); + Batch batch = datastore.newBatch(); + Entity entity1 = Entity.builder(key1).set("name", "John").build(); + Entity entity2 = Entity.builder(key2).set("title", "title").build(); + batch.add(entity1); + batch.add(entity2); + batch.submit(); + // [END newBatch] + return batch; + } + + /** + * Example of allocating an id. + */ + // [TARGET allocateId(IncompleteKey)] + public Key allocateIdSingle() { + // [START allocateIdSingle] + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); + IncompleteKey incompleteKey = keyFactory.newKey(); + + // let cloud datastore automatically assign an id + Key key = datastore.allocateId(incompleteKey); + // [END allocateIdSingle] + return key; + } + + /** + * Example of allocating multiple ids in a single batch. + */ + // [TARGET allocateId(IncompleteKey...)] + public List batchAllocateId() { + // [START batchAllocateId] + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); + IncompleteKey incompleteKey1 = keyFactory.newKey(); + IncompleteKey incompleteKey2 = keyFactory.newKey(); + + // let cloud datastore automatically assign the ids + List keys = datastore.allocateId(incompleteKey1, incompleteKey2); + // [END batchAllocateId] + return keys; + } + + /** + * Example of updating multiple entities. + */ + // [TARGET update(Entity...)] + // [VARIABLE "my_key_name_1"] + // [VARIABLE "my_key_name_2"] + public void batchUpdateEntities(String keyName1, String keyName2) { + // [START batchUpdateEntities] + Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1); + Entity.Builder entityBuilder1 = Entity.builder(key1); + entityBuilder1.set("propertyName", "updatedValue1"); + Entity entity1 = entityBuilder1.build(); + + Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2); + Entity.Builder entityBuilder2 = Entity.builder(key2); + entityBuilder2.set("propertyName", "updatedValue2"); + Entity entity2 = entityBuilder2.build(); + + datastore.update(entity1, entity2); + // [END batchUpdateEntities] + } + + /** + * Example of putting a single entity. + */ + // [TARGET put(FullEntity)] + // [VARIABLE "my_key_name"] + public void putSingleEntity(String keyName) { + // [START putSingleEntity] + Key key = datastore.newKeyFactory().kind("MyKind").newKey(keyName); + Entity.Builder entityBuilder = Entity.builder(key); + entityBuilder.set("propertyName", "value"); + Entity entity = entityBuilder.build(); + datastore.put(entity); + // [END putSingleEntity] + } + + /** + * Example of putting multiple entities. + */ + // [TARGET put(FullEntity...)] + // [VARIABLE "my_key_name1"] + // [VARIABLE "my_key_name2"] + public void batchPutEntities(String keyName1, String keyName2) { + // [START batchPutEntities] + Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1); + Entity.Builder entityBuilder1 = Entity.builder(key1); + entityBuilder1.set("propertyName", "value1"); + Entity entity1 = entityBuilder1.build(); + + Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2); + Entity.Builder entityBuilder2 = Entity.builder(key2); + entityBuilder2.set("propertyName", "value2"); + Entity entity2 = entityBuilder2.build(); + + datastore.put(entity1, entity2); + // [END batchPutEntities] + } + + /** + * Example of deleting multiple entities. + */ + // [TARGET delete(Key...)] + // [VARIABLE "my_key_name1"] + // [VARIABLE "my_key_name2"] + public void batchDeleteEntities(String keyName1, String keyName2) { + // [START batchDeleteEntities] + Key key1 = datastore.newKeyFactory().kind("MyKind").newKey(keyName1); + Key key2 = datastore.newKeyFactory().kind("MyKind").newKey(keyName2); + datastore.delete(key1, key2); + // [END batchDeleteEntities] + } + + /** + * Example of creating a {@code KeyFactory}. + */ + // [TARGET newKeyFactory()] + public KeyFactory createKeyFactory() { + // [START createKeyFactory] + KeyFactory keyFactory = datastore.newKeyFactory(); + // [END createKeyFactory] + return keyFactory; + } + + /** + * Example of getting an entity. + */ + // [TARGET get(Key, ReadOption...)] + // [VARIABLE "my_key_name"] + public Entity getEntityWithKey(String keyName) { + // [START getEntityWithKey] + Key key = datastore.newKeyFactory().kind("MyKind").newKey(keyName); + Entity entity = datastore.get(key); + // Do something with the entity + // [END getEntityWithKey] + return entity; + } + + /** + * Example of getting multiple entity objects. + */ + // [TARGET get(Iterable, ReadOption...)] + // [VARIABLE "my_first_key_name"] + // [VARIABLE "my_second_key_name"] + public List getEntitiesWithKeys(String firstKeyName, String secondKeyName) { + // TODO change so that it's not necessary to hold the entities in a list for integration testing + // [START getEntitiesWithKeys] + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); + Key firstKey = keyFactory.newKey(firstKeyName); + Key secondKey = keyFactory.newKey(secondKeyName); + Iterator entitiesIterator = datastore.get(Lists.newArrayList(firstKey, secondKey)); + List entities = Lists.newArrayList(); + while (entitiesIterator.hasNext()) { + Entity entity = entitiesIterator.next(); + // do something with the entity + entities.add(entity); + } + // [END getEntitiesWithKeys] + return entities; + } + + /** + * Example of fetching a list of Entity objects. + */ + // [TARGET fetch(Iterable, ReadOption...)] + // [VARIABLE "my_first_key_name"] + // [VARIABLE "my_second_key_name"] + public List fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) { + // [START fetchEntitiesWithKeys] + KeyFactory keyFactory = datastore.newKeyFactory().kind("MyKind"); + Key firstKey = keyFactory.newKey(firstKeyName); + Key secondKey = keyFactory.newKey(secondKeyName); + List entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey)); + for (Entity entity : entities) { + // do something with the entity + } + // [END fetchEntitiesWithKeys] + return entities; + } + + /** + * Example of running a query to find all entities of one kind. + */ + // [TARGET run(Query, ReadOption...)] + // [VARIABLE "my_kind"] + public List runQuery(String kind) { + // TODO change so that it's not necessary to hold the entities in a list for integration testing + // [START runQuery] + StructuredQuery query = Query.entityQueryBuilder() + .kind(kind) + .build(); + QueryResults results = datastore.run(query); + List entities = Lists.newArrayList(); + while (results.hasNext()) { + Entity result = results.next(); + // do something with result + entities.add(result); + } + // [END runQuery] + return entities; + } + + /** + * Example of running a query to find all entities with a matching property value. + */ + // [TARGET run(Query, ReadOption...)] + // [VARIABLE "my_kind"] + // [VARIABLE "my_property"] + // [VARIABLE "my_value"] + public List runQueryOnProperty(String kind, String property, String value) { + // TODO change so that it's not necessary to hold the entities in a list for integration testing + // [START runQueryOnProperty] + StructuredQuery query = Query.entityQueryBuilder() + .kind(kind) + .filter(PropertyFilter.eq(property, value)) + .build(); + QueryResults results = datastore.run(query); + List entities = Lists.newArrayList(); + while (results.hasNext()) { + Entity result = results.next(); + // do something with result + entities.add(result); + } + // [END runQueryOnProperty] + return entities; + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITDatastoreSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITDatastoreSnippets.java new file mode 100644 index 000000000000..9aef5a2f2d8d --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITDatastoreSnippets.java @@ -0,0 +1,195 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.examples.datastore.snippets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import com.google.cloud.datastore.Batch; +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.KeyFactory; +import com.google.common.collect.Lists; + +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.Timeout; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ITDatastoreSnippets { + + private static Datastore datastore; + private static DatastoreSnippets datastoreSnippets; + + private final List registeredKeys = new ArrayList<>(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + + @BeforeClass + public static void beforeClass() { + datastore = DatastoreOptions.defaultInstance().service(); + datastoreSnippets = new DatastoreSnippets(datastore); + } + + @After + public void afterTest() { + datastore.delete(registeredKeys.toArray(new Key[registeredKeys.size()])); + } + + private String registerKey(String keyName) { + return registerKey(keyName, "MyKind"); + } + + private String registerKey(String keyName, String kind) { + Key key = datastore.newKeyFactory().kind(kind).newKey(keyName); + registeredKeys.add(key); + return key.name(); + } + + private Map createEntityMap(List entities) { + Map entityMap = new HashMap<>(); + for (Entity entity : entities) { + entityMap.put(entity.key().name(), entity); + } + return entityMap; + } + + private void addEntity(String keyName, String keyClass, String property, String value) { + Key key = datastore.newKeyFactory().kind(keyClass).newKey(keyName); + Entity.Builder entityBuilder = Entity.builder(key); + entityBuilder.set(property, value); + Entity entity = entityBuilder.build(); + datastore.put(entity); + } + + @Test + public void testRunInTransaction() { + String testString = "Test String"; + String result = datastoreSnippets.runInTransaction(testString); + assertEquals(testString, result); + } + + @Test + public void testNewBatch() { + String testKey1 = registerKey("new_batch_key1"); + String testKey2 = registerKey("new_batch_key2"); + Batch batch = datastoreSnippets.newBatch(testKey1, testKey2); + assertNotNull(batch); + } + + @Test + public void testAllocateIdSingle() { + Key key = datastoreSnippets.allocateIdSingle(); + assertNotNull(key); + } + + @Test + public void testAllocateIdMultiple() { + List keys = datastoreSnippets.batchAllocateId(); + assertEquals(2, keys.size()); + } + + @Test + public void testEntityPutGet() { + String key = registerKey("my_single_key"); + datastoreSnippets.putSingleEntity(key); + Entity entity = datastoreSnippets.getEntityWithKey(key); + assertEquals("value", entity.getString("propertyName")); + } + + @Test + public void testBatchEntityCrud() { + String key1 = registerKey("batch_key1"); + String key2 = registerKey("batch_key2"); + datastoreSnippets.batchPutEntities(key1, key2); + + assertNotNull(datastoreSnippets.getEntityWithKey(key1)); + assertNotNull(datastoreSnippets.getEntityWithKey(key2)); + + List entities = Lists + .newArrayList(datastoreSnippets.getEntitiesWithKeys(key1, key2)); + assertEquals(2, entities.size()); + Map entityMap = createEntityMap(entities); + assertEquals("value1", entityMap.get(key1).getString("propertyName")); + assertEquals("value2", entityMap.get(key2).getString("propertyName")); + + datastoreSnippets.batchUpdateEntities(key1, key2); + + List fetchedEntities = datastoreSnippets.fetchEntitiesWithKeys(key1, key2); + assertEquals(2, fetchedEntities.size()); + Map fetchedEntityMap = createEntityMap(fetchedEntities); + assertEquals("updatedValue1", fetchedEntityMap.get(key1).getString("propertyName")); + assertEquals("updatedValue2", fetchedEntityMap.get(key2).getString("propertyName")); + + datastoreSnippets.batchDeleteEntities(key1, key2); + + assertNull(datastoreSnippets.getEntityWithKey(key1)); + assertNull(datastoreSnippets.getEntityWithKey(key2)); + + List fetchedEntities2 = datastoreSnippets.fetchEntitiesWithKeys(key1, key2); + assertEquals(2, fetchedEntities2.size()); + assertNull(fetchedEntities2.get(0)); + assertNull(fetchedEntities2.get(1)); + } + + @Test + public void testCreateKeyFactory() { + KeyFactory keyFactory = datastoreSnippets.createKeyFactory(); + assertNotNull(keyFactory); + } + + @Test + public void testRunQuery() { + String kindToFind = "ClassToFind"; + String kindToMiss = "OtherClass"; + + String keyNameToFind = registerKey("my_key_name_to_find", kindToFind); + String otherKeyNameToFind = registerKey("other_key_name_to_find", kindToFind); + String keyNameToMiss = registerKey("my_key_name_to_miss", kindToMiss); + + String property = "my_property_name"; + + String valueToFind = "my_value_to_find"; + String valueToMiss = "my_value_to_miss"; + + addEntity(keyNameToFind, kindToFind, property, valueToFind); + addEntity(otherKeyNameToFind, kindToFind, property, valueToMiss); + addEntity(keyNameToMiss, kindToMiss, property, valueToFind); + + List queryResults = datastoreSnippets.runQuery(kindToFind); + assertNotNull(queryResults); + assertEquals(2, queryResults.size()); + + queryResults = datastoreSnippets.runQueryOnProperty(kindToFind, property, valueToFind); + assertNotNull(queryResults); + assertEquals(1, queryResults.size()); + } +} diff --git a/utilities/add_snippets_to_file.py b/utilities/add_snippets_to_file.py index 9ed58ce16a58..dccd02293e5d 100644 --- a/utilities/add_snippets_to_file.py +++ b/utilities/add_snippets_to_file.py @@ -41,7 +41,10 @@ def __init__(self, name, parameters, signature, regex): def parse_type(csl, ast_type, is_vararg): parameter_type = '' parameter_complete_type = '' - if isinstance(ast_type, ast.Type): + if isinstance(ast_type, ast.Wildcard): + parameter_type = '?' + parameter_complete_type = '?' + elif isinstance(ast_type, ast.Type): if isinstance(ast_type.name, ast.Name): parameter_type = ast_type.name.value parameter_complete_type = ast_type.name.value