1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-21 23:32:27 +00:00

Sync TimeSpanConverter with upstream

Co-authored-by: Bogdan <mynameisbogdan@users.noreply.github.com>

(cherry picked from commit 1374240321f08d1400faf95e84217e4b7a2d116b)

Closes #5301
This commit is contained in:
Mark McDowall 2024-12-07 20:45:39 -08:00 committed by Bogdan
parent 535caf1324
commit 41612708ff
3 changed files with 57 additions and 13 deletions

View file

@ -0,0 +1,43 @@
using System;
using System.Data.SQLite;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.Converters;
[TestFixture]
public class TimeSpanConverterFixture : CoreTest<TimeSpanConverter>
{
private SQLiteParameter _param;
[SetUp]
public void Setup()
{
_param = new SQLiteParameter();
}
[Test]
public void should_return_string_when_saving_timespan_to_db()
{
var span = TimeSpan.FromMilliseconds(10);
Subject.SetValue(_param, span);
_param.Value.Should().Be(span.ToString());
}
[Test]
public void should_return_timespan_when_getting_string_from_db()
{
var span = TimeSpan.FromMilliseconds(10);
Subject.Parse(span.ToString()).Should().Be(span);
}
[Test]
public void should_return_zero_timespan_for_db_null_value_when_getting_from_db()
{
Subject.Parse(null).Should().Be(TimeSpan.Zero);
}
}

View file

@ -2,18 +2,17 @@
using System.Data; using System.Data;
using Dapper; using Dapper;
namespace NzbDrone.Core.Datastore.Converters namespace NzbDrone.Core.Datastore.Converters;
{
public class DapperTimeSpanConverter : SqlMapper.TypeHandler<TimeSpan>
{
public override void SetValue(IDbDataParameter parameter, TimeSpan value)
{
parameter.Value = value.ToString();
}
public override TimeSpan Parse(object value) public class TimeSpanConverter : SqlMapper.TypeHandler<TimeSpan>
{ {
return TimeSpan.Parse((string)value); public override void SetValue(IDbDataParameter parameter, TimeSpan value)
} {
parameter.Value = value.ToString();
}
public override TimeSpan Parse(object value)
{
return value is string str ? TimeSpan.Parse(str) : TimeSpan.Zero;
} }
} }

View file

@ -217,7 +217,6 @@ private static void RegisterMappers()
SqlMapper.RemoveTypeMap(typeof(DateTime)); SqlMapper.RemoveTypeMap(typeof(DateTime));
SqlMapper.AddTypeHandler(new DapperUtcConverter()); SqlMapper.AddTypeHandler(new DapperUtcConverter());
SqlMapper.AddTypeHandler(new DapperTimeSpanConverter());
SqlMapper.AddTypeHandler(new DapperQualityIntConverter()); SqlMapper.AddTypeHandler(new DapperQualityIntConverter());
SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter<List<QualityProfileQualityItem>>(new QualityIntConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter<List<QualityProfileQualityItem>>(new QualityIntConverter()));
SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter<List<ProfileFormatItem>>(new CustomFormatIntConverter())); SqlMapper.AddTypeHandler(new EmbeddedDocumentConverter<List<ProfileFormatItem>>(new CustomFormatIntConverter()));
@ -241,6 +240,9 @@ private static void RegisterMappers()
SqlMapper.RemoveTypeMap(typeof(Guid)); SqlMapper.RemoveTypeMap(typeof(Guid));
SqlMapper.RemoveTypeMap(typeof(Guid?)); SqlMapper.RemoveTypeMap(typeof(Guid?));
SqlMapper.AddTypeHandler(new GuidConverter()); SqlMapper.AddTypeHandler(new GuidConverter());
SqlMapper.RemoveTypeMap(typeof(TimeSpan));
SqlMapper.RemoveTypeMap(typeof(TimeSpan?));
SqlMapper.AddTypeHandler(new TimeSpanConverter());
SqlMapper.AddTypeHandler(new CommandConverter()); SqlMapper.AddTypeHandler(new CommandConverter());
SqlMapper.AddTypeHandler(new SystemVersionConverter()); SqlMapper.AddTypeHandler(new SystemVersionConverter());
} }