Skip to content

Commit e61e044

Browse files
author
Guiners
committed
adding mocking for batch prediction
1 parent a613cea commit e61e044

File tree

5 files changed

+73
-63
lines changed

5 files changed

+73
-63
lines changed

genai/batch-prediction/batchpredict-embeddings-with-gcs.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ async function runBatchPredictionJob(
6464
await new Promise(resolve => setTimeout(resolve, 30000));
6565
job = await client.batches.get({name: job.name});
6666
console.log(`Job state: ${job.state}`);
67-
// if (job.state === 'JOB_STATE_FAILED') {
68-
// console.log(`Error: ${job.state}`);
69-
// break;
70-
// }
7167
}
7268

7369
// Example response:

genai/test/batchpredict-embeddings-with-gcs.test.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,34 @@
1616

1717
const {assert} = require('chai');
1818
const {describe, it} = require('mocha');
19-
const {Storage} = require('@google-cloud/storage');
20-
21-
const storage = new Storage();
22-
23-
const GCS_OUTPUT_BUCKET = 'nodejs-docs-samples-tests';
2419

2520
const projectId = process.env.CAIP_PROJECT_ID;
2621
const location = 'us-central1';
27-
const sample = require('../batch-prediction/batchpredict-embeddings-with-gcs');
2822
const {delay} = require('./util');
23+
const proxyquire = require('proxyquire');
24+
const {GoogleGenAI_Mock} = require('./batchprediction-utils');
25+
26+
const sample = proxyquire(
27+
'../batch-prediction/batchpredict-embeddings-with-gcs',
28+
{
29+
'@google/genai': {
30+
GoogleGenAI: GoogleGenAI_Mock,
31+
},
32+
}
33+
);
2934

3035
async function getGcsOutputUri() {
31-
const dt = new Date();
32-
const prefix = `text_output/${dt.toISOString()}`;
33-
const fullUri = `gs://${GCS_OUTPUT_BUCKET}/${prefix}`;
34-
3536
return {
36-
uri: fullUri,
37-
async cleanup() {
38-
const [files] = await storage.bucket(GCS_OUTPUT_BUCKET).getFiles({
39-
prefix,
40-
});
41-
for (const file of files) {
42-
await file.delete();
43-
}
44-
},
37+
uri: 'gs://mock/output',
38+
async cleanup() {},
4539
};
4640
}
4741

