Skip to content

Commit 346f928

Browse files
committed
chore(ci): migrate datastore/functions to new ci
1 parent ae785a3 commit 346f928

File tree

4 files changed

+66
-64
lines changed

4 files changed

+66
-64
lines changed

.github/config/nodejs-dev.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"datacatalog/quickstart",
114114
"datacatalog/snippets",
115115
"datalabeling",
116+
"datastore/functions",
116117
"dialogflow",
117118
"discoveryengine",
118119
"document-warehouse",

datastore/functions/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
'use strict';
1616

17-
const {Datastore} = require('@google-cloud/datastore');
17+
import { Datastore } from '@google-cloud/datastore';
1818

1919
// Instantiates a client
2020
const datastore = new Datastore();
@@ -58,7 +58,7 @@ const getKeyFromRequestData = requestData => {
5858
* @param {object} req.body.value Value to save to Cloud Datastore, e.g. {"description":"Buy milk"}
5959
* @param {object} res Cloud Function response context.
6060
*/
61-
exports.set = async (req, res) => {
61+
export async function set(req, res) {
6262
// The value contains a JSON document representing the entity we want to save
6363
if (!req.body.value) {
6464
const err = makeErrorObj('Value');
@@ -80,7 +80,7 @@ exports.set = async (req, res) => {
8080
console.error(new Error(err.message)); // Add to Stackdriver Error Reporting
8181
res.status(500).send(err.message);
8282
}
83-
};
83+
}
8484

8585
/**
8686
* Retrieves a record.
@@ -94,7 +94,7 @@ exports.set = async (req, res) => {
9494
* @param {string} req.body.key Key at which to retrieve the data, e.g. "sampletask1".
9595
* @param {object} res Cloud Function response context.
9696
*/
97-
exports.get = async (req, res) => {
97+
export async function get(req, res) {
9898
try {
9999
const key = await getKeyFromRequestData(req.body);
100100
const [entity] = await datastore.get(key);
@@ -110,7 +110,7 @@ exports.get = async (req, res) => {
110110
console.error(new Error(err.message)); // Add to Stackdriver Error Reporting
111111
res.status(500).send(err.message);
112112
}
113-
};
113+
}
114114

115115
/**
116116
* Deletes a record.
@@ -124,7 +124,7 @@ exports.get = async (req, res) => {
124124
* @param {string} req.body.key Key at which to delete data, e.g. "sampletask1".
125125
* @param {object} res Cloud Function response context.
126126
*/
127-
exports.del = async (req, res) => {
127+
export async function del(req, res) {
128128
// Deletes the entity
129129
// The delete operation will not fail for a non-existent entity, it just
130130
// doesn't delete anything
@@ -136,4 +136,4 @@ exports.del = async (req, res) => {
136136
console.error(new Error(err.message)); // Add to Stackdriver Error Reporting
137137
res.status(500).send(err.message);
138138
}
139-
};
139+
}

datastore/functions/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"type": "git",
88
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
99
},
10+
"type": "module",
1011
"engines": {
1112
"node": ">=16.0.0"
1213
},

datastore/functions/test/index.test.js

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414

1515
'use strict';
1616

17-
const assert = require('assert');
18-
const execPromise = require('child-process-promise').exec;
19-
const path = require('path');
20-
const uuid = require('uuid');
21-
const sinon = require('sinon');
22-
const fetch = require('node-fetch');
23-
const waitPort = require('wait-port');
24-
const {Datastore} = require('@google-cloud/datastore');
17+
import { ok, strictEqual, deepStrictEqual } from 'assert';
18+
import { exec as execPromise } from 'child-process-promise';
19+
import { join } from 'path';
20+
import { v4 } from 'uuid';
21+
import { stub } from 'sinon';
22+
import fetch from 'node-fetch';
23+
import waitPort from 'wait-port';
24+
import { Datastore } from '@google-cloud/datastore';
2525

2626
const datastore = new Datastore();
27-
const program = require('../');
27+
import { set, get, del } from '../';
2828

2929
const FF_TIMEOUT = 3000;
30-
const cwd = path.join(__dirname, '..');
30+
const cwd = join(__dirname, '..');
3131
const NAME = 'sampletask1';
32-
const KIND = `Task-${uuid.v4()}`;
32+
const KIND = `Task-${v4()}`;
3333
const VALUE = {
3434
description: 'Buy milk',
3535
};
@@ -72,14 +72,14 @@ describe('functions/datastore', () => {
7272
body: {},
7373
};
7474
const res = {
75-
status: sinon.stub().returnsThis(),
76-
send: sinon.stub(),
75+
status: stub().returnsThis(),
76+
send: stub(),
7777
};
7878

79-
await program.set(req, res);
79+
await set(req, res);
8080

81-
assert.ok(res.status.calledWith(500));
82-
assert.ok(res.send.calledWith(errorMsg('Value')));
81+
ok(res.status.calledWith(500));
82+
ok(res.send.calledWith(errorMsg('Value')));
8383
});
8484

8585
it('set: Fails without a key', async () => {
@@ -89,12 +89,12 @@ describe('functions/datastore', () => {
8989
},
9090
};
9191
const res = {
92-
status: sinon.stub().returnsThis(),
93-
send: sinon.stub(),
92+
status: stub().returnsThis(),
93+
send: stub(),
9494
};
95-
await program.set(req, res);
96-
assert.ok(res.status.calledWith(500));
97-
assert.ok(res.send.calledWith(errorMsg('Key')));
95+
await set(req, res);
96+
ok(res.status.calledWith(500));
97+
ok(res.send.calledWith(errorMsg('Key')));
9898
});
9999

100100
it('set: Fails without a kind', async () => {
@@ -105,14 +105,14 @@ describe('functions/datastore', () => {
105105
},
106106
};
107107
const res = {
108-
status: sinon.stub().returnsThis(),
109-
send: sinon.stub(),
108+
status: stub().returnsThis(),
109+
send: stub(),
110110
};
111111

112-
await program.set(req, res);
112+
await set(req, res);
113113

114-
assert.ok(res.status.calledWith(500));
115-
assert.ok(res.send.calledWith(errorMsg('Kind')));
114+
ok(res.status.calledWith(500));
115+
ok(res.send.calledWith(errorMsg('Kind')));
116116
});
117117

118118
it('set: Saves an entity', async () => {
@@ -125,9 +125,9 @@ describe('functions/datastore', () => {
125125
}),
126126
headers: {'Content-Type': 'application/json'},
127127
});
128-
assert.strictEqual(response.status, 200);
128+
strictEqual(response.status, 200);
129129
const body = await response.text();
130-
assert.ok(body.includes(`Entity ${KIND}/${NAME} saved`));
130+
ok(body.includes(`Entity ${KIND}/${NAME} saved`));
131131
});
132132
});
133133

