diff --git a/FileStorageService.sln b/FileStorageService.sln index bd453d2..bf71baf 100644 --- a/FileStorageService.sln +++ b/FileStorageService.sln @@ -2,6 +2,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileStorageService.www", "FileStorageService.www\FileStorageService.www.csproj", "{BC0E09F5-B6B3-469D-B4D3-C343EF04CAAF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileStorageService.www.Test", "FileStorageService.www.Test\FileStorageService.www.Test.csproj", "{1B6E48A3-AF16-4541-A7AA-AA13862A10A8}" + ProjectSection(ProjectDependencies) = postProject + {BC0E09F5-B6B3-469D-B4D3-C343EF04CAAF} = {BC0E09F5-B6B3-469D-B4D3-C343EF04CAAF} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +17,9 @@ Global {BC0E09F5-B6B3-469D-B4D3-C343EF04CAAF}.Debug|Any CPU.Build.0 = Debug|Any CPU {BC0E09F5-B6B3-469D-B4D3-C343EF04CAAF}.Release|Any CPU.ActiveCfg = Release|Any CPU {BC0E09F5-B6B3-469D-B4D3-C343EF04CAAF}.Release|Any CPU.Build.0 = Release|Any CPU + {1B6E48A3-AF16-4541-A7AA-AA13862A10A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1B6E48A3-AF16-4541-A7AA-AA13862A10A8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1B6E48A3-AF16-4541-A7AA-AA13862A10A8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1B6E48A3-AF16-4541-A7AA-AA13862A10A8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/FileStorageService.www.Test/FileStorageService.www.Test.csproj b/FileStorageService.www.Test/FileStorageService.www.Test.csproj new file mode 100644 index 0000000..eeec78a --- /dev/null +++ b/FileStorageService.www.Test/FileStorageService.www.Test.csproj @@ -0,0 +1,26 @@ + + + + net9.0 + enable + enable + false + + + + + + + + + + + + + + + + + + + diff --git a/FileStorageService.www.Test/Repositories/FileRepositoryTests.cs b/FileStorageService.www.Test/Repositories/FileRepositoryTests.cs new file mode 100644 index 0000000..5e329e8 --- /dev/null +++ b/FileStorageService.www.Test/Repositories/FileRepositoryTests.cs @@ -0,0 +1,78 @@ +using FileStorageService.www.Data; +using FileStorageService.www.Repositories; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace FileStorageService.www.Test.Repositories; + +public class FileRepositoryTests +{ + private readonly ApplicationDbContext _context; + + public FileRepositoryTests() + { + var connection = new SqliteConnection("Filename=:memory:"); + connection.Open(); + + var contextOptions = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + _context = new ApplicationDbContext(contextOptions); + + _context.Database.EnsureDeleted(); + _context.Database.EnsureCreated(); + } + + private FileRepository repository => new FileRepository(_context); + + + [Theory] + [InlineData("one", 1)] + [InlineData("two", 2)] + [InlineData("four", 4)] + [InlineData("eight", 8)] + public async Task TestCanCreateNewFile(string name, int blockCount) + { + var data = new byte[blockCount * 1024]; + + var stream = new MemoryStream(data); + + var id = await repository.TryNewFileAsync(name, stream); + + var handle = await _context.FileHandles.Include(f => f.FileBlocks).FirstAsync(); + + Assert.Equal(name, handle.Name); + Assert.Equal(blockCount, handle.FileBlocks.Count); + Assert.Equal(blockCount, handle.FileBlockCount); + } + + [Fact] + public async Task TestFetchingFiles() + { + var name = "FileName"; + + var handle = new FileHandle + { + Name = name, + FileBlockCount = 1 + }; + _context.Add(handle); + + var block = new FileBlock + { + BlockNumber = 0, + Data = new byte[1024], + FileHandle = handle + }; + _context.Add(block); + + await _context.SaveChangesAsync(); + + var fetchedHandle = await repository.GetFileAsync(handle.Id); + + Assert.Equal(name, fetchedHandle.Name); + Assert.Equal(1, fetchedHandle.FileBlockCount); + Assert.Single(fetchedHandle.FileBlocks); + } +} \ No newline at end of file