Skip to content

Commit

Permalink
fix test && remove dateformatter from job
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 25, 2024
1 parent 0f21c3b commit c74b767
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Threading.Tasks;

namespace starsky.feature.thumbnail.Interfaces;

public interface IDatabaseThumbnailGenerationService
{
Task StartBackgroundQueue(DateTime endTime);
Task StartBackgroundQueue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DatabaseThumbnailGenerationService(IQuery query, IWebLogger logger,
_updateStatusGeneratedThumbnailService = updateStatusGeneratedThumbnailService;
}

public async Task StartBackgroundQueue(DateTime endTime)
public async Task StartBackgroundQueue()
{
if ( _thumbnailQuery.IsRunningJob() )
{
Expand All @@ -55,7 +55,7 @@ await _bgTaskQueue.QueueBackgroundWorkItemAsync(
"DatabaseThumbnailGenerationService");
}

internal async Task WorkThumbnailGenerationLoop()
private async Task WorkThumbnailGenerationLoop()
{
_thumbnailQuery.SetRunningJob(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ await timer.WaitForNextTickAsync(cancellationToken) )
await using var asyncScope = _factory.CreateAsyncScope();
var service = asyncScope.ServiceProvider
.GetRequiredService<IDatabaseThumbnailGenerationService>();
await service.StartBackgroundQueue(DateTime.UtcNow.Add(Period));
await service.StartBackgroundQueue();
_executionCount++;
// Executed PeriodicThumbnailScanHostedService
_logger.LogDebug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class FakeIDatabaseThumbnailGenerationService : IDatabaseThumbnailGenerat
{
public int Count { get; set; }

public Task StartBackgroundQueue(DateTime endTime)
public Task StartBackgroundQueue()
{
Count++;
return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using starsky.feature.thumbnail.Services;
using starsky.foundation.database.Models;
using starsky.foundation.thumbnailgeneration.Services;
using starskytest.FakeCreateAn;
using starskytest.FakeMocks;

namespace starskytest.starsky.feature.thumbnail.Services;

[TestClass]
public class DatabaseThumbnailGenerationServiceTest
{

[TestMethod]
public async Task StartBackgroundQueue_NoContentSoNotFired()
public async Task StartBackgroundQueue_NoContentSoHitOnce()
{
var bgTaskQueue = new FakeThumbnailBackgroundTaskQueue();
var databaseThumbnailGenerationService = new DatabaseThumbnailGenerationService(
Expand All @@ -26,57 +25,55 @@ public async Task StartBackgroundQueue_NoContentSoNotFired()
new UpdateStatusGeneratedThumbnailService(new FakeIThumbnailQuery())
);

await databaseThumbnailGenerationService.StartBackgroundQueue(
DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));

Assert.AreEqual(0,bgTaskQueue.Count());
await databaseThumbnailGenerationService.StartBackgroundQueue();

Assert.AreEqual(1, bgTaskQueue.Count());
}

[TestMethod]
public async Task StartBackgroundQueue_OneItemSoTrigger()
{
var bgTaskQueue = new FakeThumbnailBackgroundTaskQueue();
var thumbnailQuery = new FakeIThumbnailQuery(new List<ThumbnailItem>
{
new ThumbnailItem("12",null,null,null,null)
new("12", null, null, null, null)
});

var databaseThumbnailGenerationService = new DatabaseThumbnailGenerationService(
new FakeIQuery(), new FakeIWebLogger(), new FakeIWebSocketConnectionsService(),
new FakeIThumbnailService(),
thumbnailQuery,
bgTaskQueue,
new UpdateStatusGeneratedThumbnailService(new FakeIThumbnailQuery())
);

await databaseThumbnailGenerationService.StartBackgroundQueue(
DateTime.UtcNow.Add(TimeSpan.FromMinutes(1)));

Assert.AreEqual(1,bgTaskQueue.Count());

await databaseThumbnailGenerationService.StartBackgroundQueue();

Assert.AreEqual(1, bgTaskQueue.Count());
}


[TestMethod]
public async Task WorkThumbnailGeneration_ZeroItems()
{
var bgTaskQueue = new FakeThumbnailBackgroundTaskQueue();
var thumbnailQuery = new FakeIThumbnailQuery(new List<ThumbnailItem>
{
new ThumbnailItem("12",null,null,null,null)
new("12", null, null, null, null)
});

var databaseThumbnailGenerationService = new DatabaseThumbnailGenerationService(
new FakeIQuery(), new FakeIWebLogger(), new FakeIWebSocketConnectionsService(),
new FakeIThumbnailService(),
thumbnailQuery,
bgTaskQueue,
new UpdateStatusGeneratedThumbnailService(new FakeIThumbnailQuery())
);

var result = await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem>(), new List<FileIndexItem>());
Assert.AreEqual(0,result.Count());

Assert.AreEqual(0, result.Count());
}

[TestMethod]
Expand All @@ -85,9 +82,9 @@ public async Task WorkThumbnailGeneration_NotFoundItem_Database()
var bgTaskQueue = new FakeThumbnailBackgroundTaskQueue();
var thumbnailQuery = new FakeIThumbnailQuery(new List<ThumbnailItem>
{
new ThumbnailItem("12",null,null,null,null)
new("12", null, null, null, null)
});

var databaseThumbnailGenerationService = new DatabaseThumbnailGenerationService(
new FakeIQuery(), new FakeIWebLogger(), new FakeIWebSocketConnectionsService(),
new FakeIThumbnailService(),
Expand All @@ -96,130 +93,117 @@ public async Task WorkThumbnailGeneration_NotFoundItem_Database()
new UpdateStatusGeneratedThumbnailService(new FakeIThumbnailQuery(
new List<ThumbnailItem>()))
);

var result = (await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem>
{
new ThumbnailItem("74283reiotfskl",null,null,null,null)
}, new List<FileIndexItem>

var result = ( await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem> { new("74283rei_ot_fs_kl", null, null, null, null) },
new List<FileIndexItem>
{
new FileIndexItem()
new()
{
FileHash = "74283reiotfskl",
FileHash = "74283rei_ot_fs_kl",
Status = FileIndexItem.ExifStatus.NotFoundSourceMissing
}
})).ToList();
Assert.AreEqual(1,result.Count);
}) ).ToList();

Assert.AreEqual(1, result.Count);
Assert.IsFalse(result.FirstOrDefault()!.Large);
}

