using System; using System.Collections.Generic; using System.Data; using System.Linq; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Datastore; using ServiceStack.OrmLite; namespace NzbDrone.Core.Test.Framework { public abstract class DbTest : DbTest where TSubject : class where TModel : ModelBase, new() { private TSubject _subject; protected BasicRepository Storage { get; private set; } protected IList AllStoredModels { get { return Storage.All().ToList(); } } protected TModel StoredModel { get { return Storage.All().Single(); } } [SetUp] public void CoreTestSetup() { _subject = null; Storage = Mocker.Resolve>(); } protected TSubject Subject { get { if (_subject == null) { _subject = Mocker.Resolve(); } return _subject; } } } public abstract class DbTest : CoreTest { private IDatabase _db; protected IDatabase Db { 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; } } private void WithObjectDb(bool memory = true) { var factory = new DbFactory(); var dbConnection = factory.Create(); _db = new TestDatabase(dbConnection); Mocker.SetConstant(dbConnection); } [SetUp] public void SetupReadDb() { WithObjectDb(); } } public interface IDatabase { void InsertMany(IEnumerable items) where T : new(); void Insert(T item) where T : new(); IEnumerable All() where T : new(); void Update(T childModel) where T : new(); void Delete(T childModel) where T : new(); } public class TestDatabase : IDatabase { private readonly IDbConnection _dbConnection; public TestDatabase(IDbConnection dbConnection) { _dbConnection = dbConnection; } public void InsertMany(IEnumerable items) where T : new() { _dbConnection.InsertAll(items); } public void Insert(T item) where T : new() { _dbConnection.Insert(item); } public IEnumerable All() where T : new() { return _dbConnection.Select(); } public void Update(T childModel) where T : new() { _dbConnection.Update(childModel); } public void Delete(T childModel) where T : new() { _dbConnection.Delete(childModel); } } }