@@ -159,9 +159,9 @@ describe('functions/datastore', () => {
159159
validateStatus: () => true,
160160
});
161161

162-
assert.strictEqual(response.status, 500);
162+
strictEqual(response.status, 500);
163163
const body = await response.text();
164-
assert.ok(
164+
ok(
165165
new RegExp(
166166
/(Missing or insufficient permissions.)|(No entity found for key)/
167167
).test(body)
@@ -177,9 +177,9 @@ describe('functions/datastore', () => {
177177
}),
178178
headers: {'Content-Type': 'application/json'},
179179
});
180-
assert.strictEqual(response.status, 200);
180+
strictEqual(response.status, 200);
181181
const body = await response.json();
182-
assert.deepStrictEqual(body, {
182+
deepStrictEqual(body, {
183183
description: 'Buy milk',
184184
});
185185
});
@@ -189,14 +189,14 @@ describe('functions/datastore', () => {
189189
body: {},
190190
};
191191
const res = {
192-
status: sinon.stub().returnsThis(),
193-
send: sinon.stub(),
192+
status: stub().returnsThis(),
193+
send: stub(),
194194
};
195195

196-
await program.get(req, res);
196+
await get(req, res);
197197

198-
assert.ok(res.status.calledWith(500));
199-
assert.ok(res.send.calledWith(errorMsg('Key')));
198+
ok(res.status.calledWith(500));
199+
ok(res.send.calledWith(errorMsg('Key')));
200200
});
201201

