-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli-test(feat): Added
datastore
CLI commands (#2041)
- Loading branch information
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |