Skip to content
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

test: Created unit test cases for backend API in DKM. #107

Open
wants to merge 24 commits into
base: PSL-DEV
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4274f78
create a function to validate the required fields
pradeepjha-microsoft Nov 28, 2024
8bc5dbc
Revert the changes
pradeepjha-microsoft Nov 28, 2024
11b7f39
Implemented the required validation
pradeepjha-microsoft Nov 28, 2024
f6e4ce2
Implemented the validation with failure banner.
pradeepjha-microsoft Dec 2, 2024
8d2d66c
Added a common function to validate the required variables
pradeepjha-microsoft Dec 5, 2024
bb89f62
Added the correct variable name
pradeepjha-microsoft Dec 5, 2024
ea42bdd
Merge pull request #21 from microsoft/psl-dkm-us-11110
pradeepjha-microsoft Dec 5, 2024
928971b
remove $fqdn= $null
pradeepjha-microsoft Dec 6, 2024
c0040ed
Merge pull request #24 from microsoft/psl-dkm-us-11110
pradeepjha-microsoft Dec 6, 2024
e0b97ff
Added test project and created on testcases
pradeepjha-microsoft Dec 11, 2024
a964763
Updated the test cases project structure.
pradeepjha-microsoft Dec 11, 2024
4977137
Created test cases for pagingRequestWithSearchValidatorsTests
pradeepjha-microsoft Dec 13, 2024
b2e09e0
Adding 3 entities test cases
Dec 18, 2024
fd359f1
Mongo Entity Collection and Generic Specification i have completed
Dec 20, 2024
8b7be62
All local code
Dec 24, 2024
4b15136
deleting one file
Dec 24, 2024
08da819
Create test cases for chatRequest and ChatRequestValidator
pradeepjha-microsoft Dec 24, 2024
7e6beb9
SearchParameter test cases
Dec 26, 2024
4f61a74
Completed the testcases for Answer and DocumentQuerySet.
pradeepjha-microsoft Dec 26, 2024
bc0d1e0
Merge branch 'psl-dev-dkm-utc-us-11598' of https://github.com/microso…
pradeepjha-microsoft Dec 26, 2024
796c362
All backend test cases Failed test cases i removed and Serach Paramet…
Dec 27, 2024
6fe602a
Added testcases for ChatResponse, ChatResponseAsync and ChatSessionRe…
pradeepjha-microsoft Dec 27, 2024
a14c95d
Unit test cases DocImported and FilethumbnailService
Jan 2, 2025
228cb80
Merge branch 'psl-dev-dkm-utc-us-11598' of https://github.com/microso…
Jan 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using FluentAssertions;
using Microsoft.GS.DPS.API;
using Microsoft.GS.DPS.Model;
using Moq;

namespace Microsoft.GS.DPS.Tests.API.Services
{
public class ChatHostTests
{

}

}

