From 64a3e1caf041e28d6f2052de0a7251222ea01291 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 19 Jan 2013 15:55:58 -0800 Subject: [PATCH] cleaned up CoreTest base class. --- NzbDrone.Core.Test/Framework/CoreTest.cs | 64 +++++++++++++------ NzbDrone.Core.Test/Framework/TestDbHelper.cs | 40 +----------- ...isodeProviderTest_DeleteInvalidEpisodes.cs | 63 ++++++++---------- .../ProviderTests/IndexerProviderTest.cs | 10 ++- .../ProviderTests/MediaFileProviderTest.cs | 24 +++---- .../ProviderTests/QualityTypeProviderTest.cs | 28 +++----- .../ProviderTests/SceneMappingProviderTest.cs | 12 ++-- NzbDrone.Core.Test/QualityProfileTest.cs | 44 +++++-------- NzbDrone.Core.Test/dbBenchmark.cs | 11 ++-- NzbDrone.Web/NzbDrone.Web.csproj | 7 ++ NzbDrone.Web/Web.config | 19 ++++++ NzbDrone.Web/packages.config | 2 + 12 files changed, 154 insertions(+), 170 deletions(-) diff --git a/NzbDrone.Core.Test/Framework/CoreTest.cs b/NzbDrone.Core.Test/Framework/CoreTest.cs index 14bbe9828..b8742e9bf 100644 --- a/NzbDrone.Core.Test/Framework/CoreTest.cs +++ b/NzbDrone.Core.Test/Framework/CoreTest.cs @@ -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) + _dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf"; + CreateDataBaseTemplate(); + } + + private IDatabase GetEmptyDatabase(string fileName = "") + { + Console.WriteLine("====================DataBase===================="); + Console.WriteLine("Cloning database from template."); + + if (String.IsNullOrWhiteSpace(fileName)) { - try - { - File.Delete(file); - } - catch { } + fileName = Guid.NewGuid() + ".sdf"; } -/* //Delete App_data folder - var appData = new EnvironmentProvider().GetAppDataPath(); + File.Copy(_dbTemplateName, fileName); - if (Directory.Exists(appData)) - { - //Directory.Delete(appData, true); - }*/ + var connectionString = ConnectionFactory.GetConnectionString(fileName); + var database = ConnectionFactory.GetPetaPocoDb(connectionString); - TestDbHelper.CreateDataBaseTemplate(); + 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) { } + } } } } diff --git a/NzbDrone.Core.Test/Framework/TestDbHelper.cs b/NzbDrone.Core.Test/Framework/TestDbHelper.cs index 3be24eb44..ce6f9a1f8 100644 --- a/NzbDrone.Core.Test/Framework/TestDbHelper.cs +++ b/NzbDrone.Core.Test/Framework/TestDbHelper.cs @@ -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(); - } + } } \ No newline at end of file diff --git a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs index 70c185136..2f4ce89a7 100644 --- a/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs +++ b/NzbDrone.Core.Test/ProviderTests/EpisodeProviderTests/EpisodeProviderTest_DeleteInvalidEpisodes.cs @@ -27,7 +27,7 @@ public void Delete_None_Valid_TvDbEpisodeId() All() .With(l => l.Language = new TvdbLanguage(0, "eng", "a")) .Build(); - + var fakeSeries = Builder.CreateNew() .With(c => c.SeriesId = seriesId) @@ -38,19 +38,18 @@ public void Delete_None_Valid_TvDbEpisodeId() .With(e => e.TvDbEpisodeId = tvDbSeries.First().Id) .Build(); - - var db = TestDbHelper.GetEmptyDatabase(); - Mocker.SetConstant(db); - db.Insert(fakeSeries); - db.Insert(fakeEpisode); + WithRealDb(); + + Db.Insert(fakeSeries); + Db.Insert(fakeEpisode); //Act Mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert - var result = db.Fetch(); + var result = Db.Fetch(); result.Should().HaveCount(1); } @@ -75,19 +74,18 @@ public void Delete_None_TvDbEpisodeId_is_zero() .With(e => e.TvDbEpisodeId = 0) .Build(); - - var db = TestDbHelper.GetEmptyDatabase(); - Mocker.SetConstant(db); - db.Insert(fakeSeries); - db.Insert(fakeEpisode); + WithRealDb(); + + Db.Insert(fakeSeries); + Db.Insert(fakeEpisode); //Act Mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert - var result = db.Fetch(); + var result = Db.Fetch(); 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().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert - var result = db.Fetch(); + var result = Db.Fetch(); result.Should().HaveCount(1); } @@ -151,19 +146,18 @@ public void Delete_TvDbId() .With(e => e.TvDbEpisodeId = 300) .Build(); - - var db = TestDbHelper.GetEmptyDatabase(); - Mocker.SetConstant(db); - db.Insert(fakeSeries); - db.Insert(fakeEpisode); + WithRealDb(); + + Db.Insert(fakeSeries); + Db.Insert(fakeEpisode); //Act Mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert - var result = db.Fetch(); + var result = Db.Fetch(); result.Should().HaveCount(0); } @@ -203,24 +197,23 @@ public void Delete_TvDbId_multiple_series() .With(e => e.TvDbEpisodeId = 300) .Build(); - - var db = TestDbHelper.GetEmptyDatabase(); - Mocker.SetConstant(db); - db.Insert(fakeSeries); - db.Insert(fakeEpisode); - db.Insert(otherFakeSeries); - db.Insert(otherFakeEpisode); + WithRealDb(); + + Db.Insert(fakeSeries); + Db.Insert(fakeEpisode); + Db.Insert(otherFakeSeries); + Db.Insert(otherFakeEpisode); //Act Mocker.Resolve().DeleteEpisodesNotInTvdb(fakeSeries, tvDbSeries); //Assert - var result = db.Fetch(); + var result = Db.Fetch(); result.Should().HaveCount(1); } - + [Test] public void should_not_do_anything_if_episode_list_is_empty() { diff --git a/NzbDrone.Core.Test/ProviderTests/IndexerProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/IndexerProviderTest.cs index c76057a04..bfb906ebf 100644 --- a/NzbDrone.Core.Test/ProviderTests/IndexerProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/IndexerProviderTest.cs @@ -24,9 +24,7 @@ public class IndexerProviderTest : CoreTest [Test] public void Init_indexer_test() { - - - Mocker.SetConstant(TestDbHelper.GetEmptyDatabase()); + WithRealDb(); Mocker.SetConstant>(new List { Mocker.Resolve() }); @@ -46,7 +44,7 @@ public void Init_indexer_test() [Test] public void Init_indexer_with_disabled_job() { - Mocker.SetConstant(TestDbHelper.GetEmptyDatabase()); + WithRealDb(); Mocker.SetConstant>(new List { Mocker.Resolve() }); @@ -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>(new List { Mocker.Resolve() }); @@ -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>(new List { Mocker.Resolve() }); diff --git a/NzbDrone.Core.Test/ProviderTests/MediaFileProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/MediaFileProviderTest.cs index 58b34fd3a..cf4275220 100644 --- a/NzbDrone.Core.Test/ProviderTests/MediaFileProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/MediaFileProviderTest.cs @@ -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().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().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().Delete(1); - var result = database.Fetch(); + var result = Db.Fetch(); //Assert result.Should().HaveCount(9); diff --git a/NzbDrone.Core.Test/ProviderTests/QualityTypeProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/QualityTypeProviderTest.cs index 211c7078a..f11662e6f 100644 --- a/NzbDrone.Core.Test/ProviderTests/QualityTypeProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/QualityTypeProviderTest.cs @@ -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(); @@ -45,11 +46,8 @@ public void SetupDefault_should_add_all_profiles() [Test] 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(); @@ -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.CreateListOfSize(6) .Build(); var ids = new List { 1 }; - db.InsertMany(fakeQualityTypes); + Db.InsertMany(fakeQualityTypes); var result = Mocker.Resolve().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.CreateListOfSize(6) .Build(); var ids = new List { 1, 2 }; - db.InsertMany(fakeQualityTypes); + Db.InsertMany(fakeQualityTypes); var result = Mocker.Resolve().GetList(ids); diff --git a/NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs index 90cf36b2c..e959b5385 100644 --- a/NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/SceneMappingProviderTest.cs @@ -25,6 +25,8 @@ public void Setup() { Mocker.GetMock().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 diff --git a/NzbDrone.Core.Test/QualityProfileTest.cs b/NzbDrone.Core.Test/QualityProfileTest.cs index 8c1b91d9e..39776532b 100644 --- a/NzbDrone.Core.Test/QualityProfileTest.cs +++ b/NzbDrone.Core.Test/QualityProfileTest.cs @@ -17,11 +17,16 @@ namespace NzbDrone.Core.Test // ReSharper disable InconsistentNaming public class QualityProfileTest : CoreTest { + [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(id); + var id = Convert.ToInt32(Db.Insert(testProfile)); + var fetch = Db.SingleOrDefault(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(id); + var id = Convert.ToInt32(Db.Insert(testProfile)); + var fetch = Db.SingleOrDefault(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(id); + var id = Convert.ToInt32(Db.Insert(testProfile)); + var currentProfile = Db.SingleOrDefault(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.CreateNew().Build(); series.QualityProfileId = profileId; - database.Insert(testProfile); - database.Insert(series); + Db.Insert(testProfile); + Db.Insert(series); - var result = database.Fetch(); + var result = Db.Fetch(); result.Should().HaveCount(1); - var profile = database.SingleOrDefault(result[0].QualityProfileId); + var profile = Db.SingleOrDefault(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(); @@ -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(); diff --git a/NzbDrone.Core.Test/dbBenchmark.cs b/NzbDrone.Core.Test/dbBenchmark.cs index 2b0161461..e3da5cf16 100644 --- a/NzbDrone.Core.Test/dbBenchmark.cs +++ b/NzbDrone.Core.Test/dbBenchmark.cs @@ -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.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); } diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index 6fdcc3518..961bb6fab 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -100,6 +100,13 @@ False ..\packages\MiniProfiler.2.0.2\lib\net40\MiniProfiler.dll + + False + ..\packages\Nancy.0.15.3\lib\net40\Nancy.dll + + + ..\packages\Nancy.Hosting.Aspnet.0.15.3\lib\net40\Nancy.Hosting.Aspnet.dll + False ..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll diff --git a/NzbDrone.Web/Web.config b/NzbDrone.Web/Web.config index 8c66df141..f4fbd3b20 100644 --- a/NzbDrone.Web/Web.config +++ b/NzbDrone.Web/Web.config @@ -41,6 +41,7 @@ + @@ -81,4 +82,22 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NzbDrone.Web/packages.config b/NzbDrone.Web/packages.config index 0d7d3f721..f191e21de 100644 --- a/NzbDrone.Web/packages.config +++ b/NzbDrone.Web/packages.config @@ -26,6 +26,8 @@ + +