Skip to content

Commit

Permalink
GSAGH-550: test modified
Browse files Browse the repository at this point in the history
  • Loading branch information
SandeepArup committed Dec 20, 2024
1 parent 487de55 commit 03b5bf5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
5 changes: 1 addition & 4 deletions GsaGH/Helpers/Assembly/ModelAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,8 @@ private void CheckIfModelIsEmpty() {
private void ConvertAndAssembleAnalysisTasks(List<GsaAnalysisTask> analysisTasks) {
// Set Analysis Tasks in model
if (analysisTasks != null) {
ReadOnlyDictionary<int, AnalysisTask> existingTasks = _model.AnalysisTasks();
foreach (GsaAnalysisTask task in analysisTasks) {
if (!existingTasks.Keys.Contains(task.Id)) {
TaskHelper.ImportAnalysisTask(task, ref _model);
}
task.Id = TaskHelper.ImportAnalysisTask(task, ref _model);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion GsaGH/Parameters/4_Analysis/GsaAnalysisTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public GsaAnalysisTask() {
internal GsaAnalysisTask(int id, Model model) {
Id = id;
ApiTask = model.AnalysisTasks()[Id];
CreateCases(model);
}

internal GsaAnalysisTask(AnalysisTask task, Model model) {
ApiTask = task;
CreateCases(model);
}
private void CreateCases(Model model) {
ReadOnlyDictionary<int, AnalysisCase> analysisCases = model.AnalysisCases();
foreach (int caseId in ApiTask.Cases) {
if (analysisCases.ContainsKey(caseId)) {
Expand All @@ -32,7 +40,6 @@ internal GsaAnalysisTask(int id, Model model) {
}
}
}

public override string ToString() {
return (Id > 0 ? $"ID:{Id} " : string.Empty) + $"'{ApiTask.Name}' {ApiTask.Type}".Replace("_", " ")
.TrimSpaces();
Expand Down
67 changes: 36 additions & 31 deletions GsaGHTests/1_BaseParameters/4_Analysis/GsaAnalysisTaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,40 @@ private List<GsaAnalysisTask> GsaAnalysisTasksFromSeedModel(bool withCase = fals
return new List<GsaAnalysisTask>() { new GsaAnalysisTask(id, gsaSeedModel.ApiModel) };
}

private GsaAnalysisTask CreateTask() {
return new GsaAnalysisTask(AnalysisTaskFactory.CreateStaticAnalysisTask("static"), _model.ApiModel);
}

[Fact]
public void WhenNoCasesProvidedShouldAddDefaultCaseCreatedFromLoadCase() {
GsaAPI.Model apiModel = _model.ApiModel;
TaskHelper.ImportAnalysisTask(new GsaAnalysisTask(apiModel.AddAnalysisTask(), apiModel), ref apiModel);
TaskHelper.ImportAnalysisTask(CreateTask(), ref _model);
AnalysisTask outTask = _model.ApiModel.AnalysisTasks().Last().Value;
//Gsa model has load case at Row(Or Id) 1 and 3
Assert.Equal(1, outTask.Cases[0]);
Assert.Equal(3, outTask.Cases[1]);
}

//[Fact]
//public void AnalysisCasesAreAppendedWhenAnalysisCaseIsAlreadyAttachedToAnotherTask() {
// GsaAPI.Model apiModel = _model.ApiModel;
// int taskId = apiModel.AddAnalysisTask();
// //this will create analysis case with Id = 1
// apiModel.AddAnalysisCaseToTask(taskId, "AnyName", "L1");
// int analysisCaseId = apiModel.AnalysisTasks()[taskId].Cases[0];
// Assert.Equal(1, analysisCaseId);

// //now create another task
// var newTask = new GsaAnalysisTask(apiModel.AddAnalysisTask(), apiModel);
// //and assign same analysis case Id
// newTask.Cases.Add(new GsaAnalysisCase() {
// Id = analysisCaseId,
// ApiCase = new AnalysisCase("AnyName", "L1")
// });

// ReadOnlyDictionary<int, AnalysisTask> ttasks = apiModel.AnalysisTasks();
// TaskHelper.AddAnalysisTask(newTask, ref apiModel);
// ttasks = apiModel.AnalysisTasks();
// Assert.Equal(2, ttasks.Keys.Max());
// int newAnalysCaseId = apiModel.AnalysisTasks()[newTask.Id].Cases[0];
// Assert.Equal(analysisCaseId + 1, newAnalysCaseId);
//}
[Fact]
public void AnalysisCasesAreAppendedWhenAnalysisCaseIsAlreadyAttachedToAnotherTask() {
int taskId = _model.ApiModel.AddAnalysisTask();
//this will create analysis case with Id = 1
_model.ApiModel.AddAnalysisCaseToTask(taskId, "AnyName", "L1");
int analysisCaseId = _model.ApiModel.AnalysisTasks()[taskId].Cases[0];
Assert.Equal(1, analysisCaseId);

//now create another task
GsaAnalysisTask newTask = CreateTask();
//and assign same analysis case Id
newTask.Cases.Add(new GsaAnalysisCase() {
Id = analysisCaseId,
ApiCase = new AnalysisCase("AnyName", "L1")
});

int newTaskId = TaskHelper.ImportAnalysisTask(newTask, ref _model);
Assert.Equal(2, _model.ApiModel.AnalysisTasks().Keys.Max());
int newAnalysCaseId = _model.ApiModel.AnalysisTasks()[newTaskId].Cases[0];
Assert.Equal(analysisCaseId + 1, newAnalysCaseId);
}

[Fact]
public void ShouldCreateDefaultTask() {
Expand All @@ -78,22 +78,27 @@ public void ShouldCreateDefaultTask() {
[Fact]
public void ShouldCreateDefaultCaseWhenCaseCountIsZero() {
GsaAPI.Model apiModel = _model.ApiModel;
TaskHelper.ImportAnalysisTask(GsaAnalysisTasksFromSeedModel().First(), ref apiModel);
TaskHelper.ImportAnalysisTask(CreateTask(), ref _model);
Assert.Equal(2, apiModel.AnalysisTasks().First().Value.Cases.Count);
}

[Fact]
public void AnalysisCasesWillBeEmptyInAbsenceOfLoadCase() {
var noLoadModel = new GsaModel();
GsaAPI.Model apiModel = noLoadModel.ApiModel;
TaskHelper.ImportAnalysisTask(GsaAnalysisTasksFromSeedModel().First(), ref apiModel);
Assert.Empty(apiModel.AnalysisTasks().First().Value.Cases);
TaskHelper.ImportAnalysisTask(CreateTask(), ref noLoadModel);
Assert.Empty(noLoadModel.ApiModel.AnalysisTasks().First().Value.Cases);
}

[Fact]
public void NullTaskWillNotBeImported() {
int TaskId = TaskHelper.ImportAnalysisTask(new GsaAnalysisTask(), ref _model);
Assert.Equal(-1, TaskId);
Assert.Empty(_model.ApiModel.AnalysisTasks());
}

[Fact]
public void EmptyConstructorTest() {
var task = new GsaAnalysisTask();

Assert.Equal(0, task.Id);
Assert.Null(task.ApiTask);
Assert.Empty(task.Cases);
Expand Down

0 comments on commit 03b5bf5

Please sign in to comment.