mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-12 11:15:43 +02:00
Added ReferenceDataProvider to provide lookups (and refreshing) of IsDaily - which will check if the series is a daily series.
This commit is contained in:
parent
4180684a82
commit
e16f83c433
@ -97,6 +97,7 @@
|
||||
<Compile Include="JobTests\BacklogSearchJobTest.cs" />
|
||||
<Compile Include="JobTests\BannerDownloadJobTest.cs" />
|
||||
<Compile Include="JobTests\RecentBacklogSearchJobTest.cs" />
|
||||
<Compile Include="ProviderTests\ReferenceDataProviderTest.cs" />
|
||||
<Compile Include="ProviderTests\NotificationProviderTests\NotificationProviderFixture.cs" />
|
||||
<Compile Include="ProviderTests\SearchProviderTests\PerformSearchFixture.cs" />
|
||||
<Compile Include="ProviderTests\SearchProviderTests\ProcessSearchResultsFixture.cs" />
|
||||
|
187
NzbDrone.Core.Test/ProviderTests/ReferenceDataProviderTest.cs
Normal file
187
NzbDrone.Core.Test/ProviderTests/ReferenceDataProviderTest.cs
Normal file
@ -0,0 +1,187 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Providers;
|
||||
using NzbDrone.Core.Providers.Core;
|
||||
using NzbDrone.Core.Repository;
|
||||
using NzbDrone.Core.Repository.Quality;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Test.Common;
|
||||
using NzbDrone.Test.Common.AutoMoq;
|
||||
using PetaPoco;
|
||||
|
||||
namespace NzbDrone.Core.Test.ProviderTests
|
||||
{
|
||||
[TestFixture]
|
||||
// ReSharper disable InconsistentNaming
|
||||
public class ReferenceDataProviderTest : CoreTest
|
||||
{
|
||||
private string validSeriesIds = String.Format("1{0}2{0}3{0}4{0}5", Environment.NewLine);
|
||||
private string invalidSeriesIds = String.Format("1{0}2{0}NaN{0}4{0}5", Environment.NewLine);
|
||||
|
||||
[Test]
|
||||
public void GetDailySeriesIds_should_return_list_of_int_when_all_are_valid()
|
||||
{
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Returns(validSeriesIds);
|
||||
|
||||
//Act
|
||||
var result = Mocker.Resolve<ReferenceDataProvider>().GetDailySeriesIds();
|
||||
|
||||
//Assert
|
||||
result.Should().HaveCount(5);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetDailySeriesIds_should_return_list_of_int_when_any_are_valid()
|
||||
{
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Returns(invalidSeriesIds);
|
||||
|
||||
//Act
|
||||
var result = Mocker.Resolve<ReferenceDataProvider>().GetDailySeriesIds();
|
||||
|
||||
//Assert
|
||||
result.Should().HaveCount(4);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetDailySeriesIds_should_return_empty_list_of_int_when_server_is_unavailable()
|
||||
{
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Throws(new Exception());
|
||||
|
||||
//Act
|
||||
var result = Mocker.Resolve<ReferenceDataProvider>().GetDailySeriesIds();
|
||||
|
||||
//Assert
|
||||
result.Should().HaveCount(0);
|
||||
ExceptionVerification.ExcpectedWarns(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsDailySeries_should_return_true()
|
||||
{
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Returns(validSeriesIds);
|
||||
|
||||
//Act
|
||||
var result = Mocker.Resolve<ReferenceDataProvider>().IsSeriesDaily(1);
|
||||
|
||||
//Assert
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsDailySeries_should_return_false()
|
||||
{
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Returns(validSeriesIds);
|
||||
|
||||
//Act
|
||||
var result = Mocker.Resolve<ReferenceDataProvider>().IsSeriesDaily(10);
|
||||
|
||||
//Assert
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateDailySeries_should_update_series_that_match_daily_series_list()
|
||||
{
|
||||
WithRealDb();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateListOfSize(5)
|
||||
.All()
|
||||
.With(s => s.IsDaily = false)
|
||||
.Build();
|
||||
|
||||
Db.InsertMany(fakeSeries);
|
||||
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Returns(validSeriesIds);
|
||||
|
||||
//Act
|
||||
Mocker.Resolve<ReferenceDataProvider>().UpdateDailySeries();
|
||||
|
||||
//Assert
|
||||
var result = Db.Fetch<Series>();
|
||||
|
||||
result.Where(s => s.IsDaily).Should().HaveCount(5);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateDailySeries_should_update_series_should_skip_series_that_dont_match()
|
||||
{
|
||||
WithRealDb();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateListOfSize(5)
|
||||
.All()
|
||||
.With(s => s.IsDaily = false)
|
||||
.TheFirst(1)
|
||||
.With(s => s.SeriesId = 10)
|
||||
.TheNext(1)
|
||||
.With(s => s.SeriesId = 11)
|
||||
.TheNext(1)
|
||||
.With(s => s.SeriesId = 12)
|
||||
.Build();
|
||||
|
||||
Db.InsertMany(fakeSeries);
|
||||
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Returns(validSeriesIds);
|
||||
|
||||
//Act
|
||||
Mocker.Resolve<ReferenceDataProvider>().UpdateDailySeries();
|
||||
|
||||
//Assert
|
||||
var result = Db.Fetch<Series>();
|
||||
|
||||
result.Where(s => !s.IsDaily).Should().HaveCount(3);
|
||||
result.Where(s => s.IsDaily).Should().HaveCount(2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateDailySeries_should_update_series_should_not_overwrite_existing_isDaily()
|
||||
{
|
||||
WithRealDb();
|
||||
|
||||
var fakeSeries = Builder<Series>.CreateListOfSize(5)
|
||||
.All()
|
||||
.With(s => s.IsDaily = false)
|
||||
.TheFirst(1)
|
||||
.With(s => s.SeriesId = 10)
|
||||
.With(s => s.IsDaily = true)
|
||||
.TheNext(1)
|
||||
.With(s => s.SeriesId = 11)
|
||||
.TheNext(1)
|
||||
.With(s => s.SeriesId = 12)
|
||||
.Build();
|
||||
|
||||
Db.InsertMany(fakeSeries);
|
||||
|
||||
//Setup
|
||||
Mocker.GetMock<HttpProvider>().Setup(s => s.DownloadString("http://www.nzbdrone.com/DailySeries.csv"))
|
||||
.Returns(validSeriesIds);
|
||||
|
||||
//Act
|
||||
Mocker.Resolve<ReferenceDataProvider>().UpdateDailySeries();
|
||||
|
||||
//Assert
|
||||
var result = Db.Fetch<Series>();
|
||||
|
||||
result.Where(s => s.IsDaily).Should().HaveCount(3);
|
||||
result.Where(s => !s.IsDaily).Should().HaveCount(2);
|
||||
}
|
||||
}
|
||||
}
|
21
NzbDrone.Core/Datastore/Migrations/Migration20111125.cs
Normal file
21
NzbDrone.Core/Datastore/Migrations/Migration20111125.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using Migrator.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migrations
|
||||
{
|
||||
|
||||
[Migration(20111125)]
|
||||
public class Migration2011125 : Migration
|
||||
{
|
||||
public override void Up()
|
||||
{
|
||||
Database.AddColumn("Series", "IsDaily", DbType.Boolean, ColumnProperty.Null);
|
||||
}
|
||||
|
||||
public override void Down()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -199,6 +199,7 @@
|
||||
<Compile Include="Datastore\MigrationLogger.cs" />
|
||||
<Compile Include="Datastore\MigrationsHelper.cs" />
|
||||
<Compile Include="Datastore\CustomeMapper.cs" />
|
||||
<Compile Include="Datastore\Migrations\Migration20111125.cs" />
|
||||
<Compile Include="Datastore\Migrations\Migration20111112.cs" />
|
||||
<Compile Include="Datastore\Migrations\Migration20111011.cs" />
|
||||
<Compile Include="Datastore\Migrations\Migration20110909.cs" />
|
||||
@ -256,6 +257,7 @@
|
||||
<Compile Include="Providers\MisnamedProvider.cs" />
|
||||
<Compile Include="Providers\PostDownloadProvider.cs" />
|
||||
<Compile Include="Providers\QualityTypeProvider.cs" />
|
||||
<Compile Include="Providers\ReferenceDataProvider.cs" />
|
||||
<Compile Include="Providers\SearchProvider.cs" />
|
||||
<Compile Include="Providers\SmtpProvider.cs" />
|
||||
<Compile Include="Providers\TwitterProvider.cs" />
|
||||
|
@ -31,7 +31,7 @@ public string Name
|
||||
|
||||
public int DefaultInterval
|
||||
{
|
||||
get { return 1440; } //Daily
|
||||
get { return 720; } //Daily
|
||||
}
|
||||
|
||||
public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
|
||||
|
73
NzbDrone.Core/Providers/ReferenceDataProvider.cs
Normal file
73
NzbDrone.Core/Providers/ReferenceDataProvider.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Providers.Core;
|
||||
using PetaPoco;
|
||||
|
||||
namespace NzbDrone.Core.Providers
|
||||
{
|
||||
public class ReferenceDataProvider
|
||||
{
|
||||
private readonly IDatabase _database;
|
||||
private readonly HttpProvider _httpProvider;
|
||||
|
||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public ReferenceDataProvider(IDatabase database, HttpProvider httpProvider)
|
||||
{
|
||||
_database = database;
|
||||
_httpProvider = httpProvider;
|
||||
}
|
||||
|
||||
public virtual void UpdateDailySeries()
|
||||
{
|
||||
//Update all series in DB
|
||||
//DailySeries.csv
|
||||
|
||||
var seriesIds = GetDailySeriesIds();
|
||||
|
||||
var dailySeriesString = String.Join(", ", seriesIds);
|
||||
var sql = String.Format("UPDATE Series SET IsDaily = 1 WHERE SeriesId in ({0})", dailySeriesString);
|
||||
|
||||
_database.Execute(sql);
|
||||
}
|
||||
|
||||
public virtual bool IsSeriesDaily(int seriesId)
|
||||
{
|
||||
return GetDailySeriesIds().Contains(seriesId);
|
||||
}
|
||||
|
||||
public List<int> GetDailySeriesIds()
|
||||
{
|
||||
try
|
||||
{
|
||||
var dailySeries = _httpProvider.DownloadString("http://www.nzbdrone.com/DailySeries.csv");
|
||||
|
||||
var seriesIds = new List<int>();
|
||||
|
||||
using (var reader = new StringReader(dailySeries))
|
||||
{
|
||||
string line;
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
{
|
||||
int seriesId;
|
||||
|
||||
if (Int32.TryParse(line, out seriesId))
|
||||
seriesIds.Add(seriesId);
|
||||
}
|
||||
}
|
||||
|
||||
return seriesIds;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Logger.WarnException("Failed to get Daily Series", ex);
|
||||
return new List<int>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -41,6 +41,8 @@ public class Series
|
||||
|
||||
public string BannerUrl { get; set; }
|
||||
|
||||
public bool IsDaily { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this <see cref="Series"/> is hidden.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user