1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Fixed issue #1445 where sabnzbd has changed the time format reported for downloads

This commit is contained in:
Dion Woolley 2016-09-02 00:39:03 +12:00
parent 6ac9e5ec18
commit a6a7732cd5
2 changed files with 53 additions and 8 deletions

View File

@ -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<SabnzbdQueueItem>(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<SabnzbdQueueItem>(thing), Throws.TypeOf<ArgumentException>());
}
[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<SabnzbdQueueItem>(thing);
item.Timeleft.Should().Be(new TimeSpan(40, 12, 14));
}
}
}

View File

@ -14,17 +14,17 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 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) public override bool CanConvert(Type objectType)