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 b3bbc72 commit 4ba7b95
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/FileSystem.Adapters.Memory/src/MemoryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,12 @@ public override async Task WriteFileAsync(string path, byte[] contents, bool ove

public override async Task AppendFileAsync(string path, byte[] contents, CancellationToken cancellationToken = default)
{
await GetFileAsync(path, cancellationToken);

var memoryFile = new MemoryFile();
var file = await GetFileAsync(path, cancellationToken);

await Task.Run(() => _files.TryGetValue(path, out var memoryFile));
var content = file.Content ?? Array.Empty<byte>();
file.Content = content.Concat(contents).ToArray();

memoryFile.Content = memoryFile.Content.Concat(contents).ToArray();
await Task.Run(() => _files[PrependRootPath(path)].Content = file.Content);
}
}
}
93 changes: 93 additions & 0 deletions tests/FileSystem.Tests/FileSystem/FileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,23 @@ public void FileSystem_WriteFile_IfOverwriteFile_Should_ReturnContentOverWrite()
Assert.AreEqual("HelloWorldOverWrite", System.Text.Encoding.UTF8.GetString(file.Content));
}

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

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

//Act
fileSystem.WriteFile("memory-1://home/helloworld.txt", "HelloWorld");

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_CopyFile_IfSuccess_Should_ReturnExistsDestinationFile()
Expand Down Expand Up @@ -553,4 +570,80 @@ public void FileSystem_CopyFile_AcrossAdapter_IfOverWriteSuccess_Should_ReturnCo
//Assert
Assert.AreEqual("HelloWorldNew", System.Text.Encoding.UTF8.GetString(file.Content));
}

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

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);
fileSystem.WriteFile("memory-1://home/helloworld.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorld"));

//Act
fileSystem.AppendFile("memory-1://home/helloworld.txt", System.Text.Encoding.UTF8.GetBytes("AppendFile"));
var file = fileSystem.GetFile("memory-1://home/helloworld.txt");

//Assert
Assert.AreEqual("HelloWorldAppendFile", System.Text.Encoding.UTF8.GetString(file.Content));
}

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

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);
fileSystem.WriteFile("memory-1://home/helloworld.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorld"));

//Act
fileSystem.AppendFile("memory-1://home/helloworld.txt", "AppendFile");
var file = fileSystem.GetFile("memory-1://home/helloworld.txt");

//Assert
Assert.AreEqual("HelloWorldAppendFile", System.Text.Encoding.UTF8.GetString(file.Content));
}

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

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);
fileSystem.WriteFile("memory-1://home/helloworld.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorld"));

//Act
fileSystem.MoveFile("memory-1://home/helloworld.txt", "memory-1://home1/helloworld.txt");

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

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

var fileSystem = new Maurosoft.FileSystem.FileSystem();
fileSystem.Adapters.Add(memoryAdapter);
fileSystem.WriteFile("memory-1://home1/helloworld.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorld1"));
fileSystem.WriteFile("memory-1://home2/helloworld.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorld2"));

//Act
fileSystem.MoveFile("memory-1://home1/helloworld.txt", "memory-1://home2/helloworld.txt", true);
var file = fileSystem.GetFile("memory-1://home2/helloworld.txt");

//Assert
Assert.AreEqual("HelloWorld1", System.Text.Encoding.UTF8.GetString(file.Content));
}
}

0 comments on commit 4ba7b95

Please sign in to comment.