|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import * as sinon from 'sinon'; |
| 20 | + |
| 21 | +import { Timestamp } from '../../../src'; |
| 22 | +import { Firestore } from '../../../src/api/database'; |
| 23 | +import { execute } from '../../../src/api/pipeline_impl'; |
| 24 | +import { |
| 25 | + MemoryOfflineComponentProvider, |
| 26 | + OnlineComponentProvider |
| 27 | +} from '../../../src/core/component_provider'; |
| 28 | +import { |
| 29 | + ExecutePipelineRequest as ProtoExecutePipelineRequest, |
| 30 | + ExecutePipelineResponse as ProtoExecutePipelineResponse |
| 31 | +} from '../../../src/protos/firestore_proto_api'; |
| 32 | +import { newTestFirestore } from '../../util/api_helpers'; |
| 33 | + |
| 34 | +const FIRST_CALL = 0; |
| 35 | +const EXECUTE_PIPELINE_REQUEST = 3; |
| 36 | + |
| 37 | +function fakePipelineResponse( |
| 38 | + firestore: Firestore, |
| 39 | + response?: ProtoExecutePipelineResponse[] |
| 40 | +): sinon.SinonSpy { |
| 41 | + response = response ?? [ |
| 42 | + { |
| 43 | + executionTime: Timestamp.now().toDate().toISOString(), |
| 44 | + results: [] |
| 45 | + } |
| 46 | + ]; |
| 47 | + const fake = sinon.fake.resolves(response); |
| 48 | + |
| 49 | + firestore._componentsProvider = { |
| 50 | + _offline: { |
| 51 | + build: () => new MemoryOfflineComponentProvider() |
| 52 | + }, |
| 53 | + _online: { |
| 54 | + build: () => { |
| 55 | + const provider = new OnlineComponentProvider(); |
| 56 | + const ogCreateDatastore = provider.createDatastore.bind(provider); |
| 57 | + provider.createDatastore = config => { |
| 58 | + const datastore = ogCreateDatastore(config); |
| 59 | + // @ts-ignore |
| 60 | + datastore.invokeStreamingRPC = fake; |
| 61 | + return datastore; |
| 62 | + }; |
| 63 | + return provider; |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + return fake; |
| 69 | +} |
| 70 | + |
| 71 | +describe('execute(Pipeline|PipelineOptions)', () => { |
| 72 | + it('returns execution time with empty results', async () => { |
| 73 | + const firestore = newTestFirestore(); |
| 74 | + |
| 75 | + const executeTime = Timestamp.now(); |
| 76 | + const spy = fakePipelineResponse(firestore, [ |
| 77 | + { |
| 78 | + executionTime: executeTime.toDate().toISOString(), |
| 79 | + results: [] |
| 80 | + } |
| 81 | + ]); |
| 82 | + |
| 83 | + const pipelineSnapshot = await execute( |
| 84 | + firestore.pipeline().collection('foo') |
| 85 | + ); |
| 86 | + |
| 87 | + expect(pipelineSnapshot.results.length).to.equal(0); |
| 88 | + expect(spy.calledOnce); |
| 89 | + |
| 90 | + expect(pipelineSnapshot.executionTime.toJSON()).to.deep.equal( |
| 91 | + executeTime.toJSON() |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('serializes the pipeline', async () => { |
| 96 | + const firestore = newTestFirestore(); |
| 97 | + const spy = fakePipelineResponse(firestore); |
| 98 | + |
| 99 | + await execute({ |
| 100 | + pipeline: firestore.pipeline().collection('foo') |
| 101 | + }); |
| 102 | + |
| 103 | + const executePipelineRequest: ProtoExecutePipelineRequest = { |
| 104 | + database: 'projects/new-project/databases/(default)', |
| 105 | + structuredPipeline: { |
| 106 | + 'options': {}, |
| 107 | + 'pipeline': { |
| 108 | + 'stages': [ |
| 109 | + { |
| 110 | + 'args': [ |
| 111 | + { |
| 112 | + 'referenceValue': '/foo' |
| 113 | + } |
| 114 | + ], |
| 115 | + 'name': 'collection' |
| 116 | + } |
| 117 | + ] |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
| 121 | + expect(spy.args[FIRST_CALL][EXECUTE_PIPELINE_REQUEST]).to.deep.equal( |
| 122 | + executePipelineRequest |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('serializes the pipeline options', async () => { |
| 127 | + const firestore = newTestFirestore(); |
| 128 | + const spy = fakePipelineResponse(firestore); |
| 129 | + |
| 130 | + await execute({ |
| 131 | + pipeline: firestore.pipeline().collection('foo'), |
| 132 | + indexMode: 'recommended' |
| 133 | + }); |
| 134 | + |
| 135 | + const executePipelineRequest: ProtoExecutePipelineRequest = { |
| 136 | + database: 'projects/new-project/databases/(default)', |
| 137 | + structuredPipeline: { |
| 138 | + 'options': { |
| 139 | + 'index_mode': { |
| 140 | + 'stringValue': 'recommended' |
| 141 | + } |
| 142 | + }, |
| 143 | + 'pipeline': { |
| 144 | + 'stages': [ |
| 145 | + { |
| 146 | + 'args': [ |
| 147 | + { |
| 148 | + 'referenceValue': '/foo' |
| 149 | + } |
| 150 | + ], |
| 151 | + 'name': 'collection' |
| 152 | + } |
| 153 | + ] |
| 154 | + } |
| 155 | + } |
| 156 | + }; |
| 157 | + expect(spy.args[FIRST_CALL][EXECUTE_PIPELINE_REQUEST]).to.deep.equal( |
| 158 | + executePipelineRequest |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it('serializes the pipeline generic options', async () => { |
| 163 | + const firestore = newTestFirestore(); |
| 164 | + const spy = fakePipelineResponse(firestore); |
| 165 | + |
| 166 | + await execute({ |
| 167 | + pipeline: firestore.pipeline().collection('foo'), |
| 168 | + genericOptions: { |
| 169 | + 'foo': 'bar' |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + const executePipelineRequest: ProtoExecutePipelineRequest = { |
| 174 | + database: 'projects/new-project/databases/(default)', |
| 175 | + structuredPipeline: { |
| 176 | + 'options': { |
| 177 | + 'foo': { |
| 178 | + 'stringValue': 'bar' |
| 179 | + } |
| 180 | + }, |
| 181 | + 'pipeline': { |
| 182 | + 'stages': [ |
| 183 | + { |
| 184 | + 'args': [ |
| 185 | + { |
| 186 | + 'referenceValue': '/foo' |
| 187 | + } |
| 188 | + ], |
| 189 | + 'name': 'collection' |
| 190 | + } |
| 191 | + ] |
| 192 | + } |
| 193 | + } |
| 194 | + }; |
| 195 | + expect(spy.args[FIRST_CALL][EXECUTE_PIPELINE_REQUEST]).to.deep.equal( |
| 196 | + executePipelineRequest |
| 197 | + ); |
| 198 | + }); |
| 199 | +}); |
0 commit comments