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 25fb5bd commit b3bbc72
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/FileSystem.Adapters.Memory/src/ModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static FileModel CreateFile(MemoryFile file)
Path = file.FullName,
Length = file.Content.Length,
LastModifiedDateTime = DateTime.Now,
Content = file.Content
};
}

Expand Down
1 change: 1 addition & 0 deletions src/FileSystem/src/Models/FileModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Maurosoft.FileSystem.Models
public class FileModel : Model, IFile
{
public long? Length { get; set; }
public byte[] Content { get; set; }

public FileModel()

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Test

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Test

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Test

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/FileSystem/src/Models/FileModel.cs

View workflow job for this annotation

GitHub Actions / Test

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
Expand Down
1 change: 1 addition & 0 deletions src/FileSystem/src/Models/IFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface IFile
public long? Length { get; set; }
public DateTime? LastModifiedDateTime { get; set; }
public DateTime? CreatedDateTime { get; set; }
public byte[] Content { get; set; }
}
}
122 changes: 119 additions & 3 deletions tests/FileSystem.Tests/FileSystem/FileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public void FileSystem_DeleteFile_IfSuccess_Should_ReturnExistsFalse()
fileSystem.Adapters.Add(memoryAdapter);

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

//Assert
Assert.IsFalse(fileSystem.FileExists("memory-1://helloworld.txt"));
Expand All @@ -414,7 +414,7 @@ public void FileSystem_ReadFile_IfExist_Should_ReturnContents()
fileSystem.Adapters.Add(memoryAdapter);

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

//Assert
Assert.AreEqual("HelloWorld", System.Text.Encoding.UTF8.GetString(contents));
Expand All @@ -432,9 +432,125 @@ public void FileSystem_ReadTextFile_IfExist_Should_ReturnContents()
fileSystem.Adapters.Add(memoryAdapter);

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

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_WriteFile_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", System.Text.Encoding.UTF8.GetBytes("HelloWorld"));

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_WriteFile_IfOverwriteFile_Should_ReturnContentOverWrite()
{
//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.WriteFile("memory-1://home/helloworld.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorldOverWrite"), true);
var file = fileSystem.GetFile("memory-1://home/helloworld.txt");

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_CopyFile_IfSuccess_Should_ReturnExistsDestinationFile()
{
//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.CopyFile("memory-1://home/helloworld.txt", "memory-1://home/helloworld-copy.txt");

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_CopyFile_IfOverWriteFile_Should_ReturnContentSourceFile()
{
//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("HelloWorldOverWrite"));
fileSystem.WriteFile("memory-1://home/helloworld-copy.txt", System.Text.Encoding.UTF8.GetBytes("HelloWorldCopy"));

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

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_CopyFile_AcrossAdapter_IfSuccess_Should_ReturnExistsFile()
{
//Arrange
var memoryAdapter1 = new MemoryAdapter("memory-1", "/");
var memoryAdapter2 = new MemoryAdapter("memory-2", "/");

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

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

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

[TestMethod]
[TestCategory("UnitTest")]
public void FileSystem_CopyFile_AcrossAdapter_IfOverWriteSuccess_Should_ReturnContentOverWrite()
{
//Arrange
var memoryAdapter1 = new MemoryAdapter("memory-1", "/");
var memoryAdapter2 = new MemoryAdapter("memory-2", "/");

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

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

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

0 comments on commit b3bbc72

Please sign in to comment.