1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 16:02:29 +01:00
Sonarr/NzbDrone.Core.Test/Framework/SqlCeTest.cs

147 lines
3.7 KiB
C#
Raw Normal View History

2011-11-26 03:06:40 +01:00
using System;
using System.IO;
using NUnit.Framework;
2013-02-17 00:29:21 +01:00
using NzbDrone.Common;
2013-02-24 07:48:52 +01:00
using NzbDrone.Core.Configuration;
2013-01-20 00:55:58 +01:00
using NzbDrone.Core.Datastore;
2011-11-18 05:36:37 +01:00
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
2011-11-13 08:27:16 +01:00
using NzbDrone.Test.Common;
using PetaPoco;
namespace NzbDrone.Core.Test.Framework
{
2013-02-02 21:54:03 +01:00
public abstract class SqlCeTest : CoreTest
2011-11-13 08:27:16 +01:00
{
2013-01-20 00:55:58 +01:00
private string _dbTemplateName;
[SetUp]
public void CoreTestSetup()
{
2013-02-17 00:29:21 +01:00
if (EnvironmentProvider.IsMono)
{
throw new IgnoreException("SqlCe is not supported in mono.");
}
2013-02-16 20:23:31 +01:00
if (NCrunch.Framework.NCrunchEnvironment.NCrunchIsResident())
{
_dbTemplateName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()) + ".sdf";
}
else
{
_dbTemplateName = "db_template.sdf";
}
2013-01-20 00:55:58 +01:00
CreateDataBaseTemplate();
}
private IDatabase GetEmptyDatabase(string fileName = "")
2011-11-13 08:27:16 +01:00
{
2013-01-20 00:55:58 +01:00
Console.WriteLine("====================DataBase====================");
Console.WriteLine("Cloning database from template.");
if (String.IsNullOrWhiteSpace(fileName))
2011-11-13 08:27:16 +01:00
{
2013-01-20 00:55:58 +01:00
fileName = Guid.NewGuid() + ".sdf";
2011-11-13 08:27:16 +01:00
}
2013-01-20 00:55:58 +01:00
File.Copy(_dbTemplateName, fileName);
2013-01-20 00:55:58 +01:00
var connectionString = ConnectionFactory.GetConnectionString(fileName);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
Console.WriteLine("====================DataBase====================");
Console.WriteLine();
Console.WriteLine();
2013-01-20 00:55:58 +01:00
return database;
2011-11-13 08:27:16 +01:00
}
2013-01-20 00:55:58 +01: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 03:06:40 +01:00
private IDatabase _db;
protected IDatabase Db
2011-11-13 08:27:16 +01:00
{
2011-11-26 03:06:40 +01:00
get
2011-11-13 08:27:16 +01:00
{
2011-11-26 03:06:40 +01: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 08:27:16 +01:00
}
}
2011-11-13 08:27:16 +01:00
protected void WithRealDb()
{
2013-02-17 17:23:53 +01:00
if (EnvironmentProvider.IsMono)
{
throw new IgnoreException("SqlCe is not supported in mono.");
}
2013-01-20 00:55:58 +01:00
_db = GetEmptyDatabase();
2011-11-13 08:27:16 +01:00
Mocker.SetConstant(Db);
}
2011-11-18 05:36:37 +01:00
2012-04-30 03:24:24 +02:00
[TearDown]
public void CoreTestTearDown()
{
2013-02-24 07:48:52 +01:00
ConfigService.ClearCache();
2013-01-20 00:55:58 +01:00
2013-02-17 06:39:17 +01:00
if (EnvironmentProvider.IsMono)
{
return;
}
2013-01-20 00:55:58 +01: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 08:27:16 +01:00
}
2013-02-05 05:07:07 +01: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 08:27:16 +01:00
}