Search in sources :

Example 16 with Query

use of com.google.datastore.v1.Query in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreTest method testEventualConsistencyQuery.

@Test
public void testEventualConsistencyQuery() {
    ReadOptions readOption = ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build();
    com.google.datastore.v1.GqlQuery query = com.google.datastore.v1.GqlQuery.newBuilder().setQueryString("FROM * SELECT *").build();
    RunQueryRequest.Builder expectedRequest = RunQueryRequest.newBuilder().setReadOptions(readOption).setGqlQuery(query).setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build());
    EasyMock.expect(rpcMock.runQuery(expectedRequest.build())).andReturn(RunQueryResponse.newBuilder().build());
    EasyMock.replay(rpcFactoryMock, rpcMock);
    Datastore datastore = rpcMockOptions.getService();
    datastore.run(Query.newGqlQueryBuilder("FROM * SELECT *").build(), ReadOption.eventualConsistency());
    EasyMock.verify(rpcFactoryMock, rpcMock);
}
Also used : ReadOptions(com.google.datastore.v1.ReadOptions) RunQueryRequest(com.google.datastore.v1.RunQueryRequest) Test(org.junit.Test)

Example 17 with Query

use of com.google.datastore.v1.Query 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());
}
Also used : Entity(com.google.datastore.v1.Entity) DeleteEntity(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity) GqlQuery(com.google.datastore.v1.GqlQuery) Query(com.google.datastore.v1.Query) ReadFn(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.ReadFn) RunQueryResponse(com.google.datastore.v1.RunQueryResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RunQueryRequest(com.google.datastore.v1.RunQueryRequest)

Aggregations

Test (org.junit.Test)11 Query (com.google.datastore.v1.Query)10 RunQueryRequest (com.google.datastore.v1.RunQueryRequest)9 GqlQuery (com.google.datastore.v1.GqlQuery)8 RunQueryResponse (com.google.datastore.v1.RunQueryResponse)6 Datastore (com.google.datastore.v1.client.Datastore)4 SplitQueryFn (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.SplitQueryFn)4 KV (org.apache.beam.sdk.values.KV)4 Entity (com.google.datastore.v1.Entity)2 PartitionId (com.google.datastore.v1.PartitionId)2 QueryResultBatch (com.google.datastore.v1.QueryResultBatch)2 ArrayList (java.util.ArrayList)2 DatastoreRpc (com.google.cloud.datastore.spi.v1.DatastoreRpc)1 ReadOptions (com.google.datastore.v1.ReadOptions)1 DatastoreException (com.google.datastore.v1.client.DatastoreException)1 ByteString (com.google.protobuf.ByteString)1 Pipeline (org.apache.beam.sdk.Pipeline)1 DeleteEntity (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity)1 ReadFn (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.ReadFn)1 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)1