-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
311 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using starsky.foundation.platform.Enums; | ||
using starsky.foundation.platform.Thumbnails; | ||
using starsky.foundation.thumbnailgeneration.GenerationFactory.Generators.Interfaces; | ||
using starsky.foundation.thumbnailgeneration.Models; | ||
|
||
namespace starskytest.FakeMocks; | ||
|
||
public class FakeThumbnailGenerator : IThumbnailGenerator | ||
{ | ||
private Exception? _exception; | ||
private IEnumerable<GenerationResultModel> _results = new List<GenerationResultModel>(); | ||
|
||
public Task<IEnumerable<GenerationResultModel>> GenerateThumbnail(string singleSubPath, | ||
string fileHash, ThumbnailImageFormat imageFormat, List<ThumbnailSize> thumbnailSizes) | ||
{ | ||
if ( _exception != null ) | ||
{ | ||
throw _exception; | ||
} | ||
|
||
return Task.FromResult(_results); | ||
} | ||
|
||
public void SetResults(IEnumerable<GenerationResultModel> results) | ||
{ | ||
_results = results; | ||
} | ||
|
||
public void SetException(Exception exception) | ||
{ | ||
_exception = exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
starsky/starskytest/starsky.foundation.platform/Thumbnails/ThumbnailSizesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using starsky.foundation.platform.Thumbnails; | ||
|
||
namespace starskytest.starsky.foundation.platform.Thumbnails; | ||
|
||
[TestClass] | ||
public class ThumbnailSizesTests | ||
{ | ||
[TestMethod] | ||
public void GetLargeToSmallSizes_WithExtraLarge() | ||
{ | ||
// Arrange | ||
var expectedSizes = new List<ThumbnailSize> | ||
{ | ||
ThumbnailSize.ExtraLarge, ThumbnailSize.Large, ThumbnailSize.Small | ||
}; | ||
|
||
// Act | ||
var result = ThumbnailSizes.GetLargeToSmallSizes(false); | ||
|
||
// Assert | ||
CollectionAssert.AreEqual(expectedSizes, result); | ||
} | ||
|
||
[TestMethod] | ||
public void GetLargeToSmallSizes_WithoutExtraLarge() | ||
{ | ||
// Arrange | ||
var expectedSizes = new List<ThumbnailSize> { ThumbnailSize.Large, ThumbnailSize.Small }; | ||
|
||
// Act | ||
var result = ThumbnailSizes.GetLargeToSmallSizes(true); | ||
|
||
// Assert | ||
CollectionAssert.AreEqual(expectedSizes, result); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...rsky.foundation.thumbnailgeneration/GenerationFactory/CompositeThumbnailGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using starsky.foundation.platform.Enums; | ||
using starsky.foundation.platform.Thumbnails; | ||
using starsky.foundation.thumbnailgeneration.GenerationFactory; | ||
using starsky.foundation.thumbnailgeneration.GenerationFactory.Generators.Interfaces; | ||
using starsky.foundation.thumbnailgeneration.Models; | ||
using starskytest.FakeMocks; | ||
|
||
namespace starskytest.starsky.foundation.thumbnailgeneration.GenerationFactory; | ||
|
||
[TestClass] | ||
public class CompositeThumbnailGeneratorTests | ||
{ | ||
private readonly FakeIWebLogger _logger; | ||
private readonly CompositeThumbnailGenerator _compositeGenerator; | ||
private readonly List<FakeThumbnailGenerator> _generatorMocks; | ||
|
||
public CompositeThumbnailGeneratorTests() | ||
{ | ||
_logger = new FakeIWebLogger(); | ||
_generatorMocks = | ||
[ | ||
new FakeThumbnailGenerator(), | ||
new FakeThumbnailGenerator() | ||
]; | ||
_compositeGenerator = new CompositeThumbnailGenerator( | ||
_generatorMocks.Cast<IThumbnailGenerator>().ToList(), _logger); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow(true, true, true)] | ||
[DataRow(false, true, true)] | ||
[DataRow(false, false, false)] | ||
public async Task GenerateThumbnail_VariousScenarios_ReturnsExpectedResults(bool firstSuccess, | ||
bool secondSuccess, bool expectedSuccess) | ||
{ | ||
// Arrange | ||
const string singleSubPath = "test.jpg"; | ||
const string fileHash = "hash"; | ||
const ThumbnailImageFormat imageFormat = ThumbnailImageFormat.jpg; | ||
var thumbnailSizes = new List<ThumbnailSize> { ThumbnailSize.Small }; | ||
|
||
var firstResults = new List<GenerationResultModel> { new() { Success = firstSuccess } }; | ||
|
||
var secondResults = new List<GenerationResultModel> { new() { Success = secondSuccess } }; | ||
|
||
_generatorMocks[0].SetResults(firstResults); | ||
_generatorMocks[1].SetResults(secondResults); | ||
|
||
// Act | ||
var results = | ||
await _compositeGenerator.GenerateThumbnail(singleSubPath, fileHash, imageFormat, | ||
thumbnailSizes); | ||
|
||
// Assert | ||
Assert.AreEqual(expectedSuccess, results.All(r => r.Success)); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow(true)] | ||
[DataRow(false)] | ||
public async Task GenerateThumbnail_GeneratorThrowsException_LogsError(bool secondSuccess) | ||
{ | ||
// Arrange | ||
const string singleSubPath = "test.jpg"; | ||
const string fileHash = "hash"; | ||
const ThumbnailImageFormat imageFormat = ThumbnailImageFormat.jpg; | ||
var thumbnailSizes = new List<ThumbnailSize> { ThumbnailSize.Small }; | ||
|
||
_generatorMocks[0].SetException(new Exception("Test exception")); | ||
|
||
var secondResults = new List<GenerationResultModel> { new() { Success = secondSuccess } }; | ||
|
||
_generatorMocks[1].SetResults(secondResults); | ||
|
||
// Act | ||
var results = | ||
await _compositeGenerator.GenerateThumbnail(singleSubPath, fileHash, imageFormat, | ||
thumbnailSizes); | ||
|
||
// Assert | ||
Assert.IsTrue( | ||
_logger.TrackedExceptions.Any(log => log.Item2?.Contains("Test exception") == true)); | ||
Assert.AreEqual(secondSuccess, results.All(r => r.Success)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...tarsky.foundation.thumbnailgeneration/GenerationFactory/ThumbnailGeneratorFactoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using starsky.foundation.thumbnailgeneration.GenerationFactory; | ||
using starsky.foundation.thumbnailgeneration.GenerationFactory.Generators; | ||
using starskytest.FakeMocks; | ||
|
||
namespace starskytest.starsky.foundation.thumbnailgeneration.GenerationFactory; | ||
|
||
[TestClass] | ||
public class ThumbnailGeneratorFactoryTests | ||
{ | ||
private readonly ThumbnailGeneratorFactory _factory; | ||
|
||
public ThumbnailGeneratorFactoryTests() | ||
{ | ||
var selectorStorageMock = new FakeSelectorStorage(); | ||
var loggerMock = new FakeIWebLogger(); | ||
var videoProcessMock = new FakeIVideoProcess(selectorStorageMock); | ||
_factory = new ThumbnailGeneratorFactory(selectorStorageMock, loggerMock, videoProcessMock); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow("test.jpg", typeof(CompositeThumbnailGenerator))] | ||
[DataRow("test.mp4", typeof(CompositeThumbnailGenerator))] | ||
[DataRow("test.txt", typeof(NotSupportedFallbackThumbnailGenerator))] | ||
public void GetGenerator_ReturnsCorrectGenerator(string filePath, Type expectedType) | ||
{ | ||
// Act | ||
var generator = _factory.GetGenerator(filePath); | ||
|
||
// Assert | ||
Assert.IsInstanceOfType(generator, expectedType); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
starsky/starskytest/starsky.foundation.video/Process/VideoProcessThumbnailPostTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using MetadataExtractor; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using starsky.foundation.platform.Models; | ||
using starsky.foundation.readmeta.ReadMetaHelpers; | ||
using starsky.foundation.video.Process; | ||
using starskytest.FakeCreateAn; | ||
using starskytest.FakeMocks; | ||
|
||
namespace starskytest.starsky.foundation.video.Process; | ||
|
||
[TestClass] | ||
public class VideoProcessThumbnailPostTests | ||
{ | ||
private readonly VideoProcessThumbnailPost _videoProcessThumbnailPost; | ||
private readonly FakeIStorage _storage; | ||
|
||
public VideoProcessThumbnailPostTests() | ||
{ | ||
_storage = new FakeIStorage(); | ||
|
||
var selectorStorage = new FakeSelectorStorage(_storage); | ||
var exifTool = new FakeExifTool(_storage, new AppSettings()); | ||
var logger = new FakeIWebLogger(); | ||
var thumbnailQuery = new FakeIThumbnailQuery(); | ||
|
||
var appSettings = new AppSettings(); | ||
_videoProcessThumbnailPost = new VideoProcessThumbnailPost( | ||
selectorStorage, appSettings, exifTool, logger, thumbnailQuery); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("/test.mp4", "/test.jpg")] | ||
[DataRow("/test/test.mov", "/test/test.jpg")] | ||
public async Task PostPrepThumbnail_Success_ReturnsVideoResult(string subPath, | ||
string jpegInFolderSubPath) | ||
{ | ||
// Arrange | ||
var runResult = new VideoResult(true, subPath); | ||
var stream = new MemoryStream([.. CreateAnImageNoExif.Bytes]); | ||
|
||
// Act | ||
var result = await _videoProcessThumbnailPost.PostPrepThumbnail(runResult, | ||
stream, jpegInFolderSubPath); | ||
|
||
// Assert | ||
Assert.IsTrue(result.IsSuccess); | ||
Assert.AreEqual(jpegInFolderSubPath, result.ResultPath); | ||
|
||
Assert.IsTrue(_storage.ExistFile(result.ResultPath ?? "")); | ||
|
||
var writtenStream = _storage.ReadStream(result.ResultPath ?? ""); | ||
var meta = ImageMetadataReader.ReadMetadata(writtenStream).ToList(); | ||
await writtenStream.DisposeAsync(); | ||
|
||
Assert.AreEqual(3, ReadMetaExif.GetImageWidthHeight(meta, true)); | ||
Assert.AreEqual(2, ReadMetaExif.GetImageWidthHeight(meta, false)); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("/test.mp4")] | ||
[DataRow("/test/test.mov")] | ||
public async Task PostPrepThumbnail_Failure_ReturnsOriginalResult(string subPath) | ||
{ | ||
// Arrange | ||
var runResult = new VideoResult(false, subPath); | ||
var stream = new MemoryStream(); | ||
|
||
// Act | ||
var result = await _videoProcessThumbnailPost.PostPrepThumbnail(runResult, stream, subPath); | ||
|
||
// Assert | ||
Assert.IsFalse(result.IsSuccess); | ||
Assert.AreEqual(subPath, result.ResultPath); | ||
} | ||
} |