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

Commit 7007637

Browse files
author
JSpru
authored
Adding version unit tests (#410)
* Adding version unit tests * Remove success mssg from version export and update test
1 parent d1e1d3b commit 7007637

File tree

7 files changed

+457
-1
lines changed

7 files changed

+457
-1
lines changed

packages/luis/src/commands/luis/version/export.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export default class LuisVersionExport extends Command {
5151
this.log(`File successfully written: ${writtenFilePath}`)
5252
} else {
5353
await utils.writeToConsole(appJSON)
54-
this.log('App successfully exported\n')
5554
}
5655
} catch (error) {
5756
throw new CLIError(error)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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:clone', () => {
7+
8+
beforeEach(() => {
9+
sinon.stub(utils, 'processInputs').returnsArg(0)
10+
})
11+
12+
afterEach(() => {
13+
sinon.restore();
14+
});
15+
16+
test
17+
.stdout()
18+
.command(['luis:version:clone', '--help'])
19+
.it('should print the help contents when --help is passed as an argument', (ctx: any) => {
20+
expect(ctx.stdout).to.contain('Creates a new version equivalent to the current snapshot of the selected application version.')
21+
})
22+
23+
test
24+
.stdout()
25+
.stderr()
26+
.command(['luis:version:clone', '--versionId', '0.1', '--targetVersionId', '0.2', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
27+
.it('displays an error if any required input parameters are missing', (ctx: any) => {
28+
expect(ctx.stderr).to.contain(`Required input property 'appId' missing.`)
29+
})
30+
31+
test
32+
.stdout()
33+
.stderr()
34+
.command(['luis:version:clone', '--appId', uuidv1(), '--versionId', '0.1', '--targetVersionId', '0.2', '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
35+
.it('displays an error if any required input parameters are missing', (ctx: any) => {
36+
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
37+
})
38+
39+
test
40+
.nock('https://westus.api.cognitive.microsoft.com', api => api
41+
.post(uri => uri.includes('clone'))
42+
.reply(201, '0.2')
43+
)
44+
.stdout()
45+
.command(['luis:version:clone', '--appId', uuidv1(), '--versionId', '0.1', '--targetVersionId', '0.2', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
46+
.it('clones a luis app and returns the new version number', (ctx: any) => {
47+
expect(ctx.stdout).to.contain('App successfully cloned.')
48+
})
49+
50+
})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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:delete', () => {
7+
8+
beforeEach(() => {
9+
sinon.stub(utils, 'processInputs').returnsArg(0)
10+
})
11+
12+
afterEach(() => {
13+
sinon.restore();
14+
});
15+
16+
test
17+
.stdout()
18+
.command(['luis:version:delete', '--help'])
19+
.it('should print the help contents when --help is passed as an argument', ctx => {
20+
expect(ctx.stdout).to.contain('Deletes a LUIS application version')
21+
})
22+
23+
test
24+
.stdout()
25+
.stderr()
26+
.command(['luis:version:delete', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
27+
.it('displays an error if any required input parameters are missing', ctx => {
28+
expect(ctx.stderr).to.contain(`Required input property 'appId' missing.`)
29+
})
30+
31+
test
32+
.stdout()
33+
.stderr()
34+
.command(['luis:version:delete', '--appId', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
35+
.it('displays an error if any required input parameters are missing', ctx => {
36+
expect(ctx.stderr).to.contain(`Required input property 'versionId' missing.`)
37+
})
38+
39+
test
40+
.nock('https://westus.api.cognitive.microsoft.com', api => api
41+
.delete(uri => uri.includes('version'))
42+
.reply(200)
43+
)
44+
.stdout()
45+
.command(['luis:version:delete', '--appId', uuidv1(), '--versionId', '0.2', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
46+
.it('deletes a luis app and displays a success message', ctx => {
47+
expect(ctx.stdout).to.contain('Successfully deleted version')
48+
})
49+
50+
test
51+
.stdout()
52+
.stderr()
53+
.command(['luis:version:delete', '--appId', uuidv1(), '--versionId', '0.2', '--endpoint', 'undefined', '--subscriptionKey', uuidv1()])
54+
.it('fails to delete an app and displays an error message if the endpoint is undefined', ctx => {
55+
expect(ctx.stderr).to.contain('Failed to delete app version')
56+
})
57+
58+
})
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
const fs = require('fs-extra')
6+
import * as rimraf from 'rimraf'
7+
8+
describe('luis:version:export', () => {
9+
10+
before(() => {
11+
fs.mkdirSync('./testout');
12+
});
13+
14+
after(() => {
15+
rimraf('./testout', (err) => {
16+
if (err) console.log(err);
17+
})
18+
});
19+
20+
beforeEach(() => {
21+
sinon.stub(utils, 'processInputs').returnsArg(0)
22+
})
23+
24+
afterEach(() => {
25+
sinon.restore();
26+
});
27+
28+
test
29+
.stdout()
30+
.command(['luis:version:export', '--help'])
31+
.it('should print the help contents when --help is passed as an argument', ctx => {
32+
expect(ctx.stdout).to.contain('Exports a LUIS application to JSON format')
33+
})
34+
35+
test
36+
.stdout()
37+
.stderr()
38+
.command(['luis:version:export', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
39+
.it('displays an error if any required input parameters are missing', ctx => {
40+
expect(ctx.stderr).to.contain(`Required input property 'appId' missing.`)
41+
})
42+
43+
test
44+
.stdout()
45+
.stderr()
46+
.command(['luis:version:export', '--appId', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()])
47+
.it('displays an error if any required input parameters are missing', ctx => {
48+
expect(ctx.stderr).to.contain(`Required input property 'versionId' missing.`)
49+
})
50+
51+
test
52+
.nock('https://westus.api.cognitive.microsoft.com', api => api
53+
.get(uri => uri.includes('export'))
54+
.reply(200, {name: 'testname'})
55+
)
56+
.stdout()
57+
.command(['luis:version:export', '--appId', uuidv1(), '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
58+
.it('exports a luis app and displays the export contents in the console', ctx => {
59+
expect(ctx.stdout).to.contain('testname')
60+
})
61+
62+
test
63+
.nock('https://westus.api.cognitive.microsoft.com', api => api
64+
.get(uri => uri.includes('export'))
65+
.reply(200, {name: 'testname'})
66+
)
67+
.stdout()
68+
.command(['luis:version:export', '--appId', uuidv1(), '--out', './testout/test.json', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
69+
.it('exports a luis app, displays a success message in the console and the export contents to the specified file', ctx => {
70+
expect(ctx.stdout).to.contain('File successfully written:')
71+
})
72+
73+
test
74+
.nock('https://westus.api.cognitive.microsoft.com', api => api
75+
.get(uri => uri.includes('export'))
76+
.reply(200, {name: 'testname'})
77+
)
78+
.stdout()
79+
.stderr()
80+
.command(['luis:version:export', '--appId', uuidv1(), '--out', 'xyz', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
81+
.it('exports a luis app and displays a success message and the export contents in the console (since the target path provided is invalid)', ctx => {
82+
expect(ctx.stderr).to.contain('Target directory path doesn\'t exist:')
83+
})
84+
85+
test
86+
.nock('https://westus.api.cognitive.microsoft.com', api => api
87+
.get(uri => uri.includes('export'))
88+
.reply(200, {name: 'testname'})
89+
)
90+
.stdout()
91+
.stderr()
92+
.command(['luis:version:export', '--appId', uuidv1(), '--out', './testout/test.json', '--force', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
93+
.it('exports a luis app, displays a success message in the console and the export contents to the specified file, overwriting the existing file of the same name', ctx => {
94+
expect(ctx.stdout).to.contain('File successfully written')
95+
})
96+
97+
test
98+
.nock('https://westus.api.cognitive.microsoft.com', api => api
99+
.get(uri => uri.includes('export'))
100+
.reply(200, {name: 'testname'})
101+
)
102+
.stdout()
103+
.command(['luis:version:export', '--appId', uuidv1(), '--out', './testout/test.json', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
104+
.it('exports a luis app, displays a success message in the console and the export contents to the specified file, incrementing the filename', ctx => {
105+
expect(ctx.stdout).to.contain('File successfully written')
106+
expect(ctx.stdout).to.contain('test(1).json')
107+
})
108+
109+
})
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 from JSON or LU content.')
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+
})

0 commit comments

Comments
 (0)