cleaned up test db path for tests.

This commit is contained in:
Keivan Beigi 2013-06-27 18:03:04 -07:00
parent 02c175950b
commit dde91569ac
15 changed files with 123 additions and 133 deletions

View File

@ -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
WorkingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create), "NzbDrone");
StartUpPath = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
SystemTemp = Path.GetTempPath();
if (!Directory.Exists(WorkingDirectory))
{
var path = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
return path;
Directory.CreateDirectory(WorkingDirectory);
}
}
public String SystemTemp
{
get
{
return Path.GetTempPath();
}
}
public string WorkingDirectory { get; private set; }
public string StartUpPath { get; private set; }
public String SystemTemp { get; private set; }
}
}

View File

@ -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;

View File

@ -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<IAppDirectoryInfo>().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)
{
}
}
}
}
}

View File

@ -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<IAppDirectoryInfo>(new AppDirectoryInfo());
}
[Test]

View File

@ -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<IMediaFileService>().Verify(c => c.Add(result), Times.Once());
}

View File

@ -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
{

View File

@ -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)
{
_migrationController = migrationController;
container.Register(c => c.Resolve<IDbFactory>().Create());
container.Register<ILogRepository>(c =>
{
var db = c.Resolve<IDbFactory>().Create(MigrationType.Log);
return new LogRepository(db, c.Resolve<IMessageAggregator>());
});
}
public IDatabase Create(string dbPath, MigrationType migrationType = MigrationType.Main)
public DbFactory(IMigrationController migrationController, IAppDirectoryInfo appDirectoryInfo)
{
_migrationController = migrationController;
_appDirectoryInfo = appDirectoryInfo;
}
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);

View File

@ -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();

View File

@ -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<IDbFactory>().Create(dbPath));
}
[SetUp]
public void SmokeTestSetup()
{
Container = MainAppContainerBuilder.BuildContainer();
Container.Register(typeof(IAppDirectoryInfo), new IntegrationTestDirectoryInfo());
InitDatabase();
DbFactory.RegisterDatabase(Container);
var taskManagerMock = new Mock<ITaskManager>();
taskManagerMock.Setup(c => c.GetPending()).Returns(new List<ScheduledTask>());

View File

@ -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; }
}
}

View File

@ -90,6 +90,7 @@
<Compile Include="Client\SeriesClient.cs" />
<Compile Include="CommandIntegerationTests.cs" />
<Compile Include="IndexerIntegrationFixture.cs" />
<Compile Include="IntegrationTestDirectoryInfo.cs" />
<Compile Include="QualityProfileIntegrationTest.cs" />
<Compile Include="ReleaseIntegrationTest.cs" />
<Compile Include="IntegrationTest.cs" />

View File

@ -105,22 +105,11 @@ 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()
{
if (OsInfo.IsLinux)
@ -143,6 +132,8 @@ namespace NzbDrone.Test.Common
Mocker.GetMock<IAppDirectoryInfo>()
.SetupGet(c => c.WorkingDirectory)
.Returns(VirtualPath);
TestDirectoryInfo = Mocker.GetMock<IAppDirectoryInfo>().Object;
}
protected string GetTestFilePath(string fileName)

View File

@ -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);

View File

@ -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<IUpdateService>().Execute(new ApplicationUpdateCommand());
}
catch (Exception e)
{
logger.ErrorException("Application update failed.", e);
}
*/
DbFactory.RegisterDatabase(container);
container.Resolve<Router>().Route(args);
}
catch (Exception e)
{
logger.FatalException("Epic Fail " + e.Message, e);
Logger.FatalException("Epic Fail " + e.Message, e);
}
}

View File

@ -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<NamingConfig>), typeof(BasicRepository<NamingConfig>));
Container.Register<INancyBootstrapper, NancyBootstrapper>();
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<IDbFactory>().Create(IAppDirectoryInfo.GetNzbDroneDatabase()));
Container.Register<ILogRepository>(c =>
{
var db = c.Resolve<IDbFactory>().Create(IAppDirectoryInfo.GetLogDatabase(), MigrationType.Log);
return new LogRepository(db, c.Resolve<IMessageAggregator>());
});
}
}
}