2011-11-26 02:06:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2013-02-02 19:39:27 +00:00
|
|
|
|
using Db4objects.Db4o.IO;
|
2012-02-12 00:01:52 +00:00
|
|
|
|
using NUnit.Framework;
|
2013-01-19 23:55:58 +00:00
|
|
|
|
using NzbDrone.Core.Datastore;
|
2011-11-18 04:36:37 +00:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2012-02-12 00:01:52 +00:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2011-11-13 07:27:16 +00:00
|
|
|
|
using NzbDrone.Test.Common;
|
|
|
|
|
using PetaPoco;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Framework
|
|
|
|
|
{
|
2013-02-02 19:39:27 +00:00
|
|
|
|
public abstract class CoreTest : TestBase
|
2011-11-13 07:27:16 +00:00
|
|
|
|
{
|
2013-01-19 23:55:58 +00:00
|
|
|
|
private string _dbTemplateName;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void CoreTestSetup()
|
|
|
|
|
{
|
2013-02-02 19:39:27 +00:00
|
|
|
|
if (NCrunch.Framework.NCrunchEnvironment.NCrunchIsResident())
|
2013-01-20 01:41:12 +00:00
|
|
|
|
{
|
|
|
|
|
_dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_dbTemplateName = "db_template.sdf";
|
|
|
|
|
}
|
2013-02-02 19:39:27 +00:00
|
|
|
|
|
2013-01-19 23:55:58 +00:00
|
|
|
|
CreateDataBaseTemplate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IDatabase GetEmptyDatabase(string fileName = "")
|
2011-11-13 07:27:16 +00:00
|
|
|
|
{
|
2013-01-19 23:55:58 +00:00
|
|
|
|
Console.WriteLine("====================DataBase====================");
|
|
|
|
|
Console.WriteLine("Cloning database from template.");
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(fileName))
|
2011-11-13 07:27:16 +00:00
|
|
|
|
{
|
2013-01-19 23:55:58 +00:00
|
|
|
|
fileName = Guid.NewGuid() + ".sdf";
|
2011-11-13 07:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 23:55:58 +00:00
|
|
|
|
File.Copy(_dbTemplateName, fileName);
|
2011-11-14 00:22:18 +00:00
|
|
|
|
|
2013-01-19 23:55:58 +00:00
|
|
|
|
var connectionString = ConnectionFactory.GetConnectionString(fileName);
|
|
|
|
|
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("====================DataBase====================");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine();
|
2011-11-14 00:22:18 +00:00
|
|
|
|
|
2013-01-19 23:55:58 +00:00
|
|
|
|
return database;
|
2011-11-13 07:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 23:55:58 +00:00
|
|
|
|
private void CreateDataBaseTemplate()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Creating an empty PetaPoco database");
|
|
|
|
|
var connectionString = ConnectionFactory.GetConnectionString(_dbTemplateName);
|
|
|
|
|
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
|
|
|
|
|
database.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-02-05 06:34:36 +00:00
|
|
|
|
|
2011-11-26 02:06:40 +00:00
|
|
|
|
private IDatabase _db;
|
|
|
|
|
protected IDatabase Db
|
2011-11-13 07:27:16 +00:00
|
|
|
|
{
|
2011-11-26 02:06:40 +00:00
|
|
|
|
get
|
2011-11-13 07:27:16 +00:00
|
|
|
|
{
|
2011-11-26 02:06:40 +00:00
|
|
|
|
if (_db == null)
|
|
|
|
|
throw new InvalidOperationException("Test db doesn't exists. Make sure you call WithRealDb() if you intend to use an actual database.");
|
|
|
|
|
|
|
|
|
|
return _db;
|
2011-11-13 07:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 19:39:27 +00:00
|
|
|
|
private IObjectDbSession _objDb;
|
|
|
|
|
protected IObjectDbSession ObjDb
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_objDb == null)
|
|
|
|
|
throw new InvalidOperationException("Test object database doesn't exists. Make sure you call WithRealDb() if you intend to use an actual database.");
|
|
|
|
|
|
|
|
|
|
return _objDb;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-13 07:27:16 +00:00
|
|
|
|
protected void WithRealDb()
|
|
|
|
|
{
|
2013-01-19 23:55:58 +00:00
|
|
|
|
_db = GetEmptyDatabase();
|
2011-11-13 07:27:16 +00:00
|
|
|
|
Mocker.SetConstant(Db);
|
|
|
|
|
}
|
2011-11-18 04:36:37 +00:00
|
|
|
|
|
2013-02-02 20:22:46 +00:00
|
|
|
|
protected void WithObjectDb(bool memory = true)
|
2013-02-02 19:39:27 +00:00
|
|
|
|
{
|
2013-02-02 20:22:46 +00:00
|
|
|
|
if (memory)
|
|
|
|
|
{
|
|
|
|
|
_objDb = new ObjectDbSessionFactory().Create(new PagingMemoryStorage());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_objDb = new ObjectDbSessionFactory().Create(dbName: Guid.NewGuid().ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 19:39:27 +00:00
|
|
|
|
Mocker.SetConstant(ObjDb);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-02 01:33:17 +00:00
|
|
|
|
protected static ProgressNotification MockNotification
|
2011-11-18 04:36:37 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new ProgressNotification("Mock notification");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-12 00:01:52 +00:00
|
|
|
|
|
2012-04-30 01:24:24 +00:00
|
|
|
|
protected static void ThrowException()
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("This is a message for test exception");
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-12 00:01:52 +00:00
|
|
|
|
[TearDown]
|
|
|
|
|
public void CoreTestTearDown()
|
|
|
|
|
{
|
|
|
|
|
ConfigProvider.ClearCache();
|
2013-01-19 23:55:58 +00:00
|
|
|
|
|
|
|
|
|
if (_db != null && _db.Connection != null && File.Exists(_db.Connection.Database))
|
|
|
|
|
{
|
|
|
|
|
var file = _db.Connection.Database;
|
|
|
|
|
_db.Dispose();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
File.Delete(file);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (IOException) { }
|
|
|
|
|
}
|
2013-02-02 19:39:27 +00:00
|
|
|
|
|
|
|
|
|
if (_objDb != null)
|
|
|
|
|
{
|
|
|
|
|
_objDb.Dispose();
|
|
|
|
|
}
|
2012-02-12 00:01:52 +00:00
|
|
|
|
}
|
2011-11-13 07:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|