Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data Driven Test Tool] Add YAML support #2302

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Tools/AI Test Toolkit/src/AITCommandLineCard.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ page 149042 "AIT CommandLine Card"
InputDatasetOutStream := TempBlob.CreateOutStream();
InputDatasetOutStream.WriteText(InputDataset);
TempBlob.CreateInStream(InputDatasetInStream);
TestInputsManagement.UploadAndImportDataInputsFromJson(InputDatasetFilename, InputDatasetInStream);
TestInputsManagement.UploadAndImportDataInputs(InputDatasetFilename, InputDatasetInStream);
end;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ codeunit 149037 "AIT AL Test Suite Mgt"
Error(SameDatasetNameErr, DatasetFileName, StrSubstNo(SourceOfTheDatasetIsAppIdLbl, TestInputGroup."Imported by AppId"));
end;

TestInputsManagement.UploadAndImportDataInputsFromJson(DatasetFileName, DatasetInStream, CallerModuleInfo.Id);
TestInputsManagement.UploadAndImportDataInputs(DatasetFileName, DatasetInStream, CallerModuleInfo.Id);
end;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pageextension 130451 "Data Driven Test Tool" extends "AL Test Tool"
var
TestInputsManagement: Codeunit "Test Inputs Management";
begin
TestInputsManagement.UploadAndImportDataInputsFromJson();
TestInputsManagement.UploadAndImportDataInputs();
end;
}
action(DataInputs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ page 130462 "Test Input Groups"
{
Caption = 'Import data-driven test inputs';
AllowMultipleFiles = true;
ToolTip = 'Import data-driven test inputs from a JSON or JSONL file';
AllowedFileExtensions = '.jsonl', '.json';
ToolTip = 'Import data-driven test inputs from a JSON, JSONL or YAML file';
AllowedFileExtensions = '.jsonl', '.json', '.yaml';
Image = Attach;

trigger OnAction(Files: List of [FileUpload])
Expand All @@ -55,9 +55,9 @@ page 130462 "Test Input Groups"
CurrentFile: FileUpload;
FileDataInStream: InStream;
begin
foreach CurrentFile in files do begin
foreach CurrentFile in Files do begin
CurrentFile.CreateInStream(FileDataInStream, TextEncoding::UTF8);
TestInputsManagement.UploadAndImportDataInputsFromJson(CurrentFile.FileName, FileDataInStream);
TestInputsManagement.UploadAndImportDataInputs(CurrentFile.FileName, FileDataInStream);
end;
end;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ page 130459 "Test Input Part"
{
Caption = 'Import';
Image = ImportCodes;
ToolTip = 'Import data-driven test inputs from a JSON file';
ToolTip = 'Import data-driven test inputs from a JSON, JSONL or YAML file';

trigger OnAction()
var
TestInputGroup: Record "Test Input Group";
TestInputsManagement: Codeunit "Test Inputs Management";
begin
TestInputGroup.Get(Rec."Test Input Group Code");
TestInputsManagement.UploadAndImportDataInputsFromJson(TestInputGroup);
TestInputsManagement.UploadAndImportDataInputs();
end;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ codeunit 130458 "Test Inputs Management"
EventSubscriberInstance = Manual;

procedure SelectTestGroupsAndExpandTestLine(var TestMethodLine: Record "Test Method Line")

var
TestInputGroup: Record "Test Input Group";
TestInput: Record "Test Input";
Expand Down Expand Up @@ -99,21 +98,27 @@ codeunit 130458 "Test Inputs Management"
CodeunitTestMethodLine.ModifyAll("Data Input", DataInput.Code);
end;

procedure UploadAndImportDataInputsFromJson()
procedure UploadAndImportDataInputs()
var
TestInputGroup: Record "Test Input Group";
TempDummyTestInput: Record "Test Input" temporary;
TestInputInStream: InStream;
FileName: Text;
begin
UploadAndImportDataInputsFromJson(TestInputGroup);
TempDummyTestInput."Test Input".CreateInStream(TestInputInStream);
if not UploadIntoStream(ChooseFileLbl, '', '', FileName, TestInputInStream) then
exit;

UploadAndImportDataInputs(FileName, TestInputInStream);
end;

procedure UploadAndImportDataInputsFromJson(FileName: Text; TestInputInStream: InStream)
procedure UploadAndImportDataInputs(FileName: Text; TestInputInStream: InStream)
var
EmptyGuid: Guid;
begin
UploadAndImportDataInputsFromJson(FileName, TestInputInStream, EmptyGuid);
UploadAndImportDataInputs(FileName, TestInputInStream, EmptyGuid);
end;

procedure UploadAndImportDataInputsFromJson(FileName: Text; TestInputInStream: InStream; ImportedByAppId: Guid)
procedure UploadAndImportDataInputs(FileName: Text; TestInputInStream: InStream; ImportedByAppId: Guid)
var
TestInputGroup: Record "Test Input Group";
TestInput: Record "Test Input";
Expand All @@ -135,25 +140,44 @@ codeunit 130458 "Test Inputs Management"
ParseDataInputsJsonl(TestInputInStream, TestInputGroup);
end;

if FileName.EndsWith(YamlFileExtensionTxt) then begin
FileType := YamlFileExtensionTxt;
TestInputInStream.Read(InputText);
ParseDataInputsYaml(InputText, TestInputGroup);
end;

// Log telemetry for the number of lines imported and the file type
TestInput.SetRange("Test Input Group Code", TestInputGroup.Code);
TelemetryCD.Add('File Type', FileType);
TelemetryCD.Add('No. of entries', Format(TestInput.Count()));
Session.LogMessage('0000NF1', 'Data Driven Test: Test Input Imported', Verbosity::Normal, DataClassification::SystemMetadata, TelemetryScope::All, TelemetryCD);
end;

#if not CLEAN26
[Obsolete('Replaced by UploadAndImportDataInputs.', '26.0')]
procedure UploadAndImportDataInputsFromJson()
begin
UploadAndImportDataInputs();
end;

[Obsolete('Replaced by UploadAndImportDataInputs.', '26.0')]
procedure UploadAndImportDataInputsFromJson(var TestInputGroup: Record "Test Input Group")
var
TempDummyTestInput: Record "Test Input" temporary;
TestInputInStream: InStream;
FileName: Text;
begin
TempDummyTestInput."Test Input".CreateInStream(TestInputInStream);
if not UploadIntoStream(ChooseFileLbl, '', '', FileName, TestInputInStream) then
exit;
UploadAndImportDataInputs()
end;

[Obsolete('Replaced by UploadAndImportDataInputs.', '26.0')]
procedure UploadAndImportDataInputsFromJson(FileName: Text; TestInputInStream: InStream)
begin
UploadAndImportDataInputs(FileName, TestInputInStream);
end;

UploadAndImportDataInputsFromJson(FileName, TestInputInStream);
[Obsolete('Replaced by UploadAndImportDataInputs.', '26.0')]
procedure UploadAndImportDataInputsFromJson(FileName: Text; TestInputInStream: InStream; ImportedByAppId: Guid)
begin
UploadAndImportDataInputs(FileName, TestInputInStream, ImportedByAppId);
end;
#endif

procedure ImportDataInputsFromText(var TestInputGroup: Record "Test Input Group"; DataInputText: Text)
begin
Expand Down Expand Up @@ -212,6 +236,29 @@ codeunit 130458 "Test Inputs Management"
Error(CouldNotParseJsonlInputErr, JsonLine);
end;

local procedure ParseDataInputsYaml(TestData: Text; var TestInputGroup: Record "Test Input Group")
var
DataInputJsonObject: JsonObject;
DataInputJsonToken: JsonToken;
DataInputJsonArray: JsonArray;
begin
if not DataInputJsonObject.ReadFromYaml(TestData) then
Error(CouldNotParseInputErr);

if not DataInputJsonObject.Get(TestsTok, DataInputJsonToken) then begin
InsertDataInputLine(DataInputJsonObject, TestInputGroup);
exit;
end;

if DataInputJsonToken.IsArray() then begin
DataInputJsonArray := DataInputJsonToken.AsArray();
InsertDataInputsFromJsonArray(TestInputGroup, DataInputJsonArray);
exit;
end;

InsertDataInputLine(DataInputJsonObject, TestInputGroup);
end;

local procedure InsertDataInputsFromJsonArray(var TestInputGroup: Record "Test Input Group"; var DataOnlyTestInputsArray: JsonArray)
var
TestInputJsonToken: JsonToken;
Expand Down Expand Up @@ -302,6 +349,7 @@ codeunit 130458 "Test Inputs Management"
var
DataNameTok: Label 'name', Locked = true;
DescriptionTok: Label 'description', Locked = true;
TestsTok: Label 'tests', Locked = true;
TestInputTok: Label 'testInput', Locked = true;
ChooseFileLbl: Label 'Choose a file to import';
TestInputNameTok: Label 'INPUT-', Locked = true;
Expand All @@ -310,4 +358,5 @@ codeunit 130458 "Test Inputs Management"
LineTypeMustBeCodeunitErr: Label 'Line type must be Codeunit.';
JsonFileExtensionTxt: Label '.json', Locked = true;
JsonlFileExtensionTxt: Label '.jsonl', Locked = true;
YamlFileExtensionTxt: Label '.yaml', Locked = true;
}
Loading