Skip to content

Commit

Permalink
test: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HamedSY committed Jul 25, 2024
1 parent 4de335a commit 5d1b3bd
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using MediatR;
using Mohaymen.GiteaClient.Gitea.File.Common.Facade;
using Mohaymen.GiteaClient.Gitea.File.Common.Facade.Abstractions;
using Mohaymen.GiteaClient.Gitea.File.GetFile.Commands;
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos;
using NSubstitute;
using Xunit;

namespace Mohaymen.GiteaClient.Tests.Gitea.File.Common.Facade;

public class FileFacadeTests
{
private readonly IMediator _mediator;
private readonly IFileFacade _sut;

public FileFacadeTests()
{
_mediator = Substitute.For<IMediator>();
_sut = new FileFacade(_mediator);
}

[Fact]
public async Task GetFileAsync_ShouldCallSend_WhenEver()
{
// Arrange
const string repositoryName = "repo";
const string filePath = "file_path";
const string referenceName = "ref";
var commandDto = new GetFileCommandDto
{
RepositoryName = repositoryName,
FilePath = filePath,
ReferenceName = referenceName,
};

// Act
await _sut.GetFileAsync(commandDto, default);

// Assert
await _mediator.Received(1).Send(Arg.Is<GetFileCommand>(x => x.RepositoryName == repositoryName
&& x.FilePath == filePath
&& x.ReferenceName == referenceName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using FluentAssertions;
using FluentValidation;
using MediatR;
using Microsoft.Extensions.Options;
using Mohaymen.GiteaClient.Core.Configs;
using Mohaymen.GiteaClient.Gitea.File.Common.ApiCall.Abstractions;
using Mohaymen.GiteaClient.Gitea.File.GetFile.Commands;
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Context;
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos;
using NSubstitute;
using Refit;
using Xunit;

namespace Mohaymen.GiteaClient.Tests.Gitea.File.GetFile.Commands;

public class GetFileCommandHandlerTests
{
private readonly IFileRestClient _fileRestClient;
private readonly IOptions<GiteaApiConfiguration> _options;
private readonly InlineValidator<GetFileCommand> _validator;
private readonly IRequestHandler<GetFileCommand, ApiResponse<GetFileResponseDto>> _sut;

public GetFileCommandHandlerTests()
{
_fileRestClient = Substitute.For<IFileRestClient>();
_options = Substitute.For<IOptions<GiteaApiConfiguration>>();
_validator = new InlineValidator<GetFileCommand>();
_sut = new GetFileCommandHandler(_fileRestClient, _options, _validator);
}

[Fact]
public async Task Handle_ShouldThrowsValidationException_WhenInputIsInvalid()
{
// Arrange
_validator.RuleFor(x => x).Must(_ => false);
var command = new GetFileCommand
{
RepositoryName = "repo",
FilePath = "path"
};

// Act
var actual = async () => await _sut.Handle(command, default);

// Assert
await actual.Should().ThrowAsync<ValidationException>();
}

[Fact]
public async Task Handle_ShouldCallGetFileAsync_AndInputsAreValid()
{
// Arrange
_validator.RuleFor(x => x).Must(_ => true);
const string owner = "owner";
const string repositoryName = "repo";
const string filePath = "file_path";
const string referenceName = "ref";
var command = new GetFileCommand
{
RepositoryName = repositoryName,
FilePath = filePath,
ReferenceName = referenceName
};
_options.Value.Returns(new GiteaApiConfiguration
{
BaseUrl = "url",
PersonalAccessToken = "token",
RepositoriesOwner = owner
});

// Act
await _sut.Handle(command, default);

// Assert
await _fileRestClient.Received(1).GetFileAsync(owner,
repositoryName,
filePath,
Arg.Is<GetFileRequest>(x =>
x.ReferenceName == referenceName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using FluentAssertions;
using FluentValidation;
using Mohaymen.GiteaClient.Gitea.File.GetFile.Commands;
using Mohaymen.GiteaClient.Gitea.File.GetFile.Validators;
using Xunit;

namespace Mohaymen.GiteaClient.Tests.Gitea.File.GetFile.Validators;

public class GetFileCommandValidatorTests
{
private readonly IValidator<GetFileCommand> _sut;

public GetFileCommandValidatorTests()
{
_sut = new GetFileCommandValidator();
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void Validate_ShouldReturnEmptyRepositoryNameErrorCode_WhenRepositoryNameIsNullOrEmpty(string repositoryName)
{
// Arrange
var command = new GetFileCommand
{
RepositoryName = repositoryName,
FilePath = "filePath"
};

// Act
var actual = _sut.Validate(command);

// Assert
actual.IsValid.Should().BeFalse();
actual.Errors.Select(x => x.ErrorCode)
.Should().Contain(GetFileErrorCodes.EmptyRepositoryNameErrorCode);
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void Validate_ShouldReturnEmptyFilePathErrorCode_WhenFilePathIsNullOrEmpty(string filePath)
{
// Arrange
var command = new GetFileCommand
{
RepositoryName = "repo",
FilePath = filePath
};

// Act
var actual = _sut.Validate(command);

// Assert
actual.IsValid.Should().BeFalse();
actual.Errors.Select(x => x.ErrorCode)
.Should().Contain(GetFileErrorCodes.EmptyFilePathErrorCode);
}

[Fact]
public void Validate_ShouldReturnValidResult_WhenEveryThingIsProvidedProperly()
{
// Arrange
var command = new GetFileCommand
{
RepositoryName = "repo",
FilePath = "path"
};

// Act
var actual = _sut.Validate(command);

// Assert
actual.IsValid.Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Mohaymen.GiteaClient.Gitea.File.Common.ApiCall.Abstractions;
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos;
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Mappers;
using Mohaymen.GiteaClient.Gitea.Repository.Common.ApiCall.Abstractions;
using Refit;

namespace Mohaymen.GiteaClient.Gitea.File.GetFile.Commands;
Expand Down

0 comments on commit 5d1b3bd

Please sign in to comment.