Tests project and Resize bug
This commit is contained in:
174
Tests/UnitTestResize.cs
Normal file
174
Tests/UnitTestResize.cs
Normal file
@ -0,0 +1,174 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Serilog;
|
||||
using System.Reflection;
|
||||
using System.Diagnostics;
|
||||
using View_by_Distance.Tests.Models;
|
||||
using View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
using Phares.Shared;
|
||||
using View_by_Distance.Resize.Models;
|
||||
using System.Drawing.Imaging;
|
||||
using View_by_Distance.Metadata.Models;
|
||||
|
||||
namespace View_by_Distance.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class UnitTestResize
|
||||
{
|
||||
|
||||
private readonly ILogger _Logger;
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly string _WorkingDirectory;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly IConfigurationRoot _ConfigurationRoot;
|
||||
private readonly Property.Models.Configuration _PropertyConfiguration;
|
||||
|
||||
public UnitTestResize()
|
||||
{
|
||||
ILogger logger;
|
||||
AppSettings appSettings;
|
||||
string workingDirectory;
|
||||
Configuration configuration;
|
||||
IsEnvironment isEnvironment;
|
||||
IConfigurationRoot configurationRoot;
|
||||
LoggerConfiguration loggerConfiguration = new();
|
||||
Property.Models.Configuration propertyConfiguration;
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
|
||||
isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
|
||||
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.AddJsonFile(isEnvironment.AppSettingsFileName);
|
||||
configurationRoot = configurationBuilder.Build();
|
||||
appSettings = Models.Stateless.AppSettings.Get(configurationRoot);
|
||||
workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
|
||||
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
|
||||
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
logger = Log.ForContext<UnitTestResize>();
|
||||
propertyConfiguration = Property.Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory);
|
||||
configuration = Models.Stateless.Configuration.Get(isEnvironment, configurationRoot, workingDirectory, propertyConfiguration);
|
||||
logger.Information("Complete");
|
||||
_Logger = logger;
|
||||
_AppSettings = appSettings;
|
||||
_Configuration = configuration;
|
||||
_IsEnvironment = isEnvironment;
|
||||
_WorkingDirectory = workingDirectory;
|
||||
_ConfigurationRoot = configurationRoot;
|
||||
_PropertyConfiguration = propertyConfiguration;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestMethodNull()
|
||||
{
|
||||
Assert.IsFalse(_Logger is null);
|
||||
Assert.IsFalse(_AppSettings is null);
|
||||
Assert.IsFalse(_Configuration is null);
|
||||
Assert.IsFalse(_IsEnvironment is null);
|
||||
Assert.IsFalse(_WorkingDirectory is null);
|
||||
Assert.IsFalse(_ConfigurationRoot is null);
|
||||
Assert.IsFalse(_PropertyConfiguration is null);
|
||||
}
|
||||
|
||||
private Property.Models.PropertyLogic GetPropertyLogic()
|
||||
{
|
||||
Property.Models.PropertyLogic result;
|
||||
if (_AppSettings.MaxDegreeOfParallelism is null)
|
||||
throw new Exception($"{nameof(_AppSettings.MaxDegreeOfParallelism)} is null!");
|
||||
if (_Configuration?.PropertyConfiguration is null)
|
||||
throw new Exception($"{nameof(_Configuration.PropertyConfiguration)} must be set!");
|
||||
result = new(_AppSettings.MaxDegreeOfParallelism.Value, _Configuration.PropertyConfiguration);
|
||||
return result;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestMethodResize()
|
||||
{
|
||||
// string sourceFileName = "IMG_0067.jpg";
|
||||
// string sourceDirectoryName = "Mackenzie Prom 2017";
|
||||
string sourceFileName = "Fall 2005 (113).jpg";
|
||||
string sourceDirectoryName = "=2005.3 Fall";
|
||||
if (_Configuration.ForceMetadataLastWriteTimeToCreationTime is null)
|
||||
throw new Exception($"{nameof(_Configuration.ForceMetadataLastWriteTimeToCreationTime)} is null!");
|
||||
if (_Configuration.ForceResizeLastWriteTimeToCreationTime is null)
|
||||
throw new Exception($"{nameof(_Configuration.ForceResizeLastWriteTimeToCreationTime)} is null!");
|
||||
if (_Configuration.OutputQuality is null)
|
||||
throw new Exception($"{nameof(_Configuration.OutputQuality)} is null!");
|
||||
if (_Configuration.OverrideForResizeImages is null)
|
||||
throw new Exception($"{nameof(_Configuration.OverrideForResizeImages)} is null!");
|
||||
if (_Configuration.PropertiesChangedForMetadata is null)
|
||||
throw new Exception($"{nameof(_Configuration.PropertiesChangedForMetadata)} is null!");
|
||||
if (_Configuration.PropertiesChangedForResize is null)
|
||||
throw new Exception($"{nameof(_Configuration.PropertiesChangedForResize)} is null!");
|
||||
int g = 1;
|
||||
int r = 1;
|
||||
string original = "Original";
|
||||
List<string> parseExceptions = new();
|
||||
string modelName = "model.ToString()";
|
||||
Property.Models.A_Property? property = null;
|
||||
Property.Models.PropertyHolder propertyHolder;
|
||||
Dictionary<string, int[]> imageResizeKeyValuePairs;
|
||||
List<Tuple<string, DateTime>> subFileTuples = new();
|
||||
List<KeyValuePair<string, string>> metadataCollection;
|
||||
string predictorModelName = "predictorModel.ToString()";
|
||||
int length = _PropertyConfiguration.RootDirectory.Length;
|
||||
string outputResolution = _Configuration.OutputResolutions[0];
|
||||
Property.Models.PropertyLogic propertyLogic = GetPropertyLogic();
|
||||
string sourceDirectory = Path.Combine(_PropertyConfiguration.RootDirectory, sourceDirectoryName);
|
||||
string aPropertySingletonDirectory = Property.Models.Stateless.IResult.GetResultsDateGroupDirectory(_PropertyConfiguration, nameof(Property.Models.A_Property), "{}");
|
||||
B_Metadata metadata = new(_Configuration.ForceMetadataLastWriteTimeToCreationTime.Value, _Configuration.PropertiesChangedForMetadata.Value);
|
||||
(ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters) = C_Resize.GetTuple(_Configuration.OutputExtension, _Configuration.OutputQuality.Value);
|
||||
C_Resize resize = new(_Configuration.ForceResizeLastWriteTimeToCreationTime.Value, _Configuration.OverrideForResizeImages.Value, _Configuration.PropertiesChangedForResize.Value, _Configuration.ValidResolutions, imageCodecInfo, encoderParameters);
|
||||
propertyLogic.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(_PropertyConfiguration,
|
||||
modelName,
|
||||
predictorModelName,
|
||||
sourceDirectory,
|
||||
nameof(Property.Models.A_Property),
|
||||
outputResolution,
|
||||
includeResizeGroup: false,
|
||||
includeModel: false,
|
||||
includePredictorModel: false,
|
||||
contentDescription: string.Empty,
|
||||
singletonDescription: "Properties for each image",
|
||||
collectionDescription: string.Empty));
|
||||
metadata.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(_PropertyConfiguration,
|
||||
modelName,
|
||||
predictorModelName,
|
||||
sourceDirectory,
|
||||
nameof(B_Metadata),
|
||||
outputResolution,
|
||||
includeResizeGroup: false,
|
||||
includeModel: false,
|
||||
includePredictorModel: false,
|
||||
contentDescription: string.Empty,
|
||||
singletonDescription: "Metadata as key value pairs",
|
||||
collectionDescription: string.Empty));
|
||||
resize.AngleBracketCollection.AddRange(Property.Models.Stateless.IResult.GetDirectoryInfoCollection(_PropertyConfiguration,
|
||||
modelName,
|
||||
predictorModelName,
|
||||
sourceDirectory,
|
||||
nameof(C_Resize),
|
||||
outputResolution,
|
||||
includeResizeGroup: true,
|
||||
includeModel: false,
|
||||
includePredictorModel: false,
|
||||
contentDescription: "Resized image",
|
||||
singletonDescription: "Resize dimensions for each resolution",
|
||||
collectionDescription: string.Empty));
|
||||
string sourceDirectoryFile = ".json";
|
||||
FileInfo fileInfo = new(Path.Combine(sourceDirectory, sourceFileName));
|
||||
string relativePath = Property.Models.Stateless.IPath.GetRelativePath(fileInfo.FullName, length);
|
||||
sourceDirectory = Path.Combine(aPropertySingletonDirectory, sourceDirectoryName);
|
||||
propertyHolder = new(g, sourceDirectory, sourceDirectoryFile, relativePath, r, fileInfo, property, false, false, null, null, null);
|
||||
if (propertyHolder.ImageFileInfo is null)
|
||||
throw new Exception($"{nameof(propertyHolder.ImageFileInfo)} is null!");
|
||||
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(propertyHolder.ImageFileInfo.FullName);
|
||||
property = propertyLogic.GetProperty(propertyLogic.AngleBracketCollection[0], propertyHolder, subFileTuples, parseExceptions);
|
||||
(int _, metadataCollection) = metadata.GetMetadataCollection(subFileTuples, parseExceptions, propertyHolder.ImageFileInfo.FullName, propertyHolder.RelativePath, fileNameWithoutExtension);
|
||||
imageResizeKeyValuePairs = resize.GetResizeKeyValuePairs(subFileTuples, parseExceptions, original, metadataCollection, property, propertyHolder.ImageFileInfo.FullName, propertyHolder.RelativePath, fileNameWithoutExtension);
|
||||
FileInfo resizedFileInfo = new(Path.Combine(resize.AngleBracketCollection[0].Replace("<>", "()"), Path.GetFileName(propertyHolder.ImageFileInfo.FullName)));
|
||||
resize.SaveResizedSubfile(outputResolution, subFileTuples, propertyHolder.ImageFileInfo.FullName, original, property, imageResizeKeyValuePairs, resizedFileInfo);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user