-
Notifications
You must be signed in to change notification settings - Fork 2
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
Setup unit testing project #164
Open
stanriders
wants to merge
1
commit into
main
Choose a base branch
from
setup-units
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="Moq" Version="4.18.1" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Integration\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CircleForms\CircleForms.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
180 changes: 180 additions & 0 deletions
180
backend/CircleForms.Tests/Unit/Database/Services/RedisCacheRepositoryTests.cs
This file contains 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,180 @@ | ||
| ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using CircleForms.Database.Models.Users; | ||
using CircleForms.Database.Services; | ||
using CircleForms.Database.Services.Extensions; | ||
using CircleForms.Tests.Utils; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using MongoDB.Bson; | ||
using Moq; | ||
using StackExchange.Redis; | ||
using Xunit; | ||
using JsonConvert = Newtonsoft.Json.JsonConvert; | ||
|
||
namespace CircleForms.Tests.Unit.Database.Services | ||
{ | ||
public class RedisCacheRepositoryTests | ||
{ | ||
private readonly RedisCacheRepository _cache; | ||
private readonly Mock<IDatabase> _redisDatabaseMock; | ||
|
||
public RedisCacheRepositoryTests() | ||
{ | ||
_redisDatabaseMock = new Mock<IDatabase>(MockBehavior.Strict); | ||
|
||
var redisMock = new Mock<IConnectionMultiplexer>(); | ||
redisMock.Setup(x => x.GetDatabase(It.IsAny<int>(), It.IsAny<object>())).Returns(_redisDatabaseMock.Object); | ||
|
||
_cache = new RedisCacheRepository(redisMock.Object, MappingHelper.CreateMapper(), | ||
new NullLogger<RedisCacheRepository>()); | ||
} | ||
|
||
[Fact] | ||
public async Task IncrementAnswersUsesCorrectId() | ||
{ | ||
const string id = "123456"; | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.StringIncrementAsync(It.Is<RedisKey>(y => y == id.ToPostAnswersCount()), It.IsAny<long>(), | ||
It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(1); | ||
|
||
await _cache.IncrementAnswers(id); | ||
} | ||
|
||
[Fact] | ||
public async Task PinPostAddsPostToTheSet() | ||
{ | ||
var postId = new Guid().ToString(); | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.KeyExists(It.Is<RedisKey>(y => y == postId.ToPostId()), It.IsAny<CommandFlags>())) | ||
.Returns(true); | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.SetAddAsync(It.Is<RedisKey>(y => y == "posts:pinned"), | ||
It.Is<RedisValue>(y => y == postId.ToPostId()), It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(true); | ||
|
||
var result = await _cache.PinPost(postId); | ||
|
||
Assert.True(result); | ||
_redisDatabaseMock.Verify(); | ||
} | ||
|
||
[Fact] | ||
public async Task PinPostDoesntAddPostToTheSetWhenItDoesntExist() | ||
{ | ||
var postId = new Guid().ToString(); | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.KeyExists(It.Is<RedisKey>(y => y == postId.ToPostId()), It.IsAny<CommandFlags>())) | ||
.Returns(false); | ||
|
||
var result = await _cache.PinPost(postId); | ||
|
||
Assert.False(result); | ||
_redisDatabaseMock.Verify(); | ||
} | ||
|
||
[Fact] | ||
public async Task AddUserAddsCorrectData() | ||
{ | ||
var user = new User | ||
{ | ||
ID = "123456", | ||
Discord = "Test#7270", | ||
Osu = new BsonDocument(new Dictionary<string, object> | ||
{ | ||
{ "Username", "VsevolodVolkov" }, | ||
{ "AvatarUrl", "https://a.ppy.sh/123456" } | ||
}) | ||
}; | ||
|
||
var expectedRedisUser = JsonConvert.SerializeObject(new UserMinimalRedis | ||
{ | ||
ID = user.ID, | ||
Discord = user.Discord, | ||
Username = user.Osu["Username"].ToString(), | ||
AvatarUrl = user.Osu["AvatarUrl"].ToString() | ||
}); | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.SetAddAsync(It.Is<RedisKey>(y => y == "user_ids"), It.Is<RedisValue>(y => y == user.ID), | ||
It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(true); | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.StringSetAsync(It.Is<RedisKey>(y => y == user.ID.ToUserId()), | ||
It.Is<RedisValue>(y => y == expectedRedisUser), | ||
It.IsAny<TimeSpan?>(), It.IsAny<When>(), It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(true); | ||
|
||
await _cache.AddUser(user); | ||
|
||
_redisDatabaseMock.Verify(); | ||
} | ||
|
||
[Fact] | ||
public async Task GetMinimalUserReturnsUserOnCorrectId() | ||
{ | ||
var user = new UserMinimalRedis | ||
{ | ||
ID = "123456", | ||
Username = "VsevolodVolkov", | ||
AvatarUrl = "https://a.ppy.sh/123456", | ||
Discord = "Test#7270" | ||
}; | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.StringGetAsync(It.Is<RedisKey>(y => y == user.ID.ToUserId()), It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(new RedisValue(JsonConvert.SerializeObject(user))); | ||
|
||
var result = await _cache.GetMinimalUser(user.ID); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal(user.ID, result.ID); | ||
Assert.Equal(user.Username, result.Username); | ||
Assert.Equal(user.AvatarUrl, result.AvatarUrl); | ||
Assert.Equal(user.Discord, result.Discord); | ||
} | ||
|
||
[Fact] | ||
public async Task GetMinimalUserReturnsNullOnIncorrectId() | ||
{ | ||
var user = new UserMinimalRedis | ||
{ | ||
ID = "123456", | ||
Username = "VsevolodVolkov", | ||
AvatarUrl = "https://a.ppy.sh/123456", | ||
Discord = "Test#7270" | ||
}; | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.StringGetAsync(It.Is<RedisKey>(y => y == user.ID.ToUserId()), It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(new RedisValue()); | ||
|
||
var result = await _cache.GetMinimalUser(user.ID); | ||
|
||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public async Task RemoveUserUsesCorrectIds() | ||
{ | ||
const string id = "123456"; | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.KeyDeleteAsync(It.Is<RedisKey>(y => y == id.ToUserId()), It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(true); | ||
|
||
_redisDatabaseMock | ||
.Setup(x => x.SetRemoveAsync(It.Is<RedisKey>(y => y == "user_ids"), It.Is<RedisValue>(y => y == id), It.IsAny<CommandFlags>())) | ||
.ReturnsAsync(true); | ||
|
||
await _cache.RemoveUser(id); | ||
} | ||
} | ||
} |
This file contains 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,17 @@ | ||
using FastExpressionCompiler; | ||
using Mapster; | ||
using MapsterMapper; | ||
|
||
namespace CircleForms.Tests.Utils | ||
{ | ||
internal static class MappingHelper | ||
{ | ||
public static IMapper CreateMapper() | ||
{ | ||
TypeAdapterConfig.GlobalSettings.Scan(typeof(Program).Assembly); | ||
TypeAdapterConfig.GlobalSettings.Compiler = x => x.CompileFast(); | ||
|
||
return new Mapper(); | ||
} | ||
} | ||
} |
This file contains 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 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 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,25 @@ | ||
using System; | ||
|
||
namespace CircleForms; | ||
|
||
/// <summary> | ||
/// This exists ONLY for MongoDB.Entities-based models and MUST NOT be used anywhere else. | ||
/// Doing workarounds for unit testing is a very very bad design, but unfortunately we can't avoid it here. | ||
/// </summary> | ||
public static class UnitTestDetector | ||
{ | ||
static UnitTestDetector() | ||
{ | ||
foreach (var assem in AppDomain.CurrentDomain.GetAssemblies()) | ||
{ | ||
if (assem.FullName != null && | ||
assem.FullName.ToLowerInvariant().StartsWith("xunit")) | ||
{ | ||
IsRunningFromXUnit = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public static bool IsRunningFromXUnit { get; } | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need to mix aspects, refactor it