Skip to content

Commit 9f4c5f3

Browse files
CopilotcrickmanCopilot
authored
.NET: Add unit tests for EditTableV2Executor (microsoft#3773)
* Initial plan * Add comprehensive unit tests for EditTableV2Executor - Test AddItemOperation with record and scalar values - Test ClearItemsOperation - Test RemoveItemOperation - Test TakeLastItemOperation (with items and empty table) - Test TakeFirstItemOperation (with items and empty table) - Test error cases (null ItemsVariable, non-table variable) - Include ExecuteTestAsync and CreateModel helper methods - All 10 tests passing Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Add comprehensive unit tests for EditTableV2Executor - complete with 100% coverage - Added 13 comprehensive tests covering all code paths - Test AddItemOperation with record and scalar values - Test ClearItemsOperation - Test RemoveItemOperation (including non-table value case) - Test TakeLastItemOperation (with items and empty table) - Test TakeFirstItemOperation (with items and empty table) - Test error cases (null ItemsVariable, non-table variable, null operation values) - Include ExecuteTestAsync and CreateModel helper methods - 100% line and branch coverage achieved Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Update tests / refine product code * Checkpoint * Updated * Update dotnet/tests/Microsoft.Agents.AI.Workflows.Declarative.UnitTests/ObjectModel/SetTextVariableExecutorTest.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address code review feedback - Fix typo: rename metadataExpresssion to metadataExpression - Fix test name in AddMessageWithMetadataAsync (was using wrong test name) - Fix test name in ClearGlobalScopeAsync (was using wrong test name) - Remove pre-population in SetTextVariableExecutorTest that made tests ineffective - Use explicit .Where() filter in SetMultipleVariablesExecutorTest foreach loop Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> Co-authored-by: Chris Rickman <crickman@microsoft.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0521f5b commit 9f4c5f3

20 files changed

Lines changed: 630 additions & 186 deletions

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/AddConversationMessageExecutor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ internal sealed class AddConversationMessageExecutor(AddConversationMessage mode
1717
{
1818
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
1919
{
20+
Throw.IfNull(this.Model.Message);
2021
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
22+
2123
string conversationId = this.Evaluator.GetValue(this.Model.ConversationId).Value;
2224
bool isWorkflowConversation = context.IsWorkflowConversation(conversationId, out string? _);
2325

@@ -26,7 +28,7 @@ internal sealed class AddConversationMessageExecutor(AddConversationMessage mode
2628
// Capture the created message, which includes the assigned ID.
2729
newMessage = await agentProvider.CreateMessageAsync(conversationId, newMessage, cancellationToken).ConfigureAwait(false);
2830

29-
await this.AssignAsync(this.Model.Message?.Path, newMessage.ToRecord(), context).ConfigureAwait(false);
31+
await this.AssignAsync(this.Model.Message.Path, newMessage.ToRecord(), context).ConfigureAwait(false);
3032

3133
if (isWorkflowConversation)
3234
{

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/ClearAllVariablesExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal sealed class ClearAllVariablesExecutor(ClearAllVariables model, Workflo
2323
VariablesToClear.ConversationScopedVariables => WorkflowFormulaState.DefaultScopeName,
2424
VariablesToClear.ConversationHistory => null,
2525
VariablesToClear.UserScopedVariables => null,
26-
_ => null
26+
_ => null,
2727
};
2828

2929
if (scope is not null)

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/CreateConversationExecutor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
88
using Microsoft.Agents.ObjectModel;
99
using Microsoft.PowerFx.Types;
10+
using Microsoft.Shared.Diagnostics;
1011

1112
namespace Microsoft.Agents.AI.Workflows.Declarative.ObjectModel;
1213

@@ -15,8 +16,10 @@ internal sealed class CreateConversationExecutor(CreateConversation model, Workf
1516
{
1617
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
1718
{
19+
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
20+
1821
string conversationId = await agentProvider.CreateConversationAsync(cancellationToken).ConfigureAwait(false);
19-
await this.AssignAsync(this.Model.ConversationId?.Path, FormulaValue.New(conversationId), context).ConfigureAwait(false);
22+
await this.AssignAsync(this.Model.ConversationId.Path, FormulaValue.New(conversationId), context).ConfigureAwait(false);
2023
await context.QueueConversationUpdateAsync(conversationId, cancellationToken).ConfigureAwait(false);
2124

2225
return default;

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/EditTableV2Executor.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
1818
{
1919
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
2020
{
21-
PropertyPath variablePath = Throw.IfNull(this.Model.ItemsVariable?.Path, $"{nameof(this.Model)}.{nameof(this.Model.ItemsVariable)}");
21+
Throw.IfNull(this.Model.ItemsVariable, $"{nameof(this.Model)}.{nameof(this.Model.ItemsVariable)}");
2222

23-
FormulaValue table = context.ReadState(variablePath);
23+
FormulaValue table = context.ReadState(this.Model.ItemsVariable);
2424
if (table is not TableValue tableValue)
2525
{
26-
throw this.Exception($"Require '{variablePath}' to be a table, not: '{table.GetType().Name}'.");
26+
throw this.Exception($"Require '{this.Model.ItemsVariable.Path}' to be a table, not: '{table.GetType().Name}'.");
2727
}
2828

2929
EditTableOperation? changeType = this.Model.ChangeType;
@@ -33,21 +33,21 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
3333
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(addItemValue);
3434
RecordValue newRecord = BuildRecord(tableValue.Type.ToRecord(), expressionResult.Value.ToFormula());
3535
await tableValue.AppendAsync(newRecord, cancellationToken).ConfigureAwait(false);
36-
await this.AssignAsync(variablePath, newRecord, context).ConfigureAwait(false);
36+
await this.AssignAsync(this.Model.ItemsVariable, newRecord, context).ConfigureAwait(false);
3737
}
3838
else if (changeType is ClearItemsOperation)
3939
{
4040
await tableValue.ClearAsync(cancellationToken).ConfigureAwait(false);
41-
await this.AssignAsync(variablePath, FormulaValue.NewBlank(), context).ConfigureAwait(false);
41+
await this.AssignAsync(this.Model.ItemsVariable, FormulaValue.NewBlank(), context).ConfigureAwait(false);
4242
}
4343
else if (changeType is RemoveItemOperation removeItemOperation)
4444
{
4545
ValueExpression removeItemValue = Throw.IfNull(removeItemOperation.Value, $"{nameof(this.Model)}.{nameof(this.Model.ChangeType)}");
4646
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(removeItemValue);
4747
if (expressionResult.Value.ToFormula() is TableValue removeItemTable)
4848
{
49-
await tableValue.RemoveAsync(removeItemTable?.Rows.Select(row => row.Value), all: true, cancellationToken).ConfigureAwait(false);
50-
await this.AssignAsync(variablePath, FormulaValue.NewBlank(), context).ConfigureAwait(false);
49+
await tableValue.RemoveAsync(removeItemTable.Rows.Select(row => row.Value), all: true, cancellationToken).ConfigureAwait(false);
50+
await this.AssignAsync(this.Model.ItemsVariable, FormulaValue.NewBlank(), context).ConfigureAwait(false);
5151
}
5252
}
5353
else if (changeType is TakeLastItemOperation)
@@ -56,7 +56,7 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
5656
if (lastRow is not null)
5757
{
5858
await tableValue.RemoveAsync([lastRow], all: true, cancellationToken).ConfigureAwait(false);
59-
await this.AssignAsync(variablePath, lastRow, context).ConfigureAwait(false);
59+
await this.AssignAsync(this.Model.ItemsVariable, lastRow, context).ConfigureAwait(false);
6060
}
6161
}
6262
else if (changeType is TakeFirstItemOperation)
@@ -65,7 +65,7 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
6565
if (firstRow is not null)
6666
{
6767
await tableValue.RemoveAsync([firstRow], all: true, cancellationToken).ConfigureAwait(false);
68-
await this.AssignAsync(variablePath, firstRow, context).ConfigureAwait(false);
68+
await this.AssignAsync(this.Model.ItemsVariable, firstRow, context).ConfigureAwait(false);
6969
}
7070
}
7171

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/ParseValueExecutor.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,18 @@ internal sealed class ParseValueExecutor(ParseValue model, WorkflowFormulaState
1919
{
2020
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
2121
{
22-
PropertyPath variablePath = Throw.IfNull(this.Model.Variable?.Path, $"{nameof(this.Model)}.{nameof(model.Variable)}");
22+
Throw.IfNull(this.Model.ValueType, $"{nameof(this.Model)}.{nameof(model.ValueType)}");
23+
Throw.IfNull(this.Model.Variable, $"{nameof(this.Model)}.{nameof(model.Variable)}");
2324
ValueExpression valueExpression = Throw.IfNull(this.Model.Value, $"{nameof(this.Model)}.{nameof(this.Model.Value)}");
2425

2526
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(valueExpression);
2627

2728
FormulaValue parsedValue;
28-
if (this.Model.ValueType is not null)
29-
{
30-
VariableType targetType = new(this.Model.ValueType);
31-
object? parsedResult = expressionResult.Value.ToObject().ConvertType(targetType);
32-
parsedValue = parsedResult.ToFormula();
33-
}
34-
else
35-
{
36-
parsedValue = expressionResult.Value.ToFormula();
37-
}
29+
VariableType targetType = new(this.Model.ValueType);
30+
object? parsedResult = expressionResult.Value.ToObject().ConvertType(targetType);
31+
parsedValue = parsedResult.ToFormula();
3832

39-
await this.AssignAsync(variablePath, parsedValue, context).ConfigureAwait(false);
33+
await this.AssignAsync(this.Model.Variable.Path, parsedValue, context).ConfigureAwait(false);
4034

4135
return default;
4236
}

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/ResetVariableExecutor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal sealed class ResetVariableExecutor(ResetVariable model, WorkflowFormula
1717
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
1818
{
1919
Throw.IfNull(this.Model.Variable, $"{nameof(this.Model)}.{nameof(model.Variable)}");
20+
2021
await context.QueueStateResetAsync(this.Model.Variable, cancellationToken).ConfigureAwait(false);
2122
Debug.WriteLine(
2223
$"""

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/RetrieveConversationMessageExecutor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ internal sealed class RetrieveConversationMessageExecutor(RetrieveConversationMe
1616
{
1717
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
1818
{
19+
Throw.IfNull(this.Model.Message);
1920
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
21+
2022
string conversationId = this.Evaluator.GetValue(this.Model.ConversationId).Value;
2123
string messageId = this.Evaluator.GetValue(Throw.IfNull(this.Model.MessageId, $"{nameof(this.Model)}.{nameof(this.Model.MessageId)}")).Value;
2224

2325
ChatMessage message = await agentProvider.GetMessageAsync(conversationId, messageId, cancellationToken).ConfigureAwait(false);
2426

25-
await this.AssignAsync(this.Model.Message?.Path, message.ToRecord(), context).ConfigureAwait(false);
27+
await this.AssignAsync(this.Model.Message.Path, message.ToRecord(), context).ConfigureAwait(false);
2628

2729
return default;
2830
}

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/RetrieveConversationMessagesExecutor.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,30 @@ internal sealed class RetrieveConversationMessagesExecutor(RetrieveConversationM
1818
{
1919
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
2020
{
21+
Throw.IfNull(this.Model.Messages);
2122
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
23+
2224
string conversationId = this.Evaluator.GetValue(this.Model.ConversationId).Value;
2325

2426
List<ChatMessage> messages = [];
25-
await foreach (var m in agentProvider.GetMessagesAsync(
27+
await foreach (ChatMessage message in agentProvider.GetMessagesAsync(
2628
conversationId,
2729
limit: this.GetLimit(),
2830
after: this.GetMessage(this.Model.MessageAfter),
2931
before: this.GetMessage(this.Model.MessageBefore),
3032
newestFirst: this.IsDescending(),
3133
cancellationToken).ConfigureAwait(false))
3234
{
33-
messages.Add(m);
35+
messages.Add(message);
3436
}
3537

36-
await this.AssignAsync(this.Model.Messages?.Path, messages.ToTable(), context).ConfigureAwait(false);
38+
await this.AssignAsync(this.Model.Messages.Path, messages.ToTable(), context).ConfigureAwait(false);
3739

3840
return default;
3941
}
4042

4143
private int? GetLimit()
4244
{
43-
if (this.Model.Limit is null)
44-
{
45-
return null;
46-
}
47-
4845
long limit = this.Evaluator.GetValue(this.Model.Limit).Value;
4946
return Convert.ToInt32(Math.Min(limit, 100));
5047
}
@@ -61,11 +58,6 @@ internal sealed class RetrieveConversationMessagesExecutor(RetrieveConversationM
6158

6259
private bool IsDescending()
6360
{
64-
if (this.Model.SortOrder is null)
65-
{
66-
return false;
67-
}
68-
6961
AgentMessageSortOrderWrapper sortOrderWrapper = this.Evaluator.GetValue(this.Model.SortOrder).Value;
7062

7163
return sortOrderWrapper.Value == AgentMessageSortOrder.NewestFirst;

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/SetTextVariableExecutor.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
88
using Microsoft.Agents.ObjectModel;
99
using Microsoft.PowerFx.Types;
10+
using Microsoft.Shared.Diagnostics;
1011

1112
namespace Microsoft.Agents.AI.Workflows.Declarative.ObjectModel;
1213

@@ -15,16 +16,12 @@ internal sealed class SetTextVariableExecutor(SetTextVariable model, WorkflowFor
1516
{
1617
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
1718
{
18-
if (this.Model.Value is null)
19-
{
20-
await this.AssignAsync(this.Model.Variable?.Path, FormulaValue.NewBlank(), context).ConfigureAwait(false);
21-
}
22-
else
23-
{
24-
FormulaValue expressionResult = FormulaValue.New(this.Engine.Format(this.Model.Value));
19+
Throw.IfNull(this.Model.Variable);
20+
Throw.IfNull(this.Model.Value);
2521

26-
await this.AssignAsync(this.Model.Variable?.Path, expressionResult, context).ConfigureAwait(false);
27-
}
22+
FormulaValue expressionResult = FormulaValue.New(this.Engine.Format(this.Model.Value));
23+
24+
await this.AssignAsync(this.Model.Variable.Path, expressionResult, context).ConfigureAwait(false);
2825

2926
return default;
3027
}

dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/ObjectModel/SetVariableExecutor.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
88
using Microsoft.Agents.ObjectModel;
99
using Microsoft.Agents.ObjectModel.Abstractions;
10-
using Microsoft.PowerFx.Types;
10+
using Microsoft.Shared.Diagnostics;
1111

1212
namespace Microsoft.Agents.AI.Workflows.Declarative.ObjectModel;
1313

@@ -16,16 +16,12 @@ internal sealed class SetVariableExecutor(SetVariable model, WorkflowFormulaStat
1616
{
1717
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
1818
{
19-
if (this.Model.Value is null)
20-
{
21-
await this.AssignAsync(this.Model.Variable?.Path, FormulaValue.NewBlank(), context).ConfigureAwait(false);
22-
}
23-
else
24-
{
25-
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(this.Model.Value);
19+
Throw.IfNull(this.Model.Variable);
20+
Throw.IfNull(this.Model.Value);
2621

27-
await this.AssignAsync(this.Model.Variable?.Path, expressionResult.Value.ToFormula(), context).ConfigureAwait(false);
28-
}
22+
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(this.Model.Value);
23+
24+
await this.AssignAsync(this.Model.Variable.Path, expressionResult.Value.ToFormula(), context).ConfigureAwait(false);
2925

3026
return default;
3127
}

0 commit comments

Comments
 (0)