Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Adding luis:application:create cmd and tests / refactor utils and clo… #351

Merged
merged 4 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
62 changes: 62 additions & 0 deletions packages/luis/src/commands/luis/application/create.ts
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__'
Copy link
Contributor

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


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}`)
}
}
}
3 changes: 2 additions & 1 deletion packages/luis/src/commands/luis/version/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ export default class LuisVersionClone extends Command {

async run() {
const {flags} = this.parse(LuisVersionClone)
const flagLabels = Object.keys(LuisVersionClone.flags)
const configDir = this.config.configDir
const configPrefix = 'luis__'

const {appId, endpoint, subscriptionKey, versionId, targetVersionId} = await utils.processInputs(flags, configDir, configPrefix)
const {appId, endpoint, subscriptionKey, versionId, targetVersionId} = await utils.processInputs(flags, flagLabels, configDir, configPrefix)

const requiredProps = {appId, endpoint, subscriptionKey, versionId, targetVersionId}
utils.validateRequiredProps(requiredProps)
Expand Down
16 changes: 8 additions & 8 deletions packages/luis/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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
}

Expand Down
62 changes: 62 additions & 0 deletions packages/luis/test/commands/luis/application/create.test.ts
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.')
})

})
6 changes: 3 additions & 3 deletions packages/luis/test/commands/luis/version/clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe('luis:version:clone', () => {
test
.stdout()
.stderr()
.command(['luis:version:clone', '--versionId', '0.1', '--targetVersionId', '0.2', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com/luis/authoring/v3.0-preview/'])
.command(['luis:version:clone', '--versionId', '0.1', '--targetVersionId', '0.2', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays an error if any required input parameters are missing', ctx => {
expect(ctx.stderr).to.contain(`Required input property 'appId' missing.`)
})

test
.stdout()
.stderr()
.command(['luis:version:clone', '--appId', uuidv1(), '--versionId', '0.1', '--targetVersionId', '0.2', '--endpoint', 'https://westus.api.cognitive.microsoft.com/luis/authoring/v3.0-preview/'])
.command(['luis:version:clone', '--appId', uuidv1(), '--versionId', '0.1', '--targetVersionId', '0.2', '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays an error if any required input parameters are missing', ctx => {
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
})
Expand All @@ -42,7 +42,7 @@ describe('luis:version:clone', () => {
.reply(201, '0.2')
)
.stdout()
.command(['luis:version:clone', '--appId', uuidv1(), '--versionId', '0.1', '--targetVersionId', '0.2', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com/luis/authoring/v3.0-preview/'])
.command(['luis:version:clone', '--appId', uuidv1(), '--versionId', '0.1', '--targetVersionId', '0.2', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('clones a luis app and returns the new version number', ctx => {
expect(ctx.stdout).to.contain('App successfully cloned.')
})
Expand Down