1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

cleaned up CoreTest base class.

This commit is contained in:
kay.one 2013-01-19 15:55:58 -08:00
parent 77bb790672
commit 64a3e1caf0
12 changed files with 154 additions and 170 deletions

View File

@ -1,7 +1,7 @@
using System;
using System.IO;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Test.Common;
@ -11,30 +11,46 @@ namespace NzbDrone.Core.Test.Framework
{
public class CoreTest : TestBase
{
static CoreTest()
private string _dbTemplateName;
[SetUp]
public void CoreTestSetup()
{
//Delete old db files
var oldDbFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sdf", SearchOption.AllDirectories);
foreach (var file in oldDbFiles)
{
try
{
File.Delete(file);
}
catch { }
_dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf";
CreateDataBaseTemplate();
}
/* //Delete App_data folder
var appData = new EnvironmentProvider().GetAppDataPath();
if (Directory.Exists(appData))
private IDatabase GetEmptyDatabase(string fileName = "")
{
//Directory.Delete(appData, true);
}*/
Console.WriteLine("====================DataBase====================");
Console.WriteLine("Cloning database from template.");
TestDbHelper.CreateDataBaseTemplate();
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = Guid.NewGuid() + ".sdf";
}
File.Copy(_dbTemplateName, fileName);
var connectionString = ConnectionFactory.GetConnectionString(fileName);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
Console.WriteLine("====================DataBase====================");
Console.WriteLine();
Console.WriteLine();
return database;
}
private void CreateDataBaseTemplate()
{
Console.WriteLine("Creating an empty PetaPoco database");
var connectionString = ConnectionFactory.GetConnectionString(_dbTemplateName);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
database.Dispose();
}
private IDatabase _db;
protected IDatabase Db
@ -50,7 +66,7 @@ protected IDatabase Db
protected void WithRealDb()
{
_db = TestDbHelper.GetEmptyDatabase();
_db = GetEmptyDatabase();
Mocker.SetConstant(Db);
}
@ -71,6 +87,18 @@ protected static void ThrowException()
public void CoreTestTearDown()
{
ConfigProvider.ClearCache();
if (_db != null && _db.Connection != null && File.Exists(_db.Connection.Database))
{
var file = _db.Connection.Database;
_db.Dispose();
try
{
File.Delete(file);
}
catch (IOException) { }
}
}
}
}

View File

@ -16,44 +16,6 @@ namespace NzbDrone.Core.Test.Framework
{
internal static class TestDbHelper
{
private static readonly string dbTemplateName;
static TestDbHelper()
{
dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf";
}
internal static string ConnectionString { get; private set; }
internal static IDatabase GetEmptyDatabase(string fileName = "")
{
Console.WriteLine("====================DataBase====================");
Console.WriteLine("Cloning database from template.");
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = Guid.NewGuid() + ".sdf";
}
File.Copy(dbTemplateName, fileName);
ConnectionString = ConnectionFactory.GetConnectionString(fileName);
var database = ConnectionFactory.GetPetaPocoDb(ConnectionString);
Console.WriteLine("====================DataBase====================");
Console.WriteLine();
Console.WriteLine();
return database;
}
internal static void CreateDataBaseTemplate()
{
Console.WriteLine("Creating an empty PetaPoco database");
var connectionString = ConnectionFactory.GetConnectionString(dbTemplateName);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
database.Dispose();
}
}
}

View File

@ -40,17 +40,16 @@ public void Delete_None_Valid_TvDbEpisodeId()
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
WithRealDb();
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}
@ -77,17 +76,16 @@ public void Delete_None_TvDbEpisodeId_is_zero()
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
WithRealDb();
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}
@ -112,19 +110,16 @@ public void Delete_None_TvDbEpisodeId_is_null()
.With(e => e.TvDbEpisodeId = null)
.Build();
WithRealDb();
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}
@ -153,17 +148,16 @@ public void Delete_TvDbId()
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
WithRealDb();
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(0);
}
@ -205,19 +199,18 @@ public void Delete_TvDbId_multiple_series()
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
WithRealDb();
db.Insert(fakeSeries);
db.Insert(fakeEpisode);
db.Insert(otherFakeSeries);
db.Insert(otherFakeEpisode);
Db.Insert(fakeSeries);
Db.Insert(fakeEpisode);
Db.Insert(otherFakeSeries);
Db.Insert(otherFakeEpisode);
//Act
Mocker.Resolve<EpisodeProvider>().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries);
//Assert
var result = db.Fetch<Episode>();
var result = Db.Fetch<Episode>();
result.Should().HaveCount(1);
}

