Skip to content

Commit

Permalink
Add some tests for Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cardillo committed Sep 2, 2023
1 parent dc1056e commit 93b914a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/FileSystem.Adapters.Sftp/src/SftpAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string p
{
try
{
directories = client.ListDirectory(path).Where(item => item.IsDirectory).Select(ModelFactory.CreateDirectory).ToList();
directories = client.ListDirectory(path).Where(item => item.IsDirectory && item.Name != "." && item.Name != "..").Select(ModelFactory.CreateDirectory).ToList();
}
catch (Exception ex)
{
Expand Down
18 changes: 14 additions & 4 deletions tests/FileSystem.Tests/Base/IntegrationTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public void Init()
[TestCategory("IntegrationTest")]
public override async Task GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => await base.GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException();

[Fact(DisplayName = "GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories")]
[TestMethod]
[TestCategory("IntegrationTest")]
public override async Task GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories() => await base.GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories();

[Fact(DisplayName = "GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException")]
[TestMethod]
[TestCategory("IntegrationTest")]
public override async Task GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => await base.GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException();

[Fact(DisplayName = "CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists")]
[TestMethod]
[TestCategory("IntegrationTest")]
Expand Down Expand Up @@ -95,17 +105,17 @@ public void Init()
[TestCategory("IntegrationTest")]
public override async Task DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException();

[Fact(DisplayName = "ReadFile")]
[Fact(DisplayName = "ReadFile_IfSuccess_Should_ReturnLength")]
[TestCategory("IntegrationTest")]
public override void ReadFile() => base.ReadFile();
public override void ReadFile_IfSuccess_Should_ReturnLength() => base.ReadFile_IfSuccess_Should_ReturnLength();

[Fact(DisplayName = "ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException")]
[TestCategory("IntegrationTest")]
public override void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException() => base.ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException();

[Fact(DisplayName = "ReadFileAsync")]
[Fact(DisplayName = "ReadFileAsync_IfSuccess_Should_ReturnLength")]
[TestCategory("IntegrationTest")]
public override async Task ReadFileAsync() => await base.ReadFileAsync();
public override async Task ReadFileAsync_IfSuccess_Should_ReturnLength() => await base.ReadFileAsync_IfSuccess_Should_ReturnLength();

[Fact(DisplayName = "ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException")]
[TestCategory("IntegrationTest")]
Expand Down
63 changes: 61 additions & 2 deletions tests/FileSystem.Tests/Base/TestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,39 @@ public virtual async Task GetFilesAsync_IfDirectoryNotExist_Should_Throw_Directo
await Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(async () => await _adapter!.GetFilesAsync(directory));
}

public virtual async Task GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);
var directory1 = faker.Database.Random.AlphaNumeric(30);
var directory2 = faker.Database.Random.AlphaNumeric(30);
var directory3 = faker.Database.Random.AlphaNumeric(30);
var directory4 = faker.Database.Random.AlphaNumeric(30);
var directory5 = faker.Database.Random.AlphaNumeric(30);

_adapter!.CreateDirectory(directory);
_adapter!.CreateDirectory(directory + "/" + directory1);
_adapter!.CreateDirectory(directory + "/" + directory2);
_adapter!.CreateDirectory(directory + "/" + directory3);
_adapter!.CreateDirectory(directory + "/" + directory4);
_adapter!.CreateDirectory(directory + "/" + directory5);

//Act
var directories = await _adapter!.GetDirectoriesAsync(directory, new System.Threading.CancellationToken());

//Assert
Assert.AreEqual(5, directories.Count());
}

public virtual async Task GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);

//Assert
await Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(async () => await _adapter!.GetDirectoriesAsync(directory));
}

public virtual async Task CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists()
{
//Arrange
Expand Down Expand Up @@ -219,7 +252,7 @@ public virtual async Task DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFo
await Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(async () => await _adapter!.DeleteDirectoryAsync(directory));
}

public virtual void ReadFile()
public virtual void ReadFile_IfSuccess_Should_ReturnLength()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);
Expand All @@ -245,7 +278,7 @@ public virtual void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException()
Assert.ThrowsException<FileNotFoundException>(() => _adapter!.ReadFile(directory + "/" + fileName));
}

public virtual async Task ReadFileAsync()
public virtual async Task ReadFileAsync_IfSuccess_Should_ReturnLength()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);
Expand All @@ -270,4 +303,30 @@ public virtual async Task ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundE
//Act and Assert
await Assert.ThrowsExceptionAsync<FileNotFoundException>(async () => await _adapter!.ReadFileAsync(directory + "/" + fileName));
}

public virtual void ReadTextFile_IfSuccess_Should_ReturnLength()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);
_adapter!.CreateDirectory(directory);
var fileName = faker.Database.Random.AlphaNumeric(50);
_adapter!.WriteFile(directory + "/" + fileName, "ReadFile");

//Act
var readFile = _adapter!.ReadTextFile(directory + "/" + fileName);

//Act and Assert
Assert.AreEqual("ReadFile", readFile);
}

public virtual void ReadTextFile_IfFileNotExist_Should_ThrowFileNotFoundException()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);
_adapter!.CreateDirectory(directory);
var fileName = faker.Database.Random.AlphaNumeric(50);

//Act and Assert
Assert.ThrowsException<FileNotFoundException>(() => _adapter!.ReadTextFile(directory + "/" + fileName));
}
}
4 changes: 2 additions & 2 deletions tests/FileSystem.Tests/Base/UnitTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ public void Init()

[TestMethod]
[TestCategory("UnitTest")]
public override void ReadFile() => base.ReadFile();
public override void ReadFile_IfSuccess_Should_ReturnLength() => base.ReadFile_IfSuccess_Should_ReturnLength();

[TestMethod]
[TestCategory("UnitTest")]
public override void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException() => base.ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException();

[TestMethod]
[TestCategory("UnitTest")]
public override async Task ReadFileAsync() => await base.ReadFileAsync();
public override async Task ReadFileAsync_IfSuccess_Should_ReturnLength() => await base.ReadFileAsync_IfSuccess_Should_ReturnLength();

[TestMethod]
[TestCategory("UnitTest")]
Expand Down

0 comments on commit 93b914a

Please sign in to comment.