Radarr/NzbDrone.Core.Test/Framework/SqlCeTest.cs

146 lines
3.7 KiB
C#
Raw Normal View History

2011-11-26 02:06:40 +00:00
using System;
using System.IO;
using NUnit.Framework;
2013-02-16 23:29:21 +00:00
using NzbDrone.Common;
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
{
2013-02-02 20:54:03 +00:00
public abstract class SqlCeTest : CoreTest
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-16 23:29:21 +00:00
if (EnvironmentProvider.IsMono)
{
throw new IgnoreException("SqlCe is not supported in mono.");
}
2013-02-16 19:23:31 +00:00
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
}
}
2011-11-13 07:27:16 +00:00
protected void WithRealDb()
{
2013-02-17 16:23:53 +00:00
if (EnvironmentProvider.IsMono)
{
throw new IgnoreException("SqlCe is not supported in mono.");
}
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
2012-04-30 01:24:24 +00:00
[TearDown]
public void CoreTestTearDown()
{
ConfigProvider.ClearCache();
2013-01-19 23:55:58 +00:00
2013-02-17 05:39:17 +00:00
if (EnvironmentProvider.IsMono)
{
return;
}
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) { }
}
}
2011-11-13 07:27:16 +00:00
}
2013-02-05 04:07:07 +00:00
public abstract class SqlCeTest<TSubject> : SqlCeTest where TSubject : class
{
private TSubject _subject;
protected TSubject Subject
{
get
{
if (_subject == null)
{
_subject = Mocker.Resolve<TSubject>();
}
return _subject;
}
}
protected void InitiateSubject()
{
_subject = Mocker.Resolve<TSubject>();
}
}
2011-11-13 07:27:16 +00:00
}