1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Core.Test/Framework/SqlCeTest.cs

146 lines
3.7 KiB
C#
Raw Normal View History

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