use of com.google.datastore.v1.Entity in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreTest method testNewBatch.
@Test
public void testNewBatch() {
Batch batch = datastore.newBatch();
Entity entity1 = Entity.newBuilder(ENTITY1).clear().build();
Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
List<Entity> entities = batch.add(entity4, PARTIAL_ENTITY2, entity5);
Entity entity6 = entities.get(1);
assertSame(entity4, entities.get(0));
assertEquals(PARTIAL_ENTITY2.getProperties(), entity6.getProperties());
assertEquals(PARTIAL_ENTITY2.getKey().getProjectId(), entity6.getKey().getProjectId());
assertEquals(PARTIAL_ENTITY2.getKey().getNamespace(), entity6.getKey().getNamespace());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors());
assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind());
assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build());
assertNotEquals(PARTIAL_ENTITY2.getKey().getPath(), entity6.getKey().getPath());
assertNotEquals(PARTIAL_ENTITY2.getKey(), entity6.getKey());
assertSame(entity5, entities.get(2));
batch.addWithDeferredIdAllocation(PARTIAL_ENTITY3);
batch.put(ENTITY3, entity1, entity2);
Batch.Response response = batch.submit();
entities = datastore.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
assertEquals(entity1, entities.get(0));
assertEquals(entity2, entities.get(1));
assertEquals(ENTITY3, entities.get(2));
assertEquals(entity4, entities.get(3));
assertEquals(entity5, entities.get(4));
assertEquals(entity6, entities.get(5));
assertEquals(6, entities.size());
List<Key> generatedKeys = response.getGeneratedKeys();
assertEquals(1, generatedKeys.size());
assertEquals(PARTIAL_ENTITY3.getProperties(), datastore.get(generatedKeys.get(0)).getProperties());
assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
try {
batch.submit();
fail("Expecting a failure");
} catch (DatastoreException ex) {
// expected to fail
}
verifyNotUsable(batch);
batch = datastore.newBatch();
batch.delete(entity4.getKey(), entity5.getKey());
batch.update(ENTITY1, ENTITY2, ENTITY3);
batch.submit();
entities = datastore.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey());
assertEquals(ENTITY1, entities.get(0));
assertEquals(ENTITY2, entities.get(1));
assertEquals(ENTITY3, entities.get(2));
assertNull(entities.get(3));
assertNull(entities.get(4));
assertEquals(5, entities.size());
}
use of com.google.datastore.v1.Entity in project beam by apache.
the class DatastoreV1Test method readFnTest.
/** Helper function to run a test reading from a {@link ReadFn}. */
private void readFnTest(int numEntities) throws Exception {
// An empty query to read entities.
Query query = Query.newBuilder().setLimit(Int32Value.newBuilder().setValue(numEntities)).build();
// Use mockResponseForQuery to generate results.
when(mockDatastore.runQuery(any(RunQueryRequest.class))).thenAnswer(new Answer<RunQueryResponse>() {
@Override
public RunQueryResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
Query q = ((RunQueryRequest) invocationOnMock.getArguments()[0]).getQuery();
return mockResponseForQuery(q);
}
});
ReadFn readFn = new ReadFn(V_1_OPTIONS, mockDatastoreFactory);
DoFnTester<Query, Entity> doFnTester = DoFnTester.of(readFn);
/**
* Although Datastore client is marked transient in {@link ReadFn}, when injected through
* mock factory using a when clause for unit testing purposes, it is not serializable
* because it doesn't have a no-arg constructor. Thus disabling the cloning to prevent the
* test object from being serialized.
*/
doFnTester.setCloningBehavior(CloningBehavior.DO_NOT_CLONE);
List<Entity> entities = doFnTester.processBundle(query);
int expectedNumCallsToRunQuery = (int) Math.ceil((double) numEntities / QUERY_BATCH_LIMIT);
verify(mockDatastore, times(expectedNumCallsToRunQuery)).runQuery(any(RunQueryRequest.class));
// Validate the number of results.
assertEquals(numEntities, entities.size());
}
use of com.google.datastore.v1.Entity in project beam by apache.
the class DatastoreV1Test method testDeleteEntitiesWithIncompleteKeys.
/**
* Test that entities with incomplete keys cannot be deleted.
*/
@Test
public void testDeleteEntitiesWithIncompleteKeys() throws Exception {
Key key = makeKey("bird").build();
Entity entity = Entity.newBuilder().setKey(key).build();
DeleteEntityFn deleteEntityFn = new DeleteEntityFn();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Entities to be deleted from the Cloud Datastore must have complete keys");
deleteEntityFn.apply(entity);
}
use of com.google.datastore.v1.Entity in project beam by apache.
the class DatastoreV1Test method testAddEntitiesWithIncompleteKeys.
/**
* Test that entities with incomplete keys cannot be updated.
*/
@Test
public void testAddEntitiesWithIncompleteKeys() throws Exception {
Key key = makeKey("bird").build();
Entity entity = Entity.newBuilder().setKey(key).build();
UpsertFn upsertFn = new UpsertFn();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Entities to be written to the Cloud Datastore must have complete keys");
upsertFn.apply(entity);
}
use of com.google.datastore.v1.Entity in project beam by apache.
the class DatastoreV1Test method testDeleteEntities.
/**
* Test that entities with valid keys are transformed to delete mutations.
*/
@Test
public void testDeleteEntities() throws Exception {
Key key = makeKey("bird", "finch").build();
Entity entity = Entity.newBuilder().setKey(key).build();
DeleteEntityFn deleteEntityFn = new DeleteEntityFn();
Mutation exceptedMutation = makeDelete(entity.getKey()).build();
assertEquals(deleteEntityFn.apply(entity), exceptedMutation);
}
Aggregations