diff --git a/packages/cli-test/src/cli/commands/datastore.spec.ts b/packages/cli-test/src/cli/commands/datastore.spec.ts new file mode 100644 index 000000000..4694a1ea2 --- /dev/null +++ b/packages/cli-test/src/cli/commands/datastore.spec.ts @@ -0,0 +1,59 @@ +import sinon from 'sinon'; + +import { mockProcess } from '../../utils/test'; +import { shell } from '../shell'; +import datastore from './datastore'; + +describe('datastore commands', () => { + const sandbox = sinon.createSandbox(); + let spawnSpy: sinon.SinonStub; + + beforeEach(() => { + const process = mockProcess(); + spawnSpy = sandbox.stub(shell, 'spawnProcess').returns({ + command: 'something', + finished: true, + output: 'hi', + process, + }); + sandbox.stub(shell, 'checkIfFinished').resolves(); + }); + afterEach(() => { + sandbox.restore(); + }); + + describe('get method', () => { + it('should invoke `datastore get `', async () => { + await datastore.datastoreGet({ appPath: '/some/path', datastoreName: 'datastore', primaryKeyValue: '1' }); + sandbox.assert.calledWith(spawnSpy, sinon.match('datastore get')); + }); + }); + describe('delete method', () => { + it('should invoke `datastore delete `', async () => { + await datastore.datastoreDelete({ appPath: '/some/path', datastoreName: 'datastore', primaryKeyValue: '1' }); + sandbox.assert.calledWith(spawnSpy, sinon.match('datastore delete')); + }); + }); + describe('put method', () => { + it('should invoke `datastore put [item details]`', async () => { + const itemObj = { + id: "1", + content: "text" + }; + await datastore.datastorePut({ appPath: '/some/path', datastoreName: 'datastore', putItem: itemObj }); + sandbox.assert.calledWith( + spawnSpy, + sinon.match('datastore put'), + ); + }); + }); + describe('query method', () => { + it('should invoke `datastore query [expression]`', async () => { + const expressObj = { + id: "1", + }; + await datastore.datastoreQuery({ appPath: '/some/path', datastoreName: 'datastore', queryExpression: 'id = :id', queryExpressionValues: expressObj}); + sandbox.assert.calledWith(spawnSpy, sinon.match('datastore query')); + }); + }); +}); diff --git a/packages/cli-test/src/cli/commands/datastore.ts b/packages/cli-test/src/cli/commands/datastore.ts new file mode 100644 index 000000000..9c8716fe1 --- /dev/null +++ b/packages/cli-test/src/cli/commands/datastore.ts @@ -0,0 +1,105 @@ +import type { ProjectCommandArguments } from '../../types/commands/common_arguments'; +import { SlackCLIProcess } from '../cli-process'; + +export interface DatastoreCommandArguments { + /** @description datastore name */ + datastoreName: string; + /** @description datastore get primary key value*/ + primaryKeyValue: string; + /** @description datastore put [item details] */ + putItem: object; + /** @description datastore query [expression] */ + queryExpression: string; + /** @description datastore query [expression expression_values] */ + queryExpressionValues: object; +} + +/** + * `slack datastore get` + * @returns command output + */ +export const datastoreGet = async function datastoreGet( + args: ProjectCommandArguments & Pick, +): Promise { + const getQueryObj = { + datastore: args.datastoreName, + id: args.primaryKeyValue + }; + const getQuery = JSON.stringify(getQueryObj); + const cmd = new SlackCLIProcess(`datastore get '${getQuery}'`, args); + const proc = await cmd.execAsync({ + cwd: args.appPath, + }); + return proc.output; +}; + +/** + * `slack datastore delete` + * @returns command output + */ +export const datastoreDelete = async function datastoreDelete( + args: ProjectCommandArguments & Pick, +): Promise { + const deleteQueryObj = { + datastore: args.datastoreName, + id: args.primaryKeyValue + }; + const deleteQuery = JSON.stringify(deleteQueryObj); + const cmd = new SlackCLIProcess(`datastore delete '${deleteQuery}'`, args); + const proc = await cmd.execAsync({ + cwd: args.appPath, + }); + return proc.output; +}; + +/** + * `slack datastore put` + * @returns command output + */ +export const datastorePut = async function datastorePut( + args: ProjectCommandArguments & Pick, +): Promise { + const putQueryObj = { + datastore: args.datastoreName, + item: args.putItem + }; + const putQuery = JSON.stringify(putQueryObj); + const cmd = new SlackCLIProcess( + `datastore put '${putQuery}'`, + args, + ); + const proc = await cmd.execAsync({ + cwd: args.appPath, + }); + return proc.output; +}; + +/** + * `slack datastore query` + * @returns command output + */ +export const datastoreQuery = async function datastoreQuery( + args: ProjectCommandArguments & Pick, +): Promise { + const queryObj = { + datastore: args.datastoreName, + expression: args.queryExpression, + expression_values: args.queryExpressionValues + }; + const query = JSON.stringify(queryObj); + const cmd = new SlackCLIProcess( + `datastore query '${query}'`, + args, + ); + const proc = await cmd.execAsync({ + cwd: args.appPath, + }); + return proc.output; +}; + +export default { + datastoreGet, + datastoreDelete, + datastorePut, + datastoreQuery, +};