Added unit test For FileRepository.cs
This commit is contained in:
parent
c36b6a06fe
commit
dade1ddb62
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../FileStorageService.www/FileStorageService.www.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
|
||||
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
|
||||
<PackageReference Include="xunit" Version="2.9.2"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -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<ApplicationDbContext>()
|
||||
.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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue