Skip to content

Commit

Permalink
refactor(test): cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
codingawayy committed Jan 6, 2024
1 parent 91f6191 commit 11b5df9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 45 deletions.
21 changes: 6 additions & 15 deletions Filer.EntityFrameworkCore.Tests/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,26 @@ public static async Task<TException> ThrowsAsync<TException>(
}
catch (Exception ex)
{
if (allowDerivedTypes && !(ex is TException))
if (allowDerivedTypes && ex is not TException)
{
throw new Exception(
"Delegate threw exception of type " +
ex.GetType().Name + ", but " + typeof(TException).Name +
" or a derived type was expected.",
$"Delegate threw exception of type {ex.GetType().Name}, " +
$"but {typeof(TException).Name} or a derived type was expected.",
ex);
}

if (!allowDerivedTypes && ex.GetType() != typeof(TException))
{
throw new Exception(
"Delegate threw exception of type " +
ex.GetType().Name + ", but " + typeof(TException).Name +
" was expected.",
$"Delegate threw exception of type {ex.GetType().Name}, " +
$"but {typeof(TException).Name} was expected.",
ex);
}

return (TException)ex;
}

throw new Exception(
"Delegate did not throw expected exception " +
typeof(TException).Name + ".");
}

public static Task<Exception> ThrowsAsync(Func<Task> action)
{
return ThrowsAsync<Exception>(action, true);
throw new Exception($"Delegate did not throw expected exception {typeof(TException).Name}.");
}
}
}
4 changes: 3 additions & 1 deletion Filer.EntityFrameworkCore.Tests/DatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public DatabaseFixture()
.AddJsonFile("appsettings.json")
.Build();

this.options = new DbContextOptionsBuilder().UseSqlServer(config.GetConnectionString("filer")).Options;
this.options = new DbContextOptionsBuilder()
.UseSqlServer(config.GetConnectionString("filer"))
.Options;
}

public DataContext CreateDataContext()
Expand Down
22 changes: 11 additions & 11 deletions Filer.EntityFrameworkCore.Tests/DbTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public DbTest(DatabaseFixture dbFixture)
public void CanAttachToContext()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

fileManager.AttachFileToContexts(fileId, "invoice:123", "contract:321");

Expand All @@ -35,7 +35,7 @@ public void CanAttachToContext()
public void CanCreateFile()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

Assert.NotEqual(0, fileId);
Assert.NotNull(fileManager.GetById(fileId));
Expand All @@ -45,7 +45,7 @@ public void CanCreateFile()
public void CanDeleteFiles()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

fileManager.AttachFileToContexts(fileId, "invoice:123", "contract:321", "order:123");

Expand All @@ -57,8 +57,8 @@ public void CanDeleteFiles()
public void CanDeleteFilesInBulk()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId1 = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId2 = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId1 = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);
var fileId2 = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

fileManager.AttachFileToContexts(fileId1, "invoice:1", "contract:1", "order:3");
fileManager.AttachFileToContexts(fileId2, "invoice:2", "contract:2", "order:3");
Expand All @@ -84,7 +84,7 @@ public void CanDeleteFilesInBulk()
public void CanDetachFiles()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

fileManager.AttachFileToContexts(fileId, "invoice:123", "contract:321", "order:123");

Expand All @@ -100,7 +100,7 @@ public void CanDetachFiles()
public void CanGetFilesByContext()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

fileManager.AttachFileToContexts(fileId, "invoice:123", "contract:321");

Expand All @@ -112,8 +112,8 @@ public void CanGetFilesByContext()
public void CanRemoveContext()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId1 = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId2 = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId1 = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);
var fileId2 = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

fileManager.AttachFileToContexts(fileId1, "invoice:1", "contract:1", "user:123");
fileManager.AttachFileToContexts(fileId2, "invoice:2", "contract:2", "user:123");
Expand All @@ -128,10 +128,10 @@ public void CanRemoveContext()
public void CanSpecifyUploader()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip, 12345);
var fileId = fileManager.SaveFile("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip, 12345);

var file = fileManager.Files
.SingleOrDefault(t => t.Id == fileId);
.Single(t => t.Id == fileId);

Assert.StrictEqual(12345, file.CreatedByUserId);
}
Expand Down
37 changes: 19 additions & 18 deletions Filer.EntityFrameworkCore.Tests/DbTestAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Linq;
using System.Threading.Tasks;
using Filer.Core;
using Microsoft.EntityFrameworkCore;
using Xunit;
Expand All @@ -17,10 +18,10 @@ public DbTestAsync(DatabaseFixture dbFixture)
private readonly DatabaseFixture dbFixture;

[Fact]
public async void CanAttachToContext()
public async Task CanAttachToContext()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

await fileManager.AttachFileToContextsAsync(fileId, "invoice:123", "contract:321");

Expand All @@ -32,20 +33,20 @@ public async void CanAttachToContext()
}

[Fact]
public async void CanCreateFile()
public async Task CanCreateFile()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

Assert.NotEqual(0, fileId);
Assert.NotNull(fileManager.GetById(fileId));
}

[Fact]
public async void CanDeleteFiles()
public async Task CanDeleteFiles()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

await fileManager.AttachFileToContextsAsync(fileId, "invoice:123", "contract:321", "order:123");

Expand All @@ -55,11 +56,11 @@ public async void CanDeleteFiles()
}

[Fact]
public async void CanDeleteFilesInBulk()
public async Task CanDeleteFilesInBulk()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId1 = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId2 = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId1 = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);
var fileId2 = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

await fileManager.AttachFileToContextsAsync(fileId1, "invoice:1", "contract:1", "order:3");
await fileManager.AttachFileToContextsAsync(fileId2, "invoice:2", "contract:2", "order:3");
Expand All @@ -82,10 +83,10 @@ await fileManager.DeleteFilesAsync(
}

[Fact]
public async void CanDetachFiles()
public async Task CanDetachFiles()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

await fileManager.AttachFileToContextsAsync(fileId, "invoice:123", "contract:321", "order:123");

Expand All @@ -98,10 +99,10 @@ public async void CanDetachFiles()
}

[Fact]
public async void CanGetFilesByContext()
public async Task CanGetFilesByContext()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

await fileManager.AttachFileToContextsAsync(fileId, "invoice:123", "contract:321");

Expand All @@ -110,11 +111,11 @@ public async void CanGetFilesByContext()
}

[Fact]
public async void CanRemoveContext()
public async Task CanRemoveContext()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId1 = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId2 = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId1 = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);
var fileId2 = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip);

await fileManager.AttachFileToContextsAsync(fileId1, "invoice:1", "contract:1", "user:123");
await fileManager.AttachFileToContextsAsync(fileId2, "invoice:2", "contract:2", "user:123");
Expand All @@ -126,10 +127,10 @@ public async void CanRemoveContext()
}

[Fact]
public async void CanSpecifyUploader()
public async Task CanSpecifyUploader()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", new byte[0], CompressionFormat.GZip, 12345);
var fileId = await fileManager.SaveFileAsync("test.txt", "text/plain", Array.Empty<byte>(), CompressionFormat.GZip, 12345);

var file = await fileManager.Files
.SingleOrDefaultAsync(t => t.Id == fileId);
Expand Down

0 comments on commit 11b5df9

Please sign in to comment.