refactor(config): make radarr quality_definition.type required

recyclarr
Robert Dailey 4 years ago
parent 03ee600346
commit 070a961ed0

@ -0,0 +1,39 @@
using System;
using System.IO;
using System.IO.Abstractions;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
using Trash.Config;
using Trash.Radarr;
using YamlDotNet.Core;
using YamlDotNet.Serialization.ObjectFactories;
namespace Trash.Tests.Radarr
{
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class RadarrConfigurationTest
{
[Test]
public void Deserialize_QualityDefinitionTypeMissing_Throw()
{
const string yaml = @"
radarr:
- base_url: a
api_key: b
quality_definition:
preferred_ratio: 0.5
";
var loader = new ConfigurationLoader<RadarrConfiguration>(
Substitute.For<IConfigurationProvider<RadarrConfiguration>>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());
Action act = () => loader.LoadFromStream(new StringReader(yaml), "radarr");
act.Should().Throw<YamlException>()
.WithMessage("*'type' is required for 'quality_definition'");
}
}
}

@ -25,7 +25,6 @@ namespace Trash.Config
_deserializer = new DeserializerBuilder()
.WithRequiredPropertyValidation()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
// .WithNamingConvention(CamelCaseNamingConvention.Instance)
.WithTypeConverter(new YamlNullableEnumTypeConverter())
.WithObjectFactory(objectFactory)
.Build();

@ -1,4 +1,5 @@
using Flurl;
using System.ComponentModel.DataAnnotations;
using Flurl;
using JetBrains.Annotations;
using Trash.Config;
using Trash.Radarr.QualityDefinition;
@ -21,7 +22,13 @@ namespace Trash.Radarr
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
public class QualityDefinitionConfig
{
public RadarrQualityDefinitionType Type { get; init; }
// -1 does not map to a valid enumerator. this is to force validation to fail if it is not set from YAML
// all of this craziness is to avoid making the enum type nullable which will make using the property
// frustrating.
[EnumDataType(typeof(RadarrQualityDefinitionType),
ErrorMessage = "'type' is required for 'quality_definition'")]
public RadarrQualityDefinitionType Type { get; init; } = (RadarrQualityDefinitionType) (-1);
public decimal PreferredRatio { get; set; } = 1.0m;
}
}

Loading…
Cancel
Save