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 242891c commit 25fb5bd
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/FileSystem.Adapters.Memory/src/MemoryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MemoryAdapter : Adapter

public MemoryAdapter(string prefix, string rootPath) : base(prefix, rootPath)
{
_directories.Add(System.IO.Path.Combine(rootPath), new MemoryDirectory() { FullName = rootPath, Name = GetLastPathPart(rootPath) });
_directories.Add(System.IO.Path.Combine(rootPath), new MemoryDirectory() { FullName = rootPath, Name = GetLastPathPart(rootPath), Root = "" });
}

public override void Dispose()
Expand Down Expand Up @@ -111,7 +111,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string p

try
{
return await Task.Run(() => _directories.Where(p => p.Key.StartsWith(path)).Select(p => ModelFactory.CreateDirectory(p.Value)).AsEnumerable(), cancellationToken);
return await Task.Run(() => _directories.Where(p => p.Value.Root == path).Select(p => ModelFactory.CreateDirectory(p.Value)).AsEnumerable(), cancellationToken);
}
catch (Exception exception)
{
Expand All @@ -126,7 +126,7 @@ public override async Task CreateDirectoryAsync(string path, CancellationToken c

try
{
await Task.Run(() => _directories.Add(PrependRootPath(path), new MemoryDirectory() { Name = System.IO.Path.GetDirectoryName(PrependRootPath(path)), FullName = PrependRootPath(path) }), cancellationToken);
await Task.Run(() => _directories.Add(PrependRootPath(path), new MemoryDirectory() { Name = System.IO.Path.GetFileName(path), Root = System.IO.Path.GetDirectoryName(PrependRootPath(path)).Replace("\\", "/"), FullName = PrependRootPath(path) }), cancellationToken);
}
catch (Exception exception)
{
Expand Down
1 change: 1 addition & 0 deletions src/FileSystem.Adapters.Memory/src/MemoryDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class MemoryDirectory
{
public string Name { get; set; } = String.Empty;
public string FullName { get; set; } = String.Empty;
public string Root { get; set; } = String.Empty;
}
}
218 changes: 218 additions & 0 deletions tests/FileSystem.Tests/FileSystem/FileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,222 @@ public void FileSystem_GetFiles_IfFilesExists_Should_ReturnFiles()
//Assert
Assert.AreEqual(2, files.Count);
}

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_GetFiles_IfPathNotExists_Should_ThrowException_DirectoryNotFoundException()
{
//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 & Assert
var aggregateException = Assert.ThrowsException<AggregateException>(() => fileSystem.GetFiles("memory-1://home1"));
Assert.AreEqual(aggregateException.InnerException.GetType(), typeof(DirectoryNotFoundException));
}

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_GetDirectories_IfPathExists_Should_ReturnDirectories()
{
//Arrange
var memoryAdapter = new MemoryAdapter("memory-1", "/");
memoryAdapter.CreateDirectory("home");
memoryAdapter.CreateDirectory("home/home1");
memoryAdapter.CreateDirectory("home/home2");
memoryAdapter.CreateDirectory("home/home3");

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

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

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_GetDirectories_IfPathNotExists_Should_ThrowException_DirectoryNotFoundException()
{
//Arrange
var memoryAdapter = new MemoryAdapter("memory-1", "/");
memoryAdapter.CreateDirectory("home");
memoryAdapter.CreateDirectory("home/home1");
memoryAdapter.CreateDirectory("home/home2");
memoryAdapter.CreateDirectory("home/home3");

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

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

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

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

//Act
var fileExists = fileSystem.FileExists("memory-1://home/helloworld1.txt");

//Assert
Assert.IsTrue(fileExists);
}

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

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

//Act
var fileExists = fileSystem.FileExists("memory-1://home/helloworld2.txt");

//Assert
Assert.IsFalse(fileExists);
}

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

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

//Act
var dirExists = fileSystem.DirectoryExists("memory-1://home");

//Assert
Assert.IsTrue(dirExists);
}

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

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

//Act
var dirExists = fileSystem.DirectoryExists("memory-1://home1");

//Assert
Assert.IsFalse(dirExists);
}

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

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

//Act
fileSystem.CreateDirectory("memory-1://home");

//Assert
Assert.IsTrue(fileSystem.DirectoryExists("memory-1://home"));
}

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

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);
fileSystem.CreateDirectory("memory-1://home");

//Act
fileSystem.DeleteDirectory("memory-1://home");

//Assert
Assert.IsFalse(fileSystem.DirectoryExists("memory-1://home"));
}

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

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

//Act
fileSystem.DeleteFile("memory-1:///home/helloworld.txt");

//Assert
Assert.IsFalse(fileSystem.FileExists("memory-1://helloworld.txt"));
}

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

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

//Act
var contents = fileSystem.ReadFile("memory-1:///home/helloworld.txt");

//Assert
Assert.AreEqual("HelloWorld", System.Text.Encoding.UTF8.GetString(contents));
}

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

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

//Act
var contents = fileSystem.ReadTextFile("memory-1:///home/helloworld.txt");

//Assert
Assert.AreEqual("HelloWorld", contents);
}
}

0 comments on commit 25fb5bd

Please sign in to comment.