Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli-test(feat): Added datastore CLI commands #2041

Merged
merged 7 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};