Skip to content

Commit

Permalink
ci(test): improvement test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrhak committed May 30, 2024
1 parent 025dccd commit defb8a4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
4 changes: 2 additions & 2 deletions S3FileProvider/S3FileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ protected virtual void Dispose(bool disposing)
/// <returns>An instance of the IDirectoryContents interface representing the directory contents.</returns>
public IDirectoryContents GetDirectoryContents(string subpath)
{
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath.TrimStart(S3Constant.PATH_SEPARATORS);
subpath = subpath.TrimStart(S3Constant.PATH_SEPARATORS);
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath;
return new S3DirectoryContents(amazonS3, bucketName, subpath);
}

Expand All @@ -72,8 +72,8 @@ public IDirectoryContents GetDirectoryContents(string subpath)
public IFileInfo GetFileInfo(string subpath)
{
if (HasInvalidFileNameChars(subpath)) throw new ArgumentException("Invalid file name.", nameof(subpath));
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath.TrimStart(S3Constant.PATH_SEPARATORS);
subpath = subpath.TrimStart(S3Constant.PATH_SEPARATORS);
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath;
if (string.IsNullOrEmpty(subpath)) throw new ArgumentException("Empty file name.", nameof(subpath));
return new S3FileInfo(amazonS3, bucketName, subpath);
}
Expand Down
4 changes: 2 additions & 2 deletions S3FileProvider/S3OwinFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ protected virtual void Dispose(bool disposing)
/// <param name="contents">The directory contents.</param>
public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> contents)
{
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath.TrimStart(S3Constant.PATH_SEPARATORS);
subpath = subpath.TrimStart(S3Constant.PATH_SEPARATORS);
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath;
var s3DirectoryContents = new S3OwinDirectoryContents(amazonS3, bucketName, subpath);
contents = s3DirectoryContents.GetEnumerable();
return true;
Expand All @@ -73,8 +73,8 @@ public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> c
public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
{
if (HasInvalidFileNameChars(subpath)) throw new ArgumentException("Invalid file name.", nameof(subpath));
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath.TrimStart(S3Constant.PATH_SEPARATORS);
subpath = subpath.TrimStart(S3Constant.PATH_SEPARATORS);
if (!string.IsNullOrEmpty(rootPath)) subpath = rootPath!.TrimStart(S3Constant.PATH_SEPARATORS) + "/" + subpath;
if (string.IsNullOrEmpty(subpath)) throw new ArgumentException("Empty file name.", nameof(subpath));
fileInfo = new S3OwinFileInfo(amazonS3, bucketName, subpath);
return true;
Expand Down
40 changes: 35 additions & 5 deletions Test/S3FileProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,37 @@ public void T003_GetFileInfo()
}

[Fact]
public void T004_GetFileInfo_NotFound()
public void T004_GetFileInfo_RootPath()
{
// Arrange
const string rootPath = "/";
const string key = "hello-world.txt";
const string expectedContent = "Hello, World!";
// Mock IAmazonS3 client
var mockS3Client = new Mock<IAmazonS3>();
mockS3Client
.Setup(client => client.GetObjectAsync(It.IsAny<string>(), It.IsAny<string>(), default))
.ReturnsAsync(new GetObjectResponse
{
BucketName = bucketName,
HttpStatusCode = HttpStatusCode.OK,
Key = key,
ResponseStream = new MemoryStream(Encoding.UTF8.GetBytes(expectedContent))
});

// Act
var s3FileProvider = new S3FileProvider(mockS3Client.Object, bucketName, rootPath);
var fileInfo = s3FileProvider.GetFileInfo(key);
using var textReader = new StreamReader(fileInfo.CreateReadStream());

// Assert
Assert.True(fileInfo.Exists);
Assert.Equal(key, fileInfo.Name);
Assert.Equal(expectedContent, textReader.ReadToEnd());
}

[Fact]
public void T005_GetFileInfo_NotFound()
{
// Arrange
const string key = "hello-world.txt";
Expand All @@ -160,7 +190,7 @@ public void T004_GetFileInfo_NotFound()
}

[Fact]
public void T005_GetFileInfo_EmptyKey()
public void T006_GetFileInfo_EmptyKey()
{
// Arrange
// Mock IAmazonS3 client
Expand All @@ -174,7 +204,7 @@ public void T005_GetFileInfo_EmptyKey()
}

[Fact]
public void T006_GetFileInfo_InvalidKey()
public void T007_GetFileInfo_InvalidKey()
{
// Arrange
const string invalidKey = "invalid-#.txt";
Expand All @@ -189,7 +219,7 @@ public void T006_GetFileInfo_InvalidKey()
}

[Fact]
public void T007_Watch()
public void T008_Watch()
{
// Arrange
const string key = "hello-world.txt";
Expand All @@ -214,7 +244,7 @@ public void T007_Watch()
}

[Fact]
public void T008_Dispose()
public void T009_Dispose()
{
// Arrange
// Mock IAmazonS3 client
Expand Down
44 changes: 40 additions & 4 deletions Test/S3OwinFileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,43 @@ public void T003_TryGetFileInfo()
}

[Fact]
public void T004_TryGetFileInfo_NotFound()
public void T004_TryGetFileInfo_RootPath()
{
// Arrange
const string rootPath = "/";
const string key = "hello-world.txt";
const string expectedContent = "Hello, World!";
// Mock IAmazonS3 client
var mockS3Client = new Mock<IAmazonS3>();
mockS3Client
.Setup(client => client.GetObjectAsync(It.IsAny<string>(), It.IsAny<string>(), default))
.ReturnsAsync(new GetObjectResponse
{
BucketName = bucketName,
HttpStatusCode = HttpStatusCode.OK,
Key = key,
ResponseStream = new MemoryStream(Encoding.UTF8.GetBytes(expectedContent))
});

// Act and Assert
var s3OwinFileSystem = new S3OwinFileSystem(mockS3Client.Object, bucketName, rootPath);

try
{
var resFileInfo = s3OwinFileSystem.TryGetFileInfo(key, out var fileInfo);
using var textReader = new StreamReader(fileInfo.CreateReadStream());
Assert.True(resFileInfo);
Assert.Equal(key, fileInfo.Name);
Assert.Equal(expectedContent, textReader.ReadToEnd());
}
catch (ArgumentException ex)
{
Assert.NotNull(ex);
}
}

[Fact]
public void T005_TryGetFileInfo_NotFound()
{
// Arrange
const string key = "hello-world.txt";
Expand Down Expand Up @@ -172,7 +208,7 @@ public void T004_TryGetFileInfo_NotFound()
}

[Fact]
public void T005_TryGetFileInfo_EmptyKey()
public void T006_TryGetFileInfo_EmptyKey()
{
// Arrange
// Mock IAmazonS3 client
Expand All @@ -186,7 +222,7 @@ public void T005_TryGetFileInfo_EmptyKey()
}

[Fact]
public void T006_TryGetFileInfo_InvalidKey()
public void T007_TryGetFileInfo_InvalidKey()
{
// Arrange
const string invalidKey = "invalid-#.txt";
Expand All @@ -201,7 +237,7 @@ public void T006_TryGetFileInfo_InvalidKey()
}

[Fact]
public void T007_Dispose()
public void T008_Dispose()
{
// Arrange
// Mock IAmazonS3 client
Expand Down

0 comments on commit defb8a4

Please sign in to comment.