Search in sources :

Example 11 with RunQueryRequest

use of com.google.datastore.v1.RunQueryRequest 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(invocationOnMock -> {
        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) RunQueryRequest(com.google.datastore.v1.RunQueryRequest)

Example 12 with RunQueryRequest

use of com.google.datastore.v1.RunQueryRequest in project beam by apache.

the class PartitionQueryResponseToRunQueryRequestTest method ensureCursorPairingWorks.

@Test
public void ensureCursorPairingWorks() {
    StructuredQuery query = StructuredQuery.newBuilder().addFrom(CollectionSelector.newBuilder().setAllDescendants(true).setCollectionId("c1").build()).build();
    Cursor cursor1 = referenceValueCursor("projects/p1/databases/d1/documents/c1/doc1");
    Cursor cursor2 = referenceValueCursor("projects/p1/databases/d1/documents/c1/doc2");
    Cursor cursor3 = referenceValueCursor("projects/p1/databases/d1/documents/c1/doc2/c2/doc2");
    List<StructuredQuery> expectedQueries = newArrayList(newQueryWithCursors(query, null, cursor1), newQueryWithCursors(query, cursor1, cursor2), newQueryWithCursors(query, cursor2, cursor3), newQueryWithCursors(query, cursor3, null));
    PartitionQueryPair partitionQueryPair = new PartitionQueryPair(PartitionQueryRequest.newBuilder().setStructuredQuery(query).build(), PartitionQueryResponse.newBuilder().addPartitions(cursor3).addPartitions(cursor1).addPartitions(cursor2).build());
    ArgumentCaptor<RunQueryRequest> captor = ArgumentCaptor.forClass(RunQueryRequest.class);
    when(processContext.element()).thenReturn(partitionQueryPair);
    doNothing().when(processContext).output(captor.capture());
    PartitionQueryResponseToRunQueryRequest fn = new PartitionQueryResponseToRunQueryRequest();
    fn.processElement(processContext);
    List<StructuredQuery> actualQueries = captor.getAllValues().stream().map(RunQueryRequest::getStructuredQuery).collect(Collectors.toList());
    assertEquals(expectedQueries, actualQueries);
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) PartitionQueryResponseToRunQueryRequest(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.PartitionQuery.PartitionQueryResponseToRunQueryRequest) PartitionQueryPair(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.PartitionQueryPair) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) PartitionQueryResponseToRunQueryRequest(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.PartitionQuery.PartitionQueryResponseToRunQueryRequest) Cursor(com.google.firestore.v1.Cursor) Test(org.junit.Test)

Example 13 with RunQueryRequest

use of com.google.datastore.v1.RunQueryRequest in project beam by apache.

the class PartitionQueryResponseToRunQueryRequestTest method ensureCursorPairingWorks_emptyCursorsInResponse.

@Test
public void ensureCursorPairingWorks_emptyCursorsInResponse() {
    StructuredQuery query = StructuredQuery.newBuilder().addFrom(CollectionSelector.newBuilder().setAllDescendants(true).setCollectionId("c1").build()).build();
    List<StructuredQuery> expectedQueries = newArrayList(query);
    PartitionQueryPair partitionQueryPair = new PartitionQueryPair(PartitionQueryRequest.newBuilder().setStructuredQuery(query).build(), PartitionQueryResponse.newBuilder().build());
    ArgumentCaptor<RunQueryRequest> captor = ArgumentCaptor.forClass(RunQueryRequest.class);
    when(processContext.element()).thenReturn(partitionQueryPair);
    doNothing().when(processContext).output(captor.capture());
    PartitionQueryResponseToRunQueryRequest fn = new PartitionQueryResponseToRunQueryRequest();
    fn.processElement(processContext);
    List<StructuredQuery> actualQueries = captor.getAllValues().stream().map(RunQueryRequest::getStructuredQuery).collect(Collectors.toList());
    assertEquals(expectedQueries, actualQueries);
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) PartitionQueryResponseToRunQueryRequest(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.PartitionQuery.PartitionQueryResponseToRunQueryRequest) PartitionQueryPair(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.PartitionQueryPair) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) PartitionQueryResponseToRunQueryRequest(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.PartitionQuery.PartitionQueryResponseToRunQueryRequest) Test(org.junit.Test)

Example 14 with RunQueryRequest

use of com.google.datastore.v1.RunQueryRequest in project spring-cloud-gcp by spring-cloud.

the class FirestoreTemplateTests method countWithQueryTest.

@Test
public void countWithQueryTest() {
    mockRunQueryMethod();
    StructuredQuery.Builder builder = StructuredQuery.newBuilder();
    addWhere(builder);
    StepVerifier.create(this.firestoreTemplate.count(TestEntity.class, builder)).expectNext(2L).verifyComplete();
    StructuredQuery.Builder expectedBuilder = StructuredQuery.newBuilder().addFrom(StructuredQuery.CollectionSelector.newBuilder().setCollectionId("testEntities").build()).setSelect(StructuredQuery.Projection.newBuilder().addFields(StructuredQuery.FieldReference.newBuilder().setFieldPath("__name__").build()).build());
    addWhere(expectedBuilder);
    RunQueryRequest request = RunQueryRequest.newBuilder().setParent(this.parent).setStructuredQuery(expectedBuilder).build();
    verify(this.firestoreStub, times(1)).runQuery(eq(request), any());
    verify(this.firestoreStub, times(1)).runQuery(any(), any());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 RunQueryRequest (com.google.firestore.v1.RunQueryRequest)8 RunQueryRequest (com.google.datastore.v1.RunQueryRequest)6 StructuredQuery (com.google.firestore.v1.StructuredQuery)6 GqlQuery (com.google.datastore.v1.GqlQuery)5 Query (com.google.datastore.v1.Query)3 RunQueryResponse (com.google.firestore.v1.RunQueryResponse)3 Entity (com.google.datastore.v1.Entity)2 RunQueryResponse (com.google.datastore.v1.RunQueryResponse)2 DatastoreException (com.google.datastore.v1.client.DatastoreException)2 Cursor (com.google.firestore.v1.Cursor)2 Document (com.google.firestore.v1.Document)2 FieldReference (com.google.firestore.v1.StructuredQuery.FieldReference)2 DeleteEntity (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity)2 ReadFn (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.ReadFn)2 PartitionQueryResponseToRunQueryRequest (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.PartitionQuery.PartitionQueryResponseToRunQueryRequest)2 PartitionQueryPair (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.PartitionQueryPair)2 RunQueryFn (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1ReadFn.RunQueryFn)2 ServerStream (com.google.api.gax.rpc.ServerStream)1 ServerStreamingCallable (com.google.api.gax.rpc.ServerStreamingCallable)1