4842
describe('batchpredict-with-gcs', () => {
4943
it('should return the batch job state', async function () {
5044
this.timeout(500000);
51-
// this.retries(4);
52-
// await delay(this.test);
45+
this.retries(4);
46+
await delay(this.test);
5347
const gcsOutput = await getGcsOutputUri();
5448
try {
5549
const output = await sample.runBatchPredictionJob(

genai/test/batchpredict-with-bq.test.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,24 @@
1616

1717
const {assert} = require('chai');
1818
const {describe, it} = require('mocha');
19-
const {BigQuery} = require('@google-cloud/bigquery');
20-
21-
const bigquery = new BigQuery();
22-
23-
const BQ_OUTPUT_DATASET = `${process.env.BQ_OUTPUT_DATASET}.gen_ai_batch_prediction`;
2419

2520
const projectId = process.env.CAIP_PROJECT_ID;
2621
const location = 'us-central1';
2722
const {delay} = require('./util');
2823

29-
const sample = require('../batch-prediction/batchpredict-with-bq');
24+
const proxyquire = require('proxyquire');
25+
const {GoogleGenAI_Mock} = require('./batchprediction-utils');
3026

31-
async function getBqOutputUri() {
32-
const dt = new Date();
33-
const tableName = `text_output_${dt.getFullYear()}_${dt.getMonth() + 1}_${dt.getDate()}_T${dt.getHours()}_${dt.getMinutes()}_${dt.getSeconds()}`;
34-
const tableUri = `${BQ_OUTPUT_DATASET}.${tableName}`;
35-
const fullUri = `bq://${tableUri}`;
27+
const sample = proxyquire('../batch-prediction/batchpredict-with-bq', {
28+
'@google/genai': {
29+
GoogleGenAI: GoogleGenAI_Mock,
30+
},
31+
});
3632

33+
async function getBqOutputUri() {
3734
return {
38-
uri: fullUri,
39-
async cleanup() {
40-
await bigquery.dataset(BQ_OUTPUT_DATASET).table(tableName).delete({
41-
ignoreNotFound: true,
42-
});
43-
},
35+
uri: 'gs://mock/output',
36+
async cleanup() {},
4437
};
4538
}
4639

genai/test/batchpredict-with-gcs.test.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,43 @@
1616

1717
const {assert} = require('chai');
1818
const {describe, it} = require('mocha');
19-
const {Storage} = require('@google-cloud/storage');
20-
21-
const storage = new Storage();
22-
23-
const GCS_OUTPUT_BUCKET = 'nodejs-docs-samples-tests';
19+
const proxyquire = require('proxyquire');
2420

2521
const projectId = process.env.CAIP_PROJECT_ID;
2622
const location = 'us-central1';
27-
const sample = require('../batch-prediction/batchpredict-with-gcs');
2823
const {delay} = require('./util');
24+
const {GoogleGenAI_Mock} = require('./batchprediction-utils');
2925

30-
async function getGcsOutputUri() {
31-
const dt = new Date();
32-
const prefix = `text_output/${dt.toISOString()}`;
33-
const fullUri = `gs://${GCS_OUTPUT_BUCKET}/${prefix}`;
26+
const sample = proxyquire('../batch-prediction/batchpredict-with-gcs', {
27+
'@google/genai': {
28+
GoogleGenAI: GoogleGenAI_Mock,
29+
},
30+
});
3431

32+
async function getGcsOutputUri() {
3533
return {
36-
uri: fullUri,
37-
async cleanup() {
38-
const [files] = await storage.bucket(GCS_OUTPUT_BUCKET).getFiles({
39-
prefix,
40-
});
41-
for (const file of files) {
42-
await file.delete();
43-
}
44-
},
34+
uri: 'gs://mock/output',
35+
async cleanup() {},
4536
};
4637
}
4738

48-
describe('batchpredict-with-gcs', () => {
39+
describe('batchpredict-with-gcs (mocked)', () => {
4940
it('should return the batch job state', async function () {
5041
this.timeout(500000);
5142
this.retries(4);
5243
await delay(this.test);
44+
5345
const gcsOutput = await getGcsOutputUri();
46+
5447
try {
5548
const output = await sample.runBatchPredictionJob(
5649
gcsOutput.uri,
5750
projectId,
5851
location
5952
);
60-
assert.notEqual(output, undefined);
53+
54+
console.log('output', output);
55+
assert.equal(output, 'JOB_STATE_SUCCEEDED');
6156
} finally {
6257
await gcsOutput.cleanup();
6358
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const GoogleGenAI_Mock = function () {
2+
let getCalled = false;
3+
4+
return {
5+
batches: {
6+
create: async () => ({
7+
name: 'projects/mock/locations/mock/batchPredictionJobs/123',
8+
state: 'JOB_STATE_PENDING',
9+
}),
10+
11+
get: async () => {
12+
// First call returns running, second call returns success
13+
if (!getCalled) {
14+
getCalled = true;
15+
return {
16+
name: 'projects/mock/locations/mock/batchPredictionJobs/123',
17+
state: 'JOB_STATE_RUNNING',
18+
};
19+
}
20+
21+
return {
22+
name: 'projects/mock/locations/mock/batchPredictionJobs/123',
23+
state: 'JOB_STATE_SUCCEEDED',
24+
};
25+
},
26+
},
27+
};
28+
};
29+
30+
module.exports = {
31+
GoogleGenAI_Mock,
32+
};

0 commit comments

Comments
 (0)