Skip to content

Commit

Permalink
[C#] feat: add unit test for models (#1187)
Browse files Browse the repository at this point in the history
## Linked issues

#minor: partial of #907

## Details

Provide a list of your changes here. If you are fixing a bug, please
provide steps to reproduce the bug.

#### Change details

> Describe your changes, with screenshots and code snippets as
appropriate

**code snippets**:

**screenshots**:

## Attestation Checklist

- [x] My code follows the style guidelines of this project

- I have checked for/fixed spelling, linting, and other errors
- I have commented my code for clarity
- I have made corresponding changes to the documentation (we use
[TypeDoc](https://typedoc.org/) to document our code)
- My changes generate no new warnings
- I have added tests that validates my changes, and provides sufficient
test coverage. I have tested with:
  - Local testing
  - E2E testing in Teams
- New and existing unit tests pass locally with my changes

### Additional information

> Feel free to add other relevant information below
  • Loading branch information
kuojianlu authored Jan 23, 2024
1 parent fd7be25 commit e47b5d7
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Teams.AI.AI.Models;

namespace Microsoft.Teams.AI.Tests.AITests.Models
{
public class ChatMessageExtensionsTests
{
[Fact]
public void Test_ToAzureSdkChatMessage()
{
// Arrange
var chatMessage = new ChatMessage(ChatRole.User)
{
Name = "test-name",
Content = "test-content",
};

// Act
var result1 = chatMessage.ToAzureSdkChatMessage();
chatMessage.FunctionCall = new FunctionCall("test-name", "test-args");
var result2 = chatMessage.ToAzureSdkChatMessage();

// Assert
Assert.Equal(Azure.AI.OpenAI.ChatRole.User, result1.Role);
Assert.Equal("test-name", result1.Name);
Assert.Equal("test-content", result1.Content);
Assert.Null(result1.FunctionCall);
Assert.Equal("test-name", result2.FunctionCall.Name);
Assert.Equal("test-args", result2.FunctionCall.Arguments);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Microsoft.Teams.AI.AI.Models;

namespace Microsoft.Teams.AI.Tests.AITests.Models
{
public class ChatRoleTests
{
[Fact]
public void Test_Constructor_ArgumentNullException()
{
// Arrange
string? role = null;

// Act
var exception = Assert.Throws<ArgumentNullException>(() => new ChatRole(role!));

// Assert
Assert.NotNull(exception);
}

[Fact]
public void Test_OperatorEquality()
{
// Arrange
var chatRole1 = new ChatRole("user");
var chatRole2 = ChatRole.User;
var chatRole3 = ChatRole.Assistant;

// Act
var result1 = chatRole1 == chatRole2;
var result2 = chatRole1 == chatRole3;
var result3 = chatRole2 != chatRole3;

// Assert
Assert.True(result1);
Assert.False(result2);
Assert.True(result3);
}

[Fact]
public void Test_Equals()
{
// Arrange
var chatRole1 = new ChatRole("user");
var chatRole2 = ChatRole.User;
var chatRole3 = ChatRole.Assistant;

// Act
var result1 = chatRole1.Equals(chatRole2);
var result2 = chatRole1.Equals(chatRole3);

// Assert
Assert.True(result1);
Assert.False(result2);
}

[Fact]
public void Test_ToString()
{
// Arrange
var chatRole = new ChatRole("test");

// Act
var result = chatRole.ToString();

// Assert
Assert.Equal("test", result.ToString());
}

[Fact]
public void Test_ImplicitConversion()
{
// Arrange
ChatRole chatRole = "user";

// Assert
Assert.Equal("user", chatRole.Value);
}

[Fact]
public void Test_GetHashCode()
{
// Arrange
var chatRole = ChatRole.Function;

// Act
var result = chatRole.GetHashCode();

// Assert
Assert.Equal("function".GetHashCode(), result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using ChatMessage = Microsoft.Teams.AI.AI.Models.ChatMessage;
using ChatRole = Microsoft.Teams.AI.AI.Models.ChatRole;

namespace Microsoft.Teams.AI.Tests.AITests
namespace Microsoft.Teams.AI.Tests.AITests.Models
{
public class OpenAIModelTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Azure;
using Microsoft.Teams.AI.AI.Models;

namespace Microsoft.Teams.AI.Tests.AITests.Models
{
public class SequentialDelayStrategyTests
{
[Fact]
public void Test_SequentialDelayStrategy()
{
// Arrange
var delays = new List<TimeSpan>()
{
TimeSpan.FromMilliseconds(1000),
TimeSpan.FromMilliseconds(2000),
TimeSpan.FromMilliseconds(3000),
};
var strategy = new TestSequentialDelayStrategy(delays);

// Act
var result1 = strategy.GetNextDelayCoreMethod(null, 1);
var result2 = strategy.GetNextDelayCoreMethod(null, 2);
var result3 = strategy.GetNextDelayCoreMethod(null, 3);
var result4 = strategy.GetNextDelayCoreMethod(null, 4);

// Assert
Assert.Equal(TimeSpan.FromMilliseconds(1000), result1);
Assert.Equal(TimeSpan.FromMilliseconds(2000), result2);
Assert.Equal(TimeSpan.FromMilliseconds(3000), result3);
Assert.Equal(TimeSpan.FromMilliseconds(3000), result4);
}
}

internal class TestSequentialDelayStrategy : SequentialDelayStrategy

Check warning on line 34 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/Models/SequentialDelayStrategyTests.cs

View workflow job for this annotation

GitHub Actions / Analyze

Type 'TestSequentialDelayStrategy' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check warning on line 34 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/Models/SequentialDelayStrategyTests.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint / Build/Test/Lint (6.0)

Type 'TestSequentialDelayStrategy' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check warning on line 34 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/Models/SequentialDelayStrategyTests.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint / Build/Test/Lint (7.0)

Type 'TestSequentialDelayStrategy' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check warning on line 34 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/Models/SequentialDelayStrategyTests.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (7.0)

Type 'TestSequentialDelayStrategy' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)

Check warning on line 34 in dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/Models/SequentialDelayStrategyTests.cs

View workflow job for this annotation

GitHub Actions / Build/Test/Lint (6.0)

Type 'TestSequentialDelayStrategy' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
{
public TestSequentialDelayStrategy(List<TimeSpan> delays) : base(delays)
{
}

public TimeSpan GetNextDelayCoreMethod(Response? response, int retryNumber)
{
return base.GetNextDelayCore(response, retryNumber);
}
}
}

This file was deleted.

0 comments on commit e47b5d7

Please sign in to comment.