Merge branch 'pr/n1446_Awarua-' into develop

This commit is contained in:
Taloth Saldono 2016-09-03 11:48:39 +02:00
commit 4e051bfde2
3 changed files with 57 additions and 8 deletions

View File

@ -0,0 +1,48 @@
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 = "{{ timeleft : '{0}' }}";
[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 item = JsonConvert.DeserializeObject<SabnzbdQueueItem>(thing);
item.Timeleft.Should().Be(TimeSpan.FromSeconds(expectedSeconds));
}
[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);
Assert.That(() => JsonConvert.DeserializeObject<SabnzbdQueueItem>(thing), Throws.TypeOf<ArgumentException>());
}
[Test]
public void should_support_pre_1_1_0rc4_format()
{
var thing = string.Format(QUERY, "40:12:14");
var item = JsonConvert.DeserializeObject<SabnzbdQueueItem>(thing);
item.Timeleft.Should().Be(new TimeSpan(40, 12, 14));
}
}
}

View File

@ -171,6 +171,7 @@
<Compile Include="Download\DownloadClientTests\PneumaticProviderFixture.cs" /> <Compile Include="Download\DownloadClientTests\PneumaticProviderFixture.cs" />
<Compile Include="Download\DownloadClientTests\RTorrentTests\RTorrentFixture.cs" /> <Compile Include="Download\DownloadClientTests\RTorrentTests\RTorrentFixture.cs" />
<Compile Include="Download\DownloadClientTests\QBittorrentTests\QBittorrentFixture.cs" /> <Compile Include="Download\DownloadClientTests\QBittorrentTests\QBittorrentFixture.cs" />
<Compile Include="Download\DownloadClientTests\SabnzbdTests\JsonConvertersTests\SabnzbdQueueTimeConverterFixture.cs" />
<Compile Include="Download\DownloadClientTests\SabnzbdTests\SabnzbdFixture.cs" /> <Compile Include="Download\DownloadClientTests\SabnzbdTests\SabnzbdFixture.cs" />
<Compile Include="Download\DownloadClientTests\TransmissionTests\TransmissionFixture.cs" /> <Compile Include="Download\DownloadClientTests\TransmissionTests\TransmissionFixture.cs" />
<Compile Include="Download\DownloadClientTests\TransmissionTests\TransmissionFixtureBase.cs" /> <Compile Include="Download\DownloadClientTests\TransmissionTests\TransmissionFixtureBase.cs" />

View File

@ -14,17 +14,17 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd.JsonConverters
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)