mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Add unit tests for EditTableV2Executor (#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>
This commit is contained in:
co-authored by
crickman
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Chris Rickman
Copilot
parent
0521f5bed8
commit
9f4c5f3faa
+3
-1
@@ -17,7 +17,9 @@ internal sealed class AddConversationMessageExecutor(AddConversationMessage mode
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(this.Model.Message);
|
||||
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
|
||||
|
||||
string conversationId = this.Evaluator.GetValue(this.Model.ConversationId).Value;
|
||||
bool isWorkflowConversation = context.IsWorkflowConversation(conversationId, out string? _);
|
||||
|
||||
@@ -26,7 +28,7 @@ internal sealed class AddConversationMessageExecutor(AddConversationMessage mode
|
||||
// Capture the created message, which includes the assigned ID.
|
||||
newMessage = await agentProvider.CreateMessageAsync(conversationId, newMessage, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await this.AssignAsync(this.Model.Message?.Path, newMessage.ToRecord(), context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.Message.Path, newMessage.ToRecord(), context).ConfigureAwait(false);
|
||||
|
||||
if (isWorkflowConversation)
|
||||
{
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ internal sealed class ClearAllVariablesExecutor(ClearAllVariables model, Workflo
|
||||
VariablesToClear.ConversationScopedVariables => WorkflowFormulaState.DefaultScopeName,
|
||||
VariablesToClear.ConversationHistory => null,
|
||||
VariablesToClear.UserScopedVariables => null,
|
||||
_ => null
|
||||
_ => null,
|
||||
};
|
||||
|
||||
if (scope is not null)
|
||||
|
||||
+4
-1
@@ -7,6 +7,7 @@ using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
|
||||
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using Microsoft.PowerFx.Types;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.ObjectModel;
|
||||
|
||||
@@ -15,8 +16,10 @@ internal sealed class CreateConversationExecutor(CreateConversation model, Workf
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
|
||||
|
||||
string conversationId = await agentProvider.CreateConversationAsync(cancellationToken).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.ConversationId?.Path, FormulaValue.New(conversationId), context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.ConversationId.Path, FormulaValue.New(conversationId), context).ConfigureAwait(false);
|
||||
await context.QueueConversationUpdateAsync(conversationId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
|
||||
+9
-9
@@ -18,12 +18,12 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
PropertyPath variablePath = Throw.IfNull(this.Model.ItemsVariable?.Path, $"{nameof(this.Model)}.{nameof(this.Model.ItemsVariable)}");
|
||||
Throw.IfNull(this.Model.ItemsVariable, $"{nameof(this.Model)}.{nameof(this.Model.ItemsVariable)}");
|
||||
|
||||
FormulaValue table = context.ReadState(variablePath);
|
||||
FormulaValue table = context.ReadState(this.Model.ItemsVariable);
|
||||
if (table is not TableValue tableValue)
|
||||
{
|
||||
throw this.Exception($"Require '{variablePath}' to be a table, not: '{table.GetType().Name}'.");
|
||||
throw this.Exception($"Require '{this.Model.ItemsVariable.Path}' to be a table, not: '{table.GetType().Name}'.");
|
||||
}
|
||||
|
||||
EditTableOperation? changeType = this.Model.ChangeType;
|
||||
@@ -33,12 +33,12 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
|
||||
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(addItemValue);
|
||||
RecordValue newRecord = BuildRecord(tableValue.Type.ToRecord(), expressionResult.Value.ToFormula());
|
||||
await tableValue.AppendAsync(newRecord, cancellationToken).ConfigureAwait(false);
|
||||
await this.AssignAsync(variablePath, newRecord, context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.ItemsVariable, newRecord, context).ConfigureAwait(false);
|
||||
}
|
||||
else if (changeType is ClearItemsOperation)
|
||||
{
|
||||
await tableValue.ClearAsync(cancellationToken).ConfigureAwait(false);
|
||||
await this.AssignAsync(variablePath, FormulaValue.NewBlank(), context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.ItemsVariable, FormulaValue.NewBlank(), context).ConfigureAwait(false);
|
||||
}
|
||||
else if (changeType is RemoveItemOperation removeItemOperation)
|
||||
{
|
||||
@@ -46,8 +46,8 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
|
||||
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(removeItemValue);
|
||||
if (expressionResult.Value.ToFormula() is TableValue removeItemTable)
|
||||
{
|
||||
await tableValue.RemoveAsync(removeItemTable?.Rows.Select(row => row.Value), all: true, cancellationToken).ConfigureAwait(false);
|
||||
await this.AssignAsync(variablePath, FormulaValue.NewBlank(), context).ConfigureAwait(false);
|
||||
await tableValue.RemoveAsync(removeItemTable.Rows.Select(row => row.Value), all: true, cancellationToken).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.ItemsVariable, FormulaValue.NewBlank(), context).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else if (changeType is TakeLastItemOperation)
|
||||
@@ -56,7 +56,7 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
|
||||
if (lastRow is not null)
|
||||
{
|
||||
await tableValue.RemoveAsync([lastRow], all: true, cancellationToken).ConfigureAwait(false);
|
||||
await this.AssignAsync(variablePath, lastRow, context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.ItemsVariable, lastRow, context).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else if (changeType is TakeFirstItemOperation)
|
||||
@@ -65,7 +65,7 @@ internal sealed class EditTableV2Executor(EditTableV2 model, WorkflowFormulaStat
|
||||
if (firstRow is not null)
|
||||
{
|
||||
await tableValue.RemoveAsync([firstRow], all: true, cancellationToken).ConfigureAwait(false);
|
||||
await this.AssignAsync(variablePath, firstRow, context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.ItemsVariable, firstRow, context).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-12
@@ -19,24 +19,18 @@ internal sealed class ParseValueExecutor(ParseValue model, WorkflowFormulaState
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
PropertyPath variablePath = Throw.IfNull(this.Model.Variable?.Path, $"{nameof(this.Model)}.{nameof(model.Variable)}");
|
||||
Throw.IfNull(this.Model.ValueType, $"{nameof(this.Model)}.{nameof(model.ValueType)}");
|
||||
Throw.IfNull(this.Model.Variable, $"{nameof(this.Model)}.{nameof(model.Variable)}");
|
||||
ValueExpression valueExpression = Throw.IfNull(this.Model.Value, $"{nameof(this.Model)}.{nameof(this.Model.Value)}");
|
||||
|
||||
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(valueExpression);
|
||||
|
||||
FormulaValue parsedValue;
|
||||
if (this.Model.ValueType is not null)
|
||||
{
|
||||
VariableType targetType = new(this.Model.ValueType);
|
||||
object? parsedResult = expressionResult.Value.ToObject().ConvertType(targetType);
|
||||
parsedValue = parsedResult.ToFormula();
|
||||
}
|
||||
else
|
||||
{
|
||||
parsedValue = expressionResult.Value.ToFormula();
|
||||
}
|
||||
VariableType targetType = new(this.Model.ValueType);
|
||||
object? parsedResult = expressionResult.Value.ToObject().ConvertType(targetType);
|
||||
parsedValue = parsedResult.ToFormula();
|
||||
|
||||
await this.AssignAsync(variablePath, parsedValue, context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.Variable.Path, parsedValue, context).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
+1
@@ -17,6 +17,7 @@ internal sealed class ResetVariableExecutor(ResetVariable model, WorkflowFormula
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(this.Model.Variable, $"{nameof(this.Model)}.{nameof(model.Variable)}");
|
||||
|
||||
await context.QueueStateResetAsync(this.Model.Variable, cancellationToken).ConfigureAwait(false);
|
||||
Debug.WriteLine(
|
||||
$"""
|
||||
|
||||
+3
-1
@@ -16,13 +16,15 @@ internal sealed class RetrieveConversationMessageExecutor(RetrieveConversationMe
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(this.Model.Message);
|
||||
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
|
||||
|
||||
string conversationId = this.Evaluator.GetValue(this.Model.ConversationId).Value;
|
||||
string messageId = this.Evaluator.GetValue(Throw.IfNull(this.Model.MessageId, $"{nameof(this.Model)}.{nameof(this.Model.MessageId)}")).Value;
|
||||
|
||||
ChatMessage message = await agentProvider.GetMessageAsync(conversationId, messageId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await this.AssignAsync(this.Model.Message?.Path, message.ToRecord(), context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.Message.Path, message.ToRecord(), context).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
+5
-13
@@ -18,11 +18,13 @@ internal sealed class RetrieveConversationMessagesExecutor(RetrieveConversationM
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(this.Model.Messages);
|
||||
Throw.IfNull(this.Model.ConversationId, $"{nameof(this.Model)}.{nameof(this.Model.ConversationId)}");
|
||||
|
||||
string conversationId = this.Evaluator.GetValue(this.Model.ConversationId).Value;
|
||||
|
||||
List<ChatMessage> messages = [];
|
||||
await foreach (var m in agentProvider.GetMessagesAsync(
|
||||
await foreach (ChatMessage message in agentProvider.GetMessagesAsync(
|
||||
conversationId,
|
||||
limit: this.GetLimit(),
|
||||
after: this.GetMessage(this.Model.MessageAfter),
|
||||
@@ -30,21 +32,16 @@ internal sealed class RetrieveConversationMessagesExecutor(RetrieveConversationM
|
||||
newestFirst: this.IsDescending(),
|
||||
cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
messages.Add(m);
|
||||
messages.Add(message);
|
||||
}
|
||||
|
||||
await this.AssignAsync(this.Model.Messages?.Path, messages.ToTable(), context).ConfigureAwait(false);
|
||||
await this.AssignAsync(this.Model.Messages.Path, messages.ToTable(), context).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
private int? GetLimit()
|
||||
{
|
||||
if (this.Model.Limit is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
long limit = this.Evaluator.GetValue(this.Model.Limit).Value;
|
||||
return Convert.ToInt32(Math.Min(limit, 100));
|
||||
}
|
||||
@@ -61,11 +58,6 @@ internal sealed class RetrieveConversationMessagesExecutor(RetrieveConversationM
|
||||
|
||||
private bool IsDescending()
|
||||
{
|
||||
if (this.Model.SortOrder is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
AgentMessageSortOrderWrapper sortOrderWrapper = this.Evaluator.GetValue(this.Model.SortOrder).Value;
|
||||
|
||||
return sortOrderWrapper.Value == AgentMessageSortOrder.NewestFirst;
|
||||
|
||||
+6
-9
@@ -7,6 +7,7 @@ using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
|
||||
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using Microsoft.PowerFx.Types;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.ObjectModel;
|
||||
|
||||
@@ -15,16 +16,12 @@ internal sealed class SetTextVariableExecutor(SetTextVariable model, WorkflowFor
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (this.Model.Value is null)
|
||||
{
|
||||
await this.AssignAsync(this.Model.Variable?.Path, FormulaValue.NewBlank(), context).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
FormulaValue expressionResult = FormulaValue.New(this.Engine.Format(this.Model.Value));
|
||||
Throw.IfNull(this.Model.Variable);
|
||||
Throw.IfNull(this.Model.Value);
|
||||
|
||||
await this.AssignAsync(this.Model.Variable?.Path, expressionResult, context).ConfigureAwait(false);
|
||||
}
|
||||
FormulaValue expressionResult = FormulaValue.New(this.Engine.Format(this.Model.Value));
|
||||
|
||||
await this.AssignAsync(this.Model.Variable.Path, expressionResult, context).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
+6
-10
@@ -7,7 +7,7 @@ using Microsoft.Agents.AI.Workflows.Declarative.Interpreter;
|
||||
using Microsoft.Agents.AI.Workflows.Declarative.PowerFx;
|
||||
using Microsoft.Agents.ObjectModel;
|
||||
using Microsoft.Agents.ObjectModel.Abstractions;
|
||||
using Microsoft.PowerFx.Types;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
|
||||
namespace Microsoft.Agents.AI.Workflows.Declarative.ObjectModel;
|
||||
|
||||
@@ -16,16 +16,12 @@ internal sealed class SetVariableExecutor(SetVariable model, WorkflowFormulaStat
|
||||
{
|
||||
protected override async ValueTask<object?> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (this.Model.Value is null)
|
||||
{
|
||||
await this.AssignAsync(this.Model.Variable?.Path, FormulaValue.NewBlank(), context).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(this.Model.Value);
|
||||
Throw.IfNull(this.Model.Variable);
|
||||
Throw.IfNull(this.Model.Value);
|
||||
|
||||
await this.AssignAsync(this.Model.Variable?.Path, expressionResult.Value.ToFormula(), context).ConfigureAwait(false);
|
||||
}
|
||||
EvaluationResult<DataValue> expressionResult = this.Evaluator.GetValue(this.Model.Value);
|
||||
|
||||
await this.AssignAsync(this.Model.Variable.Path, expressionResult.Value.ToFormula(), context).ConfigureAwait(false);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user