Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
max&min test (#790)
Browse files Browse the repository at this point in the history
* max&min test

* test

Co-authored-by: Qiaoqiao Zhang <55688292+qiaozha@users.noreply.github.com>
  • Loading branch information
changlong-liu and qiaozha authored Apr 2, 2021
1 parent b48d0de commit 6021d35
Show file tree
Hide file tree
Showing 23 changed files with 468 additions and 191 deletions.
4 changes: 2 additions & 2 deletions src/generate/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class CommandExample {
public Method_IsLongRun: boolean;
public MethodParams: MethodParam[];
public ExampleObj: any;
public commandStringItems;
public commandStringItems: string[];
public CommandString: string;
public WaitCommandString: string;
}
Expand Down Expand Up @@ -272,7 +272,7 @@ export interface CodeModelAz {
AzExample: CommandExample;
AzExample_CommandString: string;
AzExample_CommandStringItems: string[];
GetExamples(): CommandExample[];
GetExamples(includeGenerated: boolean): CommandExample[];
GetSubscriptionKey(): string;
GetPreparerEntities(): any[];
GatherInternalResource();
Expand Down
43 changes: 34 additions & 9 deletions src/generate/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ToSentence,
isNullOrUndefined,
ToMultiLine,
isGeneratedExampleId,
} from '../utils/helper';
import {
CodeGenConstants,
Expand Down Expand Up @@ -2507,6 +2508,14 @@ export class CodeModelCliImpl implements CodeModelAz {
return this.AzExample.CommandString;
}

public get AzExample_Id(): string {
return this.AzExample.Id;
}

public get AzExample_HttpMethod(): string {
return this.AzExample.HttpMethod;
}

public get AzExample_CommandStringItems(): string[] {
const items = [];
ToMultiLine(
Expand All @@ -2517,6 +2526,15 @@ export class CodeModelCliImpl implements CodeModelAz {
);
return items;
}

public get AzExample_RawCommandStringItems(): string[] {
return this.AzExample.commandStringItems;
}

public get AzExample_IsGenerated(): boolean {
return isGeneratedExampleId(this.AzExample?.Id);
}

/**
* Gets method parameters dict
* @returns method parameters dict : key is parameter name, value is the parameter schema
Expand Down Expand Up @@ -3107,18 +3125,24 @@ export class CodeModelCliImpl implements CodeModelAz {
return true;
}

public GetExamples(): CommandExample[] {
if (!isNullOrUndefined(this.Method_AzExamples) && this.Method_AzExamples.length > 0) {
public GetExamples(includeGenerated: boolean): CommandExample[] {
if (
!isNullOrUndefined(this.Method_AzExamples) &&
this.Method_AzExamples.length > 0 &&
includeGenerated
) {
return this.Method_AzExamples;
}
const examples: CommandExample[] = [];
if (this.Examples) {
Object.entries(this.Examples).forEach(([id, exampleObj]) => {
const example = this.CreateCommandExample(id, exampleObj);
if (!isNullOrUndefined(example)) examples.push(example);
if (includeGenerated || !isGeneratedExampleId(id)) {
const example = this.CreateCommandExample(id, exampleObj);
if (!isNullOrUndefined(example)) examples.push(example);
}
});
}
this.Method_AzExamples = examples;
if (includeGenerated) this.Method_AzExamples = examples;
return examples;
}

Expand Down Expand Up @@ -3429,7 +3453,7 @@ export class CodeModelCliImpl implements CodeModelAz {
const dependResources = [];
const dependParameters = [];

const examples = this.GetExamples();
const examples = this.GetExamples(false);
// recognize depends by endpoint in examples
for (const example of examples) {
for (const param of example.Parameters) {
Expand Down Expand Up @@ -3511,9 +3535,10 @@ export class CodeModelCliImpl implements CodeModelAz {
});

if (isNullOrUndefined(this._defaultTestScenario)) {
this._defaultTestScenario = GenerateDefaultTestScenario(this.GetAllExamples());
const allExamples = this.GetAllExamples();
this._defaultTestScenario = GenerateDefaultTestScenario(allExamples);
this._defaultTestScenario = GenerateDefaultTestScenarioByDependency(
this.GetAllExamples(),
allExamples,
this.resourcePool,
this._defaultTestScenario,
);
Expand Down Expand Up @@ -3670,7 +3695,7 @@ export class CodeModelCliImpl implements CodeModelAz {
if (isNullOrUndefined(example)) return;
examples = [example];
} else {
examples = this.GetExamples();
examples = this.GetExamples(true);
}
for (const example of examples) {
if (id && !this.matchExample(example, id)) continue;
Expand Down
6 changes: 3 additions & 3 deletions src/generate/renders/CliReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class CliReport extends TemplateBase {
converter,
),
],
['azExample', new RenderInput(['commandStringItems'], {})],
['azExample', new RenderInput(['commandStringItems'], {}, [['isGenerated', true]])],
]);

const dependencies = <[CodeModelTypes, CodeModelTypes][]>[
Expand Down Expand Up @@ -185,7 +185,7 @@ export class CliReport extends TemplateBase {
do {
if (model.SelectFirstMethod()) {
do {
if (model.GetExamples().length > 0) {
if (model.GetExamples(false).length > 0) {
mo.push(
'|[az ' +
model.CommandGroup_Name +
Expand Down Expand Up @@ -256,7 +256,7 @@ export class CliReport extends TemplateBase {
'`</a>',
);
mo.push('');
for (const example of model.GetExamples()) {
for (const example of model.GetExamples(false)) {
mo.push(
'##### <a name="' +
'Examples' +
Expand Down
2 changes: 1 addition & 1 deletion src/generate/renders/extraExt/CliExtReadme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class CliExtReadme extends TemplateBase {
const exampleCommandList: string[] = [];
if (this.model.SelectFirstCommand()) {
do {
exampleList = exampleList.concat(this.model.GetExamples());
exampleList = exampleList.concat(this.model.GetExamples(false));
} while (this.model.SelectNextCommand());
}

Expand Down
2 changes: 1 addition & 1 deletion src/generate/renders/generated/CliHelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ function generateCommandHelp(model: CodeModelAz, debug = false) {

if (model.SelectFirstMethod()) {
do {
for (const example of model.GetExamples()) {
for (const example of model.GetExamples(false)) {
if (!examplesStarted) {
output.push(' examples:');
examplesStarted = true;
Expand Down
79 changes: 53 additions & 26 deletions src/generate/renders/tests/CliTestCmdlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*-------------------------------------------------------------------------------------------- */
import * as path from 'path';
import { CodeModelAz, CommandExample } from '../../CodeModelAz';
import { CodeModelAz } from '../../CodeModelAz';
import { CliTestStep } from './CliTestStep';
import { deepCopy } from '../../../utils/helper';
import { deepCopy, isGeneratedExampleId, isNullOrUndefined } from '../../../utils/helper';
import { TemplateBase } from '../TemplateBase';
import { CodeModelTypes, PathConstants, RenderInput } from '../../../utils/models';

class ExampleInfo {
id: string;
lines: string[];
lastLine: string;
}
class CmdletTestCase {
functionName: string;
exampleInfos: ExampleInfo[] = [];
}
export class CliCmdletTest extends TemplateBase {
constructor(model: CodeModelAz, isNegativeTest: boolean) {
super(model);
Expand Down Expand Up @@ -43,7 +52,7 @@ export class CliCmdletTest extends TemplateBase {
const ret = {
testData: {
className: this.className,
cmds: [],
testCases: [] as CmdletTestCase[],
},
};
const inputProperties: Map<CodeModelTypes, RenderInput> = new Map<
Expand All @@ -53,46 +62,64 @@ export class CliCmdletTest extends TemplateBase {
['extension', new RenderInput()],
['commandGroup', new RenderInput()],
['command', new RenderInput()],
['method', new RenderInput(['azExamples'])],
['method', new RenderInput()],
['azExample', new RenderInput(['id', 'httpMethod', 'rawCommandStringItems'])],
]);

const dependencies = <[CodeModelTypes, CodeModelTypes][]>[
['extension', 'commandGroup'],
['commandGroup', 'command'],
['command', 'method'],
['method', 'azExample'],
];

for (const extension of this.model.getModelData('extension', inputProperties, dependencies)
.Extensions)
for (const commandGroup of extension.CommandGroups)
for (const command of commandGroup.Commands)
for (const method of command.Methods)
for (const example of method.azExamples as CommandExample[]) {
let functionName = CliTestStep.ToFunctionName(
{ name: example.Id },
example.commandStringItems[0],
);
const redundentPrefix = 'step_';
if (functionName.startsWith(redundentPrefix))
functionName = functionName.slice(redundentPrefix.length);
if (method.hasAzExample) {
(method.AzExamples as any[]).sort((a, b) => {
return a.id > b.id ? 1 : -1;
});
const testCase = new CmdletTestCase();
for (const example of method.AzExamples as any[]) {
const commandLines = deepCopy(
example.rawCommandStringItems,
) as string[];

const commandLines = deepCopy(example.commandStringItems) as string[];
if (
commandLines[0].indexOf(' delete') > -1 &&
example.HttpMethod.toLowerCase() === 'delete'
) {
commandLines[0] += ' -y';
}
ret.testData.cmds.push({
id: example.Id,
name: functionName,
lines: commandLines
if (
!isGeneratedExampleId(example.id) ||
isNullOrUndefined(testCase.functionName)
) {
testCase.functionName = CliTestStep.ToFunctionName(
{ name: example.id },
example.rawCommandStringItems[0],
);
const redundentPrefix = 'step_';
if (testCase.functionName.startsWith(redundentPrefix))
testCase.functionName = testCase.functionName.slice(
redundentPrefix.length,
);
}

if (
commandLines[0].indexOf(' delete') > -1 &&
example.httpMethod.toLowerCase() === 'delete'
) {
commandLines[0] += ' -y';
}
const exampleInfo = new ExampleInfo();
exampleInfo.id = example.id;
(exampleInfo.lines = commandLines
.slice(0, -1)
.map(
(x) => x.split('{').join('{{').split('}').join('}}') + ' ',
),
lastLine: commandLines.last,
});
)),
(exampleInfo.lastLine = commandLines.last);
testCase.exampleInfos.push(exampleInfo);
}
ret.testData.testCases.push(testCase);
}
return ret;
}
Expand Down
5 changes: 5 additions & 0 deletions src/templates/tests/cmdlet/conftest.py.njx
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
def pytest_runtest_logreport(report):
report.nodeid = "..." + report.nodeid[report.nodeid.rfind("::"):]


def pytest_sessionfinish(session, exitstatus):
if exitstatus == 5:
session.exitstatus = 0 # assume to be succeeded if no tests found
12 changes: 7 additions & 5 deletions src/templates/tests/cmdlet/test_negative.py.njx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ class {{testData.className}}(ScenarioTest):

def __init__(self, *args, **kwargs):
super({{testData.className}}, self).__init__(*args, **kwargs)
{% for cmd in testData.cmds %}
# EXAMPLE: {{cmd.id}}
def test_{{cmd.name}}(self):
{% for testCase in testData.testCases %}
def test_{{testCase.functionName}}(self):
{%- for exampleInfo in testCase.exampleInfos %}
# From {{exampleInfo.id}}
try:
self.cmd({% for line in cmd.lines %}'{{line}}'
self.cmd({% for line in exampleInfo.lines %}'{{line}}'
{% endfor -%}
'{{cmd.lastLine}}')
'{{exampleInfo.lastLine}}')
raise Exception("Error Expected!")
except ResourceNotFoundError as e:
assert e.message.startswith("(500)")
except SystemExit as e:
assert e.__context__.message.startswith("(500)")
{% endfor -%}
{% endfor -%}
12 changes: 7 additions & 5 deletions src/templates/tests/cmdlet/test_positive.py.njx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class {{testData.className}}(ScenarioTest):

def __init__(self, *args, **kwargs):
super({{testData.className}}, self).__init__(*args, **kwargs)
{% for cmd in testData.cmds %}
# EXAMPLE: {{cmd.id}}
def test_{{cmd.name}}(self):
self.cmd({% for line in cmd.lines %}'{{line}}'
{% for testCase in testData.testCases %}
def test_{{testCase.functionName}}(self):
{%- for exampleInfo in testCase.exampleInfos %}
# From {{exampleInfo.id}}
self.cmd({% for line in exampleInfo.lines %}'{{line}}'
{% endfor -%}
'{{cmd.lastLine}}')
'{{exampleInfo.lastLine}}')
{% endfor -%}
{% endfor -%}
5 changes: 5 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,8 @@ export function thoughtAsTrue(t: any) {
!isNullOrUndefined(t) && (t.toString().toLowerCase() === 'true' || typeof t === 'object')
);
}

export function isGeneratedExampleId(exampleId: string): boolean {
if (isNullOrUndefined(exampleId)) return false;
return exampleId.toLowerCase().endsWith('_gen');
}
6 changes: 6 additions & 0 deletions test/scenarios/attestation/input/attestation.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
"x-ms-examples": {
"AttestationProviders_Create": {
"$ref": "./examples/Create_AttestationProvider.json"
},
"AttestationProviders_Create_MaximumSet_Gen": {
"$ref": "./examples/Create_AttestationProvider_MaximumSet_Gen.json"
},
"AttestationProviders_Create_MinimumSet_Gen": {
"$ref": "./examples/Create_AttestationProvider_MinimumSet_Gen.json"
}
},
"parameters": [
Expand Down
Loading

0 comments on commit 6021d35

Please sign in to comment.