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

Commit

Permalink
20201204 minimum test (#670)
Browse files Browse the repository at this point in the history
* minimum test scenario

* output

* scenario name bug

* min call normal
  • Loading branch information
changlong-liu authored Dec 15, 2020
1 parent 733ba9b commit 4bf15b6
Show file tree
Hide file tree
Showing 31 changed files with 7,874 additions and 278 deletions.
2 changes: 1 addition & 1 deletion src/plugins/azgenerator/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export interface CodeModelAz {
GenerateTestInit(): void;
SelectFirstExample(): boolean;
SelectNextExample(): boolean;
FindExampleById(id: string, commandParams: any, examples: any[]): string[][];
FindExampleById(id: string, commandParams: any, examples: any[], minimum: boolean): string[][];
Example_Body: string[];
Example_Title: string;
Example_Params: any;
Expand Down
14 changes: 10 additions & 4 deletions src/plugins/azgenerator/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,11 @@ export class CodeModelCliImpl implements CodeModelAz {
}

public get MethodParameter_IsRequired(): boolean {
return this.MethodParameter.required;
return this.Parameter_IsRequired(this.MethodParameter);
}

private Parameter_IsRequired(param: Parameter): boolean {
return param?.required;
}

public get MethodParameter_IsFlattened(): boolean {
Expand Down Expand Up @@ -2313,13 +2317,14 @@ export class CodeModelCliImpl implements CodeModelAz {
return ret;
}

public GetExampleItems(example: CommandExample, isTest: boolean, commandParams: any): string[] {
public GetExampleItems(example: CommandExample, isTest: boolean, commandParams: any, minimum=false): string[] {
let parameters: string[] = [];
parameters.push("az " + this.Command_Name);

let hasRG = false;
let resourceObjectName = undefined;
for (let param of example.Parameters) {
if (minimum && !this.Parameter_IsRequired(param.methodParam.value)) continue;
let param_value = param.value;
if (isTest || this.FormalizeNames) {
let replaced_value = this.resource_pool.addEndpointResource(param_value, param.isJson, param.keyValue, [], [], param, isTest);
Expand Down Expand Up @@ -2357,6 +2362,7 @@ export class CodeModelCliImpl implements CodeModelAz {
resourceObject.example_params = [];
}
for (let param of example.Parameters) {
if (minimum && !this.Parameter_IsRequired(param.methodParam.value)) continue;
resourceObject.addOrUpdateParam(param);
}
resourceObject.testStatus = ObjectStatus.Created;
Expand Down Expand Up @@ -2414,11 +2420,11 @@ export class CodeModelCliImpl implements CodeModelAz {
}
}

public FindExampleById(id: string, commandParams: any, examples: CommandExample[]): string[][] {
public FindExampleById(id: string, commandParams: any, examples: CommandExample[], minimum=false): string[][] {
let ret: string[][] = [];
this.GetAllExamples(id, (example) => {
examples.push(example);
ret.push(this.GetExampleItems(example, true, commandParams));
ret.push(this.GetExampleItems(example, true, commandParams, minimum));
});
return ret;
}
Expand Down
142 changes: 88 additions & 54 deletions src/plugins/azgenerator/templates/tests/CliTestScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ export class CliTestScenario extends TemplateBase {

let class_info: string[] = [];
let initiates: string[] = [];
let body: string[] = [];

class_info.push(`# Test class for ${scenarioName}`);
class_info.push("@try_manual");
class_info.push("class " + Capitalize(this.groupName) + scenarioName + "Test(ScenarioTest):");
class_info.push("");
initiates.push("");

let subscription_id = model.GetSubscriptionKey();
if (subscription_id) {
Expand All @@ -75,73 +73,109 @@ export class CliTestScenario extends TemplateBase {
}

let decorators: string[] = [];
let parameterNames = CliTestStep.InitiateDependencies(model, this.header, decorators, initiates, this.groupName + "_" + scenarioName);
let parameterNames = CliTestStep.InitiateDependencies(model, this.header, decorators, initiates);
let jsonAdded = false;

let funcScenario: string[] = [];
let funcMinScenario: string[] = [];
let steps: string[] = [];
funcScenario.push(`# Testcase: ${scenarioName}`);
funcScenario.push("@try_manual");
funcScenario.push(...ToMultiLine(`def call_${scenarioName.toLowerCase()}(test${CliTestStep.parameterLine(parameterNames)}):`));
model.GetResourcePool().clearExampleParams();

// go through the examples to generate steps
for (var ci = 0; ci < config.length; ci++) {
let exampleId: string = config[ci].name;
let functionName: string = CliTestStep.ToFunctionName(config[ci]);
if (exampleId) {
let disabled: string = config[ci].disabled ? "# " : "";
// find example by name
let found = false;
let examples: CommandExample[] = [];
let exampleIdx = -1;
for (let exampleCmd of model.FindExampleById(exampleId, commandParams, examples)) {
exampleIdx += 1;
if (exampleCmd && exampleCmd.length > 0) {
found = true;
let checks = model.GetExampleChecks(examples[exampleIdx]);
functionName = CliTestStep.ToFunctionName({name: examples[exampleIdx].Id}, exampleCmd[0]);
if (checks.length > 0) {
funcScenario.push(...ToMultiLine(` ${disabled}${functionName}(test${CliTestStep.parameterLine(parameterNames)}, checks=[`));
for (let check of checks) {
ToMultiLine(" " + disabled + " " + check, funcScenario);
if (!jsonAdded && !disabled && check.indexOf("json.loads") >= 0) {
this.header.addImport("json");
jsonAdded = true;
funcMinScenario.push("@try_manual");
funcMinScenario.push(...ToMultiLine(`def call_${scenarioName.toLowerCase()}_min(test${CliTestStep.parameterLine(parameterNames)}):`));

function buildSenario(header: HeaderGenerator, outputFunc: string[], minimum: boolean) {
model.GetResourcePool().clearExampleParams();

// go through the examples to generate steps
for (var ci = 0; ci < config.length; ci++) {
let exampleId: string = config[ci].name;
let functionName: string = CliTestStep.ToFunctionName(config[ci]);
if (exampleId) {
let disabled: string = config[ci].disabled ? "# " : "";
// find example by name
let found = false;
let examples: CommandExample[] = [];
let exampleIdx = -1;
for (let exampleCmd of model.FindExampleById(exampleId, commandParams, examples, minimum)) {
exampleIdx += 1;
if (exampleCmd && exampleCmd.length > 0) {
found = true;
let checks = model.GetExampleChecks(examples[exampleIdx]);
functionName = CliTestStep.ToFunctionName({name: examples[exampleIdx].Id}, exampleCmd[0]);
if (minimum) functionName += "_min";
if (checks.length > 0) {
outputFunc.push(...ToMultiLine(` ${disabled}${functionName}(test${CliTestStep.parameterLine(parameterNames)}, checks=[`));
for (let check of checks) {
ToMultiLine(" " + disabled + " " + check, outputFunc);
if (!jsonAdded && !disabled && check.indexOf("json.loads") >= 0) {
header.addImport("json");
jsonAdded = true;
}
}
outputFunc.push(` ${disabled}])`);
}
else {
outputFunc.push(...ToMultiLine(` ${functionName}(test${CliTestStep.parameterLine(parameterNames)}, checks=[])`));
}
funcScenario.push(` ${disabled}])`);
}
else {
funcScenario.push(...ToMultiLine(` ${functionName}(test${CliTestStep.parameterLine(parameterNames)}, checks=[])`));
}
}
}
if (found) {
this.header.addFromImport(".example_steps", [functionName]);
if (found) {
header.addFromImport(".example_steps", [functionName]);
}
else {
outputFunc.push(...ToMultiLine(` # STEP NOT FOUND: ${exampleId}`));
}
}
else {
funcScenario.push(...ToMultiLine(` # STEP NOT FOUND: ${exampleId}`));
}
if (!minimum) {
steps.push(`# Env ${functionName}`);
steps.push("@try_manual");
steps.push(...ToMultiLine(`def ${functionName}(test${CliTestStep.parameterLine(parameterNames)}):`));
steps.push(" pass");
steps.push("");
steps.push("");
}
outputFunc.push(...ToMultiLine(` ${functionName}(test${CliTestStep.parameterLine(parameterNames)})`));
}
}
outputFunc.push("");
outputFunc.push("");
}
buildSenario(this.header, funcScenario, false);
buildSenario(this.header, funcMinScenario, true);

class_info.push(" def __init__(self):");
if (initiates.length>0) {
class_info.push(...initiates);
}
else {
class_info.push(" pass");
}
class_info.push("");
class_info.push("");

function buildTestcase(testcaseName: string, minimum: boolean) {
let ret = [...decorators];
if (minimum) testcaseName += "_min";
let funcLine = " def test_" + testcaseName + "(self";
for (let parameterName of parameterNames) {
funcLine += `, ${parameterName}`;
}
else {
steps.push(`# Env ${functionName}`);
steps.push("@try_manual");
steps.push(...ToMultiLine(`def ${functionName}(test${CliTestStep.parameterLine(parameterNames)}):`));
steps.push(" pass");
steps.push("");
steps.push("");
funcScenario.push(...ToMultiLine(` ${functionName}(test${CliTestStep.parameterLine(parameterNames)})`));
}
funcLine += "):";
ToMultiLine(funcLine, ret);
let _scenarioName = scenarioName;
if (minimum) _scenarioName += "_min";
ret.push(` call_${_scenarioName.toLowerCase()}(self${CliTestStep.parameterLine(parameterNames)})`);
ret.push(` calc_coverage(__file__)`);
ret.push(` raise_if()`);
ret.push("");
ret.push("");
return ret;
}
funcScenario.push("");
funcScenario.push("");
body.push(` call_${scenarioName.toLowerCase()}(self${CliTestStep.parameterLine(parameterNames)})`);
body.push(` calc_coverage(__file__)`);
body.push(` raise_if()`);
body.push("");
body.push("");
this.scenarios.push(...steps.concat(funcScenario, class_info, decorators, initiates, body));
let testCaseName = this.groupName + "_" + scenarioName;
this.scenarios.push(...steps.concat(funcScenario, funcMinScenario, class_info, buildTestcase(testCaseName, false), buildTestcase(testCaseName, true)));
}

private EndGenerateAzureCliTestScenario(): string[] {
Expand Down
Loading

0 comments on commit 4bf15b6

Please sign in to comment.