This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Adding luis:application:create cmd and tests / refactor utils and clo… #351
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,62 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import {CLIError, Command, flags} from '@microsoft/bf-cli-command' | ||
|
||
const utils = require('../../../utils/index') | ||
|
||
export default class LuisApplicationCreate extends Command { | ||
static description = 'Creates a new LUIS application' | ||
|
||
static examples = [` | ||
$ bf luis:application:create --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --name {NAME} --culture {CULTURE} | ||
--domain {DOMAIN} --description {DESCRIPTION} --initialVersionId {INITIAL_VERSION_ID} --usageScenario {USAGE_SCENARIO} | ||
`] | ||
|
||
static flags = { | ||
help: flags.help({char: 'h'}), | ||
endpoint: flags.string({description: 'LUIS endpoint hostname'}), | ||
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}), | ||
name: flags.string({description: 'LUIS application name'}), | ||
culture: flags.string({description: 'LUIS application culture'}), | ||
domain: flags.string({description: 'LUIS application domain'}), | ||
description: flags.string({description: 'LUIS application description'}), | ||
initialVersionId: flags.string({description: 'LUIS application initial version Id'}), | ||
usageScenario: flags.string({description: 'LUIS application usage scenario'}), | ||
} | ||
|
||
async run() { | ||
const {flags} = this.parse(LuisApplicationCreate) | ||
const flagLabels = Object.keys(LuisApplicationCreate.flags) | ||
const configDir = this.config.configDir | ||
const configPrefix = 'luis__' | ||
|
||
const { | ||
endpoint, | ||
subscriptionKey, | ||
name, | ||
culture, | ||
domain, | ||
description, | ||
initialVersionId, | ||
usageScenario | ||
} = await utils.processInputs(flags, flagLabels, configDir, configPrefix) | ||
|
||
const requiredProps = {endpoint, subscriptionKey, name, domain} | ||
utils.validateRequiredProps(requiredProps) | ||
|
||
const client = utils.getLUISClient(subscriptionKey, endpoint) | ||
const options = {} | ||
|
||
const applicationCreateObject = {name, culture, domain, description, initialVersionId, usageScenario} | ||
|
||
try { | ||
const newAppId = await client.apps.add(applicationCreateObject, options) | ||
this.log(`App successfully created with id ${newAppId}.`) | ||
} catch (err) { | ||
throw new CLIError(`Failed to create app: ${err}`) | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -43,16 +43,16 @@ const getPropFromConfig = async (prop: string, configDir: string) => { | |
} | ||
} | ||
|
||
const processInputs = async (flags: any, configDir: string, prefix: string) => { | ||
const processInputs = async (flags: any, flagLabels: string[], configDir: string, prefix: string) => { | ||
let config = await getUserConfig(configDir) | ||
config = config ? filterConfig(config, prefix) : config | ||
const input = { | ||
appId: flags.appId || (config ? config.luis__appId : null), | ||
endpoint: flags.endpoint || (config ? config.luis__endpoint : null), | ||
subscriptionKey: flags.subscriptionKey || (config ? config.luis__subscriptionKey : null), | ||
versionId: flags.versionId || (config ? config.luis__versionId : null), | ||
targetVersionId: flags.targetVersionId || (config ? config.luis__targetVersionId : null) | ||
} | ||
const input: any = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO the previous implementation is clearer to understand what the programs is retrieving from the config, plus it is easier to control the flow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the previous implementation was hardcoded to one cmd (clone) so it wasn't flexible for general use. That's why I changed it to set the flags dynamically so that it works across the board. |
||
flagLabels | ||
.filter(flag => flag !== 'help') | ||
.map((flag: string) => { | ||
input[flag] = flags[flag] || (config ? config[prefix + flag] : null) | ||
}) | ||
|
||
return input | ||
} | ||
|
||
|
62 changes: 62 additions & 0 deletions
62
packages/luis/test/commands/luis/application/create.test.ts
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,62 @@ | ||
import {expect, test} from '@oclif/test' | ||
const sinon = require('sinon') | ||
const uuidv1 = require('uuid/v1') | ||
const utils = require('../../../../src/utils/index') | ||
|
||
describe('luis:application:create', () => { | ||
|
||
before(() => { | ||
const newAppId = uuidv1() | ||
}) | ||
|
||
beforeEach(() => { | ||
sinon.stub(utils, 'processInputs').returnsArg(0) | ||
}) | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
test | ||
.stdout() | ||
.command(['luis:application:create', '--help']) | ||
.it('should print the help contents when --help is passed as an argument', ctx => { | ||
expect(ctx.stdout).to.contain('Creates a new LUIS application') | ||
}) | ||
|
||
test | ||
.stdout() | ||
.stderr() | ||
.command(['luis:application:create', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1(), '--culture', 'en-us', '--domain', 'Comics', '--description', 'test description', '--initialVersionId', '0.04', '--usageScenario', 'For use in our test app']) | ||
.it('displays an error if any required input parameters are missing', ctx => { | ||
expect(ctx.stderr).to.contain(`Required input property 'name' missing.`) | ||
}) | ||
|
||
test | ||
.stdout() | ||
.stderr() | ||
.command(['luis:application:create', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--name', 'orange_app', '--culture', 'en-us', '--domain', 'Comics', '--description', 'test description', '--initialVersionId', '0.04', '--usageScenario', 'For use in our test app']) | ||
.it('displays an error if any required input parameters are missing', ctx => { | ||
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`) | ||
}) | ||
|
||
test | ||
.nock('https://westus.api.cognitive.microsoft.com', api => api | ||
.post(uri => uri.includes('apps')) | ||
.reply(201, '99999') | ||
) | ||
.stdout() | ||
.command(['luis:application:create', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--name', 'orange_app', '--subscriptionKey', uuidv1(), '--culture', 'en-us', '--domain', 'Comics', '--description', 'test description', '--initialVersionId', '0.04', '--usageScenario', 'For use in our test app']) | ||
.it('creates a luis app and returns the new app\'s id', ctx => { | ||
expect(ctx.stdout).to.contain('App successfully created with id 99999') | ||
}) | ||
|
||
test | ||
.stdout() | ||
.stderr() | ||
.command(['luis:application:create', '--endpoint', 'undefined', '--name', 'orange_app', '--subscriptionKey', uuidv1(), '--culture', 'en-us', '--domain', 'Comics', '--description', 'test description', '--initialVersionId', '0.04', '--usageScenario', 'For use in our test app']) | ||
.it('fails to create an app and displays an error message if the endpoint is null', ctx => { | ||
expect(ctx.stderr).to.contain('Access denied due to invalid subscription key or wrong API endpoint.') | ||
}) | ||
|
||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be moved to utils since it is the same for all commands
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done