Skip to content

Commit

Permalink
cli-test(feat): Added datastore CLI commands (#2041)
Browse files Browse the repository at this point in the history
  • Loading branch information
cchensh authored Oct 1, 2024
2 parents 365b521 + 2562383 commit d8d4406
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/cli-test/src/cli/commands/datastore.spec.ts
Original file line number Diff line number Diff line change
@@ -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 <query>`', 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 <query>`', 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'));
});
});
});
105 changes: 105 additions & 0 deletions packages/cli-test/src/cli/commands/datastore.ts
Original file line number Diff line number Diff line change
@@ -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<DatastoreCommandArguments, 'datastoreName' | 'primaryKeyValue'>,
): Promise<string> {
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<DatastoreCommandArguments, 'datastoreName' | 'primaryKeyValue'>,
): Promise<string> {
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<DatastoreCommandArguments, 'datastoreName' | 'putItem'>,
): Promise<string> {
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<DatastoreCommandArguments, 'datastoreName' | 'queryExpression' | 'queryExpressionValues'>,
): Promise<string> {
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,
};

0 comments on commit d8d4406

Please sign in to comment.