|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import * as assert from 'assert'; |
| 16 | +import {describe} from 'mocha'; |
| 17 | +import {DatastoreClient, Datastore} from '../../src'; |
| 18 | +import * as protos from '../../protos/protos'; |
| 19 | +import {Callback} from 'google-gax'; |
| 20 | + |
| 21 | +describe('Run Query', () => { |
| 22 | + const PROJECT_ID = 'project-id'; |
| 23 | + const NAMESPACE = 'namespace'; |
| 24 | + const clientName = 'DatastoreClient'; |
| 25 | + const options = { |
| 26 | + projectId: PROJECT_ID, |
| 27 | + namespace: NAMESPACE, |
| 28 | + }; |
| 29 | + const datastore = new Datastore(options); |
| 30 | + // By default, datastore.clients_ is an empty map. |
| 31 | + // To mock out commit we need the map to contain the Gapic data client. |
| 32 | + // Normally a call to the data client through the datastore object would initialize it. |
| 33 | + // We don't want to make this call because it would make a grpc request. |
| 34 | + // So we just add the data client to the map. |
| 35 | + const gapic = Object.freeze({ |
| 36 | + v1: require('../../src/v1'), |
| 37 | + }); |
| 38 | + datastore.clients_.set(clientName, new gapic.v1[clientName](options)); |
| 39 | + |
| 40 | + // This function is used for doing assertion checks. |
| 41 | + // The idea is to check that the right request gets passed to the commit function in the Gapic layer. |
| 42 | + function setRunQueryComparison( |
| 43 | + compareFn: (request: protos.google.datastore.v1.IRunQueryRequest) => void |
| 44 | + ) { |
| 45 | + const dataClient = datastore.clients_.get(clientName); |
| 46 | + if (dataClient) { |
| 47 | + dataClient.runQuery = ( |
| 48 | + request: any, |
| 49 | + options: any, |
| 50 | + callback: ( |
| 51 | + err?: unknown, |
| 52 | + res?: protos.google.datastore.v1.IRunQueryResponse |
| 53 | + ) => void |
| 54 | + ) => { |
| 55 | + try { |
| 56 | + compareFn(request); |
| 57 | + } catch (e) { |
| 58 | + callback(e); |
| 59 | + } |
| 60 | + callback(null, { |
| 61 | + batch: { |
| 62 | + moreResults: |
| 63 | + protos.google.datastore.v1.QueryResultBatch.MoreResultsType |
| 64 | + .NO_MORE_RESULTS, |
| 65 | + }, |
| 66 | + }); |
| 67 | + }; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + it('should pass read time into runQuery for transactions', async () => { |
| 72 | + // First mock out beginTransaction |
| 73 | + const dataClient = datastore.clients_.get(clientName); |
| 74 | + const testId = Buffer.from(Array.from(Array(100).keys())); |
| 75 | + if (dataClient) { |
| 76 | + dataClient.beginTransaction = ( |
| 77 | + request: protos.google.datastore.v1.IBeginTransactionRequest, |
| 78 | + options: protos.google.datastore.v1.IBeginTransactionResponse, |
| 79 | + callback: Callback< |
| 80 | + protos.google.datastore.v1.IBeginTransactionResponse, |
| 81 | + | protos.google.datastore.v1.IBeginTransactionRequest |
| 82 | + | null |
| 83 | + | undefined, |
| 84 | + {} | null | undefined |
| 85 | + > |
| 86 | + ) => { |
| 87 | + callback(null, { |
| 88 | + transaction: testId, |
| 89 | + }); |
| 90 | + }; |
| 91 | + } |
| 92 | + setRunQueryComparison( |
| 93 | + (request: protos.google.datastore.v1.IRunQueryRequest) => { |
| 94 | + assert.deepStrictEqual(request, { |
| 95 | + readOptions: { |
| 96 | + transaction: testId, |
| 97 | + readTime: { |
| 98 | + seconds: 77, |
| 99 | + }, |
| 100 | + }, |
| 101 | + partitionId: { |
| 102 | + namespaceId: 'namespace', |
| 103 | + }, |
| 104 | + query: { |
| 105 | + distinctOn: [], |
| 106 | + kind: [{name: 'Task'}], |
| 107 | + order: [], |
| 108 | + projection: [], |
| 109 | + }, |
| 110 | + projectId: 'project-id', |
| 111 | + }); |
| 112 | + } |
| 113 | + ); |
| 114 | + const transaction = datastore.transaction(); |
| 115 | + const query = datastore.createQuery('Task'); |
| 116 | + await transaction.runQuery(query, {readTime: 77000}); |
| 117 | + }); |
| 118 | +}); |
0 commit comments