ImgMetadataRemover/ConsoleInterface.Tests/TestDirectoryOutputSink.cs

74 lines
2.3 KiB
C#
Raw Normal View History

2022-04-02 14:22:42 +00:00
using System;
using System.Diagnostics;
using System.IO;
using Image.Core;
using Moq;
using Xunit;
namespace ConsoleInterface.Tests;
public class TestDirectoryOutputSink
2022-04-02 14:22:42 +00:00
{
private readonly string? _testsProjectDirectory;
public TestDirectoryOutputSink()
2022-04-02 14:22:42 +00:00
{
_testsProjectDirectory = Environment.GetEnvironmentVariable("IMAGE_CORE_TESTS");
2022-04-02 14:33:48 +00:00
if (_testsProjectDirectory == null)
{
throw new Exception("Environment variable IMAGE_CORE_TESTS is not set!");
}
2022-04-02 14:22:42 +00:00
}
[Theory]
[InlineData("", "./", @".\.jpg")]
[InlineData("", @".\", @".\.jpg")]
[InlineData("", "asd", @".\asd.jpg")]
[InlineData("dir", "asd", @"dir\asd.jpg")]
public void TestGetOutputPath(string directory, string file, string expectedPath)
{
var sink = DirectoryOutputSink.Create(directory);
2022-04-02 14:22:42 +00:00
Assert.Equal(expectedPath, sink.GetOutputPath(file));
}
[Fact]
public void TestGetOutputPathNull()
{
var sink = DirectoryOutputSink.Create("directory");
2022-04-02 14:22:42 +00:00
Assert.Throws<ArgumentException>(() => sink.GetOutputPath(""));
}
[Fact]
public void TestSave()
{
// Setup
var sink = DirectoryOutputSink.Create("directory");
2022-04-02 14:22:42 +00:00
var metadataRemoverMock = new Mock<IMetadataRemover>();
metadataRemoverMock.Setup(i => i.GetImagePath()).Returns("alo.wtf");
// Test
sink.Save(metadataRemoverMock.Object);
// Assert
metadataRemoverMock.Verify(i => i.CleanImage());
metadataRemoverMock.Verify(i => i.SaveImage("directory\\alo.jpg"));
2022-04-02 14:22:42 +00:00
}
[Fact]
public void TestSaveFileExists()
{
// Setup
Debug.Assert(_testsProjectDirectory != null, nameof(_testsProjectDirectory) + " != null");
var sink = DirectoryOutputSink.Create(Path.Combine(_testsProjectDirectory, "test_pictures"));
2022-04-02 14:22:42 +00:00
var metadataRemoverMock = new Mock<IMetadataRemover>();
var sourceFileName = Path.Combine(_testsProjectDirectory, "test_pictures\\IMG_0138.HEIC");
metadataRemoverMock.Setup(i => i.GetImagePath()).Returns(sourceFileName);
// Test
sink.Save(metadataRemoverMock.Object);
// Assert
metadataRemoverMock.Verify(i => i.CleanImage(), Times.Never);
metadataRemoverMock.Verify(i => i.SaveImage(It.IsAny<string>()), Times.Never);
2022-04-02 14:22:42 +00:00
}
}