View File

@ -24,9 +24,7 @@ public class IndexerProviderTest : CoreTest
[Test]
public void Init_indexer_test()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<MockIndexer>() });
@ -46,7 +44,7 @@ public void Init_indexer_test()
[Test]
public void Init_indexer_with_disabled_job()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<MockIndexer>() });
@ -65,7 +63,7 @@ public void Init_indexer_with_disabled_job()
[Test]
public void Init_indexer_should_enable_indexer_that_is_enabled_by_default()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<DefaultEnabledIndexer>() });
@ -82,7 +80,7 @@ public void Init_indexer_should_enable_indexer_that_is_enabled_by_default()
[Test]
public void Init_indexer_should_not_enable_indexer_that_is_not_enabled_by_default()
{
Mocker.SetConstant(TestDbHelper.GetEmptyDatabase());
WithRealDb();
Mocker.SetConstant<IEnumerable<IndexerBase>>(new List<IndexerBase> { Mocker.Resolve<MockIndexer>() });

View File

@ -37,13 +37,11 @@ public void get_series_files()
var database = TestDbHelper.GetEmptyDatabase();
WithRealDb();
database.InsertMany(firstSeriesFiles);
database.InsertMany(secondSeriesFiles);
Mocker.SetConstant(database);
Db.InsertMany(firstSeriesFiles);
Db.InsertMany(secondSeriesFiles);
var result = Mocker.Resolve<MediaFileProvider>().GetSeriesFiles(12);
@ -69,13 +67,10 @@ public void get_season_files()
.Build();
WithRealDb();
var database = TestDbHelper.GetEmptyDatabase();
database.InsertMany(firstSeriesFiles);
database.InsertMany(secondSeriesFiles);
Mocker.SetConstant(database);
Db.InsertMany(firstSeriesFiles);
Db.InsertMany(secondSeriesFiles);
var result = Mocker.Resolve<MediaFileProvider>().GetSeasonFiles(12, 1);
@ -157,13 +152,12 @@ public void DeleteEpisodeFile()
.Build();
var database = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(database);
database.InsertMany(episodeFiles);
WithRealDb();
Db.InsertMany(episodeFiles);
//Act
Mocker.Resolve<MediaFileProvider>().Delete(1);
var result = database.Fetch<EpisodeFile>();
var result = Db.Fetch<EpisodeFile>();
//Assert
result.Should().HaveCount(9);

View File

@ -16,14 +16,15 @@ namespace NzbDrone.Core.Test.ProviderTests
// ReSharper disable InconsistentNaming
public class QualityTypeProviderTest : CoreTest
{
[SetUp]
public void SetuUp()
{
WithRealDb();
}
[Test]
public void SetupDefault_should_add_all_profiles()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
Mocker.Resolve<QualityTypeProvider>();
@ -46,10 +47,7 @@ public void SetupDefault_should_add_all_profiles()
public void SetupDefault_already_exists_should_insert_missing()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
db.Insert(new QualityType { QualityTypeId = 1, Name = "SDTV", MinSize = 0, MaxSize = 100 });
Db.Insert(new QualityType { QualityTypeId = 1, Name = "SDTV", MinSize = 0, MaxSize = 100 });
Mocker.Resolve<QualityTypeProvider>();
@ -63,16 +61,12 @@ public void SetupDefault_already_exists_should_insert_missing()
[Test]
public void GetList_single_quality_type()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
var fakeQualityTypes = Builder<QualityType>.CreateListOfSize(6)
.Build();
var ids = new List<int> { 1 };
db.InsertMany(fakeQualityTypes);
Db.InsertMany(fakeQualityTypes);
var result = Mocker.Resolve<QualityTypeProvider>().GetList(ids);
@ -84,16 +78,12 @@ public void GetList_single_quality_type()
[Test]
public void GetList_multiple_quality_type()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
var fakeQualityTypes = Builder<QualityType>.CreateListOfSize(6)
.Build();
var ids = new List<int> { 1, 2 };
db.InsertMany(fakeQualityTypes);
Db.InsertMany(fakeQualityTypes);
var result = Mocker.Resolve<QualityTypeProvider>().GetList(ids);

View File

@ -25,6 +25,8 @@ public void Setup()
{
Mocker.GetMock<ConfigProvider>().SetupGet(s => s.ServiceRootUrl)
.Returns("http://services.nzbdrone.com");
WithRealDb();
}
private void WithValidJson()
@ -75,6 +77,7 @@ public void GetSeriesId_exists()
.With(f => f.SceneName = "laworder")
.Build();
Db.Insert(fakeMap);
//Act
@ -96,6 +99,7 @@ public void GetSceneName_null()
.With(f => f.SceneName = "laworder")
.Build();
Db.Insert(fakeMap);
//Act
@ -147,6 +151,8 @@ public void GetSceneName_multiple_clean_names()
.With(f => f.SeasonNumber = -1)
.Build();
Db.Insert(fakeMap);
Db.Insert(fakeMap2);
@ -177,8 +183,6 @@ public void GetSceneName_should_be_null_when_seasonNumber_does_not_match()
[Test]
public void UpdateMappings_should_add_all_mappings_to_database()
{
//Setup
WithRealDb();
WithValidJson();
//Act
@ -200,7 +204,6 @@ public void UpdateMappings_should_overwrite_existing_mappings()
.With(f => f.SceneName = "laworder")
.Build();
WithRealDb();
WithValidJson();
Db.Insert(fakeMap);
@ -223,7 +226,6 @@ public void UpdateMappings_should_not_delete_if_csv_download_fails()
.With(f => f.SceneName = "laworder")
.Build();
WithRealDb();
WithErrorDownloadingJson();
Db.Insert(fakeMap);
@ -246,7 +248,6 @@ public void UpdateIfEmpty_should_not_update_if_count_is_not_zero()
.With(f => f.SceneName = "laworder")
.Build();
WithRealDb();
Db.Insert(fakeMap);
//Act
@ -260,7 +261,6 @@ public void UpdateIfEmpty_should_not_update_if_count_is_not_zero()
public void UpdateIfEmpty_should_update_if_count_is_zero()
{
//Setup
WithRealDb();
WithValidJson();
//Act

View File

@ -17,11 +17,16 @@ namespace NzbDrone.Core.Test
// ReSharper disable InconsistentNaming
public class QualityProfileTest : CoreTest<QualityProvider>
{
[SetUp]
public void SetUp()
{
WithRealDb();
}
[Test]
public void Test_Storage()
{
//Arrange
var database = TestDbHelper.GetEmptyDatabase();
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -30,8 +35,8 @@ public void Test_Storage()
};
var id = Convert.ToInt32(database.Insert(testProfile));
var fetch = database.SingleOrDefault<QualityProfile>(id);
var id = Convert.ToInt32(Db.Insert(testProfile));
var fetch = Db.SingleOrDefault<QualityProfile>(id);
Assert.AreEqual(id, fetch.QualityProfileId);
@ -45,7 +50,6 @@ public void Test_Storage()
public void Test_Storage_no_allowed()
{
//Arrange
var database = TestDbHelper.GetEmptyDatabase();
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -53,8 +57,8 @@ public void Test_Storage_no_allowed()
};
var id = Convert.ToInt32(database.Insert(testProfile));
var fetch = database.SingleOrDefault<QualityProfile>(id);
var id = Convert.ToInt32(Db.Insert(testProfile));
var fetch = Db.SingleOrDefault<QualityProfile>(id);
Assert.AreEqual(id, fetch.QualityProfileId);
@ -67,11 +71,6 @@ public void Test_Storage_no_allowed()
[Test]
public void Update_Success()
{
//Arrange
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -79,8 +78,8 @@ public void Update_Success()
};
var id = Convert.ToInt32(db.Insert(testProfile));
var currentProfile = db.SingleOrDefault<QualityProfile>(id);
var id = Convert.ToInt32(Db.Insert(testProfile));
var currentProfile = Db.SingleOrDefault<QualityProfile>(id);
//Update
@ -99,9 +98,6 @@ public void Update_Success()
[Test]
public void Test_Series_Quality()
{
//Arrange
var database = TestDbHelper.GetEmptyDatabase();
var testProfile = new QualityProfile
{
Name = Guid.NewGuid().ToString(),
@ -110,18 +106,18 @@ public void Test_Series_Quality()
};
var profileId = Convert.ToInt32(database.Insert(testProfile));
var profileId = Convert.ToInt32(Db.Insert(testProfile));
var series = Builder<Series>.CreateNew().Build();
series.QualityProfileId = profileId;
database.Insert(testProfile);
database.Insert(series);
Db.Insert(testProfile);
Db.Insert(series);
var result = database.Fetch<Series>();
var result = Db.Fetch<Series>();
result.Should().HaveCount(1);
var profile = database.SingleOrDefault<QualityProfile>(result[0].QualityProfileId);
var profile = Db.SingleOrDefault<QualityProfile>(result[0].QualityProfileId);
Assert.AreEqual(profileId, result[0].QualityProfileId);
Assert.AreEqual(testProfile.Name, profile.Name);
}
@ -131,10 +127,6 @@ public void Test_Series_Quality()
public void SetupInitial_should_add_two_profiles()
{
var db = TestDbHelper.GetEmptyDatabase();
Mocker.SetConstant(db);
Mocker.Resolve<QualityProvider>();
@ -152,8 +144,6 @@ public void SetupInitial_should_add_two_profiles()
//We don't want to keep adding them back if a user deleted them on purpose.
public void SetupInitial_should_skip_if_any_profile_exists()
{
WithRealDb();
InitiateSubject();
var profiles = Subject.All();

View File

@ -32,7 +32,8 @@ public class DbBenchmark : CoreTest
[TestFixtureSetUp]
public void Setup()
{
db = TestDbHelper.GetEmptyDatabase();
WithRealDb();
int currentFileId = 0;
@ -42,7 +43,7 @@ public void Setup()
Allowed = new List<QualityTypes> { QualityTypes.DVD, QualityTypes.Bluray1080p },
Cutoff = QualityTypes.DVD
};
db.Insert(qulityProfile);
Db.Insert(qulityProfile);
foreach (var _seriesId in seriesIds)
{
@ -52,7 +53,7 @@ public void Setup()
.With(s => s.Monitored = true)
.Build();
db.Insert(series);
Db.Insert(series);
foreach (var _seasonNumber in seasonsNumbers)
{
@ -94,8 +95,8 @@ public void Setup()
}
db.InsertMany(episodes);
db.InsertMany(files);
Db.InsertMany(episodes);
Db.InsertMany(files);
}

View File

@ -100,6 +100,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MiniProfiler.2.0.2\lib\net40\MiniProfiler.dll</HintPath>
</Reference>
<Reference Include="Nancy, Version=0.15.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Nancy.0.15.3\lib\net40\Nancy.dll</HintPath>
</Reference>
<Reference Include="Nancy.Hosting.Aspnet">
<HintPath>..\packages\Nancy.Hosting.Aspnet.0.15.3\lib\net40\Nancy.Hosting.Aspnet.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>

View File

@ -41,6 +41,7 @@
<remove name="UrlRoutingHandler" />
<add name="MvcHttpHandler" preCondition="integratedMode" verb="" path=".mvc" type="System.Web.Mvc.MvcHttpHandler" />
<add name="CassetteHttpHandler" path="cassette.axd" preCondition="integratedMode" verb="*" allowPathInfo="true" type="Cassette.Aspnet.CassetteHttpHandler, Cassette.Aspnet" />
<add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
</handlers>
<directoryBrowse enabled="false" />
<staticContent>
@ -81,4 +82,22 @@
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
<location path="api" allowOverride="false">
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="0" />
</customHeaders>
</httpProtocol>
<handlers>
<add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
</handlers>
<httpErrors errorMode="Detailed" />
</system.webServer>
</location>
</configuration>

View File

@ -26,6 +26,8 @@
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
<package id="MiniProfiler" version="2.0.2" />
<package id="MiniProfiler.MVC3" version="2.0.2" />
<package id="Nancy" version="0.15.3" targetFramework="net40" />
<package id="Nancy.Hosting.Aspnet" version="0.15.3" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" />
<package id="NLog" version="2.0.0.2000" />
<package id="ServiceStack.Text" version="3.9.27" targetFramework="net40" />