file-storage/FileStorageService.www.Test/Repositories/FileRepositoryTests.cs

86 lines
2.0 KiB
C#

using FileStorageService.www.Data;
using FileStorageService.www.Repositories;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
namespace FileStorageService.www.Test.Repositories;
public class FileRepositoryTests
{
private readonly Logger<FileRepository> _logger;
private readonly ApplicationDbContext _context;
public FileRepositoryTests()
{
var mock = new Mock<Logger<FileRepository>>();
mock.SetupAllProperties();
_logger = mock.Object;
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();
var contextOptions = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlite(connection)
.Options;
_context = new ApplicationDbContext(contextOptions);
_context.Database.EnsureDeleted();
_context.Database.EnsureCreated();
}
private FileRepository repository => new(_logger, _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);
}
}