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

93 lines
2.1 KiB
C#
Raw Normal View History

2013-02-04 00:10:15 +00:00
using System;
2013-02-23 19:38:25 +00:00
using System.Collections.Generic;
2013-02-17 00:50:04 +00:00
using System.IO;
2013-02-04 00:10:15 +00:00
using System.Linq;
using NUnit.Framework;
2013-02-04 04:18:59 +00:00
using NzbDrone.Common;
2013-02-04 00:10:15 +00:00
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Test.Framework
{
2013-02-23 19:38:25 +00:00
public abstract class ObjectDbTest<TSubject, TModel> : ObjectDbTest
where TSubject : class
where TModel : ModelBase, new()
{
2013-02-23 19:38:25 +00:00
private TSubject _subject;
protected BasicRepository<TModel> Storage { get; private set; }
2013-02-23 19:38:25 +00:00
protected IList<TModel> AllStoredModels
{
2013-02-23 19:38:25 +00:00
get
{
return Storage.All().ToList();
}
}
2013-02-23 19:38:25 +00:00
protected TModel StoredModel
{
get
{
return Storage.All().Single();
}
}
[SetUp]
public void CoreTestSetup()
{
_subject = null;
2013-02-23 19:38:25 +00:00
Storage = Mocker.Resolve<BasicRepository<TModel>>();
}
protected TSubject Subject
{
get
{
if (_subject == null)
{
_subject = Mocker.Resolve<TSubject>();
}
return _subject;
}
}
}
2013-02-04 00:10:15 +00:00
public abstract class ObjectDbTest : CoreTest
{
2013-02-17 01:52:40 +00:00
private IObjectDatabase _db;
protected IObjectDatabase Db
2013-02-04 00:10:15 +00:00
{
get
{
if (_db == null)
throw new InvalidOperationException("Test object database doesn't exists. Make sure you call WithRealDb() if you intend to use an actual database.");
return _db;
}
}
2013-02-23 19:38:25 +00:00
private void WithObjectDb(bool memory = true)
2013-02-04 00:10:15 +00:00
{
_db = new SiaqoDbFactory(new DiskProvider(), new EnvironmentProvider()).Create(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Guid.NewGuid().ToString()));
2013-02-04 00:10:15 +00:00
Mocker.SetConstant(Db);
}
2013-02-23 19:38:25 +00:00
[SetUp]
public void SetupReadDb()
{
WithObjectDb();
}
2013-02-04 00:10:15 +00:00
[TearDown]
public void ObjectDbTearDown()
{
if (_db != null)
{
_db.Dispose();
}
}
}
}