2013-02-04 00:10:15 +00:00
|
|
|
|
using System;
|
2013-02-23 19:38:25 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-03-25 03:51:32 +00:00
|
|
|
|
using System.IO;
|
2013-02-04 00:10:15 +00:00
|
|
|
|
using System.Linq;
|
2013-07-05 03:56:27 +00:00
|
|
|
|
using FluentMigrator.Runner;
|
2013-03-25 03:51:32 +00:00
|
|
|
|
using Marr.Data;
|
2013-02-04 00:10:15 +00:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-25 06:13:53 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
|
2013-02-04 00:10:15 +00:00
|
|
|
|
namespace NzbDrone.Core.Test.Framework
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public abstract class DbTest<TSubject, TModel> : DbTest
|
2013-02-23 19:38:25 +00:00
|
|
|
|
where TSubject : class
|
2013-02-18 07:59:43 +00:00
|
|
|
|
where TModel : ModelBase, new()
|
|
|
|
|
{
|
2013-02-23 19:38:25 +00:00
|
|
|
|
private TSubject _subject;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
|
|
protected BasicRepository<TModel> Storage { get; private set; }
|
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
protected IList<TModel> AllStoredModels => Storage.All().ToList();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
protected TModel StoredModel => Storage.All().Single();
|
2013-02-16 03:50:22 +00:00
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void CoreTestSetup()
|
|
|
|
|
{
|
|
|
|
|
_subject = null;
|
2013-02-23 19:38:25 +00:00
|
|
|
|
Storage = Mocker.Resolve<BasicRepository<TModel>>();
|
2013-02-16 03:50:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected TSubject Subject
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_subject == null)
|
|
|
|
|
{
|
|
|
|
|
_subject = Mocker.Resolve<TSubject>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _subject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-12 20:00:49 +00:00
|
|
|
|
[Category("DbTest")]
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public abstract class DbTest : CoreTest
|
2013-02-04 00:10:15 +00:00
|
|
|
|
{
|
2013-03-25 03:51:32 +00:00
|
|
|
|
private ITestDatabase _db;
|
2013-03-27 04:03:02 +00:00
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
protected virtual MigrationType MigrationType => MigrationType.Main;
|
2013-03-27 04:03:02 +00:00
|
|
|
|
|
2013-03-25 03:51:32 +00:00
|
|
|
|
protected ITestDatabase Db
|
2013-02-04 00:10:15 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_db == null)
|
|
|
|
|
throw new InvalidOperationException("Test object database doesn't exists. Make sure you call WithRealDb() if you intend to use an actual database.");
|
|
|
|
|
|
|
|
|
|
return _db;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-13 21:23:37 +00:00
|
|
|
|
protected virtual ITestDatabase WithTestDb(MigrationContext migrationContext)
|
2014-12-02 20:08:37 +00:00
|
|
|
|
{
|
|
|
|
|
var factory = Mocker.Resolve<DbFactory>();
|
2016-02-13 21:23:37 +00:00
|
|
|
|
var database = factory.Create(migrationContext);
|
2014-12-02 20:08:37 +00:00
|
|
|
|
Mocker.SetConstant(database);
|
|
|
|
|
|
2015-05-03 23:30:16 +00:00
|
|
|
|
switch (MigrationType)
|
|
|
|
|
{
|
|
|
|
|
case MigrationType.Main:
|
|
|
|
|
{
|
|
|
|
|
var mainDb = new MainDatabase(database);
|
|
|
|
|
|
|
|
|
|
Mocker.SetConstant<IMainDatabase>(mainDb);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MigrationType.Log:
|
|
|
|
|
{
|
|
|
|
|
var logDb = new LogDatabase(database);
|
|
|
|
|
|
|
|
|
|
Mocker.SetConstant<ILogDatabase>(logDb);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Invalid MigrationType");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-02 20:08:37 +00:00
|
|
|
|
var testDb = new TestDatabase(database);
|
|
|
|
|
|
|
|
|
|
return testDb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void SetupContainer()
|
2013-02-04 00:10:15 +00:00
|
|
|
|
{
|
2013-06-28 01:03:04 +00:00
|
|
|
|
WithTempAsAppPath();
|
2013-03-25 03:51:32 +00:00
|
|
|
|
|
2013-07-05 03:56:27 +00:00
|
|
|
|
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
|
|
|
|
|
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
|
|
|
|
|
|
2013-03-25 03:51:32 +00:00
|
|
|
|
MapRepository.Instance.EnableTraceLogging = true;
|
2013-02-04 00:10:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 19:38:25 +00:00
|
|
|
|
[SetUp]
|
2014-12-02 20:08:37 +00:00
|
|
|
|
public virtual void SetupDb()
|
2013-02-23 19:38:25 +00:00
|
|
|
|
{
|
2014-12-02 20:08:37 +00:00
|
|
|
|
SetupContainer();
|
2016-02-13 21:23:37 +00:00
|
|
|
|
_db = WithTestDb(new MigrationContext(MigrationType));
|
2013-02-23 19:38:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 03:51:32 +00:00
|
|
|
|
[TearDown]
|
|
|
|
|
public void TearDown()
|
|
|
|
|
{
|
2013-07-09 22:06:41 +00:00
|
|
|
|
if (TestFolderInfo != null && Directory.Exists(TestFolderInfo.AppDataFolder))
|
2013-03-25 03:51:32 +00:00
|
|
|
|
{
|
2013-07-05 04:43:28 +00:00
|
|
|
|
var files = Directory.GetFiles(TestFolderInfo.AppDataFolder);
|
2013-03-25 03:51:32 +00:00
|
|
|
|
|
2013-06-28 01:03:04 +00:00
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Delete(file);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2013-03-25 03:51:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-24 04:16:00 +00:00
|
|
|
|
}
|
2013-02-04 00:10:15 +00:00
|
|
|
|
}
|