12 changes: 12 additions & 0 deletions App/backend-api/Microsoft.GS.DPS.Tests/Common/MockData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.GS.DPS.Tests.Common
{
public class MockData
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.GS.DPS.Images;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.GS.DPS.Tests.Images
{
public class FileThumbnailServiceTests
{
[Theory]
[InlineData("image/jpeg", "IMG")]
[InlineData("image/png", "IMG")]
[InlineData("application/pdf", "PDF")]
[InlineData("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "XLS")]
[InlineData("application/vnd.ms-excel", "XLS")]
[InlineData("application/vnd.openxmlformats-officedocument.presentationml.presentation", "PPT")]
[InlineData("application/vnd.ms-powerpoint", "PPT")]
[InlineData("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "DOC")]
[InlineData("application/msword", "DOC")]
public void GetThumbnail_ShouldGenerateCorrectThumbnail(string contentType, string expectedText)
{
// Act
byte[] thumbnailBytes = FileThumbnailService.GetThumbnail(contentType);

// Assert
Assert.NotNull(thumbnailBytes);
Assert.True(thumbnailBytes.Length > 0);

// Additional validation: You can save the byte array as an image and verify the content manually if needed.
// For now, the test ensures that the thumbnail is generated.
}

[Fact]
public void GetThumbnail_ShouldReturnEmptyThumbnailForUnknownContentType()
{
// Arrange
string contentType = "unknown/type";

// Act
byte[] thumbnailBytes = FileThumbnailService.GetThumbnail(contentType);

// Assert
Assert.NotNull(thumbnailBytes);
Assert.True(thumbnailBytes.Length > 0);

// Note: Since the logic does not handle unknown types explicitly, it may default to an empty thumbnail.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.GS.DPS\Microsoft.GS.DPS.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Storage\Document\Entities\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Microsoft.GS.DPS.Model.ChatHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.GS.DPS.Tests.ModelsTest.ChatHost
{

public class AnswerTests
{
[Fact]
public void Test_Response_Property_Setter_Getter()
{
// Arrange
var answer = new Answer();
string expectedResponse = "This is a response.";

// Act
answer.Response = expectedResponse;

// Assert
Assert.Equal(expectedResponse, answer.Response);
}

[Fact]
public void Test_Followings_Property_Setter_Getter()
{
// Arrange
var answer = new Answer();
string[] expectedFollowings = new string[] { "Followup1", "Followup2" };

// Act
answer.Followings = expectedFollowings;

// Assert
Assert.Equal(expectedFollowings, answer.Followings);
}

[Fact]
public void Test_Keywords_Property_Setter_Getter()
{
// Arrange
var answer = new Answer();
string[] expectedKeywords = new string[] { "Keyword1", "Keyword2" };

// Act
answer.Keywords = expectedKeywords;

// Assert
Assert.Equal(expectedKeywords, answer.Keywords);
}

[Fact]
public void Test_Followings_Empty_Array()
{
// Arrange
var answer = new Answer();

// Act
answer.Followings = new string[] { };

// Assert
Assert.Empty(answer.Followings);
}

[Fact]
public void Test_Keywords_Empty_Array()
{
// Arrange
var answer = new Answer();

// Act
answer.Keywords = new string[] { };

// Assert
Assert.Empty(answer.Keywords);
}

[Fact]
public void Test_Null_Followings_Property()
{
// Arrange
var answer = new Answer();

// Act
answer.Followings = null;

// Assert
Assert.Null(answer.Followings);
}

[Fact]
public void Test_Null_Keywords_Property()
{
// Arrange
var answer = new Answer();

// Act
answer.Keywords = null;

// Assert
Assert.Null(answer.Keywords);
}

[Fact]
public void Test_Response_IsNullInitially()
{
// Arrange
var answer = new Answer();

// Act
string response = answer.Response;

// Assert
Assert.Null(response);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using FluentValidation.TestHelper;
using Microsoft.GS.DPS.Model.ChatHost;
using Xunit;

namespace Microsoft.GS.DPS.Tests.ModelsTest.ChatHost
{
public class ChatRequestTest
{
private readonly ChatRequestValidator _validator;

public ChatRequestTest()
{
_validator = new ChatRequestValidator();
}

[Fact]
public void Should_Have_Error_When_Question_Is_Null()
{
// Arrange
var request = new ChatRequest { Question = null };

// Act
var result = _validator.TestValidate(request);

// Assert
result.ShouldHaveValidationErrorFor(x => x.Question)
.WithErrorMessage("'Question' must not be empty.");
}

[Fact]
public void Should_Have_Error_When_Question_Is_Empty()
{
// Arrange
var request = new ChatRequest { Question = string.Empty };

// Act
var result = _validator.TestValidate(request);

// Assert
result.ShouldHaveValidationErrorFor(x => x.Question)
.WithErrorMessage("'Question' must not be empty.");
}

[Fact]
public void Should_Not_Have_Error_When_Question_Is_Valid()
{
// Arrange
var request = new ChatRequest { Question = "What is your name?" };

// Act
var result = _validator.TestValidate(request);

// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Question);
}

[Fact]
public void Should_Not_Have_Error_When_ChatSessionId_Is_Valid()
{
// Arrange
var request = new ChatRequest { Question = "What is the time?", ChatSessionId = "session123" };

// Act
var result = _validator.TestValidate(request);

// Assert
result.ShouldNotHaveValidationErrorFor(x => x.ChatSessionId);
}

[Fact]
public void Should_Not_Have_Error_When_DocumentIds_Is_Valid()
{
// Arrange
var request = new ChatRequest
{
Question = "What is the time?",
DocumentIds = new string[5] // Valid length (less than 10)
};

// Act
var result = _validator.TestValidate(request);

// Assert
result.ShouldNotHaveValidationErrorFor(x => x.DocumentIds);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.GS.DPS.Model.ChatHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.GS.DPS.Tests.ModelsTest.ChatHost
{
public class ChatResponseAsyncTests
{
[Fact]
public void ChatResponseAsync_SetAndGetProperties_ShouldWork()
{
// Arrange
var chatResponseAsync = new ChatResponseAsync
{
ChatSessionId = "asyncSession123",
Answer = "This is an asynchronous answer.",
DocumentIds = new[] { "doc3", "doc4" },
SuggestingQuestions = new[] { "What is a chatbot?", "What are its uses?" },
Keywords = new[] { "chatbot", "AI" }
};

// Act & Assert
Assert.Equal("asyncSession123", chatResponseAsync.ChatSessionId);
Assert.Equal("This is an asynchronous answer.", chatResponseAsync.Answer);
Assert.Equal(2, chatResponseAsync.DocumentIds.Length);
Assert.Equal("doc3", chatResponseAsync.DocumentIds[0]);
Assert.Equal("doc4", chatResponseAsync.DocumentIds[1]);
Assert.Equal(2, chatResponseAsync.SuggestingQuestions.Length);
Assert.Equal("What is a chatbot?", chatResponseAsync.SuggestingQuestions[0]);
Assert.Equal("What are its uses?", chatResponseAsync.SuggestingQuestions[1]);
Assert.Equal(2, chatResponseAsync.Keywords.Length);
Assert.Equal("chatbot", chatResponseAsync.Keywords[0]);
Assert.Equal("AI", chatResponseAsync.Keywords[1]);
}
}
}
Loading