Skip to content

Commit

Permalink
reformat and add test code smells 25
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 25, 2024
1 parent 97604b0 commit d3e93d6
Showing 1 changed file with 63 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
Expand All @@ -16,39 +17,84 @@ public class GetObjectsByFileHashAsyncTest
{
private readonly Query _query;

public GetObjectsByFileHashAsyncTest()
{
_query = new Query(CreateNewScope().CreateScope().ServiceProvider
.GetRequiredService<ApplicationDbContext>(), new AppSettings(), null!,
new FakeIWebLogger(), new FakeMemoryCache());
}

private static IServiceScopeFactory CreateNewScope()
{
var services = new ServiceCollection();
services.AddMemoryCache();
services.AddDbContext<ApplicationDbContext>(options =>
services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase(nameof(GetObjectsByFileHashAsyncTest)));
var serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetRequiredService<IServiceScopeFactory>();
}

public GetObjectsByFileHashAsyncTest()
{
_query = new Query(CreateNewScope().CreateScope().ServiceProvider
.GetRequiredService<ApplicationDbContext>(), new AppSettings(), null!, new FakeIWebLogger(), new FakeMemoryCache()) ;
}

[TestMethod]
public async Task GetObjectsByFileHashAsyncTest_NoContent()
{
var items = await _query.GetObjectsByFileHashAsync(new List<string>());
var items = await _query.GetObjectsByFileHashAsync(new List<string>());
Assert.AreEqual(0, items.Count);
}

[TestMethod]
public async Task GetObjectsByFileHashAsyncTest_GetByHash()
{
await _query.AddItemAsync(new FileIndexItem(){FileHash = "123456"});
var items = await _query.GetObjectsByFileHashAsync(new List<string>
{
"123456"
});

await _query.AddItemAsync(new FileIndexItem { FileHash = "123456" });
var items = await _query.GetObjectsByFileHashAsync(new List<string> { "123456" });

Assert.AreEqual(1, items.Count);
Assert.AreEqual( "123456", items.Find(p => p.FileHash == "123456")?.FileHash);
}
Assert.AreEqual("123456", items.Find(p => p.FileHash == "123456")?.FileHash);
}

[TestMethod]
public async Task GetObjectsByFileHashAsync_Disposed_NoServiceScope()
{
// Arrange
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase("Add_writes_to_database11")
.Options;

var dbContext = new ApplicationDbContext(options);
var query =
new Query(dbContext, new AppSettings(), null!,
new FakeIWebLogger()); // <-- no service scope

// Dispose the DbContext
await dbContext.DisposeAsync();

// Act & Assert
await Assert.ThrowsExceptionAsync<AggregateException>(async () =>
await query.GetObjectsByFileHashAsync(new List<string> { "test123" }, 1));
}

[TestMethod]
public async Task GetObjectsByFileHashAsync_Disposed_Success()
{
// Arrange
var serviceScope = CreateNewScope();
var scope = serviceScope.CreateScope();
var dbContext = scope.ServiceProvider
.GetRequiredService<ApplicationDbContext>();
var query =
new Query(dbContext, new AppSettings(), serviceScope,
new FakeIWebLogger()); // <-- no service scope

dbContext.FileIndex.Add(new FileIndexItem { FileHash = "test123" });
await dbContext.SaveChangesAsync();

// And dispose
await dbContext.DisposeAsync();

// Act
var result = await query.GetObjectsByFileHashAsync(new List<string> { "test123" }, 1);

// Assert
Assert.AreEqual(1, result.Count);
Assert.AreEqual("test123", result[0].FileHash);
}
}

0 comments on commit d3e93d6

Please sign in to comment.