-
Notifications
You must be signed in to change notification settings - Fork 4.6k
.Net: Validate step types #13901
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
Merged
Merged
.Net: Validate step types #13901
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
dotnet/src/Experimental/Process.Runtime.Dapr.UnitTests/DaprStepInfoTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.SemanticKernel; | ||
| using Xunit; | ||
|
|
||
| namespace SemanticKernel.Process.Dapr.Runtime.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Unit tests for the <see cref="DaprStepInfo"/> class. | ||
| /// </summary> | ||
| public class DaprStepInfoTests | ||
| { | ||
| /// <summary> | ||
| /// Tests that ToKernelProcessStepInfo throws when InnerStepDotnetType is not a KernelProcessStep subclass. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ToKernelProcessStepInfoThrowsForInvalidStepType() | ||
| { | ||
| // Arrange | ||
| var stepInfo = new DaprStepInfo | ||
| { | ||
| InnerStepDotnetType = typeof(string).AssemblyQualifiedName!, | ||
| State = new KernelProcessStepState("TestStep", version: "v1"), | ||
| Edges = new Dictionary<string, List<KernelProcessEdge>>() | ||
| }; | ||
|
|
||
| // Act & Assert | ||
| var ex = Assert.Throws<KernelException>(() => stepInfo.ToKernelProcessStepInfo()); | ||
| Assert.Contains("is not a valid KernelProcessStep type", ex.Message); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that ToKernelProcessStepInfo throws when InnerStepDotnetType cannot be resolved. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ToKernelProcessStepInfoThrowsForUnresolvableType() | ||
| { | ||
| // Arrange | ||
| var stepInfo = new DaprStepInfo | ||
| { | ||
| InnerStepDotnetType = "NonExistent.Type, NonExistent.Assembly", | ||
| State = new KernelProcessStepState("TestStep", version: "v1"), | ||
| Edges = new Dictionary<string, List<KernelProcessEdge>>() | ||
| }; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<KernelException>(() => stepInfo.ToKernelProcessStepInfo()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that ToKernelProcessStepInfo succeeds for a valid KernelProcessStep subclass. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ToKernelProcessStepInfoSucceedsForValidStepType() | ||
| { | ||
| // Arrange | ||
| var stepInfo = new DaprStepInfo | ||
| { | ||
| InnerStepDotnetType = typeof(ValidTestStep).AssemblyQualifiedName!, | ||
| State = new KernelProcessStepState("TestStep", version: "v1"), | ||
| Edges = new Dictionary<string, List<KernelProcessEdge>>() | ||
| }; | ||
|
|
||
| // Act | ||
| var result = stepInfo.ToKernelProcessStepInfo(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal(typeof(ValidTestStep), result.InnerStepType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A valid test step for type validation testing. | ||
| /// </summary> | ||
| public sealed class ValidTestStep : KernelProcessStep | ||
| { | ||
| /// <summary> | ||
| /// A test function. | ||
| /// </summary> | ||
| [KernelFunction] | ||
| public void TestFunction() | ||
| { | ||
| } | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
dotnet/src/Experimental/Process.Runtime.Dapr.UnitTests/TypeInfoTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Text.Json; | ||
| using Microsoft.SemanticKernel; | ||
| using Microsoft.SemanticKernel.Process.Serialization; | ||
| using Xunit; | ||
|
|
||
| namespace SemanticKernel.Process.Dapr.Runtime.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Unit tests for the <see cref="TypeInfo"/> class. | ||
| /// </summary> | ||
| public class TypeInfoTests | ||
| { | ||
| /// <summary> | ||
| /// Tests that ConvertValue deserializes a JsonElement to the correct type. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ConvertValueDeserializesJsonElement() | ||
| { | ||
| // Arrange | ||
| var json = JsonSerializer.SerializeToElement(42); | ||
| var typeName = typeof(int).AssemblyQualifiedName; | ||
|
|
||
| // Act | ||
| var result = TypeInfo.ConvertValue(typeName, json); | ||
|
|
||
| // Assert | ||
| Assert.Equal(42, result); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that ConvertValue throws when the type name cannot be resolved. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ConvertValueThrowsForUnresolvableType() | ||
| { | ||
| // Arrange | ||
| var json = JsonSerializer.SerializeToElement(42); | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<KernelException>(() => | ||
| TypeInfo.ConvertValue("NonExistent.Type, NonExistent.Assembly", json)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that ConvertValue returns non-JsonElement values unchanged. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ConvertValueReturnsNonJsonElementUnchanged() | ||
| { | ||
| // Arrange | ||
| var value = "plain string"; | ||
|
|
||
| // Act | ||
| var result = TypeInfo.ConvertValue(typeof(string).AssemblyQualifiedName, value); | ||
|
|
||
| // Assert | ||
| Assert.Equal("plain string", result); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that ConvertValue returns null when value is null. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ConvertValueReturnsNullWhenValueIsNull() | ||
| { | ||
| // Act | ||
| var result = TypeInfo.ConvertValue(typeof(string).AssemblyQualifiedName, null); | ||
|
|
||
| // Assert | ||
| Assert.Null(result); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.