Skip to content

Commit

Permalink
refactor integration test for checking the file sha
Browse files Browse the repository at this point in the history
  • Loading branch information
Alireza Eiji committed Aug 6, 2024
1 parent 1d61433 commit e22cc4f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public static IServiceCollection AddGiteaIntegrationTestsServices(this IServiceC
serviceCollection.AddSingleton<ITestFileCreator, TestFileCreator>();
serviceCollection.AddSingleton<ITestPullRequestCreator, TestPullRequestCreator>();
serviceCollection.AddSingleton<ITestRepositoryChecker, TestRepositoryChecker>();
serviceCollection.AddSingleton<ITestFileMetadataGetter, TestFileMetadataGetter>();
serviceCollection.AddSingleton<ITestBranchChecker, TestBranchChecker>();
serviceCollection.AddSingleton<ITestBranchCreator, TestBranchCreator>();
serviceCollection.AddSingleton<ITestCommitChecker, TestCommitChecker>();
serviceCollection.AddSingleton<ITestFileChecker, TestFileChecker>();
serviceCollection.AddSingleton<ITestCommiter, TestCommiter>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Mohaymen.GiteaClient.Gitea.File.CreateFile.Dtos;
using Refit;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;

public interface ITestFileCreator
{
Task CreateFileAsync(CreateFileCommandDto createFileCommandDto, CancellationToken cancellationToken);
Task<ApiResponse<CreateFileResponseDto>> CreateFileAsync(CreateFileCommandDto createFileCommandDto,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;

internal interface ITestFileMetadataGetter
{
Task<GetFileResponseDto> GetFileMetadataAsync(string repositoryName,
string filePath,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Mohaymen.GiteaClient.Gitea.File.Common.Facade.Abstractions;
using Mohaymen.GiteaClient.Gitea.File.CreateFile.Dtos;
using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;
using Refit;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData;

Expand All @@ -13,8 +14,9 @@ public TestFileCreator(IFileFacade fileFacade)
_fileFacade = fileFacade ?? throw new ArgumentNullException(nameof(fileFacade));
}

public async Task CreateFileAsync(CreateFileCommandDto createFileCommandDto, CancellationToken cancellationToken)
public async Task<ApiResponse<CreateFileResponseDto>> CreateFileAsync(CreateFileCommandDto createFileCommandDto,
CancellationToken cancellationToken)
{
await _fileFacade.CreateFileAsync(createFileCommandDto, cancellationToken);
return await _fileFacade.CreateFileAsync(createFileCommandDto, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Mohaymen.GiteaClient.Gitea.File.Common.Facade.Abstractions;
using Mohaymen.GiteaClient.Gitea.File.GetRepositoryFile.Dtos;
using Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData.Abstractions;

namespace Mohaymen.GiteaClient.IntegrationTests.Common.Initializers.TestData;

internal class TestFileMetadataGetter : ITestFileMetadataGetter
{
private readonly IFileFacade _fileFacade;

public TestFileMetadataGetter(IFileFacade fileFacade)
{
_fileFacade = fileFacade ?? throw new ArgumentNullException(nameof(fileFacade));
}

public async Task<GetFileResponseDto> GetFileMetadataAsync(string repositoryName,
string filePath,
CancellationToken cancellationToken)
{
var fileMetadata = await _fileFacade.GetFileAsync(new GetFileMetadataQueryDto
{
RepositoryName = repositoryName,
FilePath = filePath
}, cancellationToken).ConfigureAwait(false);
return fileMetadata.Content!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public class GetFilesMetadataTests
private readonly IGiteaClient _sut;
private readonly ITestRepositoryCreator _repositoryCreator;
private readonly ITestFileCreator _fileCreator;
private readonly ITestFileMetadataGetter _fileMetadataGetter;
private readonly GiteaCollectionFixture _giteaCollectionFixture;

public GetFilesMetadataTests(GiteaCollectionFixture giteaCollectionFixture)
{
_giteaCollectionFixture = giteaCollectionFixture ?? throw new ArgumentNullException(nameof(giteaCollectionFixture));
_repositoryCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestRepositoryCreator>();
_fileCreator = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestFileCreator>();
_fileMetadataGetter = _giteaCollectionFixture.ServiceProvider.GetRequiredService<ITestFileMetadataGetter>();
_sut = _giteaCollectionFixture.ServiceProvider.GetRequiredService<IGiteaClient>();
}

Expand All @@ -35,34 +37,35 @@ public async Task GetFile_ShouldGetFileWithOkStatusCode_WhenInputsAreProvidedPro
const string content = "Hello, World!";
var cancellationToken = _giteaCollectionFixture.CancellationToken;
await _repositoryCreator.CreateRepositoryAsync(repositoryName, cancellationToken);
var readmeFileMetadata = await _fileMetadataGetter.GetFileMetadataAsync(repositoryName, "README.md", cancellationToken);
var createFileCommandDto = new CreateFileCommandDto
{
RepositoryName = repositoryName,
FilePath = filePath,
Content = content
};
await _fileCreator.CreateFileAsync(createFileCommandDto, cancellationToken);
var newFileMetadata = await _fileCreator.CreateFileAsync(createFileCommandDto, cancellationToken);

var getFileCommandDto = new GetFilesMetadataQueryDto
{
RepositoryName = repositoryName,
BranchName = "main"
};
var expectedFiles = new List<GetFileResponseDto>()
var expectedFiles = new List<GetFileResponseDto>
{
new()
{
Content = null,

Check warning on line 58 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 58 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 58 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 58 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.
FileName = "README.md",
FilePath = "README.md",
FileSha = ""
FileSha = readmeFileMetadata.FileSha
},
new()
{
Content = null,

Check warning on line 65 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 65 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 65 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 65 in Mohaymen.GiteaClient.IntegrationTests/Gitea/File/GetFilesMetadata/GetFilesMetadataTests.cs

View workflow job for this annotation

GitHub Actions / build-test-deploy

Cannot convert null literal to non-nullable reference type.
FileName = filePath,
FilePath = filePath,
FileSha = ""
FileSha = newFileMetadata.Content!.Content.FileSha
}
};

Expand All @@ -71,7 +74,6 @@ public async Task GetFile_ShouldGetFileWithOkStatusCode_WhenInputsAreProvidedPro

// Assert
actual.StatusCode.Should().Be(HttpStatusCode.OK);
actual.Content.Should().BeEquivalentTo(expectedFiles, options => options.Excluding(x => x.FileSha));
actual.Content.Should().BeEquivalentTo(expectedFiles);
}

}
3 changes: 3 additions & 0 deletions Mohaymen.GiteaClient/Gitea/File/CreateFile/Models/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public class Content

[JsonProperty("content")]
public required string StringContent { get; init; }

[JsonProperty("sha")]
public required string FileSha { get; init; }
}

0 comments on commit e22cc4f

Please sign in to comment.