[TestMethod]
public async Task WorkThumbnailGeneration_NotFoundItem_2()
{
var bgTaskQueue = new FakeThumbnailBackgroundTaskQueue();
var thumbnailQuery = new FakeIThumbnailQuery(new List<ThumbnailItem>
{
new ThumbnailItem("74283reiotfskl",null,null,null,null)
new("74283rei_ot_fs_kl", null, null, null, null)
});

var databaseThumbnailGenerationService = new DatabaseThumbnailGenerationService(
new FakeIQuery(), new FakeIWebLogger(), new FakeIWebSocketConnectionsService(),
new FakeIThumbnailService(new FakeSelectorStorage()),
thumbnailQuery,
bgTaskQueue,
new UpdateStatusGeneratedThumbnailService(thumbnailQuery)
);

var result = (await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem>
{
new ThumbnailItem("74283reiotfskl",null,null,null,null)
}, new List<FileIndexItem>

var result = ( await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem> { new("74283rei_ot_fs_kl", null, null, null, null) },
new List<FileIndexItem>
{
new FileIndexItem()
new()
{
FileHash = "74283reiotfskl",
FileHash = "74283rei_ot_fs_kl",
FilePath = "/test.jpg",
Status = FileIndexItem.ExifStatus.Ok
}
})).ToList();
Assert.AreEqual(1,result.Count);
Assert.AreEqual(0,(await thumbnailQuery.Get( "74283reiotfskl")).Count);
}) ).ToList();