202202
it('get: Fails without a kind', async () => {
@@ -206,14 +206,14 @@ describe('functions/datastore', () => {
206206
},
207207
};
208208
const res = {
209-
status: sinon.stub().returnsThis(),
210-
send: sinon.stub(),
209+
status: stub().returnsThis(),
210+
send: stub(),
211211
};
212212

213-
await program.get(req, res);
213+
await get(req, res);
214214

215-
assert.ok(res.status.calledWith(500));
216-
assert.ok(res.send.calledWith(errorMsg('Kind')));
215+
ok(res.status.calledWith(500));
216+
ok(res.send.calledWith(errorMsg('Kind')));
217217
});
218218
});
219219

@@ -239,14 +239,14 @@ describe('functions/datastore', () => {
239239
body: {},
240240
};
241241
const res = {
242-
status: sinon.stub().returnsThis(),
243-
send: sinon.stub(),
242+
status: stub().returnsThis(),
243+
send: stub(),
244244
};
245245

246-
await program.del(req, res);
246+
await del(req, res);
247247

248-
assert.ok(res.status.calledWith(500));
249-
assert.ok(res.send.calledWith(errorMsg('Key')));
248+
ok(res.status.calledWith(500));
249+
ok(res.send.calledWith(errorMsg('Key')));
250250
});
251251

252252
it('del: Fails without a kind', async () => {
@@ -256,14 +256,14 @@ describe('functions/datastore', () => {
256256
},
257257
};
258258
const res = {
259-
status: sinon.stub().returnsThis(),
260-
send: sinon.stub(),
259+
status: stub().returnsThis(),
260+
send: stub(),
261261
};
262262

263-
await program.del(req, res);
263+
await del(req, res);
264264

265-
assert.ok(res.status.calledWith(500));
266-
assert.ok(res.send.calledWith(errorMsg('Kind')));
265+
ok(res.status.calledWith(500));
266+
ok(res.send.calledWith(errorMsg('Kind')));
267267
});
268268

269269
it("del: Doesn't fail when entity does not exist", async () => {
@@ -275,9 +275,9 @@ describe('functions/datastore', () => {
275275
}),
276276
headers: {'Content-Type': 'application/json'},
277277
});
278-
assert.strictEqual(response.status, 200);
278+
strictEqual(response.status, 200);
279279
const body = await response.text();
280-
assert.strictEqual(body, `Entity ${KIND}/nonexistent deleted.`);
280+
strictEqual(body, `Entity ${KIND}/nonexistent deleted.`);
281281
});
282282

283283
it('del: Deletes an entity', async () => {
@@ -289,13 +289,13 @@ describe('functions/datastore', () => {
289289
}),
290290
headers: {'Content-Type': 'application/json'},
291291
});
292-
assert.strictEqual(response.status, 200);
292+
strictEqual(response.status, 200);
293293
const body = await response.text();
294-
assert.strictEqual(body, `Entity ${KIND}/${NAME} deleted.`);
294+
strictEqual(body, `Entity ${KIND}/${NAME} deleted.`);
295295

296296
const key = datastore.key([KIND, NAME]);
297297
const [entity] = await datastore.get(key);
298-
assert.ok(!entity);
298+
ok(!entity);
299299
});
300300
});
301301
});

0 commit comments

Comments
 (0)