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

Commit d6ed93c

Browse files
JSprumunozemilio
authored andcommitted
307 luis version import (#369)
* Adding luis:version:import * Update examples
1 parent 8700bd0 commit d6ed93c

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*!
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
import {CLIError, Command, flags} from '@microsoft/bf-cli-command'
7+
8+
const utils = require('../../../utils/index')
9+
10+
export default class LuisVersionImport extends Command {
11+
static description = 'Imports a new version into a LUIS application'
12+
13+
static examples = [`
14+
$ bf luis:version:import --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --appId {APP_ID} --in {PATH_TO_JSON} --versionId {VERSION_ID}
15+
$ echo {SERIALIZED_JSON} | bf luis:version:import --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --appId {APP_ID}
16+
`]
17+
18+
static flags = {
19+
help: flags.help({char: 'h'}),
20+
appId: flags.string({description: 'LUIS application Id'}),
21+
versionId: flags.string({description: 'LUIS application version to import'}),
22+
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
23+
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}),
24+
in: flags.string({char: 'i', description: 'File path containing LUIS application contents'})
25+
}
26+
27+
async run() {
28+
const {flags} = this.parse(LuisVersionImport)
29+
const flagLabels = Object.keys(LuisVersionImport.flags)
30+
const configDir = this.config.configDir
31+
const stdin = await this.readStdin()
32+
const options: any = {}
33+
34+
let {appId, versionId, endpoint, subscriptionKey, inVal} = await utils.processInputs(flags, flagLabels, configDir)
35+
36+
const requiredProps = {appId, endpoint, subscriptionKey}
37+
utils.validateRequiredProps(requiredProps)
38+
39+
inVal = inVal ? inVal.trim() : flags.in
40+
41+
const appJSON = stdin ? stdin : await utils.getInputFromFile(inVal)
42+
if (!appJSON) throw new CLIError('No import data found - please provide input through stdin or the --in flag')
43+
44+
if (versionId) options.versionId = versionId
45+
46+
const client = utils.getLUISClient(subscriptionKey, endpoint)
47+
48+
try {
49+
const newVersionId = await client.versions.importMethod(appId, JSON.parse(appJSON), options)
50+
this.log(`App version successfully imported as version ${newVersionId}.`)
51+
} catch (err) {
52+
throw new CLIError(`Failed to import app version: ${err}`)
53+
}
54+
}
55+
56+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import {expect, test} from '@oclif/test'
2+
const sinon = require('sinon')
3+
const uuidv1 = require('uuid/v1')
4+
const utils = require('../../../../src/utils/index')
5+
6+
describe('luis:version:import', () => {
7+
8+
beforeEach(() => {
9+
sinon.stub(utils, 'processInputs').returnsArg(0)
10+
11+
})
12+
13+
afterEach(() => {
14+
sinon.restore();
15+
});
16+
17+
test
18+
.stdout()
19+
.command(['luis:version:import', '--help'])
20+
.it('should print the help contents when --help is passed as an argument', ctx => {
21+
expect(ctx.stdout).to.contain('Imports a new version into a LUIS application')
22+
})
23+
24+
test
25+
.stdout()
26+
.stderr()
27+
.command(['luis:version:import', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
28+
.it('displays an error if any required input parameters are missing', ctx => {
29+
expect(ctx.stderr).to.contain(`Required input property 'appId' missing.`)
30+
})
31+
32+
test
33+
.stdout()
34+
.stderr()
35+
.command(['luis:version:import', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--appId', uuidv1()])
36+
.it('displays an error if any required input parameters are missing', ctx => {
37+
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
38+
})
39+
40+
test
41+
.nock('https://westus.api.cognitive.microsoft.com', api => api
42+
.post(uri => uri.includes('apps'))
43+
.reply(201, '0.9')
44+
)
45+
.stdout()
46+
.stderr()
47+
.command(['luis:version:import', '--appId', uuidv1(), '--in', './test/fixtures/sample-app-version.json', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1(), '--appId', uuidv1()])
48+
.it('imports a luis app version from a file and returns the app\'s new version id', ctx => {
49+
expect(ctx.stdout).to.contain('App version successfully imported as version 0.9')
50+
})
51+
52+
test
53+
.nock('https://westus.api.cognitive.microsoft.com', api => api
54+
.post(uri => uri.includes('apps'))
55+
.reply(201, '0.7')
56+
)
57+
.stdout()
58+
.stderr()
59+
.command(['luis:version:import', '--versionId', '0.7', '--appId', uuidv1(), '--in', './test/fixtures/sample-app-version.json', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1(), '--appId', uuidv1()])
60+
.it('imports a luis app version from a file and returns the app\'s new version id, as specified by the versionId flag', ctx => {
61+
expect(ctx.stdout).to.contain('App version successfully imported as version 0.7')
62+
})
63+
64+
test
65+
.stdout()
66+
.stderr()
67+
.command(['luis:version:import', '--appId', uuidv1(), '--in', './test/fixtures/xyz.json', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
68+
.it('displays an error message if the import file cannot be found', ctx => {
69+
expect(ctx.stderr).to.contain('Failed to read app JSON')
70+
})
71+
72+
test
73+
.stdout()
74+
.stderr()
75+
.command(['luis:version:import', '--appId', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
76+
.it('displays an error message if no input data detected', ctx => {
77+
expect(ctx.stderr).to.contain('No import data found - please provide input through stdin or the --in flag')
78+
})
79+
80+
test
81+
.stdin('{"luis_schema_version": "4.0.0","versionId": "0.9","name": "sampleapp","desc": "test description","culture": "en-us","tokenizerVersion": "1.0.0","intents": [{"name": "None"}],"entities": [],"composites": [],"closedLists": [],"patternAnyEntities": [],"regex_entities": [],"prebuiltEntities": [],"model_features": [],"regex_features": [],"patterns": [],"utterances": [],"settings": []}')
82+
.stdout()
83+
.stderr()
84+
.command(['luis:version:import', '--appId', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
85+
.it('imports a luis app version from stdin and returns the app\'s id', ctx => {
86+
process.stdin.setEncoding('utf8')
87+
process.stdin.once('data', data => {
88+
expect(ctx.stderr).to.contain('App version successfully imported as version 0.9')
89+
})
90+
})
91+
92+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"luis_schema_version": "4.0.0",
3+
"versionId": "0.9",
4+
"name": "minimal-sample_app",
5+
"desc": "test description",
6+
"culture": "en-us",
7+
"tokenizerVersion": "1.0.0",
8+
"intents": [
9+
{
10+
"name": "None"
11+
}
12+
],
13+
"entities": [],
14+
"composites": [],
15+
"closedLists": [],
16+
"patternAnyEntities": [],
17+
"regex_entities": [],
18+
"prebuiltEntities": [],
19+
"model_features": [],
20+
"regex_features": [],
21+
"patterns": [],
22+
"utterances": [],
23+
"settings": []
24+
}

0 commit comments

Comments
 (0)