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

[PR-14] Unit tests #14

Merged
merged 6 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:

- name: Tests
run: |
dotnet test --no-build --no-restore -c Release tests/ChatService.Core.Tests/ChatService.Core.Tests.csproj
dotnet test --no-build --no-restore -c Release tests/UnitTests/ChatService.Core.Tests/ChatService.Core.Tests.csproj
14 changes: 7 additions & 7 deletions SimplyChat.ChatService.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatService.Core", "src\Cor
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatService.Infrastructure", "src\Infrastructure\ChatService.Infrastructure.csproj", "{98A73A9A-FA41-4845-8CFB-F60646F8E106}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatService.Core.Tests", "tests\ChatService.Core.Tests\ChatService.Core.Tests.csproj", "{10B272DC-DD05-4364-B417-4FC3C0CD0F9D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnitTests", "UnitTests", "{6F832C49-B5F7-4369-8FAA-A2CA4E16A46E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmarks", "Benchmarks", "{54010D84-03E9-4AC6-8E80-BA05FD5819B8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatService.Core.Tests", "tests\UnitTests\ChatService.Core.Tests\ChatService.Core.Tests.csproj", "{FA19F1E9-4D4B-4BF4-A778-324A0853AD51}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,10 +63,10 @@ Global
{98A73A9A-FA41-4845-8CFB-F60646F8E106}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98A73A9A-FA41-4845-8CFB-F60646F8E106}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98A73A9A-FA41-4845-8CFB-F60646F8E106}.Release|Any CPU.Build.0 = Release|Any CPU
{10B272DC-DD05-4364-B417-4FC3C0CD0F9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10B272DC-DD05-4364-B417-4FC3C0CD0F9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10B272DC-DD05-4364-B417-4FC3C0CD0F9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10B272DC-DD05-4364-B417-4FC3C0CD0F9D}.Release|Any CPU.Build.0 = Release|Any CPU
{FA19F1E9-4D4B-4BF4-A778-324A0853AD51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA19F1E9-4D4B-4BF4-A778-324A0853AD51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA19F1E9-4D4B-4BF4-A778-324A0853AD51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA19F1E9-4D4B-4BF4-A778-324A0853AD51}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,8 +80,8 @@ Global
{D803BB96-9241-4111-8A0F-040F0E63601E} = {E27AA7E3-CCA1-4968-925A-C9ADAC389FE8}
{42953750-76B6-4208-B2E7-DEAAC376A020} = {FAB9262E-3185-46E0-85FC-252CAB9ED201}
{98A73A9A-FA41-4845-8CFB-F60646F8E106} = {FAB9262E-3185-46E0-85FC-252CAB9ED201}
{10B272DC-DD05-4364-B417-4FC3C0CD0F9D} = {6F832C49-B5F7-4369-8FAA-A2CA4E16A46E}
{6F832C49-B5F7-4369-8FAA-A2CA4E16A46E} = {23467FC8-9C82-4387-8118-B04D09E731A9}
{54010D84-03E9-4AC6-8E80-BA05FD5819B8} = {23467FC8-9C82-4387-8118-B04D09E731A9}
{FA19F1E9-4D4B-4BF4-A778-324A0853AD51} = {6F832C49-B5F7-4369-8FAA-A2CA4E16A46E}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion src/Core/ChatRooms/Models/ChatRoomUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private ChatRoomUser(string id, string username, ChatRoomUserSettings settings,

private static string GenerateId() => Guid.NewGuid().ToString();

public static ChatRoomUser Create(string username, bool isSuperUser = false) => new(GenerateId(), username, ChatRoomUserSettings.Create(ChatRoomColorSchemes.Light), false);
public static ChatRoomUser Create(string username, bool isSuperUser = false) => new(GenerateId(), username, ChatRoomUserSettings.Create(ChatRoomColorSchemes.Light), isSuperUser);

public static ChatRoomUser CreateSuperUser(string username) => Create(username, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using ChatService.Core.ChatMessages.Models;
using ChatService.Core.Messages;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ChatService.Core.Tests.ChatMessages;

public class DummyChatMessage : ChatMessageBase
{
public DummyChatMessage(string chatRoomId, string userId, string content, DateTimeOffset createdAt, ChatMessageTypes type)
: base(chatRoomId, userId, content, createdAt, type)
{
}
}

[TestClass]
public class ChatMessageBaseTests
{
[TestMethod]
public void Constructor_WithValidParameters_ShouldAssignProperties()
{
var chatRoomId = "room1";
var userId = "user1";
var content = "Hello";
DateTimeOffset createdAt = DateTimeOffset.UtcNow;
var type = ChatMessageTypes.Text;

var dummy = new DummyChatMessage(chatRoomId, userId, content, createdAt, type);

_ = dummy.ChatroomId.Should().Be(chatRoomId);
_ = dummy.UserId.Should().Be(userId);
_ = dummy.Content.Should().Be(content);
_ = dummy.CreatedAt.Should().Be(createdAt);
_ = dummy.Type.Should().Be(type);
}

[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void Constructor_WithInvalidChatRoomId_ShouldThrowArgumentException(string invalidChatRoomId)
{
var userId = "user1";
var content = "Hello";
DateTimeOffset createdAt = DateTimeOffset.UtcNow;
var type = ChatMessageTypes.Text;

Action act = () => new DummyChatMessage(invalidChatRoomId, userId, content, createdAt, type);

//
_ = act.Should().Throw<ArgumentException>();
}

[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void Constructor_WithInvalidUserId_ShouldThrowArgumentException(string invalidUserId)
{
var chatRoomId = "room1";
var content = "Hello";
DateTimeOffset createdAt = DateTimeOffset.UtcNow;
var type = ChatMessageTypes.Text;

Action act = () => new DummyChatMessage(chatRoomId, invalidUserId, content, createdAt, type);

_ = act.Should().Throw<ArgumentException>();
}

[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void Constructor_WithInvalidContent_ShouldThrowArgumentException(string invalidContent)
{
var chatRoomId = "room1";
var userId = "user1";
DateTimeOffset createdAt = DateTimeOffset.UtcNow;
var type = ChatMessageTypes.Text;

Action act = () => new DummyChatMessage(chatRoomId, userId, invalidContent, createdAt, type);

_ = act.Should().Throw<ArgumentException>();
}

[TestMethod]
public void Constructor_WithDefaultCreatedAt_ShouldThrowArgumentException()
{
var chatRoomId = "room1";
var userId = "user1";
var content = "Hello";
DateTimeOffset createdAt = default;
var type = ChatMessageTypes.Text;

Action act = () => new DummyChatMessage(chatRoomId, userId, content, createdAt, type);

_ = act.Should().Throw<ArgumentException>()
.WithMessage("createdAt is required.");
}
}
Loading