From dde91569ac7ebba546949552a6c9968ccda3414f Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Thu, 27 Jun 2013 18:03:04 -0700 Subject: [PATCH] cleaned up test db path for tests. --- .../EnvironmentInfo/AppDirectoryInfo.cs | 27 +++++------ .../Instrumentation/LogglyTarget.cs | 6 --- NzbDrone.Core.Test/Framework/DbTest.cs | 36 +++++++-------- .../MediaCoverServiceFixture.cs | 4 +- .../ImportFileFixture.cs | 2 +- .../QualityProfileRepositoryFixture.cs | 1 - NzbDrone.Core/Datastore/DbFactory.cs | 45 +++++++++++++++++-- .../Framework/MigrationController.cs | 2 + NzbDrone.Integration.Test/IntegrationTest.cs | 31 ++----------- .../IntegrationTestDirectoryInfo.cs | 26 +++++++++++ .../NzbDrone.Integration.Test.csproj | 1 + NzbDrone.Test.Common/TestBase.cs | 17 ++----- NzbDrone.Update/Program.cs | 2 +- NzbDrone/AppMain.cs | 24 ++++------ NzbDrone/MainAppContainerBuilder.cs | 28 +----------- 15 files changed, 121 insertions(+), 131 deletions(-) create mode 100644 NzbDrone.Integration.Test/IntegrationTestDirectoryInfo.cs diff --git a/NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs b/NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs index 5c9a61ab8..d463be5a2 100644 --- a/NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs +++ b/NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs @@ -13,26 +13,23 @@ namespace NzbDrone.Common.EnvironmentInfo public class AppDirectoryInfo : IAppDirectoryInfo { - public string WorkingDirectory - { - get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create), "NzbDrone"); } - } - public string StartUpPath + public AppDirectoryInfo() { - get - { - var path = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; - return path; - } - } + WorkingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create), "NzbDrone"); + StartUpPath = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; + SystemTemp = Path.GetTempPath(); - public String SystemTemp - { - get + if (!Directory.Exists(WorkingDirectory)) { - return Path.GetTempPath(); + Directory.CreateDirectory(WorkingDirectory); } } + + public string WorkingDirectory { get; private set; } + + public string StartUpPath { get; private set; } + + public String SystemTemp { get; private set; } } } \ No newline at end of file diff --git a/NzbDrone.Common/Instrumentation/LogglyTarget.cs b/NzbDrone.Common/Instrumentation/LogglyTarget.cs index 532d8e761..ccb5e706f 100644 --- a/NzbDrone.Common/Instrumentation/LogglyTarget.cs +++ b/NzbDrone.Common/Instrumentation/LogglyTarget.cs @@ -11,7 +11,6 @@ namespace NzbDrone.Common.Instrumentation { public class LogglyTarget : TargetWithLayout { - private readonly IAppDirectoryInfo _appDirectoryInfo; private Logger _logger; public void Register(LogLevel minLevel) @@ -26,11 +25,6 @@ namespace NzbDrone.Common.Instrumentation LogManager.ReconfigExistingLoggers(); } - public LogglyTarget(IAppDirectoryInfo appDirectoryInfo) - { - _appDirectoryInfo = appDirectoryInfo; - } - protected override void InitializeTarget() { string apiKey = string.Empty; diff --git a/NzbDrone.Core.Test/Framework/DbTest.cs b/NzbDrone.Core.Test/Framework/DbTest.cs index b55cd1c17..896e7829d 100644 --- a/NzbDrone.Core.Test/Framework/DbTest.cs +++ b/NzbDrone.Core.Test/Framework/DbTest.cs @@ -5,6 +5,7 @@ using System.Linq; using Marr.Data; using Moq; using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore.Migration.Framework; @@ -63,9 +64,6 @@ namespace NzbDrone.Core.Test.Framework public abstract class DbTest : CoreTest { - - private string _dbName; - private ITestDatabase _db; private IDatabase _database; @@ -90,15 +88,15 @@ namespace NzbDrone.Core.Test.Framework } } - private void WithObjectDb(bool memory = true) + private void WithTestDb() { + WithTempAsAppPath(); - _dbName = DateTime.Now.Ticks.ToString() + ".db"; MapRepository.Instance.EnableTraceLogging = true; - var factory = new DbFactory(new MigrationController(new MigrationLogger(TestLogger))); - _database = factory.Create(_dbName, MigrationType); + var factory = new DbFactory(new MigrationController(new MigrationLogger(TestLogger)), Mocker.GetMock().Object); + _database = factory.Create(MigrationType); _db = new TestTestDatabase(_database); Mocker.SetConstant(_database); } @@ -106,27 +104,29 @@ namespace NzbDrone.Core.Test.Framework [SetUp] public void SetupReadDb() { - WithObjectDb(); + WithTestDb(); } [TearDown] public void TearDown() { - var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.db"); - - foreach (var file in files) + if (TestDirectoryInfo != null) { - try - { - File.Delete(file); - } - catch (Exception) - { + var files = Directory.GetFiles(TestDirectoryInfo.WorkingDirectory); + foreach (var file in files) + { + try + { + File.Delete(file); + } + catch (Exception) + { + + } } } } - } diff --git a/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs b/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs index 6566ee3c3..33cd7428f 100644 --- a/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs @@ -17,9 +17,7 @@ namespace NzbDrone.Core.Test.MediaCoverTests [SetUp] public void Setup() { - //Mocker.SetConstant(new HttpProvider(new IAppDirectoryInfo())); - //Mocker.SetConstant(new DiskProvider()); - Mocker.SetConstant(new AppDirectoryInfo()); + Mocker.SetConstant(new AppDirectoryInfo()); } [Test] diff --git a/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/ImportFileFixture.cs b/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/ImportFileFixture.cs index d9b58696e..6efbda31f 100644 --- a/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/ImportFileFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/ImportFileFixture.cs @@ -289,7 +289,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests result.Should().NotBeNull(); result.SeriesId.Should().Be(_fakeSeries.Id); result.Size.Should().Be(_fileSize); - result.DateAdded.Should().HaveDay(DateTime.Now.Day); + result.DateAdded.Should().HaveDay(DateTime.UtcNow.Day); Mocker.GetMock().Verify(c => c.Add(result), Times.Once()); } diff --git a/NzbDrone.Core.Test/TvTests/SeriesRepositoryTests/QualityProfileRepositoryFixture.cs b/NzbDrone.Core.Test/TvTests/SeriesRepositoryTests/QualityProfileRepositoryFixture.cs index 83eac638f..4829e3417 100644 --- a/NzbDrone.Core.Test/TvTests/SeriesRepositoryTests/QualityProfileRepositoryFixture.cs +++ b/NzbDrone.Core.Test/TvTests/SeriesRepositoryTests/QualityProfileRepositoryFixture.cs @@ -5,7 +5,6 @@ using NUnit.Framework; using NzbDrone.Core.Qualities; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Tv; -using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.TvTests.SeriesRepositoryTests { diff --git a/NzbDrone.Core/Datastore/DbFactory.cs b/NzbDrone.Core/Datastore/DbFactory.cs index 7b1fd6318..aad62e5c2 100644 --- a/NzbDrone.Core/Datastore/DbFactory.cs +++ b/NzbDrone.Core/Datastore/DbFactory.cs @@ -2,19 +2,25 @@ using System.Data.SQLite; using Marr.Data; using Marr.Data.Reflection; +using NzbDrone.Common.Composition; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore.Migration.Framework; +using NzbDrone.Common; +using NzbDrone.Core.Instrumentation; namespace NzbDrone.Core.Datastore { public interface IDbFactory { - IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main); + IDatabase Create(MigrationType migrationType = MigrationType.Main); } public class DbFactory : IDbFactory { private readonly IMigrationController _migrationController; + private readonly IAppDirectoryInfo _appDirectoryInfo; static DbFactory() { @@ -22,13 +28,46 @@ namespace NzbDrone.Core.Datastore TableMapping.Map(); } - public DbFactory(IMigrationController migrationController) + public static void RegisterDatabase(IContainer container) + { + container.Register(c => c.Resolve().Create()); + + container.Register(c => + { + var db = c.Resolve().Create(MigrationType.Log); + return new LogRepository(db, c.Resolve()); + }); + } + + public DbFactory(IMigrationController migrationController, IAppDirectoryInfo appDirectoryInfo) { _migrationController = migrationController; + _appDirectoryInfo = appDirectoryInfo; } - public IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main) + public IDatabase Create(MigrationType migrationType = MigrationType.Main) { + string dbPath; + + switch (migrationType) + { + case MigrationType.Main: + { + dbPath = _appDirectoryInfo.GetNzbDroneDatabase(); + break; + } + case MigrationType.Log: + { + dbPath = _appDirectoryInfo.GetLogDatabase(); + break; + } + default: + { + throw new ArgumentException("Invalid MigrationType"); + } + } + + var connectionString = GetConnectionString(dbPath); _migrationController.MigrateToLatest(connectionString, migrationType); diff --git a/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs b/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs index f07cb9535..2f3966af2 100644 --- a/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs +++ b/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs @@ -26,6 +26,8 @@ namespace NzbDrone.Core.Datastore.Migration.Framework { lock (MigrationCache) { + _announcer.Heading("Migrating " + connectionString); + if (MigrationCache.Contains(connectionString.ToLower())) return; var assembly = Assembly.GetExecutingAssembly(); diff --git a/NzbDrone.Integration.Test/IntegrationTest.cs b/NzbDrone.Integration.Test/IntegrationTest.cs index 0a38ad55c..a95f74015 100644 --- a/NzbDrone.Integration.Test/IntegrationTest.cs +++ b/NzbDrone.Integration.Test/IntegrationTest.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; +using System.Collections.Generic; using Moq; using NLog; using NLog.Config; @@ -9,7 +7,6 @@ using NUnit.Framework; using NzbDrone.Api; using NzbDrone.Api.Commands; using NzbDrone.Api.RootFolders; -using NzbDrone.Common; using NzbDrone.Common.Composition; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.Configuration; @@ -56,36 +53,14 @@ namespace NzbDrone.Integration.Test LogManager.ReconfigExistingLoggers(); } - private void InitDatabase() - { - Logger.Info("Registering Database..."); - - //TODO: move this to factory - var IAppDirectoryInfo = new AppDirectoryInfo(); - var appDataPath = IAppDirectoryInfo.GetAppDataPath(); - - if (!Directory.Exists(appDataPath)) - { - Directory.CreateDirectory(appDataPath); - } - - var dbPath = Path.Combine(IAppDirectoryInfo.WorkingDirectory, DateTime.Now.Ticks + ".db"); - - - Logger.Info("Working Folder: {0}", IAppDirectoryInfo.WorkingDirectory); - Logger.Info("Data Folder: {0}", IAppDirectoryInfo.GetAppDataPath()); - Logger.Info("DB Na: {0}", dbPath); - - - Container.Register(c => c.Resolve().Create(dbPath)); - } [SetUp] public void SmokeTestSetup() { Container = MainAppContainerBuilder.BuildContainer(); + Container.Register(typeof(IAppDirectoryInfo), new IntegrationTestDirectoryInfo()); - InitDatabase(); + DbFactory.RegisterDatabase(Container); var taskManagerMock = new Mock(); taskManagerMock.Setup(c => c.GetPending()).Returns(new List()); diff --git a/NzbDrone.Integration.Test/IntegrationTestDirectoryInfo.cs b/NzbDrone.Integration.Test/IntegrationTestDirectoryInfo.cs new file mode 100644 index 000000000..0a80eb9a1 --- /dev/null +++ b/NzbDrone.Integration.Test/IntegrationTestDirectoryInfo.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; +using NzbDrone.Common.EnvironmentInfo; + +namespace NzbDrone.Integration.Test +{ + public class IntegrationTestDirectoryInfo : IAppDirectoryInfo + { + public IntegrationTestDirectoryInfo() + { + SystemTemp = Path.GetTempPath(); + WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "integ_test", DateTime.Now.Ticks.ToString()); + + if (!Directory.Exists(WorkingDirectory)) + { + Directory.CreateDirectory(WorkingDirectory); + } + + StartUpPath = Directory.GetCurrentDirectory(); + } + + public string WorkingDirectory { get; private set; } + public string SystemTemp { get; private set; } + public string StartUpPath { get; private set; } + } +} \ No newline at end of file diff --git a/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj b/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj index df8c0433e..ed22089bc 100644 --- a/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj +++ b/NzbDrone.Integration.Test/NzbDrone.Integration.Test.csproj @@ -90,6 +90,7 @@ + diff --git a/NzbDrone.Test.Common/TestBase.cs b/NzbDrone.Test.Common/TestBase.cs index 4d0c2a30f..5611edf32 100644 --- a/NzbDrone.Test.Common/TestBase.cs +++ b/NzbDrone.Test.Common/TestBase.cs @@ -105,21 +105,10 @@ namespace NzbDrone.Test.Common catch (Exception) { } + } -/* if (TestContext.CurrentContext.Result.State == TestState.Failure || TestContext.CurrentContext.Result.State == TestState.Error) - { - var testName = TestContext.CurrentContext.Test.Name.ToLower(); - if (IAppDirectoryInfo.IsLinux && testName.Contains("windows")) - { - throw new IgnoreException("windows specific test"); - } - else if (testName.Contains("linux")) - { - throw new IgnoreException("linux specific test"); - } - }*/ - } + protected IAppDirectoryInfo TestDirectoryInfo { get; private set; } protected void WindowsOnly() { @@ -143,6 +132,8 @@ namespace NzbDrone.Test.Common Mocker.GetMock() .SetupGet(c => c.WorkingDirectory) .Returns(VirtualPath); + + TestDirectoryInfo = Mocker.GetMock().Object; } protected string GetTestFilePath(string fileName) diff --git a/NzbDrone.Update/Program.cs b/NzbDrone.Update/Program.cs index 1dcde521a..bb2013924 100644 --- a/NzbDrone.Update/Program.cs +++ b/NzbDrone.Update/Program.cs @@ -30,7 +30,7 @@ namespace NzbDrone.Update Console.WriteLine("Starting NzbDrone Update Client"); GlobalExceptionHandlers.Register(); - new LogglyTarget(new AppDirectoryInfo()).Register(LogLevel.Debug); + new LogglyTarget().Register(LogLevel.Debug); _container = UpdateContainerBuilder.Build(); logger.Info("Updating NzbDrone to version {0}", BuildInfo.Version); diff --git a/NzbDrone/AppMain.cs b/NzbDrone/AppMain.cs index b61335bfc..20122e9df 100644 --- a/NzbDrone/AppMain.cs +++ b/NzbDrone/AppMain.cs @@ -2,15 +2,15 @@ using System.Diagnostics; using System.Reflection; using NLog; -using NzbDrone.Common; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation; +using NzbDrone.Core.Datastore; namespace NzbDrone { public static class AppMain { - private static readonly Logger logger = LogManager.GetLogger("AppMain"); + private static readonly Logger Logger = LogManager.GetLogger("AppMain"); public static void Main(string[] args) @@ -19,10 +19,10 @@ namespace NzbDrone { GlobalExceptionHandlers.Register(); - new LogglyTarget(new AppDirectoryInfo()).Register(LogLevel.Warn); + new LogglyTarget().Register(LogLevel.Warn); - logger.Info("Starting NzbDrone Console. Version {0}", Assembly.GetExecutingAssembly().GetName().Version); + Logger.Info("Starting NzbDrone Console. Version {0}", Assembly.GetExecutingAssembly().GetName().Version); //Check if full version .NET is installed. @@ -32,7 +32,7 @@ namespace NzbDrone } catch (Exception) { - logger.Error("It looks like you don't have full version of .NET Framework installed. Press any key and you will be directed to the download page."); + Logger.Error("It looks like you don't have full version of .NET Framework installed. Press any key and you will be directed to the download page."); Console.Read(); try @@ -41,7 +41,7 @@ namespace NzbDrone } catch (Exception e) { - logger.Warn("Oops. can't start default browser. Please visit http://www.microsoft.com/download/en/details.aspx?id=17851 to download .NET Framework 4."); + Logger.Warn("Oops. can't start default browser. Please visit http://www.microsoft.com/download/en/details.aspx?id=17851 to download .NET Framework 4."); Console.ReadLine(); } @@ -50,20 +50,12 @@ namespace NzbDrone var container = MainAppContainerBuilder.BuildContainer(); - /*try - { - container.Resolve().Execute(new ApplicationUpdateCommand()); - } - catch (Exception e) - { - logger.ErrorException("Application update failed.", e); - } -*/ + DbFactory.RegisterDatabase(container); container.Resolve().Route(args); } catch (Exception e) { - logger.FatalException("Epic Fail " + e.Message, e); + Logger.FatalException("Epic Fail " + e.Message, e); } } diff --git a/NzbDrone/MainAppContainerBuilder.cs b/NzbDrone/MainAppContainerBuilder.cs index 82eec2093..2d6be14e9 100644 --- a/NzbDrone/MainAppContainerBuilder.cs +++ b/NzbDrone/MainAppContainerBuilder.cs @@ -1,11 +1,8 @@ -using System.IO; -using NLog; +using NLog; using Nancy.Bootstrapper; using NzbDrone.Api; using NzbDrone.Api.SignalR; -using NzbDrone.Common; using NzbDrone.Common.Composition; -using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; using NzbDrone.Core.Instrumentation; @@ -33,30 +30,9 @@ namespace NzbDrone Container.Register(typeof(IBasicRepository), typeof(BasicRepository)); Container.Register(); - - InitDatabase(); + } - private void InitDatabase() - { - Logger.Info("Registering Database..."); - - //TODO: move this to factory - var IAppDirectoryInfo = new AppDirectoryInfo(); - var appDataPath = IAppDirectoryInfo.GetAppDataPath(); - if (!Directory.Exists(appDataPath)) - { - Directory.CreateDirectory(appDataPath); - } - - Container.Register(c => c.Resolve().Create(IAppDirectoryInfo.GetNzbDroneDatabase())); - - Container.Register(c => - { - var db = c.Resolve().Create(IAppDirectoryInfo.GetLogDatabase(), MigrationType.Log); - return new LogRepository(db, c.Resolve()); - }); - } } } \ No newline at end of file