Sonarr/NzbDrone.Core.Test/Framework/CoreTest.cs

147 lines
4.0 KiB
C#
Raw Normal View History

2011-11-26 02:06:40 +00:00
using System;
using System.IO;
using Db4objects.Db4o.IO;
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;
using NzbDrone.Core.Providers.Core;
2011-11-13 07:27:16 +00:00
using NzbDrone.Test.Common;
using PetaPoco;
namespace NzbDrone.Core.Test.Framework
{
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()
{
if (NCrunch.Framework.NCrunchEnvironment.NCrunchIsResident())
{
_dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf";
}
else
{
_dbTemplateName = "db_template.sdf";
}
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);
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();
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();
}
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
}
}
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
protected void WithObjectDb(bool memory = true)
{
if (memory)
{
_objDb = new ObjectDbSessionFactory().Create(new PagingMemoryStorage());
}
else
{
_objDb = new ObjectDbSessionFactory().Create(dbName: Guid.NewGuid().ToString());
}
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-04-30 01:24:24 +00:00
protected static void ThrowException()
{
throw new ApplicationException("This is a message for test exception");
}
[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) { }
}
if (_objDb != null)
{
_objDb.Dispose();
}
}
2011-11-13 07:27:16 +00:00
}
}