From a6a7732cd5df129fa1eac68d285b67c922270fae Mon Sep 17 00:00:00 2001 From: Dion Woolley Date: Fri, 2 Sep 2016 00:39:03 +1200 Subject: [PATCH 1/2] Fixed issue #1445 where sabnzbd has changed the time format reported for downloads --- .../SabnzbdQueueTimeConverterFixture.cs | 45 +++++++++++++++++++ .../SabnzbdQueueTimeConverter.cs | 16 +++---- 2 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs new file mode 100644 index 000000000..229e3a8b6 --- /dev/null +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using FluentAssertions; +using Newtonsoft.Json; +using NUnit.Framework; +using NzbDrone.Core.Download.Clients.Sabnzbd; + +namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests.JsonConvertersTests +{ + [TestFixture] + public class SabnzbdQueueTimeConverterFixture + { + private const string QUERY = + "{0} status : 'Downloading', mb : 1000, filename : 'Droned.S01E01.Pilot.1080p.WEB-DL-DRONE', priority : 0, cat : 'tv', mbleft : 10, percentage : 90, nzo_id : 'sabnzbd_nzb12345', timeleft : '{1}' {2}"; + + [TestCase("0:0:1")] + [TestCase("0:0:0:1")] + public void valid_time_formats_should_be_parsed_correctly(string time) + { + var thing = string.Format(QUERY, "{", time, "}"); + var item = JsonConvert.DeserializeObject(thing); + item.Timeleft.Should().Be(new TimeSpan(0, 0, 1)); + } + + [TestCase("0:1")] + [TestCase("1")] + [TestCase("0:0:0:0:1")] + public void invalid_time_formats_should_throw_an_exception(string time) + { + var thing = string.Format(QUERY, "{", time, "}"); + Assert.That(() => JsonConvert.DeserializeObject(thing), Throws.TypeOf()); + } + + [TestCase("40:12:14")] + [TestCase("1:16:12:14")] + public void valid_time_formats_of_equal_value_should_be_parsed_the_same(string time) + { + var thing = string.Format(QUERY, "{", time, "}"); + var item = JsonConvert.DeserializeObject(thing); + item.Timeleft.Should().Be(new TimeSpan(40, 12, 14)); + } + } +} diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/JsonConverters/SabnzbdQueueTimeConverter.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/JsonConverters/SabnzbdQueueTimeConverter.cs index 42719b28c..c5274efb1 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/JsonConverters/SabnzbdQueueTimeConverter.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/JsonConverters/SabnzbdQueueTimeConverter.cs @@ -14,17 +14,17 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd.JsonConverters public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - var split = reader.Value.ToString().Split(':'); + var split = reader.Value.ToString().Split(':').Select(int.Parse).ToArray(); - if (split.Count() != 3) + switch (split.Count()) { - throw new ArgumentException("Expected 0:0:0 format, but received: " + reader.Value); + case 4: + return new TimeSpan(split[0] * 24 + split[1], split[2], split[3]); + case 3: + return new TimeSpan(split[0], split[1], split[2]); + default: + throw new ArgumentException("Expected either 0:0:0:0 or 0:0:0 format, but received: " + reader.Value); } - - return new TimeSpan(int.Parse(split[0]), // hours - int.Parse(split[1]), // minutes - int.Parse(split[2]) // seconds - ); } public override bool CanConvert(Type objectType) From 97d0ddb6e960d221856adafe41cc41d02e686cb0 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sat, 3 Sep 2016 11:41:34 +0200 Subject: [PATCH 2/2] Fixed: Sabnzbd 1.1.0 rc4 queue api changed time format. --- .../SabnzbdQueueTimeConverterFixture.cs | 29 ++++++++++--------- .../NzbDrone.Core.Test.csproj | 1 + 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs b/src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs index 229e3a8b6..b7241c479 100644 --- a/src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs +++ b/src/NzbDrone.Core.Test/Download/DownloadClientTests/SabnzbdTests/JsonConvertersTests/SabnzbdQueueTimeConverterFixture.cs @@ -12,32 +12,35 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabnzbdTests.JsonConve [TestFixture] public class SabnzbdQueueTimeConverterFixture { - private const string QUERY = - "{0} status : 'Downloading', mb : 1000, filename : 'Droned.S01E01.Pilot.1080p.WEB-DL-DRONE', priority : 0, cat : 'tv', mbleft : 10, percentage : 90, nzo_id : 'sabnzbd_nzb12345', timeleft : '{1}' {2}"; + private const string QUERY = "{{ timeleft : '{0}' }}"; - [TestCase("0:0:1")] - [TestCase("0:0:0:1")] - public void valid_time_formats_should_be_parsed_correctly(string time) + [TestCase("0:0:0", 0)] + [TestCase("0:1:59", 119)] + [TestCase("0:59:59", 3599)] + [TestCase("1:0:0", 3600)] + [TestCase("1:0:0:1", 24 * 3600 + 1)] + [TestCase("40:12:14", 40 * 3600 + 12 * 60 + 14)] + [TestCase("1:16:12:14", 40 * 3600 + 12 * 60 + 14)] + public void valid_time_formats_should_be_parsed_correctly(string time, int expectedSeconds) { - var thing = string.Format(QUERY, "{", time, "}"); + var thing = string.Format(QUERY, time); var item = JsonConvert.DeserializeObject(thing); - item.Timeleft.Should().Be(new TimeSpan(0, 0, 1)); + item.Timeleft.Should().Be(TimeSpan.FromSeconds(expectedSeconds)); } - [TestCase("0:1")] [TestCase("1")] + [TestCase("0:1")] [TestCase("0:0:0:0:1")] public void invalid_time_formats_should_throw_an_exception(string time) { - var thing = string.Format(QUERY, "{", time, "}"); + var thing = string.Format(QUERY, time); Assert.That(() => JsonConvert.DeserializeObject(thing), Throws.TypeOf()); } - [TestCase("40:12:14")] - [TestCase("1:16:12:14")] - public void valid_time_formats_of_equal_value_should_be_parsed_the_same(string time) + [Test] + public void should_support_pre_1_1_0rc4_format() { - var thing = string.Format(QUERY, "{", time, "}"); + var thing = string.Format(QUERY, "40:12:14"); var item = JsonConvert.DeserializeObject(thing); item.Timeleft.Should().Be(new TimeSpan(40, 12, 14)); } diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 26ae602a3..2f96f5d05 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -171,6 +171,7 @@ +