Assert.AreEqual(1, result.Count);
Assert.AreEqual(0, ( await thumbnailQuery.Get("74283rei_ot_fs_kl") ).Count);
}


[TestMethod]
public async Task WorkThumbnailGeneration_FoundUpdate()
{
var bgTaskQueue = new FakeThumbnailBackgroundTaskQueue();
var thumbnailQuery = new FakeIThumbnailQuery(new List<ThumbnailItem>
{
new ThumbnailItem("345742938fsdjkdfkj",null,null,null,null)
new("345742938fs_jk_df_kj", null, null, null, null)
});

var databaseThumbnailGenerationService = new DatabaseThumbnailGenerationService(
new FakeIQuery(), new FakeIWebLogger(), new FakeIWebSocketConnectionsService(),
new FakeIThumbnailService(new FakeSelectorStorage(new FakeIStorage(new List<string>(), new List<string>
{
"/test.jpg"
}, new List<byte[]>{FakeCreateAn.CreateAnImage.Bytes.ToArray()}))),
new FakeIThumbnailService(new FakeSelectorStorage(new FakeIStorage(new List<string>(),
new List<string> { "/test.jpg" },
new List<byte[]> { CreateAnImage.Bytes.ToArray() }))),
thumbnailQuery,
bgTaskQueue,
new UpdateStatusGeneratedThumbnailService(thumbnailQuery)
);

var result = (await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem>
{
new ThumbnailItem("345742938fsdjkdfkj",null,null,null,null)
}, new List<FileIndexItem>

var result = ( await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem> { new("345742938fs_jk_df_kj", null, null, null, null) },
new List<FileIndexItem>
{
new FileIndexItem()
new()
{
FileHash = "345742938fsdjkdfkj",
FileHash = "345742938fs_jk_df_kj",
FilePath = "/test.jpg",
Status = FileIndexItem.ExifStatus.Ok
}
})).ToList();
Assert.AreEqual(1,result.Count);
Assert.AreEqual(1,(await thumbnailQuery.Get( "345742938fsdjkdfkj")).Count);
Assert.AreEqual(null,result.FirstOrDefault()!.Large);
}) ).ToList();

Assert.AreEqual(1, result.Count);
Assert.AreEqual(1, ( await thumbnailQuery.Get("345742938fs_jk_df_kj") ).Count);
Assert.AreEqual(null, result.FirstOrDefault()!.Large);
}

[TestMethod]
public async Task WorkThumbnailGeneration_MatchItem()
{
var bgTaskQueue = new FakeThumbnailBackgroundTaskQueue();
var thumbnailQuery = new FakeIThumbnailQuery(new List<ThumbnailItem>
{
new ThumbnailItem("12",null,null,null,null)
new("12", null, null, null, null)
});

var databaseThumbnailGenerationService = new DatabaseThumbnailGenerationService(
new FakeIQuery(), new FakeIWebLogger(), new FakeIWebSocketConnectionsService(),
new FakeIThumbnailService(),
thumbnailQuery,
bgTaskQueue,
new UpdateStatusGeneratedThumbnailService(new FakeIThumbnailQuery())
);

var result = (await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem>
{
new ThumbnailItem("74283reiotfskl",null,null,null,null)
}, new List<FileIndexItem>

var result = ( await databaseThumbnailGenerationService.WorkThumbnailGeneration(
new List<ThumbnailItem> { new("345742938fs_jk_df_kj", null, null, null, null) },
new List<FileIndexItem>
{
new FileIndexItem()
{
FileHash = "74283reiotfskl",
Status = FileIndexItem.ExifStatus.Ok
}
})).ToList();

Assert.AreEqual(1,result.Count);
Assert.AreEqual(null,result.FirstOrDefault()!.Large);
new() { FileHash = "345742938fs_jk_df_kj", Status = FileIndexItem.ExifStatus.Ok }
}) ).ToList();

Assert.AreEqual(1, result.Count);
Assert.AreEqual(null, result.FirstOrDefault()!.Large);
}
}

0 comments on commit c74b767

Please sign in to comment.