Skip to content

Commit

Permalink
Add some test for class FileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cardillo committed Sep 1, 2023
1 parent f8e9aaf commit 242891c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/FileSystem.Adapters.Memory/src/MemoryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ public override async Task WriteFileAsync(string path, byte[] contents, bool ove
Name = System.IO.Path.GetFileName(path),
};

if (!_directories.Keys.Contains(PrependRootPath(System.IO.Path.GetDirectoryName(path)).Replace(@"\", "/")))
_directories.Add(PrependRootPath(System.IO.Path.GetDirectoryName(path)).Replace(@"\", "/"), new MemoryDirectory() { FullName = PrependRootPath(System.IO.Path.GetDirectoryName(path)).Replace(@"\", "/"), Name = System.IO.Path.GetDirectoryName(path) });

memoryFile.Content = contents;

if (!found)
Expand Down
80 changes: 75 additions & 5 deletions tests/FileSystem.Tests/FileSystem/FileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Maurosoft.FileSystem.Adapters.Memory;
using System.Text;
using System;
using System.Linq;

namespace Tests.FileSystem;

Expand Down Expand Up @@ -88,9 +89,7 @@ public void FileSystem_GetAdapter_RegistredWithSamePrefix_Should_ThrowException_
public void FileSystem_GetAdapter_WithPrefixNotRegistred_Should_ThrowException_AdapterNotFoundException()
{
//Arrange
#pragma warning disable S1481 // Unused local variables should be removed
var localAdapter1 = new LocalAdapter("prefix-1", "/");
#pragma warning restore S1481 // Unused local variables should be removed
var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(localAdapter1);

Expand Down Expand Up @@ -134,9 +133,9 @@ public void FileSystem_GetFile_IfFileNotExists_Should_ThrowException_FileNotFoun

[TestMethod]
[TestCategory("UnitTest")]
[DataRow("memory-1:/helloworld1.txt", DisplayName = "")]
[DataRow("memory-1:helloworld1.txt", DisplayName = "")]
[DataRow("memory-1helloworld1.txt", DisplayName = "")]
[DataRow("memory-1:/helloworld.txt", DisplayName = "")]
[DataRow("memory-1:helloworld.txt", DisplayName = "")]
[DataRow("memory-1helloworld.txt", DisplayName = "")]
public void FileSystem_GetFile_IfPrefixIsInvalid_Should_ThrowException_PrefixNotFoundInPathException(string prefix)
{
//Arrange
Expand All @@ -149,4 +148,75 @@ public void FileSystem_GetFile_IfPrefixIsInvalid_Should_ThrowException_PrefixNot
//Act & Assert
Assert.ThrowsException<PrefixNotFoundInPathException>(() => fileSystem.GetFile(prefix));
}

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_GetDirectory_IfDirectoryExists_Should_ReturnDirectory()
{
//Arrange
var memoryAdapter = new MemoryAdapter("memory-1", "/");
memoryAdapter.CreateDirectory("helloworld");

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);

//Act
var directory = fileSystem.GetDirectory("memory-1://helloworld");

//Assert
Assert.AreEqual("/helloworld", directory.Path);
}

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_GetDirectory_IfDirectoryNotExists_Should_ThrowException_DirectoryNotFoundException()
{
//Arrange
var memoryAdapter = new MemoryAdapter("memory-1", "/");
memoryAdapter.CreateDirectory("helloworld");

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);

//Act & Assert
var aggregateException = Assert.ThrowsException<AggregateException>(() => fileSystem.GetDirectory("memory-1://helloworld1"));
Assert.AreEqual(aggregateException.InnerException.GetType(), typeof(DirectoryNotFoundException));
}

[TestMethod]
[TestCategory("UnitTest")]
[DataRow("memory-1:/helloworld", DisplayName = "")]
[DataRow("memory-1:helloworld", DisplayName = "")]
[DataRow("memory-1helloworld", DisplayName = "")]
public void FileSystem_GetDirectory_IfPrefixIsInvalid_Should_ThrowException_PrefixNotFoundInPathException(string prefix)
{
//Arrange
var memoryAdapter = new MemoryAdapter("memory-1", "/");
memoryAdapter.CreateDirectory("helloworld");

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);

//Act & Assert
Assert.ThrowsException<PrefixNotFoundInPathException>(() => fileSystem.GetDirectory(prefix));
}

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_GetFiles_IfFilesExists_Should_ReturnFiles()
{
//Arrange
var memoryAdapter = new MemoryAdapter("memory-1", "/");
memoryAdapter.WriteFile("/home/helloworld1.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorld"));
memoryAdapter.WriteFile("/home/helloworld2.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorld"));

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);

//Act
var files = fileSystem.GetFiles("memory-1://home").ToList();

//Assert
Assert.AreEqual(2, files.Count);
}
}

0 comments on commit 242891c

Please sign in to comment.